1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
621
  
622
  
623
  
624
  
625
  
626
  
627
  
628
  
629
  
630
  
631
  
632
  
633
  
634
  
635
  
636
  
637
  
638
  
639
  
640
  
641
  
642
  
643
  
644
  
645
  
646
  
647
  
648
  
649
  
650
  
651
  
652
  
653
  
654
  
655
  
656
  
657
  
658
  
659
  
660
  
661
  
662
  
663
  
664
  
665
  
666
  
667
  
668
  
669
  
670
  
671
  
672
  
673
  
674
  
675
  
676
  
677
  
678
  
679
  
680
  
681
  
682
  
683
  
684
  
685
  
686
  
687
  
688
  
689
  
690
  
691
  
692
  
693
  
694
  
695
  
696
  
697
  
698
  
699
  
700
  
701
  
702
  
703
  
704
  
705
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
/* 
**! module Image 
**! class Image 
*/ 
#include "global.h" 
 
#include <math.h> 
#include <ctype.h> 
 
#include "stralloc.h" 
#include "pike_macros.h" 
#include "object.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "threads.h" 
#include "pike_error.h" 
#include "module_support.h" 
 
#include "image.h" 
 
 
#define sp Pike_sp 
 
extern struct program *image_program; 
#ifdef THIS 
#undef THIS /* Needed for NT */ 
#endif 
#define THIS ((struct image *)(Pike_fp->current_storage)) 
#define THISOBJ (Pike_fp->current_object) 
 
static const double c0=0.70710678118654752440; 
static const double my_PI=3.14159265358979323846; 
 
#if 0 
#include <sys/resource.h> 
#define CHRONO(X) chrono(X); 
 
static void chrono(char *x) 
{ 
   struct rusage r; 
   static struct rusage rold; 
   getrusage(RUSAGE_SELF,&r); 
   fprintf(stderr,"%s: %ld.%06ld - %ld.%06ld\n",x, 
           (long)r.ru_utime.tv_sec,(long)r.ru_utime.tv_usec, 
 
           (long)(((r.ru_utime.tv_usec-rold.ru_utime.tv_usec<0)?-1:0) 
                  +r.ru_utime.tv_sec-rold.ru_utime.tv_sec), 
           (long)(((r.ru_utime.tv_usec-rold.ru_utime.tv_usec<0)?1000000:0) 
                  + r.ru_utime.tv_usec-rold.ru_utime.tv_usec) 
      ); 
 
   rold=r; 
} 
#else 
#define CHRONO(X) 
#endif 
 
/* 
**! method object phaseh() 
**! method object phasev() 
**! method object phasevh() 
**! method object phasehv() 
**!     Draws images describing the phase 
**!     of the current image. phaseh gives the 
**!     horizontal phase and phasev the vertical 
**!     phase. 
**! 
**!     <tt>phaseh</tt> gives an image 
**!     where 
**!     <pre> 
**!            max  falling   min  rising 
**!     value=  0     64      128   192 
**!     </pre> 
**! 
**!     0 is set if there is no way to determine 
**!     if it is rising or falling. This is done 
**!     for the every red, green and blue part of 
**!     the image. 
**! 
**!     Phase images can be used to create ugly 
**!     effects or to find meta-information 
**!     in the orginal image. 
**! 
**!     <table border=0> 
**!     <tr> 
**!     <td><illustration> return lena(); </illustration></td> 
**!     <td><illustration> return lena()->phaseh(); </illustration></td> 
**!     <td><illustration> return lena()->phasev(); </illustration></td> 
**!     <td><illustration> return lena()->phasevh(); </illustration></td> 
**!     <td><illustration> return lena()->phasehv(); </illustration></td> 
**!     </tr> 
**!     <tr> 
**!     <td>original </td> 
**!     <td>phaseh() </td> 
**!     <td>phasev() </td> 
**!     <td>phasevh()</td> 
**!     <td>phasehv()</td> 
**!     </tr> 
**!     </table> 
**! 
**! returns the new image object 
**  see also: match_phase 
**! bugs 
**!     0 should not be set as explained above. 
**! note 
**!     <b>experimental status</b>; may not be exact the same 
**!     output in later versions 
*/ 
static inline int my_abs(int a) { return (a<0)?-a:a; } 
 
