image.c

filter: Statementstmt 74.2% 511 / 688 Branchbran 62.7% 270 / 430 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.8% 781 / 1118 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« tags.c ↑ Summary Imager.xs »
1 #define IMAGER_NO_CONTEXT
2  
3 #include "imager.h"
4 #include "imageri.h"
5  
6 /*
7 =head1 NAME
8  
9 image.c - implements most of the basic functions of Imager and much of the rest
10  
11 =head1 SYNOPSIS
12  
13 i_img *i;
14 i_color *c;
15 c = i_color_new(red, green, blue, alpha);
16 ICL_DESTROY(c);
17 i = i_img_8_new();
18 i_img_destroy(i);
19 // and much more
20  
21 =head1 DESCRIPTION
22  
23 image.c implements the basic functions to create and destroy image and
24 color objects for Imager.
25  
26 =head1 FUNCTION REFERENCE
27  
28 Some of these functions are internal.
29  
30 =over
31  
32 =cut
33 */
34  
35 im_context_t (*im_get_context)(void) = NULL;
36  
37 #define XAXIS 0
38 #define YAXIS 1
39 #define XYAXIS 2
40  
41 #define minmax(a,b,i) ( ((a>=i)?a: ( (b<=i)?b:i )) )
42  
43 /*
44 =item im_img_alloc(aIMCTX)
45 X<im_img_alloc API>X<i_img_alloc API>
46 =category Image Implementation
47 =synopsis i_img *im = im_img_alloc(aIMCTX);
48 =synopsis i_img *im = i_img_alloc();
49  
50 Allocates a new i_img structure.
51  
52 When implementing a new image type perform the following steps in your
53 image object creation function:
54  
55 =over
56  
57 =item 1.
58  
59 allocate the image with i_img_alloc().
60  
61 =item 2.
62  
63 initialize any function pointers or other data as needed, you can
64 overwrite the whole block if you need to.
65  
66 =item 3.
67  
68 initialize Imager's internal data by calling i_img_init() on the image
69 object.
70  
71 =back
72  
73 =cut
74 */
75  
76 i_img *
77 1517 im_img_alloc(pIMCTX) {
78 1517 return i_malloc(sizeof(i_img));
79 }
80  
81 /*
82 =item im_img_init(aIMCTX, image)
83 X<im_img_init API>X<i_img_init API>
84 =category Image Implementation
85 =synopsis im_img_init(aIMCTX, im);
86 =synopsis i_img_init(im);
87  
88 Imager internal initialization of images.
89  
90 See L</im_img_alloc(aIMCTX)> for more information.
91  
92 =cut
93 */
94  
95 void
96 1517 im_img_init(pIMCTX, i_img *img) {
97 1517 img->im_data = NULL;
98 1517 img->context = aIMCTX;
99 1517 im_context_refinc(aIMCTX, "img_init");
100 1517 }
101  
102 /*
103 =item ICL_new_internal(r, g, b, a)
104  
105 Return a new color object with values passed to it.
106  
107 r - red component (range: 0 - 255)
108 g - green component (range: 0 - 255)
109 b - blue component (range: 0 - 255)
110 a - alpha component (range: 0 - 255)
111  
112 =cut
113 */
114  
115 i_color *
116 2979 ICL_new_internal(unsigned char r,unsigned char g,unsigned char b,unsigned char a) {
117 2979 i_color *cl = NULL;
118 2979 dIMCTX;
119  
120 2979 im_log((aIMCTX,1,"ICL_new_internal(r %d,g %d,b %d,a %d)\n", r, g, b, a));
121  
122 2979 ▶ if ( (cl=i_malloc(sizeof(i_color))) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ( (cl=i_malloc(sizeof(i_color))) == NULL) 0 2979 2979
123 0 im_fatal(aIMCTX, 2,"malloc() error\n");
124 2979 cl->rgba.r = r;
125 2979 cl->rgba.g = g;
126 2979 cl->rgba.b = b;
127 2979 cl->rgba.a = a;
128 2979 im_log((aIMCTX,1,"(%p) <- ICL_new_internal\n",cl));
129 2979 return cl;
130 }
131  
132  
133 /*
134 =item ICL_set_internal(cl, r, g, b, a)
135  
136 Overwrite a color with new values.
137  
138 cl - pointer to color object
139 r - red component (range: 0 - 255)
140 g - green component (range: 0 - 255)
141 b - blue component (range: 0 - 255)
142 a - alpha component (range: 0 - 255)
143  
144 =cut
145 */
146  
147 i_color *
148 0 ICL_set_internal(i_color *cl,unsigned char r,unsigned char g,unsigned char b,unsigned char a) {
149 0 dIMCTX;
150 0 im_log((aIMCTX,1,"ICL_set_internal(cl* %p,r %d,g %d,b %d,a %d)\n",cl,r,g,b,a));
151 0 ▶ if (cl == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cl == NULL) 0 0 0
152 0 ▶ if ( (cl=i_malloc(sizeof(i_color))) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ( (cl=i_malloc(sizeof(i_color))) == NULL) 0 0 0
153 0 im_fatal(aIMCTX, 2,"malloc() error\n");
154 0 cl->rgba.r=r;
155 0 cl->rgba.g=g;
156 0 cl->rgba.b=b;
157 0 cl->rgba.a=a;
158 0 im_log((aIMCTX,1,"(%p) <- ICL_set_internal\n",cl));
159 0 return cl;
160 }
161  
162  
163 /*
164 =item ICL_add(dst, src, ch)
165  
166 Add src to dst inplace - dst is modified.
167  
168 dst - pointer to destination color object
169 src - pointer to color object that is added
170 ch - number of channels
171  
172 =cut
173 */
174  
175 void
176 0 ICL_add(i_color *dst,i_color *src,int ch) {
177 int tmp,i;
178 0 ▶ for(i=0;i<ch;i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<ch;i++) { 0 0 0
179 0 tmp=dst->channel[i]+src->channel[i];
180 0 dst->channel[i]= tmp>255 ? 255:tmp;
181 }
182 0 }
183  
184 /*
185 =item ICL_info(cl)
186  
187 Dump color information to log - strictly for debugging.
188  
189 cl - pointer to color object
190  
191 =cut
192 */
193  
194 void
195 6 ICL_info(i_color const *cl) {
196 6 dIMCTX;
197 6 im_log((aIMCTX, 1,"i_color_info(cl* %p)\n",cl));
198 6 im_log((aIMCTX, 1,"i_color_info: (%d,%d,%d,%d)\n",cl->rgba.r,cl->rgba.g,cl->rgba.b,cl->rgba.a));
199 6 }
200  
201 /*
202 =item ICL_DESTROY
203  
204 Destroy ancillary data for Color object.
205  
206 cl - pointer to color object
207  
208 =cut
209 */
210  
211 void
212 3957 ICL_DESTROY(i_color *cl) {
213 3957 dIMCTX;
214 3957 im_log((aIMCTX, 1,"ICL_DESTROY(cl* %p)\n",cl));
215 3957 i_free(cl);
216 3957 }
217  
218 /*
219 =item i_fcolor_new(double r, double g, double b, double a)
220  
221 =cut
222 */
223 166 i_fcolor *i_fcolor_new(double r, double g, double b, double a) {
224 166 i_fcolor *cl = NULL;
225 166 dIMCTX;
226  
227 166 im_log((aIMCTX, 1,"i_fcolor_new(r %g,g %g,b %g,a %g)\n", r, g, b, a));
228  
229 166 cl=i_malloc(sizeof(i_fcolor));
230 166 cl->rgba.r = r;
231 166 cl->rgba.g = g;
232 166 cl->rgba.b = b;
233 166 cl->rgba.a = a;
234 166 im_log((aIMCTX, 1,"(%p) <- i_fcolor_new\n",cl));
235  
236 166 return cl;
237 }
238  
239 /*
240 =item i_fcolor_destroy(i_fcolor *cl)
241  
242 =cut
243 */
244 896 void i_fcolor_destroy(i_fcolor *cl) {
245 896 myfree(cl);
246 896 }
247  
248 static void
249 1517 do_img_exorcise(pIMCTX, i_img *im) {
250 1517 i_tags_destroy(&im->tags);
251 1517 ▶ if (im->i_f_destroy)
Branch: 1 branch
BranchTrueFalseTotal
if (im->i_f_destroy) 180 1337 1517
252 180 (im->i_f_destroy)(im);
253 1517 ▶ if (im->idata != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->idata != NULL) { 1500 17 1517
254 1500 i_free(im->idata);
255 }
256 1517 im->idata = NULL;
257 1517 im->xsize = 0;
258 1517 im->ysize = 0;
259 1517 im->channels = 0;
260  
261 1517 im->ext_data=NULL;
262 1517 }
263  
264 /*
265 =item i_img_exorcise(im)
266  
267 Free image data.
268  
269 im - Image pointer
270  
271 =cut
272 */
273  
274 void
275 0 i_img_exorcise(i_img *im) {
276 0 dIMCTXim(im);
277 0 im_log((aIMCTX,1,"i_img_exorcise(im* %p)\n",im));
278 0 do_img_exorcise(aIMCTX, im);
279 0 im_context_refdec(aIMCTX, "img_destroy");
280 0 }
281  
282 /*
283 =item i_img_destroy(C<img>)
284 =order 90
285 =category Image creation/destruction
286 =synopsis i_img_destroy(img)
287  
288 Destroy an image object
289  
290 =cut
291 */
292  
293 void
294 1517 i_img_destroy(i_img *im) {
295 1517 dIMCTXim(im);
296 1517 im_log((aIMCTX, 1,"i_img_destroy(im %p)\n",im));
297 1517 do_img_exorcise(aIMCTX, im);
298 1517 i_free(im);
299 1517 im_context_refdec(aIMCTX, "img_destroy");
300 1517 }
301  
302 /*
303 =item i_img_info(im, info)
304  
305 =category Image
306  
307 Return image information
308  
309 im - Image pointer
310 info - pointer to array to return data
311  
312 info is an array of 4 integers with the following values:
313  
314 info[0] - width
315 info[1] - height
316 info[2] - channels
317 info[3] - channel mask
318  
319 =cut
320 */
321  
322  
323 void
324 39 i_img_info(i_img *im, i_img_dim *info) {
325 39 dIMCTXim(im);
326 39 im_log((aIMCTX,1,"i_img_info(im %p)\n",im));
327  
328 39 im_log((aIMCTX,1,"i_img_info: xsize=%" i_DF " ysize=%" i_DF " channels=%d "
329 "mask=%ud\n",
330 i_DFc(im->xsize), i_DFc(im->ysize), im->channels,im->ch_mask));
331 39 im_log((aIMCTX,1,"i_img_info: idata=%p\n",im->idata));
332 39 info[0] = im->xsize;
333 39 info[1] = im->ysize;
334 39 info[2] = im->channels;
335 39 info[3] = im->ch_mask;
336 39 }
337  
338 /*
339 =item i_img_setmask(C<im>, C<ch_mask>)
340 =category Image Information
341 =synopsis // only channel 0 writable
342 =synopsis i_img_setmask(img, 0x01);
343  
344 Set the image channel mask for C<im> to C<ch_mask>.
345  
346 The image channel mask gives some control over which channels can be
347 written to in the image.
348  
349 =cut
350 */
351 void
352 87 i_img_setmask(i_img *im,int ch_mask) { im->ch_mask=ch_mask; }
353  
354  
355 /*
356 =item i_img_getmask(C<im>)
357 =category Image Information
358 =synopsis int mask = i_img_getmask(img);
359  
360 Get the image channel mask for C<im>.
361  
362 =cut
363 */
364 int
365 19 i_img_getmask(i_img *im) { return im->ch_mask; }
366  
367 /*
368 =item i_img_getchannels(C<im>)
369 =category Image Information
370 =synopsis int channels = i_img_getchannels(img);
371  
372 Get the number of channels in C<im>.
373  
374 =cut
375 */
376 int
377 801 i_img_getchannels(i_img *im) { return im->channels; }
378  
379 /*
380 =item i_img_get_width(C<im>)
381 =category Image Information
382 =synopsis i_img_dim width = i_img_get_width(im);
383  
384 Returns the width in pixels of the image.
385  
386 =cut
387 */
388 i_img_dim
389 43 i_img_get_width(i_img *im) {
390 43 return im->xsize;
391 }
392  
393 /*
394 =item i_img_get_height(C<im>)
395 =category Image Information
396 =synopsis i_img_dim height = i_img_get_height(im);
397  
398 Returns the height in pixels of the image.
399  
400 =cut
401 */
402 i_img_dim
403 75 i_img_get_height(i_img *im) {
404 75 return im->ysize;
405 }
406  
407 /*
408 =item i_img_color_model(im)
409 =category Image Information
410 =synopsis i_color_model_t cm = i_img_color_model(im);
411  
412 Returns the color model for the image.
413  
414 A future version of Imager will allow for images with extra channels
415 beyond gray/rgb and alpha.
416  
417 =cut
418 */
419 i_color_model_t
420 168 i_img_color_model(i_img *im) {
421 168 return (i_color_model_t)im->channels;
422 }
423  
424 /*
425 =item i_img_alpha_channel(im, &channel)
426 =category Image Information
427 =synopsis int alpha_channel;
428 =synopsis int has_alpha = i_img_alpha_channel(im, &alpha_channel);
429  
430 Work out the alpha channel for an image.
431  
432 If the image has an alpha channel, sets C<*channel> to the alpha
433 channel index and returns non-zero.
434  
435 If the image has no alpha channel, returns zero and C<*channel> is not
436 modified.
437  
438 C<channel> may be C<NULL>.
439  
440 =cut
441 */
442  
443 int
444 78 i_img_alpha_channel(i_img *im, int *channel) {
445 78 i_color_model_t model = i_img_color_model(im);
446 78 ▶ switch (model) {
Branch: 1 branch
BranchTrueFalseTotal
switch (model) { 14 64 78
447 14 case icm_gray_alpha:
448 case icm_rgb_alpha:
449 14 ▶ if (channel) *channel = (int)model - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (channel) *channel = (int)model - 1; 4 10 14
450 14 return 1;
451  
452 64 default:
453 64 return 0;
454 }
455 }
456  
457 /*
458 =item i_img_color_channels(im)
459 =category Image Information
460 =synopsis int color_channels = i_img_color_channels(im);
461  
462 Returns the number of color channels in the image. For now this is
463 always 1 (for grayscale) or 3 (for RGB) but may be 0 in some special
464 cases in a future release of Imager.
465  
466 =cut
467 */
468  
469 int
470 84 i_img_color_channels(i_img *im) {
471 84 i_color_model_t model = i_img_color_model(im);
472 84 switch (model) {
473 16 case icm_gray_alpha:
474 case icm_rgb_alpha:
475 16 return (int)model - 1;
476  
477 68 case icm_gray:
478 case icm_rgb:
479 68 return (int)model;
480  
481 0 default:
482 0 return 0;
483 }
484 }
485  
486 /*
487 =item i_copyto_trans(C<im>, C<src>, C<x1>, C<y1>, C<x2>, C<y2>, C<tx>, C<ty>, C<trans>)
488  
489 =category Image
490  
491 (C<x1>,C<y1>) (C<x2>,C<y2>) specifies the region to copy (in the
492 source coordinates) (C<tx>,C<ty>) specifies the upper left corner for
493 the target image. pass NULL in C<trans> for non transparent i_colors.
494  
495 =cut
496 */
497  
498 void
499 0 i_copyto_trans(i_img *im,i_img *src,i_img_dim x1,i_img_dim y1,i_img_dim x2,i_img_dim y2,i_img_dim tx,i_img_dim ty,const i_color *trans) {
500 i_color pv;
501 i_img_dim x,y,t,ttx,tty,tt;
502 int ch;
503 0 dIMCTXim(im);
504  
505 0 im_log((aIMCTX, 1,"i_copyto_trans(im* %p,src %p, p1(" i_DFp "), p2(" i_DFp "), "
506 "to(" i_DFp "), trans* %p)\n",
507 im, src, i_DFcp(x1, y1), i_DFcp(x2, y2), i_DFcp(tx, ty), trans));
508
509 0 ▶ if (x2<x1) { t=x1; x1=x2; x2=t; }
Branch: 1 branch
BranchTrueFalseTotal
if (x2<x1) { t=x1; x1=x2; x2=t; } 0 0 0
510 0 ▶ if (y2<y1) { t=y1; y1=y2; y2=t; }
Branch: 1 branch
BranchTrueFalseTotal
if (y2<y1) { t=y1; y1=y2; y2=t; } 0 0 0
511  
512 0 ttx=tx;
513 0 ▶ for(x=x1;x<x2;x++)
Branch: 1 branch
BranchTrueFalseTotal
for(x=x1;x<x2;x++) 0 0 0
514 {
515 0 tty=ty;
516 0 ▶ for(y=y1;y<y2;y++)
Branch: 1 branch
BranchTrueFalseTotal
for(y=y1;y<y2;y++) 0 0 0
517 {
518 0 i_gpix(src,x,y,&pv);
519 0 ▶ if ( trans != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ( trans != NULL) 0 0 0
520 {
521 0 tt=0;
522 0 ▶ for(ch=0;ch<im->channels;ch++) if (trans->channel[ch]!=pv.channel[ch]) tt++;
Branch: 2 branches
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) if (trans->channel[ch]!=pv.channel[ch]) tt++; 0 0 0
for(ch=0;ch<im->channels;ch++) if (trans->channel[ch]!=pv.channel[ch]) tt++; 0 0 0
523 0 ▶ if (tt) i_ppix(im,ttx,tty,&pv);
Branch: 1 branch
BranchTrueFalseTotal
if (tt) i_ppix(im,ttx,tty,&pv); 0 0 0
524 0 } else i_ppix(im,ttx,tty,&pv);
525 0 tty++;
526 }
527 0 ttx++;
528 }
529 0 }
530  
531 /*
532 =item i_copy(source)
533  
534 =category Image
535  
536 Creates a new image that is a copy of the image C<source>.
537  
538 Tags are not copied, only the image data.
539  
540 Returns: i_img *
541  
542 =cut
543 */
544  
545 i_img *
546 220 i_copy(i_img *src) {
547 i_img_dim y, y1, x1;
548 220 dIMCTXim(src);
549 220 i_img *im = i_sametype(src, src->xsize, src->ysize);
550  
551 220 im_log((aIMCTX,1,"i_copy(src %p)\n", src));
552  
553 220 ▶ if (!im)
Branch: 1 branch
BranchTrueFalseTotal
if (!im) 0 220 220
554 0 return NULL;
555  
556 220 x1 = src->xsize;
557 220 y1 = src->ysize;
558 220 ▶ if (src->type == i_direct_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->type == i_direct_type) { 214 6 220
559 214 ▶ if (src->bits == i_8_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->bits == i_8_bits) { 170 44 214
560 i_color *pv;
561 170 pv = i_malloc(sizeof(i_color) * x1);
562
563 18115 ▶ for (y = 0; y < y1; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < y1; ++y) { 17945 170 18115
564 17945 i_glin(src, 0, x1, y, pv);
565 17945 i_plin(im, 0, x1, y, pv);
566 }
567 170 i_free(pv);
568 }
569 else {
570 i_fcolor *pv;
571  
572 44 pv = i_malloc(sizeof(i_fcolor) * x1);
573 4598 ▶ for (y = 0; y < y1; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < y1; ++y) { 4554 44 4598
574 4554 i_glinf(src, 0, x1, y, pv);
575 4554 i_plinf(im, 0, x1, y, pv);
576 }
577 44 i_free(pv);
578 }
579 }
580 else {
581 i_palidx *vals;
582  
583 6 vals = i_malloc(sizeof(i_palidx) * x1);
584 581 ▶ for (y = 0; y < y1; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < y1; ++y) { 575 6 581
585 575 ▶ i_gpal(src, 0, x1, y, vals);
Branch: 1 branch
BranchTrueFalseTotal
i_gpal(src, 0, x1, y, vals); 575 0 575
586 575 ▶ i_ppal(im, 0, x1, y, vals);
Branch: 1 branch
BranchTrueFalseTotal
i_ppal(im, 0, x1, y, vals); 575 0 575
587 }
588 6 i_free(vals);
589 }
590  
591 220 return im;
592 }
593  
594 /*
595  
596 http://en.wikipedia.org/wiki/Lanczos_resampling
597  
598 */
599  
600 static
601 float
602 29452 Lanczos(float x) {
603 float PIx, PIx2;
604
605 29452 PIx = PI * x;
606 29452 PIx2 = PIx / 2.0;
607
608 29452 ▶ if ((x >= 2.0) || (x <= -2.0)) return (0.0);
Branch: 2 branches
BranchTrueFalseTotal
if ((x >= 2.0) || (x <= -2.0)) return (0.0); 26377 3075 29452
if ((x >= 2.0) || (x <= -2.0)) return (0.0); 0 26377 26377
609 26377 ▶ else if (x == 0.0) return (1.0);
Branch: 1 branch
BranchTrueFalseTotal
else if (x == 0.0) return (1.0); 3075 23302 26377
610 23302 else return(sin(PIx) / PIx * sin(PIx2) / PIx2);
611 }
612  
613  
614 /*
615 =item i_scaleaxis(im, value, axis)
616  
617 Returns a new image object which is I<im> scaled by I<value> along
618 wither the x-axis (I<axis> == 0) or the y-axis (I<axis> == 1).
619  
620 =cut
621 */
622  
623 i_img*
624 54 i_scaleaxis(i_img *im, double Value, int Axis) {
625 i_img_dim hsize, vsize, i, j, k, l, lMax, iEnd, jEnd;
626 i_img_dim LanczosWidthFactor;
627 float *l0, *l1;
628 double OldLocation;
629 i_img_dim T;
630 double t;
631 float F, PictureValue[MAXCHANNELS];
632 short psave;
633 i_color val,val1,val2;
634 i_img *new_img;
635 54 int has_alpha = i_img_has_alpha(im);
636 54 int color_chans = i_img_color_channels(im);
637 54 dIMCTXim(im);
638  
639 54 i_clear_error();
640 54 im_log((aIMCTX, 1,"i_scaleaxis(im %p,Value %.2f,Axis %d)\n",im,Value,Axis));
641  
642 54 ▶ if (Axis == XAXIS) {
Branch: 1 branch
BranchTrueFalseTotal
if (Axis == XAXIS) { 27 27 54
643 27 hsize = (i_img_dim)(0.5 + im->xsize * Value);
644 27 ▶ if (hsize < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (hsize < 1) { 1 26 27
645 1 hsize = 1;
646 1 Value = 1.0 / im->xsize;
647 }
648 27 vsize = im->ysize;
649
650 27 jEnd = hsize;
651 27 iEnd = vsize;
652 } else {
653 27 hsize = im->xsize;
654 27 vsize = (i_img_dim)(0.5 + im->ysize * Value);
655  
656 27 ▶ if (vsize < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (vsize < 1) { 1 26 27
657 1 vsize = 1;
658 1 Value = 1.0 / im->ysize;
659 }
660  
661 27 jEnd = vsize;
662 27 iEnd = hsize;
663 }
664
665 54 new_img = i_img_8_new(hsize, vsize, im->channels);
666 54 ▶ if (!new_img) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_img) { 0 54 54
667 0 i_push_error(0, "cannot create output image");
668 0 return NULL;
669 }
670
671 /* 1.4 is a magic number, setting it to 2 will cause rather blurred images */
672 54 ▶ LanczosWidthFactor = (Value >= 1) ? 1 : (i_img_dim) (1.4/Value);
Branch: 1 branch
BranchTrueFalseTotal
LanczosWidthFactor = (Value >= 1) ? 1 : (i_img_dim) (1.4/Value); 13 41 54
673 54 lMax = LanczosWidthFactor << 1;
674
675 54 l0 = i_malloc(lMax * sizeof(float));
676 54 l1 = i_malloc(lMax * sizeof(float));
677
678 4787 ▶ for (j=0; j<jEnd; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j=0; j<jEnd; j++) { 4733 54 4787
679 4733 OldLocation = ((double) j) / Value;
680 4733 T = (i_img_dim) (OldLocation);
681 4733 F = OldLocation - T;
682
683 19459 ▶ for (l = 0; l<lMax; l++) {
Branch: 1 branch
BranchTrueFalseTotal
for (l = 0; l<lMax; l++) { 14726 4733 19459
684 14726 l0[lMax-l-1] = Lanczos(((float) (lMax-l-1) + F) / (float) LanczosWidthFactor);
685 14726 l1[l] = Lanczos(((float) (l+1) - F) / (float) LanczosWidthFactor);
686 }
687
688 /* Make sure filter is normalized */
689 4733 t = 0.0;
690 19459 ▶ for(l=0; l<lMax; l++) {
Branch: 1 branch
BranchTrueFalseTotal
for(l=0; l<lMax; l++) { 14726 4733 19459
691 14726 t+=l0[l];
692 14726 t+=l1[l];
693 }
694 4733 t /= (double)LanczosWidthFactor;
695
696 19459 ▶ for(l=0; l<lMax; l++) {
Branch: 1 branch
BranchTrueFalseTotal
for(l=0; l<lMax; l++) { 14726 4733 19459
697 14726 l0[l] /= t;
698 14726 l1[l] /= t;
699 }
700  
701 4733 ▶ if (Axis == XAXIS) {
Branch: 1 branch
BranchTrueFalseTotal
if (Axis == XAXIS) { 2447 2286 4733
702
703 248722 ▶ for (i=0; i<iEnd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<iEnd; i++) { 246275 2447 248722
704 986820 ▶ for (k=0; k<im->channels; k++) PictureValue[k] = 0.0;
Branch: 1 branch
BranchTrueFalseTotal
for (k=0; k<im->channels; k++) PictureValue[k] = 0.0; 740545 246275 986820
705 1078945 ▶ for (l=0; l<lMax; l++) {
Branch: 1 branch
BranchTrueFalseTotal
for (l=0; l<lMax; l++) { 832670 246275 1078945
706 832670 i_img_dim mx = T-lMax+l+1;
707 832670 i_img_dim Mx = T+l+1;
708 832670 mx = (mx < 0) ? 0 : mx;
709 832670 ▶ Mx = (Mx >= im->xsize) ? im->xsize-1 : Mx;
Branch: 1 branch
BranchTrueFalseTotal
Mx = (Mx >= im->xsize) ? im->xsize-1 : Mx; 29431 803239 832670
710
711 832670 i_gpix(im, Mx, i, &val1);
712 832670 i_gpix(im, mx, i, &val2);
713  
714 832670 ▶ if (has_alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_alpha) { 3440 829230 832670
715 3440 i_sample_t alpha1 = val1.channel[color_chans];
716 3440 i_sample_t alpha2 = val2.channel[color_chans];
717 13760 ▶ for (k=0; k < color_chans; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=0; k < color_chans; k++) { 10320 3440 13760
718 10320 PictureValue[k] += l1[l] * val1.channel[k] * alpha1 / 255;
719 10320 PictureValue[k] += l0[lMax-l-1] * val2.channel[k] * alpha2 / 255;
720 }
721 3440 PictureValue[color_chans] += l1[l] * val1.channel[color_chans];
722 3440 PictureValue[color_chans] += l0[lMax-l-1] * val2.channel[color_chans];
723 }
724 else {
725 3316920 ▶ for (k=0; k<im->channels; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=0; k<im->channels; k++) { 2487690 829230 3316920
726 2487690 PictureValue[k] += l1[l] * val1.channel[k];
727 2487690 PictureValue[k] += l0[lMax-l-1] * val2.channel[k];
728 }
729 }
730 }
731  
732 246275 ▶ if (has_alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_alpha) { 1720 244555 246275
733 1720 float fa = PictureValue[color_chans] / LanczosWidthFactor;
734 1720 ▶ int alpha = minmax(0, 255, fa+0.5);
Branch: 2 branches
BranchTrueFalseTotal
int alpha = minmax(0, 255, fa+0.5); 2 1718 1720
int alpha = minmax(0, 255, fa+0.5); 1409 309 1718
735 1720 ▶ if (alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if (alpha) { 1599 121 1720
736 6396 ▶ for (k = 0; k < color_chans; ++k) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < color_chans; ++k) { 4797 1599 6396
737 4797 psave = (short)(0.5+(PictureValue[k] / LanczosWidthFactor * 255 / fa));
738 4797 ▶ val.channel[k]=minmax(0,255,psave);
Branch: 1 branch
BranchTrueFalseTotal
val.channel[k]=minmax(0,255,psave); 4797 0 4797
739 }
740 1599 val.channel[color_chans] = alpha;
741 }
742 else {
743 /* zero alpha, so the pixel has no color */
744 605 ▶ for (k = 0; k < im->channels; ++k)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < im->channels; ++k) 484 121 605
745 484 val.channel[k] = 0;
746 }
747 }
748 else {
749 978220 ▶ for(k=0;k<im->channels;k++) {
Branch: 1 branch
BranchTrueFalseTotal
for(k=0;k<im->channels;k++) { 733665 244555 978220
750 733665 psave = (short)(0.5+(PictureValue[k] / LanczosWidthFactor));
751 733665 ▶ val.channel[k]=minmax(0,255,psave);
Branch: 1 branch
BranchTrueFalseTotal
val.channel[k]=minmax(0,255,psave); 30805 702860 733665
752 }
753 }
754 246275 i_ppix(new_img, j, i, &val);
755 }
756
757 } else {
758
759 338853 ▶ for (i=0; i<iEnd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; i<iEnd; i++) { 336567 2286 338853
760 1347868 ▶ for (k=0; k<im->channels; k++) PictureValue[k] = 0.0;
Branch: 1 branch
BranchTrueFalseTotal
for (k=0; k<im->channels; k++) PictureValue[k] = 0.0; 1011301 336567 1347868
761 1149377 ▶ for (l=0; l < lMax; l++) {
Branch: 1 branch
BranchTrueFalseTotal
for (l=0; l < lMax; l++) { 812810 336567 1149377
762 812810 i_img_dim mx = T-lMax+l+1;
763 812810 i_img_dim Mx = T+l+1;
764 812810 mx = (mx < 0) ? 0 : mx;
765 812810 ▶ Mx = (Mx >= im->ysize) ? im->ysize-1 : Mx;
Branch: 1 branch
BranchTrueFalseTotal
Mx = (Mx >= im->ysize) ? im->ysize-1 : Mx; 12818 799992 812810
766  
767 812810 i_gpix(im, i, Mx, &val1);
768 812810 i_gpix(im, i, mx, &val2);
769 812810 ▶ if (has_alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_alpha) { 3200 809610 812810
770 3200 i_sample_t alpha1 = val1.channel[color_chans];
771 3200 i_sample_t alpha2 = val2.channel[color_chans];
772 12800 ▶ for (k=0; k < color_chans; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=0; k < color_chans; k++) { 9600 3200 12800
773 9600 PictureValue[k] += l1[l] * val1.channel[k] * alpha1 / 255;
774 9600 PictureValue[k] += l0[lMax-l-1] * val2.channel[k] * alpha2 / 255;
775 }
776 3200 PictureValue[color_chans] += l1[l] * val1.channel[color_chans];
777 3200 PictureValue[color_chans] += l0[lMax-l-1] * val2.channel[color_chans];
778 }
779 else {
780 3238440 ▶ for (k=0; k<im->channels; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=0; k<im->channels; k++) { 2428830 809610 3238440
781 2428830 PictureValue[k] += l1[l] * val1.channel[k];
782 2428830 PictureValue[k] += l0[lMax-l-1] * val2.channel[k];
783 }
784 }
785 }
786 336567 ▶ if (has_alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_alpha) { 1600 334967 336567
787 1600 float fa = PictureValue[color_chans] / LanczosWidthFactor;
788 1600 ▶ int alpha = minmax(0, 255, fa+0.5);
Branch: 2 branches
BranchTrueFalseTotal
int alpha = minmax(0, 255, fa+0.5); 0 1600 1600
int alpha = minmax(0, 255, fa+0.5); 1302 298 1600
789 1600 ▶ if (alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if (alpha) { 1520 80 1600
790 6080 ▶ for (k = 0; k < color_chans; ++k) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < color_chans; ++k) { 4560 1520 6080
791 4560 psave = (short)(0.5+(PictureValue[k] / LanczosWidthFactor * 255 / fa));
792 4560 ▶ val.channel[k]=minmax(0,255,psave);
Branch: 1 branch
BranchTrueFalseTotal
val.channel[k]=minmax(0,255,psave); 4560 0 4560
793 }
794 1520 val.channel[color_chans] = alpha;
795 }
796 else {
797 400 ▶ for (k = 0; k < im->channels; ++k)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < im->channels; ++k) 320 80 400
798 320 val.channel[k] = 0;
799 }
800 }
801 else {
802 1339868 ▶ for(k=0;k<im->channels;k++) {
Branch: 1 branch
BranchTrueFalseTotal
for(k=0;k<im->channels;k++) { 1004901 334967 1339868
803 1004901 psave = (short)(0.5+(PictureValue[k] / LanczosWidthFactor));
804 1004901 ▶ val.channel[k]=minmax(0,255,psave);
Branch: 1 branch
BranchTrueFalseTotal
val.channel[k]=minmax(0,255,psave); 41445 963456 1004901
805 }
806 }
807 336567 i_ppix(new_img, i, j, &val);
808 }
809
810 }
811 }
812 54 i_free(l0);
813 54 i_free(l1);
814  
815 54 im_log((aIMCTX, 1,"(%p) <- i_scaleaxis\n", new_img));
816  
817 54 return new_img;
818 }
819  
820  
821 /*
822 =item i_scale_nn(im, scx, scy)
823  
824 Scale by using nearest neighbor
825 Both axes scaled at the same time since
826 nothing is gained by doing it in two steps
827  
828 =cut
829 */
830  
831  
832 i_img*
833 18 i_scale_nn(i_img *im, double scx, double scy) {
834  
835 i_img_dim nxsize,nysize,nx,ny;
836 i_img *new_img;
837 i_color val;
838 18 dIMCTXim(im);
839  
840 18 im_log((aIMCTX, 1,"i_scale_nn(im %p,scx %.2f,scy %.2f)\n",im,scx,scy));
841  
842 18 nxsize = (i_img_dim) ((double) im->xsize * scx);
843 18 ▶ if (nxsize < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nxsize < 1) { 1 17 18
844 1 nxsize = 1;
845 1 scx = 1.0 / im->xsize;
846 }
847 18 nysize = (i_img_dim) ((double) im->ysize * scy);
848 18 ▶ if (nysize < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nysize < 1) { 1 17 18
849 1 nysize = 1;
850 1 scy = 1.0 / im->ysize;
851 }
852 18 ▶ im_assert(scx != 0 && scy != 0);
Branch: 2 branches
BranchTrueFalseTotal
im_assert(scx != 0 && scy != 0); 18 0 18
im_assert(scx != 0 && scy != 0); 0 18 18
853
854 18 new_img = i_img_8_new(nxsize, nysize, im->channels);
855 18 ▶ if (!new_img)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_img) 0 18 18
856 0 return NULL;
857
858 122946 ▶ for(ny=0;ny<nysize;ny++) for(nx=0;nx<nxsize;nx++) {
Branch: 2 branches
BranchTrueFalseTotal
for(ny=0;ny<nysize;ny++) for(nx=0;nx<nxsize;nx++) { 121890 1038 122928
for(ny=0;ny<nysize;ny++) for(nx=0;nx<nxsize;nx++) { 1038 18 1056
859 121890 i_gpix(im,((double)nx)/scx,((double)ny)/scy,&val);
860 121890 i_ppix(new_img,nx,ny,&val);
861 }
862  
863 18 im_log((aIMCTX, 1,"(%p) <- i_scale_nn\n",new_img));
864  
865 18 return new_img;
866 }
867  
868 /*
869 =item i_sametype(C<im>, C<xsize>, C<ysize>)
870  
871 =category Image creation/destruction
872 =synopsis i_img *img = i_sametype(src, width, height);
873  
874 Returns an image of the same type (sample size, channels, paletted/direct).
875  
876 For paletted images the palette is copied from the source.
877  
878 =cut
879 */
880  
881 i_img *
882 376 i_sametype(i_img *src, i_img_dim xsize, i_img_dim ysize) {
883 376 dIMCTXim(src);
884  
885 376 ▶ if (src->type == i_direct_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->type == i_direct_type) { 358 18 376
886 358 ▶ if (src->bits == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->bits == 8) { 290 68 358
887 290 return i_img_8_new(xsize, ysize, src->channels);
888 }
889 68 ▶ else if (src->bits == i_16_bits) {
Branch: 1 branch
BranchTrueFalseTotal
else if (src->bits == i_16_bits) { 24 44 68
890 24 return i_img_16_new(xsize, ysize, src->channels);
891 }
892 44 ▶ else if (src->bits == i_double_bits) {
Branch: 1 branch
BranchTrueFalseTotal
else if (src->bits == i_double_bits) { 44 0 44
893 44 return i_img_double_new(xsize, ysize, src->channels);
894 }
895 else {
896 0 i_push_error(0, "Unknown image bits");
897 0 return NULL;
898 }
899 }
900 else {
901 i_color col;
902 int i;
903  
904 18 ▶ i_img *targ = i_img_pal_new(xsize, ysize, src->channels, i_maxcolors(src));
Branch: 1 branch
BranchTrueFalseTotal
i_img *targ = i_img_pal_new(xsize, ysize, src->channels, i_maxcolors(src)); 18 0 18
905 18 ▶ if (targ) {
Branch: 1 branch
BranchTrueFalseTotal
if (targ) { 18 0 18
906 1071 ▶ for (i = 0; i < i_colorcount(src); ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < i_colorcount(src); ++i) { 1071 0 1071
for (i = 0; i < i_colorcount(src); ++i) { 1053 18 1071
907 1053 ▶ i_getcolors(src, i, &col, 1);
Branch: 1 branch
BranchTrueFalseTotal
i_getcolors(src, i, &col, 1); 1053 0 1053
908 1053 ▶ i_addcolors(targ, &col, 1);
Branch: 1 branch
BranchTrueFalseTotal
i_addcolors(targ, &col, 1); 1053 0 1053
909 }
910 }
911  
912 18 return targ;
913 }
914 }
915  
916 /*
917 =item i_sametype_chans(C<im>, C<xsize>, C<ysize>, C<channels>)
918  
919 =category Image creation/destruction
920 =synopsis i_img *img = i_sametype_chans(src, width, height, channels);
921  
922 Returns an image of the same type (sample size).
923  
924 For paletted images the equivalent direct type is returned.
925  
926 =cut
927 */
928  
929 i_img *
930 57 i_sametype_chans(i_img *src, i_img_dim xsize, i_img_dim ysize, int channels) {
931 57 dIMCTXim(src);
932  
933 57 ▶ if (src->bits == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->bits == 8) { 39 18 57
934 39 return i_img_8_new(xsize, ysize, channels);
935 }
936 18 ▶ else if (src->bits == i_16_bits) {
Branch: 1 branch
BranchTrueFalseTotal
else if (src->bits == i_16_bits) { 4 14 18
937 4 return i_img_16_new(xsize, ysize, channels);
938 }
939 14 ▶ else if (src->bits == i_double_bits) {
Branch: 1 branch
BranchTrueFalseTotal
else if (src->bits == i_double_bits) { 14 0 14
940 14 return i_img_double_new(xsize, ysize, channels);
941 }
942 else {
943 0 i_push_error(0, "Unknown image bits");
944 0 return NULL;
945 }
946 }
947  
948 /*
949 =item i_transform(im, opx, opxl, opy, opyl, parm, parmlen)
950  
951 Spatially transforms I<im> returning a new image.
952  
953 opx for a length of opxl and opy for a length of opy are arrays of
954 operators that modify the x and y positions to retreive the pixel data from.
955  
956 parm and parmlen define extra parameters that the operators may use.
957  
958 Note that this function is largely superseded by the more flexible
959 L<transform.c/i_transform2>.
960  
961 Returns the new image.
962  
963 The operators for this function are defined in L<stackmach.c>.
964  
965 =cut
966 */
967 i_img*
968 0 i_transform(i_img *im, int *opx,int opxl,int *opy,int opyl,double parm[],int parmlen) {
969 double rx,ry;
970 i_img_dim nxsize,nysize,nx,ny;
971 i_img *new_img;
972 i_color val;
973 0 dIMCTXim(im);
974
975 0 im_log((aIMCTX, 1,"i_transform(im %p, opx %p, opxl %d, opy %p, opyl %d, parm %p, parmlen %d)\n",im,opx,opxl,opy,opyl,parm,parmlen));
976  
977 0 nxsize = im->xsize;
978 0 nysize = im->ysize ;
979
980 0 new_img=i_img_8_new(nxsize, nysize, im->channels);
981 0 ▶ if (new_img == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (new_img == NULL) 0 0 0
982 0 return NULL;
983  
984 /* fprintf(stderr,"parm[2]=%f\n",parm[2]); */
985 0 ▶ for(ny=0;ny<nysize;ny++) for(nx=0;nx<nxsize;nx++) {
Branch: 2 branches
BranchTrueFalseTotal
for(ny=0;ny<nysize;ny++) for(nx=0;nx<nxsize;nx++) { 0 0 0
for(ny=0;ny<nysize;ny++) for(nx=0;nx<nxsize;nx++) { 0 0 0
986 /* parm[parmlen-2]=(double)nx;
987 parm[parmlen-1]=(double)ny; */
988  
989 0 parm[0]=(double)nx;
990 0 parm[1]=(double)ny;
991  
992 /* fprintf(stderr,"(%d,%d) ->",nx,ny); */
993 0 rx=i_op_run(opx,opxl,parm,parmlen);
994 0 ry=i_op_run(opy,opyl,parm,parmlen);
995 /* fprintf(stderr,"(%f,%f)\n",rx,ry); */
996 0 i_gpix(im,rx,ry,&val);
997 0 i_ppix(new_img,nx,ny,&val);
998 }
999  
1000 0 im_log((aIMCTX, 1,"(%p) <- i_transform\n",new_img));
1001 0 return new_img;
1002 }
1003  
1004 /*
1005 =item i_img_diff(im1, im2)
1006  
1007 Calculates the sum of the squares of the differences between
1008 correspoding channels in two images.
1009  
1010 If the images are not the same size then only the common area is
1011 compared, hence even if images are different sizes this function
1012 can return zero.
1013  
1014 =cut
1015 */
1016  
1017 float
1018 343 i_img_diff(i_img *im1,i_img *im2) {
1019 i_img_dim x, y, xb, yb;
1020 int ch, chb;
1021 float tdiff;
1022 i_color val1,val2;
1023 343 dIMCTXim(im1);
1024  
1025 343 im_log((aIMCTX, 1,"i_img_diff(im1 %p,im2 %p)\n",im1,im2));
1026  
1027 343 xb=(im1->xsize<im2->xsize)?im1->xsize:im2->xsize;
1028 343 yb=(im1->ysize<im2->ysize)?im1->ysize:im2->ysize;
1029 343 chb=(im1->channels<im2->channels)?im1->channels:im2->channels;
1030  
1031 343 im_log((aIMCTX, 1,"i_img_diff: b=(" i_DFp ") chb=%d\n",
1032 i_DFcp(xb,yb), chb));
1033  
1034 343 tdiff=0;
1035 2984447 ▶ for(y=0;y<yb;y++) for(x=0;x<xb;x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y=0;y<yb;y++) for(x=0;x<xb;x++) { 2894113 89991 2984104
for(y=0;y<yb;y++) for(x=0;x<xb;x++) { 89991 343 90334
1036 2894113 i_gpix(im1,x,y,&val1);
1037 2894113 i_gpix(im2,x,y,&val2);
1038  
1039 11394838 ▶ for(ch=0;ch<chb;ch++) tdiff+=(val1.channel[ch]-val2.channel[ch])*(val1.channel[ch]-val2.channel[ch]);
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<chb;ch++) tdiff+=(val1.channel[ch]-val2.channel[ch])*(val1.channel[ch]-val2.channel[ch]); 8500725 2894113 11394838
1040 }
1041 343 im_log((aIMCTX, 1,"i_img_diff <- (%.2f)\n",tdiff));
1042 343 return tdiff;
1043 }
1044  
1045 /*
1046 =item i_img_diffd(im1, im2)
1047  
1048 Calculates the sum of the squares of the differences between
1049 correspoding channels in two images.
1050  
1051 If the images are not the same size then only the common area is
1052 compared, hence even if images are different sizes this function
1053 can return zero.
1054  
1055 This is like i_img_diff() but looks at floating point samples instead.
1056  
1057 =cut
1058 */
1059  
1060 double
1061 0 i_img_diffd(i_img *im1,i_img *im2) {
1062 i_img_dim x, y, xb, yb;
1063 int ch, chb;
1064 double tdiff;
1065 i_fcolor val1,val2;
1066 0 dIMCTXim(im1);
1067  
1068 0 im_log((aIMCTX, 1,"i_img_diffd(im1 %p,im2 %p)\n",im1,im2));
1069  
1070 0 xb=(im1->xsize<im2->xsize)?im1->xsize:im2->xsize;
1071 0 yb=(im1->ysize<im2->ysize)?im1->ysize:im2->ysize;
1072 0 chb=(im1->channels<im2->channels)?im1->channels:im2->channels;
1073  
1074 0 im_log((aIMCTX, 1,"i_img_diffd: b(" i_DFp ") chb=%d\n",
1075 i_DFcp(xb, yb), chb));
1076  
1077 0 tdiff=0;
1078 0 ▶ for(y=0;y<yb;y++) for(x=0;x<xb;x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y=0;y<yb;y++) for(x=0;x<xb;x++) { 0 0 0
for(y=0;y<yb;y++) for(x=0;x<xb;x++) { 0 0 0
1079 0 i_gpixf(im1,x,y,&val1);
1080 0 i_gpixf(im2,x,y,&val2);
1081  
1082 0 ▶ for(ch=0;ch<chb;ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<chb;ch++) { 0 0 0
1083 0 double sdiff = val1.channel[ch]-val2.channel[ch];
1084 0 tdiff += sdiff * sdiff;
1085 }
1086 }
1087 0 im_log((aIMCTX, 1,"i_img_diffd <- (%.2f)\n",tdiff));
1088  
1089 0 return tdiff;
1090 }
1091  
1092 int
1093 29 i_img_samef(i_img *im1,i_img *im2, double epsilon, char const *what) {
1094 i_img_dim x,y,xb,yb;
1095 int ch, chb;
1096 i_fcolor val1,val2;
1097 29 dIMCTXim(im1);
1098  
1099 29 ▶ if (what == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (what == NULL) 0 29 29
1100 0 what = "(null)";
1101  
1102 29 im_log((aIMCTX,1,"i_img_samef(im1 %p,im2 %p, epsilon %g, what '%s')\n", im1, im2, epsilon, what));
1103  
1104 29 xb=(im1->xsize<im2->xsize)?im1->xsize:im2->xsize;
1105 29 yb=(im1->ysize<im2->ysize)?im1->ysize:im2->ysize;
1106 29 chb=(im1->channels<im2->channels)?im1->channels:im2->channels;
1107  
1108 29 im_log((aIMCTX, 1,"i_img_samef: b(" i_DFp ") chb=%d\n",
1109 i_DFcp(xb, yb), chb));
1110  
1111 3129 ▶ for(y = 0; y < yb; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < yb; y++) { 3100 29 3129
1112 343100 ▶ for(x = 0; x < xb; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < xb; x++) { 340000 3100 343100
1113 340000 i_gpixf(im1, x, y, &val1);
1114 340000 i_gpixf(im2, x, y, &val2);
1115
1116 1360000 ▶ for(ch = 0; ch < chb; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < chb; ch++) { 1020000 340000 1360000
1117 1020000 double sdiff = val1.channel[ch] - val2.channel[ch];
1118 1020000 ▶ if (fabs(sdiff) > epsilon) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(sdiff) > epsilon) { 0 1020000 1020000
1119 0 im_log((aIMCTX, 1,"i_img_samef <- different %g @(" i_DFp ")\n",
1120 sdiff, i_DFcp(x, y)));
1121 0 return 0;
1122 }
1123 }
1124 }
1125 }
1126 29 im_log((aIMCTX, 1,"i_img_samef <- same\n"));
1127  
1128 29 return 1;
1129 }
1130  
1131 /* just a tiny demo of haar wavelets */
1132  
1133 i_img*
1134 0 i_haar(i_img *im) {
1135 i_img_dim mx,my;
1136 i_img_dim fx,fy;
1137 i_img_dim x,y;
1138 int ch;
1139 i_img *new_img,*new_img2;
1140 i_color val1,val2,dval1,dval2;
1141 0 dIMCTXim(im);
1142
1143 0 mx=im->xsize;
1144 0 my=im->ysize;
1145 0 fx=(mx+1)/2;
1146 0 fy=(my+1)/2;
1147  
1148  
1149 /* horizontal pass */
1150
1151 0 new_img = i_img_8_new(fx*2, fy*2, im->channels);
1152 0 ▶ if (!new_img)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_img) 0 0 0
1153 0 return NULL;
1154 0 new_img2 = i_img_8_new(fx*2, fy*2, im->channels);
1155 0 ▶ if (!new_img2) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_img2) { 0 0 0
1156 0 i_img_destroy(new_img);
1157 0 return NULL;
1158 }
1159  
1160 0 ▶ for(y=0;y<my;y++) for(x=0;x<fx;x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y=0;y<my;y++) for(x=0;x<fx;x++) { 0 0 0
for(y=0;y<my;y++) for(x=0;x<fx;x++) { 0 0 0
1161 0 i_gpix(im,x*2,y,&val1);
1162 0 i_gpix(im,x*2+1,y,&val2);
1163 0 ▶ for(ch=0;ch<im->channels;ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) { 0 0 0
1164 0 dval1.channel[ch]=(val1.channel[ch]+val2.channel[ch])/2;
1165 0 dval2.channel[ch]=(255+val1.channel[ch]-val2.channel[ch])/2;
1166 }
1167 0 i_ppix(new_img,x,y,&dval1);
1168 0 i_ppix(new_img,x+fx,y,&dval2);
1169 }
1170  
1171 0 ▶ for(y=0;y<fy;y++) for(x=0;x<mx;x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y=0;y<fy;y++) for(x=0;x<mx;x++) { 0 0 0
for(y=0;y<fy;y++) for(x=0;x<mx;x++) { 0 0 0
1172 0 i_gpix(new_img,x,y*2,&val1);
1173 0 i_gpix(new_img,x,y*2+1,&val2);
1174 0 ▶ for(ch=0;ch<im->channels;ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) { 0 0 0
1175 0 dval1.channel[ch]=(val1.channel[ch]+val2.channel[ch])/2;
1176 0 dval2.channel[ch]=(255+val1.channel[ch]-val2.channel[ch])/2;
1177 }
1178 0 i_ppix(new_img2,x,y,&dval1);
1179 0 i_ppix(new_img2,x,y+fy,&dval2);
1180 }
1181  
1182 0 i_img_destroy(new_img);
1183 0 return new_img2;
1184 }
1185  
1186 /*
1187 =item i_count_colors(im, maxc)
1188  
1189 returns number of colors or -1
1190 to indicate that it was more than max colors
1191  
1192 =cut
1193 */
1194 /* This function has been changed and is now faster. It's using
1195 * i_gsamp instead of i_gpix */
1196 int
1197 9 i_count_colors(i_img *im,int maxc) {
1198 9 dIMCTXim(im);
1199 struct octt *ct;
1200 i_img_dim x,y;
1201 int colorcnt;
1202 int channels[3];
1203 int *samp_chans;
1204 i_sample_t * samp;
1205 9 i_img_dim xsize = im->xsize;
1206 9 i_img_dim ysize = im->ysize;
1207 9 int samp_cnt = 3 * xsize;
1208  
1209 9 ▶ if (im->channels >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->channels >= 3) { 8 1 9
1210 8 samp_chans = NULL;
1211 }
1212 else {
1213 1 channels[0] = channels[1] = channels[2] = 0;
1214 1 samp_chans = channels;
1215 }
1216  
1217 9 ct = octt_new();
1218  
1219 9 samp = (i_sample_t *) i_malloc( xsize * 3 * sizeof(i_sample_t));
1220  
1221 9 colorcnt = 0;
1222 534 ▶ for(y = 0; y < ysize; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < ysize; ) { 526 8 534
1223 526 i_gsamp(im, 0, xsize, y++, samp, samp_chans, 3);
1224 45760 ▶ for(x = 0; x < samp_cnt; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < samp_cnt; ) { 45235 525 45760
1225 45235 colorcnt += octt_add(ct, samp[x], samp[x+1], samp[x+2]);
1226 45235 x += 3;
1227 45235 ▶ if (colorcnt > maxc) {
Branch: 1 branch
BranchTrueFalseTotal
if (colorcnt > maxc) { 1 45234 45235
1228 1 myfree(samp);
1229 1 octt_delete(ct);
1230 1 return -1;
1231 }
1232 }
1233 }
1234 8 i_free(samp);
1235 8 octt_delete(ct);
1236 8 return colorcnt;
1237 }
1238  
1239 /* sorts the array ra[0..n-1] into increasing order using heapsort algorithm
1240 * (adapted from the Numerical Recipes)
1241 */
1242 /* Needed by get_anonymous_color_histo */
1243 static void
1244 4 hpsort(unsigned int n, unsigned *ra) {
1245 unsigned int i,
1246 ir,
1247 j,
1248 l,
1249 rra;
1250  
1251 4 ▶ if (n < 2) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) return; 1 3 4
1252 3 l = n >> 1;
1253 3 ir = n - 1;
1254 for(;;) {
1255 8 ▶ if (l > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (l > 0) { 3 5 8
1256 3 rra = ra[--l];
1257 }
1258 else {
1259 5 rra = ra[ir];
1260 5 ra[ir] = ra[0];
1261 5 ▶ if (--ir == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ir == 0) { 3 2 5
1262 3 ra[0] = rra;
1263 3 break;
1264 }
1265 }
1266 5 i = l;
1267 5 j = 2 * l + 1;
1268 7 ▶ while (j <= ir) {
Branch: 1 branch
BranchTrueFalseTotal
while (j <= ir) { 5 2 7
1269 5 ▶ if (j < ir && ra[j] < ra[j+1]) j++;
Branch: 2 branches
BranchTrueFalseTotal
if (j < ir && ra[j] < ra[j+1]) j++; 2 3 5
if (j < ir && ra[j] < ra[j+1]) j++; 2 0 2
1270 5 ▶ if (rra < ra[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (rra < ra[j]) { 2 3 5
1271 2 ra[i] = ra[j];
1272 2 i = j;
1273 2 j++; j <<= 1; j--;
1274 }
1275 3 else break;
1276 }
1277 5 ra[i] = rra;
1278 }
1279 }
1280  
1281 /* This function constructs an ordered list which represents how much the
1282 * different colors are used. So for instance (100, 100, 500) means that one
1283 * color is used for 500 pixels, another for 100 pixels and another for 100
1284 * pixels. It's tuned for performance. You might not like the way I've hardcoded
1285 * the maxc ;-) and you might want to change the name... */
1286 /* Uses octt_histo */
1287 int
1288 5 i_get_anonymous_color_histo(i_img *im, unsigned int **col_usage, int maxc) {
1289 5 dIMCTXim(im);
1290 struct octt *ct;
1291 i_img_dim x,y;
1292 int colorcnt;
1293 unsigned int *col_usage_it;
1294 i_sample_t * samp;
1295 int channels[3];
1296 int *samp_chans;
1297
1298 5 i_img_dim xsize = im->xsize;
1299 5 i_img_dim ysize = im->ysize;
1300 5 int samp_cnt = 3 * xsize;
1301 5 ct = octt_new();
1302
1303 5 samp = (i_sample_t *) i_malloc( xsize * 3 * sizeof(i_sample_t));
1304
1305 5 ▶ if (im->channels >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->channels >= 3) { 4 1 5
1306 4 samp_chans = NULL;
1307 }
1308 else {
1309 1 channels[0] = channels[1] = channels[2] = 0;
1310 1 samp_chans = channels;
1311 }
1312  
1313 5 colorcnt = 0;
1314 230 ▶ for(y = 0; y < ysize; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < ysize; ) { 226 4 230
1315 226 i_gsamp(im, 0, xsize, y++, samp, samp_chans, 3);
1316 11476 ▶ for(x = 0; x < samp_cnt; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < samp_cnt; ) { 11251 225 11476
1317 11251 colorcnt += octt_add(ct, samp[x], samp[x+1], samp[x+2]);
1318 11251 x += 3;
1319 11251 ▶ if (colorcnt > maxc) {
Branch: 1 branch
BranchTrueFalseTotal
if (colorcnt > maxc) { 1 11250 11251
1320 1 octt_delete(ct);
1321 1 i_free(samp);
1322 1 return -1;
1323 }
1324 }
1325 }
1326 4 i_free(samp);
1327 /* Now that we know the number of colours... */
1328 4 col_usage_it = *col_usage = (unsigned int *) mymalloc(colorcnt * sizeof(unsigned int));
1329 4 octt_histo(ct, &col_usage_it);
1330 4 hpsort(colorcnt, *col_usage);
1331 4 octt_delete(ct);
1332 4 return colorcnt;
1333 }
1334  
1335 /*
1336 =back
1337  
1338 =head2 Image method wrappers
1339  
1340 These functions provide i_fsample_t functions in terms of their
1341 i_sample_t versions.
1342  
1343 =over
1344  
1345 =item i_ppixf_fp(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix)
1346  
1347 =cut
1348 */
1349  
1350 21 int i_ppixf_fp(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *pix) {
1351 i_color temp;
1352 int ch;
1353  
1354 84 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 63 21 84
1355 63 temp.channel[ch] = SampleFTo8(pix->channel[ch]);
1356
1357 21 return i_ppix(im, x, y, &temp);
1358 }
1359  
1360 /*
1361 =item i_gpixf_fp(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix)
1362  
1363 =cut
1364 */
1365 20 int i_gpixf_fp(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix) {
1366 i_color temp;
1367 int ch;
1368  
1369 20 ▶ if (i_gpix(im, x, y, &temp) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_gpix(im, x, y, &temp) == 0) { 20 0 20
1370 80 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 60 20 80
1371 60 pix->channel[ch] = Sample8ToF(temp.channel[ch]);
1372 20 return 0;
1373 }
1374 else
1375 0 return -1;
1376 }
1377  
1378 /*
1379 =item i_plinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *pix)
1380  
1381 =cut
1382 */
1383 i_img_dim
1384 0 i_plinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *pix) {
1385 i_color *work;
1386  
1387 0 ▶ if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
1388 0 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 0 0
1389 0 r = im->xsize;
1390 0 ▶ if (r > l) {
Branch: 1 branch
BranchTrueFalseTotal
if (r > l) { 0 0 0
1391 i_img_dim ret;
1392 i_img_dim i;
1393 int ch;
1394 0 dIMCTXim(im);
1395 0 work = i_malloc(sizeof(i_color) * (r-l));
1396 0 ▶ for (i = 0; i < r-l; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r-l; ++i) { 0 0 0
1397 0 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 0 0 0
1398 0 work[i].channel[ch] = SampleFTo8(pix[i].channel[ch]);
1399 }
1400 0 ret = i_plin(im, l, r, y, work);
1401 0 i_free(work);
1402  
1403 0 return ret;
1404 }
1405 else {
1406 0 return 0;
1407 }
1408 }
1409 else {
1410 0 return 0;
1411 }
1412 }
1413  
1414 /*
1415 =item i_glinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *pix)
1416  
1417 =cut
1418 */
1419 i_img_dim
1420 0 i_glinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *pix) {
1421 i_color *work;
1422  
1423 0 ▶ if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { 0 0 0
1424 0 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 0 0
1425 0 r = im->xsize;
1426 0 ▶ if (r > l) {
Branch: 1 branch
BranchTrueFalseTotal
if (r > l) { 0 0 0
1427 i_img_dim ret;
1428 i_img_dim i;
1429 int ch;
1430 0 dIMCTXim(im);
1431 0 work = i_malloc(sizeof(i_color) * (r-l));
1432 0 ret = i_plin(im, l, r, y, work);
1433 0 ▶ for (i = 0; i < r-l; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r-l; ++i) { 0 0 0
1434 0 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 0 0 0
1435 0 pix[i].channel[ch] = Sample8ToF(work[i].channel[ch]);
1436 }
1437 0 i_free(work);
1438  
1439 0 return ret;
1440 }
1441 else {
1442 0 return 0;
1443 }
1444 }
1445 else {
1446 0 return 0;
1447 }
1448 }
1449  
1450 /*
1451 =item i_gsampf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samp, int *chans, int chan_count)
1452  
1453 =cut
1454 */
1455  
1456 i_img_dim
1457 1 i_gsampf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samp,
1458 int const *chans, int chan_count) {
1459  
1460 1 ▶ if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0 && chan_count > 0) {
Branch: 5 branches
BranchTrueFalseTotal
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0 && chan_count > 0) { 1 0 1
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0 && chan_count > 0) { 1 0 1
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0 && chan_count > 0) { 1 0 1
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0 && chan_count > 0) { 1 0 1
if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0 && chan_count > 0) { 1 0 1
1461 1 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 1 1
1462 0 r = im->xsize;
1463 1 ▶ if (r > l) {
Branch: 1 branch
BranchTrueFalseTotal
if (r > l) { 1 0 1
1464 i_img_dim ret;
1465 i_img_dim i;
1466 1 dIMCTXim(im);
1467 size_t row_bytes;
1468 1 ▶ if (im_mult_overflow3(&row_bytes, sizeof(i_sample_t), (r-l), chan_count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (im_mult_overflow3(&row_bytes, sizeof(i_sample_t), (r-l), chan_count)) { 0 1 1
1469 0 im_push_error(aIMCTX, 0, "integer overflow calculating buffer size");
1470 0 return 0;
1471 }
1472 1 i_sample_t *work = i_malloc(row_bytes);
1473 1 ret = i_gsamp(im, l, r, y, work, chans, chan_count);
1474 601 ▶ for (i = 0; i < ret; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ret; ++i) { 600 1 601
1475 600 samp[i] = Sample8ToF(work[i]);
1476 }
1477 1 i_free(work);
1478  
1479 1 return ret;
1480 }
1481 else {
1482 0 return 0;
1483 }
1484 }
1485 else {
1486 0 return 0;
1487 }
1488 }
1489  
1490 /*
1491 =back
1492  
1493 =head2 Palette wrapper functions
1494  
1495 Used for virtual images, these forward palette calls to a wrapped image,
1496 assuming the wrapped image is the first pointer in the structure that
1497 im->ext_data points at.
1498  
1499 =over
1500  
1501 =item i_addcolors_forward(i_img *im, const i_color *colors, int count)
1502  
1503 =cut
1504 */
1505 1 int i_addcolors_forward(i_img *im, const i_color *colors, int count) {
1506 1 ▶ return i_addcolors(*(i_img **)im->ext_data, colors, count);
Branch: 1 branch
BranchTrueFalseTotal
return i_addcolors(*(i_img **)im->ext_data, colors, count); 1 0 1
1507 }
1508  
1509 /*
1510 =item i_getcolors_forward(i_img *im, int i, i_color *color, int count)
1511  
1512 =cut
1513 */
1514 1 int i_getcolors_forward(i_img *im, int i, i_color *color, int count) {
1515 1 ▶ return i_getcolors(*(i_img **)im->ext_data, i, color, count);
Branch: 1 branch
BranchTrueFalseTotal
return i_getcolors(*(i_img **)im->ext_data, i, color, count); 1 0 1
1516 }
1517  
1518 /*
1519 =item i_setcolors_forward(i_img *im, int i, const i_color *color, int count)
1520  
1521 =cut
1522 */
1523 0 int i_setcolors_forward(i_img *im, int i, const i_color *color, int count) {
1524 0 ▶ return i_setcolors(*(i_img **)im->ext_data, i, color, count);
Branch: 1 branch
BranchTrueFalseTotal
return i_setcolors(*(i_img **)im->ext_data, i, color, count); 0 0 0
1525 }
1526  
1527 /*
1528 =item i_colorcount_forward(i_img *im)
1529  
1530 =cut
1531 */
1532 5 int i_colorcount_forward(i_img *im) {
1533 5 ▶ return i_colorcount(*(i_img **)im->ext_data);
Branch: 1 branch
BranchTrueFalseTotal
return i_colorcount(*(i_img **)im->ext_data); 5 0 5
1534 }
1535  
1536 /*
1537 =item i_maxcolors_forward(i_img *im)
1538  
1539 =cut
1540 */
1541 0 int i_maxcolors_forward(i_img *im) {
1542 0 ▶ return i_maxcolors(*(i_img **)im->ext_data);
Branch: 1 branch
BranchTrueFalseTotal
return i_maxcolors(*(i_img **)im->ext_data); 0 0 0
1543 }
1544  
1545 /*
1546 =item i_findcolor_forward(i_img *im, const i_color *color, i_palidx *entry)
1547  
1548 =cut
1549 */
1550 0 int i_findcolor_forward(i_img *im, const i_color *color, i_palidx *entry) {
1551 0 ▶ return i_findcolor(*(i_img **)im->ext_data, color, entry);
Branch: 1 branch
BranchTrueFalseTotal
return i_findcolor(*(i_img **)im->ext_data, color, entry); 0 0 0
1552 }
1553  
1554 /*
1555 =back
1556  
1557 =head2 Fallback handler
1558  
1559 =over
1560  
1561 =item i_gsamp_bits_fb
1562  
1563 =cut
1564 */
1565  
1566 i_img_dim
1567 1 i_gsamp_bits_fb(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, unsigned *samps,
1568 const int *chans, int chan_count, int bits) {
1569 1 dIMCTXim(im);
1570  
1571 1 ▶ if (bits < 1 || bits > 32) {
Branch: 2 branches
BranchTrueFalseTotal
if (bits < 1 || bits > 32) { 1 0 1
if (bits < 1 || bits > 32) { 0 1 1
1572 0 i_push_error(0, "Invalid bits, must be 1..32");
1573 0 return -1;
1574 }
1575  
1576 1 ▶ if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { 1 0 1
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { 1 0 1
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { 1 0 1
if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { 1 0 1
1577 double scale;
1578 int ch;
1579 i_img_dim count, i, w;
1580
1581 1 ▶ if (bits == 32)
Branch: 1 branch
BranchTrueFalseTotal
if (bits == 32) 0 1 1
1582 0 scale = 4294967295.0;
1583 else
1584 1 scale = (double)(1 << bits) - 1;
1585  
1586 1 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 1 1
1587 0 r = im->xsize;
1588 1 w = r - l;
1589 1 count = 0;
1590  
1591 1 ▶ if (chans) {
Branch: 1 branch
BranchTrueFalseTotal
if (chans) { 1 0 1
1592 /* make sure we have good channel numbers */
1593 3 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 2 1 3
1594 2 ▶ if (chans[ch] < 0 || chans[ch] >= im->channels) {
Branch: 2 branches
BranchTrueFalseTotal
if (chans[ch] < 0 || chans[ch] >= im->channels) { 2 0 2
if (chans[ch] < 0 || chans[ch] >= im->channels) { 0 2 2
1595 0 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
1596 0 return -1;
1597 }
1598 }
1599 5 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 4 1 5
1600 i_fcolor c;
1601 4 i_gpixf(im, l+i, y, &c);
1602 12 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 8 4 12
1603 8 *samps++ = (unsigned)(c.channel[ch] * scale + 0.5);
1604 8 ++count;
1605 }
1606 }
1607 }
1608 else {
1609 0 ▶ if (chan_count <= 0 || chan_count > im->channels) {
Branch: 2 branches
BranchTrueFalseTotal
if (chan_count <= 0 || chan_count > im->channels) { 0 0 0
if (chan_count <= 0 || chan_count > im->channels) { 0 0 0
1610 0 i_push_error(0, "Invalid channel count");
1611 0 return -1;
1612 }
1613 0 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 0 0 0
1614 i_fcolor c;
1615 0 i_gpixf(im, l+i, y, &c);
1616 0 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 0 0 0
1617 0 *samps++ = (unsigned)(c.channel[ch] * scale + 0.5);
1618 0 ++count;
1619 }
1620 }
1621 }
1622  
1623 1 return count;
1624 }
1625 else {
1626 0 i_push_error(0, "Image position outside of image");
1627 0 return -1;
1628 }
1629 }
1630  
1631 static int
1632 4203 test_magic(unsigned char *buffer, size_t length, struct file_magic_entry const *magic) {
1633 4203 ▶ if (length < magic->magic_size)
Branch: 1 branch
BranchTrueFalseTotal
if (length < magic->magic_size) 52 4151 4203
1634 52 return 0;
1635 4151 ▶ if (magic->mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (magic->mask) { 663 3488 4151
1636 size_t i;
1637 663 unsigned char *bufp = buffer,
1638 663 *maskp = magic->mask,
1639 663 *magicp = magic->magic;
1640  
1641 2983 ▶ for (i = 0; i < magic->magic_size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < magic->magic_size; ++i) { 2976 7 2983
1642 2976 ▶ int mask = *maskp == 'x' ? 0xFF : *maskp == ' ' ? 0 : *maskp;
Branch: 2 branches
BranchTrueFalseTotal
int mask = *maskp == 'x' ? 0xFF : *maskp == ' ' ? 0 : *maskp; 2188 788 2976
int mask = *maskp == 'x' ? 0xFF : *maskp == ' ' ? 0 : *maskp; 0 2188 2188
1643 2976 ++maskp;
1644  
1645 2976 ▶ if ((*bufp++ & mask) != (*magicp++ & mask))
Branch: 1 branch
BranchTrueFalseTotal
if ((*bufp++ & mask) != (*magicp++ & mask)) 656 2320 2976
1646 656 return 0;
1647 }
1648  
1649 7 return 1;
1650 }
1651 else {
1652 3488 return !memcmp(magic->magic, buffer, magic->magic_size);
1653 }
1654 }
1655  
1656 /*
1657 =item i_test_format_probe(io_glue *data, int length)
1658  
1659 Check the beginning of the supplied file for a 'magic number'
1660  
1661 =cut
1662 */
1663  
1664 #define FORMAT_ENTRY(magic, type) \
1665 { (unsigned char *)(magic ""), sizeof(magic)-1, type, NULL }
1666 #define FORMAT_ENTRY2(magic, type, mask) \
1667 { (unsigned char *)(magic ""), sizeof(magic)-1, type, (unsigned char *)(mask) }
1668  
1669 const char *
1670 189 im_test_format_probe(im_context_t ctx, io_glue *data, int length) {
1671 static const struct file_magic_entry formats[] = {
1672 FORMAT_ENTRY("\xFF\xD8", "jpeg"),
1673 FORMAT_ENTRY("GIF87a", "gif"),
1674 FORMAT_ENTRY("GIF89a", "gif"),
1675 FORMAT_ENTRY("MM\0*", "tiff"),
1676 FORMAT_ENTRY("II*\0", "tiff"),
1677 FORMAT_ENTRY("BM", "bmp"),
1678 FORMAT_ENTRY("\x89PNG\x0d\x0a\x1a\x0a", "png"),
1679 FORMAT_ENTRY("P1", "pnm"),
1680 FORMAT_ENTRY("P2", "pnm"),
1681 FORMAT_ENTRY("P3", "pnm"),
1682 FORMAT_ENTRY("P4", "pnm"),
1683 FORMAT_ENTRY("P5", "pnm"),
1684 FORMAT_ENTRY("P6", "pnm"),
1685 FORMAT_ENTRY("/* XPM", "xpm"),
1686 FORMAT_ENTRY("\x8aMNG", "mng"),
1687 FORMAT_ENTRY("\x8aJNG", "jng"),
1688 /* SGI RGB - with various possible parameters to avoid false positives
1689 on similar files
1690 values are: 2 byte magic, rle flags (0 or 1), bytes/sample (1 or 2)
1691 */
1692 FORMAT_ENTRY("\x01\xDA\x00\x01", "sgi"),
1693 FORMAT_ENTRY("\x01\xDA\x00\x02", "sgi"),
1694 FORMAT_ENTRY("\x01\xDA\x01\x01", "sgi"),
1695 FORMAT_ENTRY("\x01\xDA\x01\x02", "sgi"),
1696
1697 FORMAT_ENTRY2("FORM ILBM", "ilbm", "xxxx xxxx"),
1698  
1699 /* different versions of PCX format
1700 http://www.fileformat.info/format/pcx/
1701 */
1702 FORMAT_ENTRY("\x0A\x00\x01", "pcx"),
1703 FORMAT_ENTRY("\x0A\x02\x01", "pcx"),
1704 FORMAT_ENTRY("\x0A\x03\x01", "pcx"),
1705 FORMAT_ENTRY("\x0A\x04\x01", "pcx"),
1706 FORMAT_ENTRY("\x0A\x05\x01", "pcx"),
1707  
1708 /* FITS - http://fits.gsfc.nasa.gov/ */
1709 FORMAT_ENTRY("SIMPLE =", "fits"),
1710  
1711 /* PSD - Photoshop */
1712 FORMAT_ENTRY("8BPS\x00\x01", "psd"),
1713
1714 /* EPS - Encapsulated Postscript */
1715 /* only reading 18 chars, so we don't include the F in EPSF */
1716 FORMAT_ENTRY("%!PS-Adobe-2.0 EPS", "eps"),
1717  
1718 /* Utah RLE */
1719 FORMAT_ENTRY("\x52\xCC", "utah"),
1720  
1721 /* GZIP compressed, only matching deflate for now */
1722 FORMAT_ENTRY("\x1F\x8B\x08", "gzip"),
1723  
1724 /* bzip2 compressed */
1725 FORMAT_ENTRY("BZh", "bzip2"),
1726  
1727 /* WEBP
1728 http://code.google.com/speed/webp/docs/riff_container.html */
1729 FORMAT_ENTRY2("RIFF WEBP", "webp", "xxxx xxxx"),
1730  
1731 /* JPEG 2000
1732 This might match a little loosely */
1733 FORMAT_ENTRY("\x00\x00\x00\x0CjP \x0D\x0A\x87\x0A", "jp2"),
1734  
1735 /* FLIF - Free Lossless Image Format - https://flif.info/spec.html */
1736 FORMAT_ENTRY("FLIF", "flif"),
1737  
1738 /* JPEG XL */
1739 FORMAT_ENTRY("\xFF\x0A", "jxl"), /* simple */
1740 FORMAT_ENTRY("\x00\x00\x00\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", "jxl"), /* complex */
1741  
1742 /* Quite OK Image Format */
1743 FORMAT_ENTRY("qoif", "qoi"),
1744  
1745 /* HEIF/HEIC see https://github.com/strukturag/libheif/issues/83 */
1746 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheic", "heif", " xxxxxxxx"),
1747 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheix", "heif", " xxxxxxxx"),
1748 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftyphevc", "heif", " xxxxxxxx"),
1749 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheim", "heif", " xxxxxxxx"),
1750 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheis", "heif", " xxxxxxxx"),
1751 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftyphevm", "heif", " xxxxxxxx"),
1752 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftyphevs", "heif", " xxxxxxxx"),
1753 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypmif1", "heif", " xxxxxxxx"),
1754 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypmsf1", "heif", " xxxxxxxx"),
1755  
1756 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypavif", "avif", " xxxxxxxx"),
1757 /* AV1 image sequence */
1758 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypavis", "avif", " xxxxxxxx")
1759 };
1760 static const struct file_magic_entry more_formats[] = {
1761 /* these were originally both listed as ico, but cur files can
1762 include hotspot information */
1763 FORMAT_ENTRY("\x00\x00\x01\x00", "ico"), /* Windows icon */
1764 FORMAT_ENTRY("\x00\x00\x02\x00", "cur"), /* Windows cursor */
1765 FORMAT_ENTRY2("\x00\x00\x00\x00\x00\x00\x00\x07",
1766 "xwd", " xxxx"), /* X Windows Dump */
1767 };
1768 (void)length; /* FIXME? */
1769 unsigned int i;
1770 unsigned char head[18];
1771 ssize_t rc;
1772  
1773 189 rc = i_io_peekn(data, head, 18);
1774 189 ▶ if (rc == -1) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == -1) return NULL; 0 189 189
1775 #if 0
1776 {
1777 int i;
1778 fprintf(stderr, "%d bytes -", (int)rc);
1779 for (i = 0; i < rc; ++i)
1780 fprintf(stderr, " %02x", head[i]);
1781 fprintf(stderr, "\n");
1782 }
1783 #endif
1784  
1785 {
1786 189 im_file_magic *p = ctx->file_magic;
1787 199 ▶ while (p) {
Branch: 1 branch
BranchTrueFalseTotal
while (p) { 11 188 199
1788 11 ▶ if (test_magic(head, rc, &p->m)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_magic(head, rc, &p->m)) { 1 10 11
1789 1 return p->m.name;
1790 }
1791 10 p = p->next;
1792 }
1793 }
1794  
1795 4189 ▶ for(i=0; i<sizeof(formats)/sizeof(formats[0]); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<sizeof(formats)/sizeof(formats[0]); i++) { 4141 48 4189
1796 4141 struct file_magic_entry const *entry = formats + i;
1797  
1798 4141 ▶ if (test_magic(head, rc, entry))
Branch: 1 branch
BranchTrueFalseTotal
if (test_magic(head, rc, entry)) 140 4001 4141
1799 140 return entry->name;
1800 }
1801  
1802 92 if ((rc == 18) &&
1803 44 tga_header_verify(head))
1804 18 return "tga";
1805  
1806 58 ▶ for(i=0; i<sizeof(more_formats)/sizeof(more_formats[0]); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<sizeof(more_formats)/sizeof(more_formats[0]); i++) { 51 7 58
1807 51 struct file_magic_entry const *entry = more_formats + i;
1808  
1809 51 ▶ if (test_magic(head, rc, entry))
Branch: 1 branch
BranchTrueFalseTotal
if (test_magic(head, rc, entry)) 23 28 51
1810 23 return entry->name;
1811 }
1812  
1813 7 return NULL;
1814 }
1815  
1816 /*
1817 =item i_img_is_monochrome(img, &zero_is_white)
1818  
1819 =category Image Information
1820  
1821 Tests an image to check it meets our monochrome tests.
1822  
1823 The idea is that a file writer can use this to test where it should
1824 write the image in whatever bi-level format it uses, eg. C<pbm> for
1825 C<pnm>.
1826  
1827 For performance of encoders we require monochrome images:
1828  
1829 =over
1830  
1831 =item *
1832  
1833 be paletted
1834  
1835 =item *
1836  
1837 have a palette of two colors, containing only C<(0,0,0)> and
1838 C<(255,255,255)> in either order.
1839  
1840 =back
1841  
1842 C<zero_is_white> is set to non-zero if the first palette entry is white.
1843  
1844 =cut
1845 */
1846  
1847 int
1848 162 i_img_is_monochrome(i_img *im, int *zero_is_white) {
1849 162 ▶ if (im->type == i_palette_type
Branch: 1 branch
BranchTrueFalseTotal
if (im->type == i_palette_type 20 142 162
1850 20 ▶ && i_colorcount(im) == 2) {
Branch: 2 branches
BranchTrueFalseTotal
&& i_colorcount(im) == 2) { 20 0 20
&& i_colorcount(im) == 2) { 17 3 20
1851 i_color colors[2];
1852 17 ▶ if (!i_getcolors(im, 0, colors, 2))
Branch: 2 branches
BranchTrueFalseTotal
if (!i_getcolors(im, 0, colors, 2)) 17 0 17
if (!i_getcolors(im, 0, colors, 2)) 0 17 17
1853 16 return 0;
1854 17 ▶ if (im->channels == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->channels == 3) { 11 6 17
1855 11 ▶ if (colors[0].rgb.r == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
if (colors[0].rgb.r == 255 && 2 9 11
1856 2 ▶ colors[0].rgb.g == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.g == 255 && 2 0 2
1857 2 ▶ colors[0].rgb.b == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.b == 255 && 2 0 2
1858 2 ▶ colors[1].rgb.r == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.r == 0 && 2 0 2
1859 2 ▶ colors[1].rgb.g == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.g == 0 && 2 0 2
1860 2 ▶ colors[1].rgb.b == 0) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.b == 0) { 2 0 2
1861 2 *zero_is_white = 1;
1862 2 return 1;
1863 }
1864 9 ▶ else if (colors[0].rgb.r == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
else if (colors[0].rgb.r == 0 && 8 1 9
1865 8 ▶ colors[0].rgb.g == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.g == 0 && 8 0 8
1866 8 ▶ colors[0].rgb.b == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.b == 0 && 8 0 8
1867 8 ▶ colors[1].rgb.r == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.r == 255 && 8 0 8
1868 8 ▶ colors[1].rgb.g == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.g == 255 && 8 0 8
1869 8 ▶ colors[1].rgb.b == 255) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.b == 255) { 8 0 8
1870 8 *zero_is_white = 0;
1871 8 return 1;
1872 }
1873 }
1874 6 ▶ else if (im->channels == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (im->channels == 1) { 6 0 6
1875 6 ▶ if (colors[0].channel[0] == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
if (colors[0].channel[0] == 255 && 3 3 6
1876 3 ▶ colors[1].channel[0] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].channel[0] == 0) { 3 0 3
1877 3 *zero_is_white = 1;
1878 3 return 1;
1879 }
1880 3 ▶ else if (colors[0].channel[0] == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
else if (colors[0].channel[0] == 0 && 3 0 3
1881 3 ▶ colors[1].channel[0] == 255) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].channel[0] == 255) { 3 0 3
1882 3 *zero_is_white = 0;
1883 3 return 1;
1884 }
1885 }
1886 }
1887  
1888 146 *zero_is_white = 0;
1889 146 return 0;
1890 }
1891  
1892 /*
1893 =item i_get_file_background(im, &bg)
1894  
1895 =category Files
1896  
1897 Retrieve the file write background color tag from the image.
1898  
1899 If not present, C<bg> is set to black.
1900  
1901 Returns 1 if the C<i_background> tag was found and valid.
1902  
1903 =cut
1904 */
1905  
1906 int
1907 49 i_get_file_background(i_img *im, i_color *bg) {
1908 49 int result = i_tags_get_color(&im->tags, "i_background", 0, bg);
1909 49 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 46 3 49
1910 /* black default */
1911 46 bg->channel[0] = bg->channel[1] = bg->channel[2] = 0;
1912 }
1913 /* always full alpha */
1914 49 bg->channel[3] = 255;
1915  
1916 49 return result;
1917 }
1918  
1919 /*
1920 =item i_get_file_backgroundf(im, &bg)
1921  
1922 =category Files
1923  
1924 Retrieve the file write background color tag from the image as a
1925 floating point color.
1926  
1927 Implemented in terms of i_get_file_background().
1928  
1929 If not present, C<bg> is set to black.
1930  
1931 Returns 1 if the C<i_background> tag was found and valid.
1932  
1933 =cut
1934 */
1935  
1936 int
1937 14 i_get_file_backgroundf(i_img *im, i_fcolor *fbg) {
1938 i_color bg;
1939 14 int result = i_get_file_background(im, &bg);
1940 14 fbg->rgba.r = Sample8ToF(bg.rgba.r);
1941 14 fbg->rgba.g = Sample8ToF(bg.rgba.g);
1942 14 fbg->rgba.b = Sample8ToF(bg.rgba.b);
1943 14 fbg->rgba.a = 1.0;
1944  
1945 14 return result;
1946 }
1947  
1948 /*
1949 =back
1950  
1951 =head1 AUTHOR
1952  
1953 Arnar M. Hrafnkelsson <addi@umich.edu>
1954  
1955 Tony Cook <tony@develop-help.com>
1956  
1957 =head1 SEE ALSO
1958  
1959 L<Imager>, L<gif.c>
1960  
1961 =cut
1962 */
« tags.c ↑ Summary Imager.xs »