Debian Codegraph @

image.c

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