/* phase-image creating functions */ 
 
#define NEIG 1 
#define IMAGE_PHASE image_phaseh 
#include "phase.h" 
#undef NEIG 
#undef IMAGE_PHASE 
 
#define NEIG xz 
#define IMAGE_PHASE image_phasev 
#include "phase.h" 
#undef NEIG 
#undef IMAGE_PHASE 
 
#define NEIG xz+1 
#define IMAGE_PHASE image_phasehv 
#include "phase.h" 
#undef NEIG 
#undef IMAGE_PHASE 
 
#define NEIG xz-1 
#define IMAGE_PHASE image_phasevh 
#include "phase.h" 
#undef NEIG 
#undef IMAGE_PHASE 
 
/* the match-functions */ 
 
/* 
**! method object match(int|float scale, object needle) 
**! method object match(int|float scale, object needle, object haystack_cert, object needle_cert) 
**! method object match(int|float scale, object needle, object haystack_avoid, int foo) 
**! method object match(int|float scale, object needle, object haystack_cert, object needle_cert, object haystack_avoid, int foo) 
**! 
**!     This method creates an image that describes the 
**!     match in every pixel in the image and the 
**!     needle-Image. 
**! 
**!     <pre> 
**!        new pixel value = 
**!          sum( my_abs(needle_pixel-haystack_pixel)) 
**!     </pre> 
**! 
**!     The new image only have the red rgb-part set. 
**! 
**! 
**! arg int|float scale 
**!     Every pixel is divided with this value. 
**!     Note that a proper value here depends on 
**!     the size of the neadle. 
**! 
**! arg object needle 
**!     The image to use for the matching. 
**! 
**! arg object haystack_cert 
**!     This image should be the same size as 
**!     the image itselves. A non-white-part of the 
**!     haystack_cert-image modifies the output 
**!     by lowering it. 
**! 
**! arg object needle_cert 
**!     The same, but for the needle-image. 
**! 
**! arg int foo 
**! arg object haystack_avoid 
**!     This image should be the same size as 
**!     the image itselves. If foo is less than the red 
**!     value in haystack_avoid the corresponding 
**!     matching-calculating is not calculated. The avoided parts 
**!     are drawn in the color 0,100,0. 
**! 
**! returns the new image object 
**! see also: phasev,phaseh 
**! note 
**!     <b>experimental status</b>; may not be exact the same 
**!     output in later versions 
*/ 
 
 
#define SCALE_MODIFY(x) (x) 
#define NAME "match" 
#define INAME image_match 
#define PIXEL_VALUE_DISTANCE(CO) \ 
       (my_abs(haystacki[j].CO-needlei[ny*nxs+nx].CO)) 
#include "match.h" 
 
#define NAME "match_phase" 
#define INAME image_match_phase 
#define PIXEL_VALUE_DISTANCE(CO)  \ 
       ((h=haystacki[j].CO),(n=needlei[ny*nxs+nx].CO), \ 
        ((h>n)? MINIMUM((h-n),(255-h+n)) : MINIMUM((n-h),(255-n+h)))) 
#include "match.h" 
 
#define NAME "match_norm" 
#define INAME image_match_norm 
#define NEEDLEAVRCODE 
#define PIXEL_VALUE_DISTANCE(CO)  \ 
 (my_abs(( haystacki[j].CO-tempavr )-( needlei[ny*nxs+nx].CO-needle_average))) 
#include "match.h" 
 
#define NAME "match_norm_corr" 
#define INAME image_match_norm_corr 
#undef SCALE_MODIFY 
#define SCALE_MODIFY(x) (1.0/MAXIMUM(1.0,x)) 
#define PIXEL_VALUE_DISTANCE(CO)  \ 
       (((haystacki[j].CO-tempavr)/2+128) \ 
        * ( (needlei[ny*nxs+nx].CO-needle_average)/2+128 )) 
