image.c

filter: Statementstmt 75.1% 517 / 688 Branchbran 62.7% 270 / 430 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.3% 787 / 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 1500 im_img_alloc(pIMCTX) {
78 1500 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 1509 im_img_init(pIMCTX, i_img *img) {
97 1509 img->im_data = NULL;
98 1509 img->context = aIMCTX;
99 1509 im_context_refinc(aIMCTX, "img_init");
100 1509 }
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 2970 ICL_new_internal(unsigned char r,unsigned char g,unsigned char b,unsigned char a) {
117 2970 i_color *cl = NULL;
118 2970 dIMCTX;
119  
120 2970 im_log((aIMCTX,1,"ICL_new_internal(r %d,g %d,b %d,a %d)\n", r, g, b, a));
121  
122 2970 ▶ if ( (cl=i_malloc(sizeof(i_color))) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ( (cl=i_malloc(sizeof(i_color))) == NULL) 0 2970 2970
123 0 im_fatal(aIMCTX, 2,"malloc() error\n");
124 2970 cl->rgba.r = r;
125 2970 cl->rgba.g = g;
126 2970 cl->rgba.b = b;
127 2970 cl->rgba.a = a;
128 2970 im_log((aIMCTX,1,"(%p) <- ICL_new_internal\n",cl));
129 2970 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 3948 ICL_DESTROY(i_color *cl) {
213 3948 dIMCTX;
214 3948 im_log((aIMCTX, 1,"ICL_DESTROY(cl* %p)\n",cl));
215 3948 i_free(cl);
216 3948 }
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 1509 do_img_exorcise(pIMCTX, i_img *im) {
250 1509 i_tags_destroy(&im->tags);
251 1509 ▶ if (im->i_f_destroy)
Branch: 1 branch
BranchTrueFalseTotal
if (im->i_f_destroy) 180 1329 1509
252 180 (im->i_f_destroy)(im);
253 1509 ▶ if (im->idata != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->idata != NULL) { 1492 17 1509
254 1492 i_free(im->idata);
255 }
256 1509 im->idata = NULL;
257 1509 im->xsize = 0;
258 1509 im->ysize = 0;
259 1509 im->channels = 0;
260  
261 1509 im->ext_data=NULL;
262 1509 }
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 9 i_img_exorcise(i_img *im) {
276 9 dIMCTXim(im);
277 9 im_log((aIMCTX,1,"i_img_exorcise(im* %p)\n",im));
278 9 do_img_exorcise(aIMCTX, im);
279 9 im_context_refdec(aIMCTX, "img_destroy");
280 9 }
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 1500 i_img_destroy(i_img *im) {
295 1500 dIMCTXim(im);
296 1500 im_log((aIMCTX, 1,"i_img_destroy(im %p)\n",im));
297 1500 do_img_exorcise(aIMCTX, im);
298 1500 i_free(im);
299 1500 im_context_refdec(aIMCTX, "img_destroy");
300 1500 }
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_empty_ch(NULL,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 373 i_sametype(i_img *src, i_img_dim xsize, i_img_dim ysize) {
883 373 dIMCTXim(src);
884  
885 373 ▶ if (src->type == i_direct_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->type == i_direct_type) { 355 18 373
886 355 ▶ if (src->bits == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->bits == 8) { 289 66 355
887 289 return i_img_empty_ch(NULL, xsize, ysize, src->channels);
888 }
889 66 ▶ else if (src->bits == i_16_bits) {
Branch: 1 branch
BranchTrueFalseTotal
else if (src->bits == i_16_bits) { 22 44 66
890 22 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_empty_ch(NULL, 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_empty_ch(NULL,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 /* fprintf(stderr,"parm[2]=%f\n",parm[2]); */
984 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
985 /* parm[parmlen-2]=(double)nx;
986 parm[parmlen-1]=(double)ny; */
987  
988 0 parm[0]=(double)nx;
989 0 parm[1]=(double)ny;
990  
991 /* fprintf(stderr,"(%d,%d) ->",nx,ny); */
992 0 rx=i_op_run(opx,opxl,parm,parmlen);
993 0 ry=i_op_run(opy,opyl,parm,parmlen);
994 /* fprintf(stderr,"(%f,%f)\n",rx,ry); */
995 0 i_gpix(im,rx,ry,&val);
996 0 i_ppix(new_img,nx,ny,&val);
997 }
998  
999 0 im_log((aIMCTX, 1,"(%p) <- i_transform\n",new_img));
1000 0 return new_img;
1001 }
1002  
1003 /*
1004 =item i_img_diff(im1, im2)
1005  
1006 Calculates the sum of the squares of the differences between
1007 correspoding channels in two images.
1008  
1009 If the images are not the same size then only the common area is
1010 compared, hence even if images are different sizes this function
1011 can return zero.
1012  
1013 =cut
1014 */
1015  
1016 float
1017 343 i_img_diff(i_img *im1,i_img *im2) {
1018 i_img_dim x, y, xb, yb;
1019 int ch, chb;
1020 float tdiff;
1021 i_color val1,val2;
1022 343 dIMCTXim(im1);
1023  
1024 343 im_log((aIMCTX, 1,"i_img_diff(im1 %p,im2 %p)\n",im1,im2));
1025  
1026 343 xb=(im1->xsize<im2->xsize)?im1->xsize:im2->xsize;
1027 343 yb=(im1->ysize<im2->ysize)?im1->ysize:im2->ysize;
1028 343 chb=(im1->channels<im2->channels)?im1->channels:im2->channels;
1029  
1030 343 im_log((aIMCTX, 1,"i_img_diff: b=(" i_DFp ") chb=%d\n",
1031 i_DFcp(xb,yb), chb));
1032  
1033 343 tdiff=0;
1034 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
1035 2894113 i_gpix(im1,x,y,&val1);
1036 2894113 i_gpix(im2,x,y,&val2);
1037  
1038 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
1039 }
1040 343 im_log((aIMCTX, 1,"i_img_diff <- (%.2f)\n",tdiff));
1041 343 return tdiff;
1042 }
1043  
1044 /*
1045 =item i_img_diffd(im1, im2)
1046  
1047 Calculates the sum of the squares of the differences between
1048 correspoding channels in two images.
1049  
1050 If the images are not the same size then only the common area is
1051 compared, hence even if images are different sizes this function
1052 can return zero.
1053  
1054 This is like i_img_diff() but looks at floating point samples instead.
1055  
1056 =cut
1057 */
1058  
1059 double
1060 0 i_img_diffd(i_img *im1,i_img *im2) {
1061 i_img_dim x, y, xb, yb;
1062 int ch, chb;
1063 double tdiff;
1064 i_fcolor val1,val2;
1065 0 dIMCTXim(im1);
1066  
1067 0 im_log((aIMCTX, 1,"i_img_diffd(im1 %p,im2 %p)\n",im1,im2));
1068  
1069 0 xb=(im1->xsize<im2->xsize)?im1->xsize:im2->xsize;
1070 0 yb=(im1->ysize<im2->ysize)?im1->ysize:im2->ysize;
1071 0 chb=(im1->channels<im2->channels)?im1->channels:im2->channels;
1072  
1073 0 im_log((aIMCTX, 1,"i_img_diffd: b(" i_DFp ") chb=%d\n",
1074 i_DFcp(xb, yb), chb));
1075  
1076 0 tdiff=0;
1077 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
1078 0 i_gpixf(im1,x,y,&val1);
1079 0 i_gpixf(im2,x,y,&val2);
1080  
1081 0 ▶ for(ch=0;ch<chb;ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<chb;ch++) { 0 0 0
1082 0 double sdiff = val1.channel[ch]-val2.channel[ch];
1083 0 tdiff += sdiff * sdiff;
1084 }
1085 }
1086 0 im_log((aIMCTX, 1,"i_img_diffd <- (%.2f)\n",tdiff));
1087  
1088 0 return tdiff;
1089 }
1090  
1091 int
1092 29 i_img_samef(i_img *im1,i_img *im2, double epsilon, char const *what) {
1093 i_img_dim x,y,xb,yb;
1094 int ch, chb;
1095 i_fcolor val1,val2;
1096 29 dIMCTXim(im1);
1097  
1098 29 ▶ if (what == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (what == NULL) 0 29 29
1099 0 what = "(null)";
1100  
1101 29 im_log((aIMCTX,1,"i_img_samef(im1 %p,im2 %p, epsilon %g, what '%s')\n", im1, im2, epsilon, what));
1102  
1103 29 xb=(im1->xsize<im2->xsize)?im1->xsize:im2->xsize;
1104 29 yb=(im1->ysize<im2->ysize)?im1->ysize:im2->ysize;
1105 29 chb=(im1->channels<im2->channels)?im1->channels:im2->channels;
1106  
1107 29 im_log((aIMCTX, 1,"i_img_samef: b(" i_DFp ") chb=%d\n",
1108 i_DFcp(xb, yb), chb));
1109  
1110 3129 ▶ for(y = 0; y < yb; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < yb; y++) { 3100 29 3129
1111 343100 ▶ for(x = 0; x < xb; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < xb; x++) { 340000 3100 343100
1112 340000 i_gpixf(im1, x, y, &val1);
1113 340000 i_gpixf(im2, x, y, &val2);
1114
1115 1360000 ▶ for(ch = 0; ch < chb; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < chb; ch++) { 1020000 340000 1360000
1116 1020000 double sdiff = val1.channel[ch] - val2.channel[ch];
1117 1020000 ▶ if (fabs(sdiff) > epsilon) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(sdiff) > epsilon) { 0 1020000 1020000
1118 0 im_log((aIMCTX, 1,"i_img_samef <- different %g @(" i_DFp ")\n",
1119 sdiff, i_DFcp(x, y)));
1120 0 return 0;
1121 }
1122 }
1123 }
1124 }
1125 29 im_log((aIMCTX, 1,"i_img_samef <- same\n"));
1126  
1127 29 return 1;
1128 }
1129  
1130 /* just a tiny demo of haar wavelets */
1131  
1132 i_img*
1133 0 i_haar(i_img *im) {
1134 i_img_dim mx,my;
1135 i_img_dim fx,fy;
1136 i_img_dim x,y;
1137 int ch;
1138 i_img *new_img,*new_img2;
1139 i_color val1,val2,dval1,dval2;
1140 0 dIMCTXim(im);
1141
1142 0 mx=im->xsize;
1143 0 my=im->ysize;
1144 0 fx=(mx+1)/2;
1145 0 fy=(my+1)/2;
1146  
1147  
1148 /* horizontal pass */
1149
1150 0 new_img=i_img_empty_ch(NULL,fx*2,fy*2,im->channels);
1151 0 ▶ if (!new_img)
Branch: 1 branch
BranchTrueFalseTotal
if (!new_img) 0 0 0
1152 0 return NULL;
1153 0 new_img2=i_img_empty_ch(NULL,fx*2,fy*2,im->channels);
1154 0 ▶ if (!new_img2) {
Branch: 1 branch
BranchTrueFalseTotal
if (!new_img2) { 0 0 0
1155 0 i_img_destroy(new_img);
1156 0 return NULL;
1157 }
1158  
1159 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
1160 0 i_gpix(im,x*2,y,&val1);
1161 0 i_gpix(im,x*2+1,y,&val2);
1162 0 ▶ for(ch=0;ch<im->channels;ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) { 0 0 0
1163 0 dval1.channel[ch]=(val1.channel[ch]+val2.channel[ch])/2;
1164 0 dval2.channel[ch]=(255+val1.channel[ch]-val2.channel[ch])/2;
1165 }
1166 0 i_ppix(new_img,x,y,&dval1);
1167 0 i_ppix(new_img,x+fx,y,&dval2);
1168 }
1169  
1170 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
1171 0 i_gpix(new_img,x,y*2,&val1);
1172 0 i_gpix(new_img,x,y*2+1,&val2);
1173 0 ▶ for(ch=0;ch<im->channels;ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) { 0 0 0
1174 0 dval1.channel[ch]=(val1.channel[ch]+val2.channel[ch])/2;
1175 0 dval2.channel[ch]=(255+val1.channel[ch]-val2.channel[ch])/2;
1176 }
1177 0 i_ppix(new_img2,x,y,&dval1);
1178 0 i_ppix(new_img2,x,y+fy,&dval2);
1179 }
1180  
1181 0 i_img_destroy(new_img);
1182 0 return new_img2;
1183 }
1184  
1185 /*
1186 =item i_count_colors(im, maxc)
1187  
1188 returns number of colors or -1
1189 to indicate that it was more than max colors
1190  
1191 =cut
1192 */
1193 /* This function has been changed and is now faster. It's using
1194 * i_gsamp instead of i_gpix */
1195 int
1196 9 i_count_colors(i_img *im,int maxc) {
1197 9 dIMCTXim(im);
1198 struct octt *ct;
1199 i_img_dim x,y;
1200 int colorcnt;
1201 int channels[3];
1202 int *samp_chans;
1203 i_sample_t * samp;
1204 9 i_img_dim xsize = im->xsize;
1205 9 i_img_dim ysize = im->ysize;
1206 9 int samp_cnt = 3 * xsize;
1207  
1208 9 ▶ if (im->channels >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->channels >= 3) { 8 1 9
1209 8 samp_chans = NULL;
1210 }
1211 else {
1212 1 channels[0] = channels[1] = channels[2] = 0;
1213 1 samp_chans = channels;
1214 }
1215  
1216 9 ct = octt_new();
1217  
1218 9 samp = (i_sample_t *) i_malloc( xsize * 3 * sizeof(i_sample_t));
1219  
1220 9 colorcnt = 0;
1221 534 ▶ for(y = 0; y < ysize; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < ysize; ) { 526 8 534
1222 526 i_gsamp(im, 0, xsize, y++, samp, samp_chans, 3);
1223 45760 ▶ for(x = 0; x < samp_cnt; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < samp_cnt; ) { 45235 525 45760
1224 45235 colorcnt += octt_add(ct, samp[x], samp[x+1], samp[x+2]);
1225 45235 x += 3;
1226 45235 ▶ if (colorcnt > maxc) {
Branch: 1 branch
BranchTrueFalseTotal
if (colorcnt > maxc) { 1 45234 45235
1227 1 myfree(samp);
1228 1 octt_delete(ct);
1229 1 return -1;
1230 }
1231 }
1232 }
1233 8 i_free(samp);
1234 8 octt_delete(ct);
1235 8 return colorcnt;
1236 }
1237  
1238 /* sorts the array ra[0..n-1] into increasing order using heapsort algorithm
1239 * (adapted from the Numerical Recipes)
1240 */
1241 /* Needed by get_anonymous_color_histo */
1242 static void
1243 4 hpsort(unsigned int n, unsigned *ra) {
1244 unsigned int i,
1245 ir,
1246 j,
1247 l,
1248 rra;
1249  
1250 4 ▶ if (n < 2) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) return; 1 3 4
1251 3 l = n >> 1;
1252 3 ir = n - 1;
1253 for(;;) {
1254 8 ▶ if (l > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (l > 0) { 3 5 8
1255 3 rra = ra[--l];
1256 }
1257 else {
1258 5 rra = ra[ir];
1259 5 ra[ir] = ra[0];
1260 5 ▶ if (--ir == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ir == 0) { 3 2 5
1261 3 ra[0] = rra;
1262 3 break;
1263 }
1264 }
1265 5 i = l;
1266 5 j = 2 * l + 1;
1267 7 ▶ while (j <= ir) {
Branch: 1 branch
BranchTrueFalseTotal
while (j <= ir) { 5 2 7
1268 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
1269 5 ▶ if (rra < ra[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (rra < ra[j]) { 2 3 5
1270 2 ra[i] = ra[j];
1271 2 i = j;
1272 2 j++; j <<= 1; j--;
1273 }
1274 3 else break;
1275 }
1276 5 ra[i] = rra;
1277 }
1278 }
1279  
1280 /* This function constructs an ordered list which represents how much the
1281 * different colors are used. So for instance (100, 100, 500) means that one
1282 * color is used for 500 pixels, another for 100 pixels and another for 100
1283 * pixels. It's tuned for performance. You might not like the way I've hardcoded
1284 * the maxc ;-) and you might want to change the name... */
1285 /* Uses octt_histo */
1286 int
1287 5 i_get_anonymous_color_histo(i_img *im, unsigned int **col_usage, int maxc) {
1288 5 dIMCTXim(im);
1289 struct octt *ct;
1290 i_img_dim x,y;
1291 int colorcnt;
1292 unsigned int *col_usage_it;
1293 i_sample_t * samp;
1294 int channels[3];
1295 int *samp_chans;
1296
1297 5 i_img_dim xsize = im->xsize;
1298 5 i_img_dim ysize = im->ysize;
1299 5 int samp_cnt = 3 * xsize;
1300 5 ct = octt_new();
1301
1302 5 samp = (i_sample_t *) i_malloc( xsize * 3 * sizeof(i_sample_t));
1303
1304 5 ▶ if (im->channels >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->channels >= 3) { 4 1 5
1305 4 samp_chans = NULL;
1306 }
1307 else {
1308 1 channels[0] = channels[1] = channels[2] = 0;
1309 1 samp_chans = channels;
1310 }
1311  
1312 5 colorcnt = 0;
1313 230 ▶ for(y = 0; y < ysize; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < ysize; ) { 226 4 230
1314 226 i_gsamp(im, 0, xsize, y++, samp, samp_chans, 3);
1315 11476 ▶ for(x = 0; x < samp_cnt; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < samp_cnt; ) { 11251 225 11476
1316 11251 colorcnt += octt_add(ct, samp[x], samp[x+1], samp[x+2]);
1317 11251 x += 3;
1318 11251 ▶ if (colorcnt > maxc) {
Branch: 1 branch
BranchTrueFalseTotal
if (colorcnt > maxc) { 1 11250 11251
1319 1 octt_delete(ct);
1320 1 i_free(samp);
1321 1 return -1;
1322 }
1323 }
1324 }
1325 4 i_free(samp);
1326 /* Now that we know the number of colours... */
1327 4 col_usage_it = *col_usage = (unsigned int *) mymalloc(colorcnt * sizeof(unsigned int));
1328 4 octt_histo(ct, &col_usage_it);
1329 4 hpsort(colorcnt, *col_usage);
1330 4 octt_delete(ct);
1331 4 return colorcnt;
1332 }
1333  
1334 /*
1335 =back
1336  
1337 =head2 Image method wrappers
1338  
1339 These functions provide i_fsample_t functions in terms of their
1340 i_sample_t versions.
1341  
1342 =over
1343  
1344 =item i_ppixf_fp(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix)
1345  
1346 =cut
1347 */
1348  
1349 21 int i_ppixf_fp(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *pix) {
1350 i_color temp;
1351 int ch;
1352  
1353 84 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 63 21 84
1354 63 temp.channel[ch] = SampleFTo8(pix->channel[ch]);
1355
1356 21 return i_ppix(im, x, y, &temp);
1357 }
1358  
1359 /*
1360 =item i_gpixf_fp(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix)
1361  
1362 =cut
1363 */
1364 20 int i_gpixf_fp(i_img *im, i_img_dim x, i_img_dim y, i_fcolor *pix) {
1365 i_color temp;
1366 int ch;
1367  
1368 20 ▶ if (i_gpix(im, x, y, &temp) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_gpix(im, x, y, &temp) == 0) { 20 0 20
1369 80 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 60 20 80
1370 60 pix->channel[ch] = Sample8ToF(temp.channel[ch]);
1371 20 return 0;
1372 }
1373 else
1374 0 return -1;
1375 }
1376  
1377 /*
1378 =item i_plinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *pix)
1379  
1380 =cut
1381 */
1382 i_img_dim
1383 0 i_plinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *pix) {
1384 i_color *work;
1385  
1386 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
1387 0 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 0 0
1388 0 r = im->xsize;
1389 0 ▶ if (r > l) {
Branch: 1 branch
BranchTrueFalseTotal
if (r > l) { 0 0 0
1390 i_img_dim ret;
1391 i_img_dim i;
1392 int ch;
1393 0 dIMCTXim(im);
1394 0 work = i_malloc(sizeof(i_color) * (r-l));
1395 0 ▶ for (i = 0; i < r-l; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r-l; ++i) { 0 0 0
1396 0 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 0 0 0
1397 0 work[i].channel[ch] = SampleFTo8(pix[i].channel[ch]);
1398 }
1399 0 ret = i_plin(im, l, r, y, work);
1400 0 i_free(work);
1401  
1402 0 return ret;
1403 }
1404 else {
1405 0 return 0;
1406 }
1407 }
1408 else {
1409 0 return 0;
1410 }
1411 }
1412  
1413 /*
1414 =item i_glinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *pix)
1415  
1416 =cut
1417 */
1418 i_img_dim
1419 0 i_glinf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *pix) {
1420 i_color *work;
1421  
1422 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
1423 0 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 0 0
1424 0 r = im->xsize;
1425 0 ▶ if (r > l) {
Branch: 1 branch
BranchTrueFalseTotal
if (r > l) { 0 0 0
1426 i_img_dim ret;
1427 i_img_dim i;
1428 int ch;
1429 0 dIMCTXim(im);
1430 0 work = i_malloc(sizeof(i_color) * (r-l));
1431 0 ret = i_plin(im, l, r, y, work);
1432 0 ▶ for (i = 0; i < r-l; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r-l; ++i) { 0 0 0
1433 0 ▶ for (ch = 0; ch < im->channels; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) 0 0 0
1434 0 pix[i].channel[ch] = Sample8ToF(work[i].channel[ch]);
1435 }
1436 0 i_free(work);
1437  
1438 0 return ret;
1439 }
1440 else {
1441 0 return 0;
1442 }
1443 }
1444 else {
1445 0 return 0;
1446 }
1447 }
1448  
1449 /*
1450 =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)
1451  
1452 =cut
1453 */
1454  
1455 i_img_dim
1456 1 i_gsampf_fp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samp,
1457 int const *chans, int chan_count) {
1458  
1459 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
1460 1 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 1 1
1461 0 r = im->xsize;
1462 1 ▶ if (r > l) {
Branch: 1 branch
BranchTrueFalseTotal
if (r > l) { 1 0 1
1463 i_img_dim ret;
1464 i_img_dim i;
1465 1 dIMCTXim(im);
1466 size_t row_bytes;
1467 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
1468 0 im_push_error(aIMCTX, 0, "integer overflow calculating buffer size");
1469 0 return 0;
1470 }
1471 1 i_sample_t *work = i_malloc(row_bytes);
1472 1 ret = i_gsamp(im, l, r, y, work, chans, chan_count);
1473 601 ▶ for (i = 0; i < ret; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ret; ++i) { 600 1 601
1474 600 samp[i] = Sample8ToF(work[i]);
1475 }
1476 1 i_free(work);
1477  
1478 1 return ret;
1479 }
1480 else {
1481 0 return 0;
1482 }
1483 }
1484 else {
1485 0 return 0;
1486 }
1487 }
1488  
1489 /*
1490 =back
1491  
1492 =head2 Palette wrapper functions
1493  
1494 Used for virtual images, these forward palette calls to a wrapped image,
1495 assuming the wrapped image is the first pointer in the structure that
1496 im->ext_data points at.
1497  
1498 =over
1499  
1500 =item i_addcolors_forward(i_img *im, const i_color *colors, int count)
1501  
1502 =cut
1503 */
1504 1 int i_addcolors_forward(i_img *im, const i_color *colors, int count) {
1505 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
1506 }
1507  
1508 /*
1509 =item i_getcolors_forward(i_img *im, int i, i_color *color, int count)
1510  
1511 =cut
1512 */
1513 1 int i_getcolors_forward(i_img *im, int i, i_color *color, int count) {
1514 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
1515 }
1516  
1517 /*
1518 =item i_setcolors_forward(i_img *im, int i, const i_color *color, int count)
1519  
1520 =cut
1521 */
1522 0 int i_setcolors_forward(i_img *im, int i, const i_color *color, int count) {
1523 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
1524 }
1525  
1526 /*
1527 =item i_colorcount_forward(i_img *im)
1528  
1529 =cut
1530 */
1531 5 int i_colorcount_forward(i_img *im) {
1532 5 ▶ return i_colorcount(*(i_img **)im->ext_data);
Branch: 1 branch
BranchTrueFalseTotal
return i_colorcount(*(i_img **)im->ext_data); 5 0 5
1533 }
1534  
1535 /*
1536 =item i_maxcolors_forward(i_img *im)
1537  
1538 =cut
1539 */
1540 0 int i_maxcolors_forward(i_img *im) {
1541 0 ▶ return i_maxcolors(*(i_img **)im->ext_data);
Branch: 1 branch
BranchTrueFalseTotal
return i_maxcolors(*(i_img **)im->ext_data); 0 0 0
1542 }
1543  
1544 /*
1545 =item i_findcolor_forward(i_img *im, const i_color *color, i_palidx *entry)
1546  
1547 =cut
1548 */
1549 0 int i_findcolor_forward(i_img *im, const i_color *color, i_palidx *entry) {
1550 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
1551 }
1552  
1553 /*
1554 =back
1555  
1556 =head2 Fallback handler
1557  
1558 =over
1559  
1560 =item i_gsamp_bits_fb
1561  
1562 =cut
1563 */
1564  
1565 i_img_dim
1566 1 i_gsamp_bits_fb(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, unsigned *samps,
1567 const int *chans, int chan_count, int bits) {
1568 1 dIMCTXim(im);
1569  
1570 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
1571 0 i_push_error(0, "Invalid bits, must be 1..32");
1572 0 return -1;
1573 }
1574  
1575 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
1576 double scale;
1577 int ch;
1578 i_img_dim count, i, w;
1579
1580 1 ▶ if (bits == 32)
Branch: 1 branch
BranchTrueFalseTotal
if (bits == 32) 0 1 1
1581 0 scale = 4294967295.0;
1582 else
1583 1 scale = (double)(1 << bits) - 1;
1584  
1585 1 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 1 1
1586 0 r = im->xsize;
1587 1 w = r - l;
1588 1 count = 0;
1589  
1590 1 ▶ if (chans) {
Branch: 1 branch
BranchTrueFalseTotal
if (chans) { 1 0 1
1591 /* make sure we have good channel numbers */
1592 3 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 2 1 3
1593 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
1594 0 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
1595 0 return -1;
1596 }
1597 }
1598 5 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 4 1 5
1599 i_fcolor c;
1600 4 i_gpixf(im, l+i, y, &c);
1601 12 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 8 4 12
1602 8 *samps++ = (unsigned)(c.channel[ch] * scale + 0.5);
1603 8 ++count;
1604 }
1605 }
1606 }
1607 else {
1608 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
1609 0 i_push_error(0, "Invalid channel count");
1610 0 return -1;
1611 }
1612 0 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 0 0 0
1613 i_fcolor c;
1614 0 i_gpixf(im, l+i, y, &c);
1615 0 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 0 0 0
1616 0 *samps++ = (unsigned)(c.channel[ch] * scale + 0.5);
1617 0 ++count;
1618 }
1619 }
1620 }
1621  
1622 1 return count;
1623 }
1624 else {
1625 0 i_push_error(0, "Image position outside of image");
1626 0 return -1;
1627 }
1628 }
1629  
1630 static int
1631 4203 test_magic(unsigned char *buffer, size_t length, struct file_magic_entry const *magic) {
1632 4203 ▶ if (length < magic->magic_size)
Branch: 1 branch
BranchTrueFalseTotal
if (length < magic->magic_size) 52 4151 4203
1633 52 return 0;
1634 4151 ▶ if (magic->mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (magic->mask) { 663 3488 4151
1635 size_t i;
1636 663 unsigned char *bufp = buffer,
1637 663 *maskp = magic->mask,
1638 663 *magicp = magic->magic;
1639  
1640 2983 ▶ for (i = 0; i < magic->magic_size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < magic->magic_size; ++i) { 2976 7 2983
1641 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
1642 2976 ++maskp;
1643  
1644 2976 ▶ if ((*bufp++ & mask) != (*magicp++ & mask))
Branch: 1 branch
BranchTrueFalseTotal
if ((*bufp++ & mask) != (*magicp++ & mask)) 656 2320 2976
1645 656 return 0;
1646 }
1647  
1648 7 return 1;
1649 }
1650 else {
1651 3488 return !memcmp(magic->magic, buffer, magic->magic_size);
1652 }
1653 }
1654  
1655 /*
1656 =item i_test_format_probe(io_glue *data, int length)
1657  
1658 Check the beginning of the supplied file for a 'magic number'
1659  
1660 =cut
1661 */
1662  
1663 #define FORMAT_ENTRY(magic, type) \
1664 { (unsigned char *)(magic ""), sizeof(magic)-1, type, NULL }
1665 #define FORMAT_ENTRY2(magic, type, mask) \
1666 { (unsigned char *)(magic ""), sizeof(magic)-1, type, (unsigned char *)(mask) }
1667  
1668 const char *
1669 189 im_test_format_probe(im_context_t ctx, io_glue *data, int length) {
1670 static const struct file_magic_entry formats[] = {
1671 FORMAT_ENTRY("\xFF\xD8", "jpeg"),
1672 FORMAT_ENTRY("GIF87a", "gif"),
1673 FORMAT_ENTRY("GIF89a", "gif"),
1674 FORMAT_ENTRY("MM\0*", "tiff"),
1675 FORMAT_ENTRY("II*\0", "tiff"),
1676 FORMAT_ENTRY("BM", "bmp"),
1677 FORMAT_ENTRY("\x89PNG\x0d\x0a\x1a\x0a", "png"),
1678 FORMAT_ENTRY("P1", "pnm"),
1679 FORMAT_ENTRY("P2", "pnm"),
1680 FORMAT_ENTRY("P3", "pnm"),
1681 FORMAT_ENTRY("P4", "pnm"),
1682 FORMAT_ENTRY("P5", "pnm"),
1683 FORMAT_ENTRY("P6", "pnm"),
1684 FORMAT_ENTRY("/* XPM", "xpm"),
1685 FORMAT_ENTRY("\x8aMNG", "mng"),
1686 FORMAT_ENTRY("\x8aJNG", "jng"),
1687 /* SGI RGB - with various possible parameters to avoid false positives
1688 on similar files
1689 values are: 2 byte magic, rle flags (0 or 1), bytes/sample (1 or 2)
1690 */
1691 FORMAT_ENTRY("\x01\xDA\x00\x01", "sgi"),
1692 FORMAT_ENTRY("\x01\xDA\x00\x02", "sgi"),
1693 FORMAT_ENTRY("\x01\xDA\x01\x01", "sgi"),
1694 FORMAT_ENTRY("\x01\xDA\x01\x02", "sgi"),
1695
1696 FORMAT_ENTRY2("FORM ILBM", "ilbm", "xxxx xxxx"),
1697  
1698 /* different versions of PCX format
1699 http://www.fileformat.info/format/pcx/
1700 */
1701 FORMAT_ENTRY("\x0A\x00\x01", "pcx"),
1702 FORMAT_ENTRY("\x0A\x02\x01", "pcx"),
1703 FORMAT_ENTRY("\x0A\x03\x01", "pcx"),
1704 FORMAT_ENTRY("\x0A\x04\x01", "pcx"),
1705 FORMAT_ENTRY("\x0A\x05\x01", "pcx"),
1706  
1707 /* FITS - http://fits.gsfc.nasa.gov/ */
1708 FORMAT_ENTRY("SIMPLE =", "fits"),
1709  
1710 /* PSD - Photoshop */
1711 FORMAT_ENTRY("8BPS\x00\x01", "psd"),
1712
1713 /* EPS - Encapsulated Postscript */
1714 /* only reading 18 chars, so we don't include the F in EPSF */
1715 FORMAT_ENTRY("%!PS-Adobe-2.0 EPS", "eps"),
1716  
1717 /* Utah RLE */
1718 FORMAT_ENTRY("\x52\xCC", "utah"),
1719  
1720 /* GZIP compressed, only matching deflate for now */
1721 FORMAT_ENTRY("\x1F\x8B\x08", "gzip"),
1722  
1723 /* bzip2 compressed */
1724 FORMAT_ENTRY("BZh", "bzip2"),
1725  
1726 /* WEBP
1727 http://code.google.com/speed/webp/docs/riff_container.html */
1728 FORMAT_ENTRY2("RIFF WEBP", "webp", "xxxx xxxx"),
1729  
1730 /* JPEG 2000
1731 This might match a little loosely */
1732 FORMAT_ENTRY("\x00\x00\x00\x0CjP \x0D\x0A\x87\x0A", "jp2"),
1733  
1734 /* FLIF - Free Lossless Image Format - https://flif.info/spec.html */
1735 FORMAT_ENTRY("FLIF", "flif"),
1736  
1737 /* JPEG XL */
1738 FORMAT_ENTRY("\xFF\x0A", "jxl"), /* simple */
1739 FORMAT_ENTRY("\x00\x00\x00\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", "jxl"), /* complex */
1740  
1741 /* Quite OK Image Format */
1742 FORMAT_ENTRY("qoif", "qoi"),
1743  
1744 /* HEIF/HEIC see https://github.com/strukturag/libheif/issues/83 */
1745 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheic", "heif", " xxxxxxxx"),
1746 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheix", "heif", " xxxxxxxx"),
1747 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftyphevc", "heif", " xxxxxxxx"),
1748 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheim", "heif", " xxxxxxxx"),
1749 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypheis", "heif", " xxxxxxxx"),
1750 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftyphevm", "heif", " xxxxxxxx"),
1751 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftyphevs", "heif", " xxxxxxxx"),
1752 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypmif1", "heif", " xxxxxxxx"),
1753 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypmsf1", "heif", " xxxxxxxx"),
1754  
1755 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypavif", "avif", " xxxxxxxx"),
1756 /* AV1 image sequence */
1757 FORMAT_ENTRY2("\x00\x00\x00\x00" "ftypavis", "avif", " xxxxxxxx")
1758 };
1759 static const struct file_magic_entry more_formats[] = {
1760 /* these were originally both listed as ico, but cur files can
1761 include hotspot information */
1762 FORMAT_ENTRY("\x00\x00\x01\x00", "ico"), /* Windows icon */
1763 FORMAT_ENTRY("\x00\x00\x02\x00", "cur"), /* Windows cursor */
1764 FORMAT_ENTRY2("\x00\x00\x00\x00\x00\x00\x00\x07",
1765 "xwd", " xxxx"), /* X Windows Dump */
1766 };
1767 (void)length; /* FIXME? */
1768 unsigned int i;
1769 unsigned char head[18];
1770 ssize_t rc;
1771  
1772 189 rc = i_io_peekn(data, head, 18);
1773 189 ▶ if (rc == -1) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == -1) return NULL; 0 189 189
1774 #if 0
1775 {
1776 int i;
1777 fprintf(stderr, "%d bytes -", (int)rc);
1778 for (i = 0; i < rc; ++i)
1779 fprintf(stderr, " %02x", head[i]);
1780 fprintf(stderr, "\n");
1781 }
1782 #endif
1783  
1784 {
1785 189 im_file_magic *p = ctx->file_magic;
1786 199 ▶ while (p) {
Branch: 1 branch
BranchTrueFalseTotal
while (p) { 11 188 199
1787 11 ▶ if (test_magic(head, rc, &p->m)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_magic(head, rc, &p->m)) { 1 10 11
1788 1 return p->m.name;
1789 }
1790 10 p = p->next;
1791 }
1792 }
1793  
1794 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
1795 4141 struct file_magic_entry const *entry = formats + i;
1796  
1797 4141 ▶ if (test_magic(head, rc, entry))
Branch: 1 branch
BranchTrueFalseTotal
if (test_magic(head, rc, entry)) 140 4001 4141
1798 140 return entry->name;
1799 }
1800  
1801 92 if ((rc == 18) &&
1802 44 tga_header_verify(head))
1803 18 return "tga";
1804  
1805 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
1806 51 struct file_magic_entry const *entry = more_formats + i;
1807  
1808 51 ▶ if (test_magic(head, rc, entry))
Branch: 1 branch
BranchTrueFalseTotal
if (test_magic(head, rc, entry)) 23 28 51
1809 23 return entry->name;
1810 }
1811  
1812 7 return NULL;
1813 }
1814  
1815 /*
1816 =item i_img_is_monochrome(img, &zero_is_white)
1817  
1818 =category Image Information
1819  
1820 Tests an image to check it meets our monochrome tests.
1821  
1822 The idea is that a file writer can use this to test where it should
1823 write the image in whatever bi-level format it uses, eg. C<pbm> for
1824 C<pnm>.
1825  
1826 For performance of encoders we require monochrome images:
1827  
1828 =over
1829  
1830 =item *
1831  
1832 be paletted
1833  
1834 =item *
1835  
1836 have a palette of two colors, containing only C<(0,0,0)> and
1837 C<(255,255,255)> in either order.
1838  
1839 =back
1840  
1841 C<zero_is_white> is set to non-zero if the first palette entry is white.
1842  
1843 =cut
1844 */
1845  
1846 int
1847 160 i_img_is_monochrome(i_img *im, int *zero_is_white) {
1848 160 ▶ if (im->type == i_palette_type
Branch: 1 branch
BranchTrueFalseTotal
if (im->type == i_palette_type 20 140 160
1849 20 ▶ && i_colorcount(im) == 2) {
Branch: 2 branches
BranchTrueFalseTotal
&& i_colorcount(im) == 2) { 20 0 20
&& i_colorcount(im) == 2) { 17 3 20
1850 i_color colors[2];
1851 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
1852 16 return 0;
1853 17 ▶ if (im->channels == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->channels == 3) { 11 6 17
1854 11 ▶ if (colors[0].rgb.r == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
if (colors[0].rgb.r == 255 && 2 9 11
1855 2 ▶ colors[0].rgb.g == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.g == 255 && 2 0 2
1856 2 ▶ colors[0].rgb.b == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.b == 255 && 2 0 2
1857 2 ▶ colors[1].rgb.r == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.r == 0 && 2 0 2
1858 2 ▶ colors[1].rgb.g == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.g == 0 && 2 0 2
1859 2 ▶ colors[1].rgb.b == 0) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.b == 0) { 2 0 2
1860 2 *zero_is_white = 1;
1861 2 return 1;
1862 }
1863 9 ▶ else if (colors[0].rgb.r == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
else if (colors[0].rgb.r == 0 && 8 1 9
1864 8 ▶ colors[0].rgb.g == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.g == 0 && 8 0 8
1865 8 ▶ colors[0].rgb.b == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[0].rgb.b == 0 && 8 0 8
1866 8 ▶ colors[1].rgb.r == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.r == 255 && 8 0 8
1867 8 ▶ colors[1].rgb.g == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.g == 255 && 8 0 8
1868 8 ▶ colors[1].rgb.b == 255) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].rgb.b == 255) { 8 0 8
1869 8 *zero_is_white = 0;
1870 8 return 1;
1871 }
1872 }
1873 6 ▶ else if (im->channels == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (im->channels == 1) { 6 0 6
1874 6 ▶ if (colors[0].channel[0] == 255 &&
Branch: 1 branch
BranchTrueFalseTotal
if (colors[0].channel[0] == 255 && 3 3 6
1875 3 ▶ colors[1].channel[0] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].channel[0] == 0) { 3 0 3
1876 3 *zero_is_white = 1;
1877 3 return 1;
1878 }
1879 3 ▶ else if (colors[0].channel[0] == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
else if (colors[0].channel[0] == 0 && 3 0 3
1880 3 ▶ colors[1].channel[0] == 255) {
Branch: 1 branch
BranchTrueFalseTotal
colors[1].channel[0] == 255) { 3 0 3
1881 3 *zero_is_white = 0;
1882 3 return 1;
1883 }
1884 }
1885 }
1886  
1887 144 *zero_is_white = 0;
1888 144 return 0;
1889 }
1890  
1891 /*
1892 =item i_get_file_background(im, &bg)
1893  
1894 =category Files
1895  
1896 Retrieve the file write background color tag from the image.
1897  
1898 If not present, C<bg> is set to black.
1899  
1900 Returns 1 if the C<i_background> tag was found and valid.
1901  
1902 =cut
1903 */
1904  
1905 int
1906 47 i_get_file_background(i_img *im, i_color *bg) {
1907 47 int result = i_tags_get_color(&im->tags, "i_background", 0, bg);
1908 47 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 44 3 47
1909 /* black default */
1910 44 bg->channel[0] = bg->channel[1] = bg->channel[2] = 0;
1911 }
1912 /* always full alpha */
1913 47 bg->channel[3] = 255;
1914  
1915 47 return result;
1916 }
1917  
1918 /*
1919 =item i_get_file_backgroundf(im, &bg)
1920  
1921 =category Files
1922  
1923 Retrieve the file write background color tag from the image as a
1924 floating point color.
1925  
1926 Implemented in terms of i_get_file_background().
1927  
1928 If not present, C<bg> is set to black.
1929  
1930 Returns 1 if the C<i_background> tag was found and valid.
1931  
1932 =cut
1933 */
1934  
1935 int
1936 12 i_get_file_backgroundf(i_img *im, i_fcolor *fbg) {
1937 i_color bg;
1938 12 int result = i_get_file_background(im, &bg);
1939 12 fbg->rgba.r = Sample8ToF(bg.rgba.r);
1940 12 fbg->rgba.g = Sample8ToF(bg.rgba.g);
1941 12 fbg->rgba.b = Sample8ToF(bg.rgba.b);
1942 12 fbg->rgba.a = 1.0;
1943  
1944 12 return result;
1945 }
1946  
1947 /*
1948 =back
1949  
1950 =head1 AUTHOR
1951  
1952 Arnar M. Hrafnkelsson <addi@umich.edu>
1953  
1954 Tony Cook <tony@develop-help.com>
1955  
1956 =head1 SEE ALSO
1957  
1958 L<Imager>, L<gif.c>
1959  
1960 =cut
1961 */
« tags.c ↑ Summary Imager.xs »