#include "match.h" 
#undef NEEDLEAVRCODE 
#undef SUMCHECK 
 
 
/* 
**! method string make_ascii(object orient1,object orient2,object orient3,object orient4,int|void tlevel,int|void xsize,int|void ysize) 
**! 
**!     This method creates a string that looks like 
**!     the image. Example: 
**!     <pre> 
**!        //Stina is an image with a cat. 
**!        array(object) Stina4=Stina->orient4(); 
**!        Stina4[1]*=215; 
**!        Stina4[3]*=215; 
**!        string foo=Stina->make_ascii(@Stina4,40,4,8); 
**!     </pre> 
**! 
**! returns some nice ASCII art. 
**! see also: orient, orient4 
**! note 
**!     <b>experimental status</b>; may not be exact the same 
**!     output in later versions 
*/ 
 
/* FIXME: This functions does not depend on the actual image object 
   and should be removed from the object or reworked. */ 
void image_make_ascii(INT32 args) 
{ 
  struct object *objs[4]; 
  struct image *img[4]; 
  INT32 xchar_size=0; 
  INT32 ychar_size=0; 
  INT32 tlevel=0; 
  int i, x, y,xy=0,y2=0, xmax=0,ymax=0,max; 
  struct pike_string *s; 
 
  get_all_args(NULL, args, "%o%o%o%o.%d%d%d", 
               &objs[0], &objs[1], &objs[2], &objs[3], 
               &tlevel, &xchar_size, &ychar_size); 
 
  for(i=0; i<4; i++) { 
    img[i]=get_storage(objs[i],image_program); 
    if(!img[i]) 
      SIMPLE_ARG_TYPE_ERROR("make_ascii",i+1,"Image.Image"); 
    if(i!=0 && 
       img[0]->xsize!=img[i]->xsize && 
       img[0]->ysize!=img[i]->ysize) 
      Pike_error("make_ascii: Different sized images.\n"); 
  } 
 
  if (!tlevel) tlevel=40; 
  if (!xchar_size) xchar_size=5; 
  if (!ychar_size) ychar_size=8; 
 
  tlevel=tlevel*xchar_size*ychar_size; 
  xmax=((img[0]->xsize-1)/xchar_size+2); 
  ymax=((img[0]->ysize-1)/ychar_size+1); 
  max=xmax*ymax; 
  s=begin_shared_string(max); 
 
  THREADS_ALLOW(); 
 
  /*fix /n at each row*/ 
  for(i=xmax-1; i<max; i+=xmax) 
    s->str[i]='\n'; 
 
  for(x=0; x<xmax-1; x++) 
    { 
      for(y=0; y<ymax-1; y++) 
        { 
          int dir0,dir1,dir2,dir3; 
          int xstop=0,ystop=0; 
          char t=' '; 
          dir0=0; 
          dir1=0; 
          dir2=0; 
          dir3=0; 
 
          ystop=y*ychar_size+ychar_size; 
          for(y2=y*ychar_size; y2<(ystop); y2++) 
            { 
              xy=y2*img[0]->xsize+x*xchar_size; 
              xstop=xy+xchar_size; 
              for(; xy<xstop; xy++) 
                { 
                  dir0+=img[0]->img[xy].r; 
                  dir1+=img[1]->img[xy].r; 
                  dir2+=img[2]->img[xy].r; 
                  dir3+=img[3]->img[xy].r; 
                } 
            } 
 
          /*set a part of the string*/ 
          if ((dir0<=tlevel)&& 
              (dir1<=tlevel)&& 
              (dir2<=tlevel)&& 
              (dir3<=tlevel)) 
            { 
              t=' '; 
            } 
          else if ((dir0>tlevel)&& 
                   (dir1>tlevel)&& 
                   (dir2>tlevel)&& 
                   (dir3>tlevel)) 
            { 
              t='*'; 
            } 
          else if ((dir0>=dir1)&&(dir0>=dir2)&& 
                   (dir0>=dir3)) 
            { 
              if ((dir2>=tlevel)&& 
                  (dir2>dir1)&&(dir2>dir3)) 
                t='+'; 
              else 
                t='|'; 
            } 
          else if ((dir1>=dir2)&&(dir1>=dir3)) 
            { 
              if ((dir3>=tlevel)&& 
                  (dir3>dir0)&&(dir3>dir2)) 
                t='X'; 
              else 
                t='/'; 
            } 
          else if (dir2>=dir3) 
            { 
              if ((dir0>=tlevel)&& 
                  (dir0>dir1)&&(dir0>dir3)) 
                t='+'; 
              else 
                t='-'; 
            } 
          else 
            { 
              if ((dir1>=tlevel)&& 
                  (dir1>dir0)&&(dir1>dir2)) 
                t='X'; 
              else 
                t='\\'; 
            } 
 
          s->str[y*xmax+x]=t; 
 
        } 
    } 
 
 
  /* Temp fix to hide garbage */ 
  for(x=0; x<xmax-1; x++) 
    s->str[(ymax-1)*xmax+x] = ' '; 
 
 
  /*fix end of rows*/ 
 
  /*fix middle*/ 
 
 
  /*fix last row*/ 
 
  /*fix last position*/ 
  /* 
**!     <pre>      |      /    -    \ 
**!          hue=  0     64   128  192  (=red in an hsv image) 
**!     </pre> 
  */ 
 
  THREADS_DISALLOW(); 
 
  pop_n_elems(args); 
  push_string(end_shared_string(s)); 
  return; 
} 
/* End make_ascii */ 
 
 
 
/* 
**! method object apply_max(array(array(int|array(int))) matrix) 
**! method object apply_max(array(array(int|array(int))) matrix,int r,int g,int b) 
**! method object apply_max(array(array(int|array(int))) matrix,int r,int g,int b,int|float div) 
**!     This is the same as apply_matrix, but it uses the maximum 
**!     instead. 
**! 
**!     This function is not very fast. 
**! 
**! returns the new image object 
**! 
**! arg array(array(int|array(int))) 
**!     the matrix; innermost is a value or an array with red, green, blue 
**!     values for red, green, blue separation. 
**! arg int r 
**! arg int g 
**! arg int b 
**!     base level of result, default is zero 
**! arg int|float div 
**!     division factor, default is 1.0. 
**! note 
**!     <b>experimental status</b>; may not be exact the same 
**!     output in later versions 
*/ 
 
 
 
static inline rgb_group _pixel_apply_max(struct image *img, 
                                            int x,int y, 
                                            int width,int height, 
                                            rgbd_group *matrix, 
                                            rgb_group default_rgb, 
                                            double div) 
{ 
   rgb_group res; 
   int i,j,bx,by,xp,yp; 
   int sumr,sumg,sumb,r,g,b; 
   double qdiv=1.0/div; 
 
  /* NOTE: 
   *    This code MUST be MT-SAFE! 
   */ 
  HIDE_GLOBAL_VARIABLES(); 
 
   sumr=sumg=sumb=0; 
   r=g=b=0; 
 
   bx=width/2; 
   by=height/2; 
 
   for (xp=x-bx,i=0; i<width; i++,xp++) 
      for (yp=y-by,j=0; j<height; j++,yp++) 
         if (xp>=0 && xp<img->xsize && yp>=0 && yp<img->ysize) 
         { 
            r = (int)MAXIMUM(r, matrix[i+j*width].r * 
                              img->img[xp+yp*img->xsize].r); 
            g = (int)MAXIMUM(g, matrix[i+j*width].g * 
                              img->img[xp+yp*img->xsize].g); 
            b = (int)MAXIMUM(b, matrix[i+j*width].b * 
                              img->img[xp+yp*img->xsize].b); 
#ifdef MATRIX_DEBUG 
            fprintf(stderr,"%d,%d %d,%d->%d,%d,%d\n", 
                    i,j,xp,yp, 
                    img->img[x+i+(y+j)*img->xsize].r, 
                    img->img[x+i+(y+j)*img->xsize].g, 
                    img->img[x+i+(y+j)*img->xsize].b); 
#endif 
            sumr = (int)MAXIMUM(sumr, matrix[i+j*width].r); 
            sumg = (int)MAXIMUM(sumg, matrix[i+j*width].g); 
            sumb = (int)MAXIMUM(sumb, matrix[i+j*width].b); 
         } 
   if (sumr) 
     res.r = (COLORTYPE)testrange(default_rgb.r + r/(sumr * div)); 
   else 
     res.r = (COLORTYPE)testrange(r * qdiv + default_rgb.r); 
   if (sumg) 
     res.g = (COLORTYPE)testrange(default_rgb.g + g/(sumg * div)); 
   else 
     res.g = (COLORTYPE)testrange(g * qdiv + default_rgb.g); 
   if (sumb) 
     res.b = (COLORTYPE)testrange(default_rgb.b + b/(sumb * div)); 
   else 
     res.b = (COLORTYPE)testrange(b * qdiv + default_rgb.b); 
#ifdef MATRIX_DEBUG 
   fprintf(stderr,"->%d,%d,%d\n",res.r,res.g,res.b); 
#endif 
   return res; 
   REVEAL_GLOBAL_VARIABLES(); 
} 
 
 
void img_apply_max(struct image *dest, 
                      struct image *img, 
                      int width,int height, 
                      rgbd_group *matrix, 
                      rgb_group default_rgb, 
                      double div) 
{ 
   rgb_group *d,*ip,*dp; 
   rgbd_group *mp; 
   int i,x,y,bx,by,ex,ey,yp; 
   int widthheight; 
   double sumr,sumg,sumb; 
   double qr,qg,qb; 
   double r=0,g=0,b=0; 
 
   d=xalloc(sizeof(rgb_group)*img->xsize*img->ysize+RGB_VEC_PAD); 
 
THREADS_ALLOW(); 
 
   widthheight=width*height; 
   sumr=sumg=sumb=0; 
   for (i=0; i<widthheight;) 
     { 
       sumr=MAXIMUM(sumr, matrix[i].r); 
       sumg=MAXIMUM(sumg, matrix[i].g); 
       sumb=MAXIMUM(sumb, matrix[i++].b); 
     } 
 
   if (!sumr) {sumr=1;} sumr*=div; qr=1.0/sumr; 
   if (!sumg) {sumg=1;} sumg*=div; qg=1.0/sumg; 
   if (!sumb) {sumb=1;} sumb*=div; qb=1.0/sumb; 
 
   bx=width/2; 
   by=height/2; 
   ex=width-bx; 
   ey=height-by; 
 
   for (y=by; y<img->ysize-ey; y++) 
   { 
      dp=d+y*img->xsize+bx; 
      for (x=bx; x<img->xsize-ex; x++) 
      { 
         r=g=b=0; 
         mp=matrix; 
         ip=img->img+(x-bx)+(y-by)*img->xsize; 
         /* for (yp=y-by,j=0; j<height; j++,yp++) */ 
#ifdef MATRIX_DEBUG 
j=-1; 
#endif 
         for (yp=y-by; yp<height+y-by; yp++) 
         { 
#ifdef MATRIX_DEBUG 
j++; 
#endif 
            for (i=0; i<width; i++) 
            { 
               r=MAXIMUM(r, ip->r*mp->r); 
               g=MAXIMUM(g, ip->g*mp->g); 
               b=MAXIMUM(b, ip->b*mp->b); 
#ifdef MATRIX_DEBUG 
               fprintf(stderr,"%d,%d ->%d,%d,%d\n", 
                       i,j, 
                       img->img[x+i+(y+j)*img->xsize].r, 
                       img->img[x+i+(y+j)*img->xsize].g, 
                       img->img[x+i+(y+j)*img->xsize].b); 
#endif 
               mp++; 
               ip++; 
            } 
            ip+=img->xsize-width; 
         } 
#ifdef MATRIX_DEBUG 
         fprintf(stderr,"->%d,%d,%d\n",r/sumr,g/sumg,b/sumb); 
#endif 
         r = default_rgb.r+(int)(r*qr+0.5); 
         dp->r = (COLORTYPE)testrange(r); 
         g = default_rgb.g+(int)(g*qg+0.5); 
         dp->g = (COLORTYPE)testrange(g); 
         b = default_rgb.b+(int)(b*qb+0.5); 
         dp->b = (COLORTYPE)testrange(b); 
         dp++; 
      } 
   } 
 
 
   for (y=0; y<img->ysize; y++) 
   { 
      for (x=0; x<bx; x++) 
         d[x+y*img->xsize]=_pixel_apply_max(img,x,y,width,height, 
                                               matrix,default_rgb,div); 
      for (x=img->xsize-ex; x<img->xsize; x++) 
         d[x+y*img->xsize]=_pixel_apply_max(img,x,y,width,height, 
                                               matrix,default_rgb,div); 
   } 
 
   for (x=0; x<img->xsize; x++) 
   { 
      for (y=0; y<by; y++) 
         d[x+y*img->xsize]=_pixel_apply_max(img,x,y,width,height, 
                                               matrix,default_rgb,div); 
      for (y=img->ysize-ey; y<img->ysize; y++) 
         d[x+y*img->xsize]=_pixel_apply_max(img,x,y,width,height, 
                                               matrix,default_rgb,div); 
   } 
 
 
   if (dest->img) free(dest->img); 
   *dest=*img; 
   dest->img=d; 
 
THREADS_DISALLOW(); 
} 
 
 
 
void image_apply_max(INT32 args) 
{ 
   int width,height,i,j; 
   rgbd_group *matrix; 
   rgb_group default_rgb; 
   struct object *o; 
   double div; 
 
   if (args<1 || 
       TYPEOF(sp[-args]) != T_ARRAY) 
     SIMPLE_ARG_TYPE_ERROR("apply_max", 1, "array"); 
 
   if (args>3) 
   { 
     struct array *a; 
     get_all_args(NULL, args, "%a%d%d%d", &a, 
                  &default_rgb.r, &default_rgb.g, &default_rgb.b); 
   } 
   else 
   { 
      default_rgb.r=0; 
      default_rgb.g=0; 
      default_rgb.b=0; 
   } 
 
   if (args>4 
       && TYPEOF(sp[4-args]) == T_INT) 
   { 
      div=sp[4-args].u.integer; 
      if (!div) div=1; 
   } 
   else if (args>4 
            && TYPEOF(sp[4-args]) == T_FLOAT) 
   { 
      div=sp[4-args].u.float_number; 
      if (!div) div=1; 
   } 
   else div=1; 
 
   height=sp[-args].u.array->size; 
   width=-1; 
   for (i=0; i<height; i++) 
   { 
      struct svalue s=sp[-args].u.array->item[i]; 
      if (TYPEOF(s) != T_ARRAY) 
         Pike_error("Illegal contents of (root) array (Image.Image->apply_max)\n"); 
      if (width==-1) 
         width=s.u.array->size; 
      else 
         if (width!=s.u.array->size) 
            Pike_error("Arrays has different size (Image.Image->apply_max)\n"); 
   } 
   if (width==-1) width=0; 
 
   matrix=xalloc(sizeof(rgbd_group)*width*height+1); 
 
   for (i=0; i<height; i++) 
   { 
      struct svalue s=sp[-args].u.array->item[i]; 
      for (j=0; j<width; j++) 
      { 
         struct svalue s2=s.u.array->item[j]; 
         if (TYPEOF(s2) == T_ARRAY && s2.u.array->size == 3) 
         { 
            struct svalue s3; 
            s3=s2.u.array->item[0]; 
            if (TYPEOF(s3) == T_INT) matrix[j+i*width].r = (float)s3.u.integer; 
            else matrix[j+i*width].r=0; 
 
            s3=s2.u.array->item[1]; 
            if (TYPEOF(s3) == T_INT) matrix[j+i*width].g = (float)s3.u.integer; 
            else matrix[j+i*width].g=0; 
 
            s3=s2.u.array->item[2]; 
            if (TYPEOF(s3) == T_INT) matrix[j+i*width].b = (float)s3.u.integer; 
            else matrix[j+i*width].b=0; 
         } 
         else if (TYPEOF(s2) == T_INT) 
            matrix[j+i*width].r=matrix[j+i*width].g= 
               matrix[j+i*width].b = (float)s2.u.integer; 
         else 
            matrix[j+i*width].r=matrix[j+i*width].g= 
               matrix[j+i*width].b=0; 
      } 
   } 
 
   o=clone_object(image_program,0); 
 
   if (THIS->img) 
      img_apply_max((struct image*)o->storage,THIS, 
                       width,height,matrix,default_rgb,div); 
 
   free(matrix); 
 
   pop_n_elems(args); 
   push_object(o); 
}