]> git.imager.perl.org - imager.git/blob - palimg.c
re-work the context macros
[imager.git] / palimg.c
1 /*
2 =head1 NAME
3
4   palimg.c - implements paletted images for Imager.
5
6 =head1 SYNOPSIS
7
8 =head1 DESCRIPTION
9
10 Implements paletted images using the new image interface.
11
12 =over
13
14 =item IIM_base_8bit_pal
15
16 Basic 8-bit/sample paletted image
17
18 =cut
19 */
20
21 #define IMAGER_NO_CONTEXT
22
23 #include "imager.h"
24 #include "imageri.h"
25
26 #define PALEXT(im) ((i_img_pal_ext*)((im)->ext_data))
27 static int i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val);
28 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val);
29 static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals);
30 static i_img_dim i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals);
31 static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int const *chans, int chan_count);
32 static i_img_dim i_gpal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals);
33 static i_img_dim i_ppal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals);
34 static int i_addcolors_p(i_img *im, const i_color *color, int count);
35 static int i_getcolors_p(i_img *im, int i, i_color *color, int count);
36 static int i_colorcount_p(i_img *im);
37 static int i_maxcolors_p(i_img *im);
38 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry);
39 static int i_setcolors_p(i_img *im, int index, const i_color *color, int count);
40
41 static void i_destroy_p(i_img *im);
42 static i_img_dim 
43 i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_sample_t *samps, const int *chans, int chan_count);
44 static i_img_dim 
45 i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fsample_t *samps, const int *chans, int chan_count);
46
47 static i_img IIM_base_8bit_pal =
48 {
49   0, /* channels set */
50   0, 0, 0, /* xsize, ysize, bytes */
51   ~0U, /* ch_mask */
52   i_8_bits, /* bits */
53   i_palette_type, /* type */
54   0, /* virtual */
55   NULL, /* idata */
56   { 0, 0, NULL }, /* tags */
57   NULL, /* ext_data */
58
59   i_ppix_p, /* i_f_ppix */
60   i_ppixf_fp, /* i_f_ppixf */
61   i_plin_p, /* i_f_plin */
62   i_plinf_fp, /* i_f_plinf */
63   i_gpix_p, /* i_f_gpix */
64   i_gpixf_fp, /* i_f_gpixf */
65   i_glin_p, /* i_f_glin */
66   i_glinf_fp, /* i_f_glinf */
67   i_gsamp_p, /* i_f_gsamp */
68   i_gsampf_fp, /* i_f_gsampf */
69
70   i_gpal_p, /* i_f_gpal */
71   i_ppal_p, /* i_f_ppal */
72   i_addcolors_p, /* i_f_addcolors */
73   i_getcolors_p, /* i_f_getcolors */
74   i_colorcount_p, /* i_f_colorcount */
75   i_maxcolors_p, /* i_f_maxcolors */
76   i_findcolor_p, /* i_f_findcolor */
77   i_setcolors_p, /* i_f_setcolors */
78
79   i_destroy_p, /* i_f_destroy */
80
81   i_gsamp_bits_fb,
82   NULL, /* i_f_psamp_bits */
83   
84   i_psamp_p,
85   i_psampf_p
86 };
87
88 /*
89 =item i_img_pal_new(C<x>, C<y>, C<channels>, C<maxpal>)
90
91 =category Image creation/destruction
92 =synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size)
93
94 Creates a new paletted image of the supplied dimensions.
95
96 C<maxpal> is the maximum palette size and should normally be 256.
97
98 Returns a new image or NULL on failure.
99
100 =cut
101 */
102 i_img *
103 im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int channels, int maxpal) {
104   i_img *im;
105   i_img_pal_ext *palext;
106   size_t bytes, line_bytes;
107
108   i_clear_error();
109   if (maxpal < 1 || maxpal > 256) {
110     i_push_error(0, "Maximum of 256 palette entries");
111     return NULL;
112   }
113   if (x < 1 || y < 1) {
114     i_push_error(0, "Image sizes must be positive");
115     return NULL;
116   }
117   if (channels < 1 || channels > MAXCHANNELS) {
118     i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS);
119     return NULL;
120   }
121   bytes = sizeof(i_palidx) * x * y;
122   if (bytes / y / sizeof(i_palidx) != x) {
123     i_push_error(0, "integer overflow calculating image allocation");
124     return NULL;
125   }
126
127   /* basic assumption: we can always allocate a buffer representing a
128      line from the image, otherwise we're going to have trouble
129      working with the image */
130   line_bytes = sizeof(i_color) * x;
131   if (line_bytes / x != sizeof(i_color)) {
132     i_push_error(0, "integer overflow calculating scanline allocation");
133     return NULL;
134   }
135
136   im = i_img_alloc();
137   memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
138   palext = mymalloc(sizeof(i_img_pal_ext));
139   palext->pal = mymalloc(sizeof(i_color) * maxpal);
140   palext->count = 0;
141   palext->alloc = maxpal;
142   palext->last_found = -1;
143   im->ext_data = palext;
144   i_tags_new(&im->tags);
145   im->bytes = bytes;
146   im->idata = mymalloc(im->bytes);
147   im->channels = channels;
148   memset(im->idata, 0, im->bytes);
149   im->xsize = x;
150   im->ysize = y;
151
152   i_img_init(im);
153   
154   return im;
155 }
156
157 /*
158 =item i_img_rgb_convert(i_img *targ, i_img *src)
159
160 Converts paletted data in src to RGB data in targ
161
162 Internal function.
163
164 src must be a paletted image and targ must be an RGB image with the
165 same width, height and channels.
166
167 =cut
168 */
169 static void i_img_rgb_convert(i_img *targ, i_img *src) {
170   i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
171   i_img_dim y;
172   for (y = 0; y < targ->ysize; ++y) {
173     i_glin(src, 0, src->xsize, y, row);
174     i_plin(targ, 0, src->xsize, y, row);
175   }
176   myfree(row);
177 }
178
179 /*
180 =item i_img_to_rgb_inplace(im)
181
182 Converts im from a paletted image to an RGB image.
183
184 The conversion is done in place.
185
186 The conversion cannot be done for virtual images.
187
188 =cut
189 */
190 int
191 i_img_to_rgb_inplace(i_img *im) {
192   i_img temp;
193   dIMCTXim(im);
194
195   if (im->virtual)
196     return 0;
197
198   if (im->type == i_direct_type)
199     return 1; /* trivial success */
200
201   i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
202   i_img_rgb_convert(&temp, im);
203
204   /* nasty hack */
205   (im->i_f_destroy)(im);
206   myfree(im->idata);
207   *im = temp;
208
209   return 1;
210 }
211
212 /*
213 =item i_img_to_pal(i_img *im, i_quantize *quant)
214
215 Converts an RGB image to a paletted image
216
217 =cut
218 */
219 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
220   i_palidx *result;
221   i_img *im;
222   dIMCTXim(src);
223
224   i_clear_error();
225   
226   i_quant_makemap(quant, &src, 1);
227   result = i_quant_translate(quant, src);
228
229   if (result) {
230
231     im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
232
233     /* copy things over */
234     memcpy(im->idata, result, im->bytes);
235     PALEXT(im)->count = quant->mc_count;
236     memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
237     
238     myfree(result);
239
240     return im;
241   }
242   else {
243     return NULL;
244   }
245 }
246
247 /*
248 =item i_img_to_rgb(i_img *src)
249
250 =cut
251 */
252 i_img *
253 i_img_to_rgb(i_img *src) {
254   dIMCTXim(src);
255   i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
256   i_img_rgb_convert(im, src);
257
258   return im;
259 }
260
261 /*
262 =item i_destroy_p(i_img *im)
263
264 Destroys data related to a paletted image.
265
266 =cut
267 */
268 static void i_destroy_p(i_img *im) {
269   if (im) {
270     i_img_pal_ext *palext = im->ext_data;
271     if (palext) {
272       if (palext->pal)
273         myfree(palext->pal);
274       myfree(palext);
275     }
276   }
277 }
278
279 /*
280 =item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)
281
282 Write to a pixel in the image.
283
284 Warning: converts the image to a RGB image if the color isn't already
285 present in the image.
286
287 =cut
288 */
289 static int 
290 i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
291   const i_color *work_val = val;
292   i_color workc;
293   i_palidx which;
294   const unsigned all_mask = ( 1 << im->channels ) - 1;
295
296   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
297     return -1;
298
299   if ((im->ch_mask & all_mask) != all_mask) {
300     unsigned mask = 1;
301     int ch;
302     i_gpix(im, x, y, &workc);
303     for (ch = 0; ch < im->channels; ++ch) {
304       if (im->ch_mask & mask)
305         workc.channel[ch] = val->channel[ch];
306       mask <<= 1;
307     }
308     work_val = &workc;
309   }
310
311   if (i_findcolor(im, work_val, &which)) {
312     ((i_palidx *)im->idata)[x + y * im->xsize] = which;
313     return 0;
314   }
315   else {
316     mm_log((1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n",
317             val->channel[0], val->channel[1], val->channel[2]));
318     if (i_img_to_rgb_inplace(im)) {
319       return i_ppix(im, x, y, val);
320     }
321     else
322       return -1;
323   }
324 }
325
326 /*
327 =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
328
329 Retrieve a pixel, converting from a palette index to a color.
330
331 =cut
332 */
333 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
334   i_palidx which;
335   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
336     return -1;
337   }
338   which = ((i_palidx *)im->idata)[x + y * im->xsize];
339   if (which > PALEXT(im)->count)
340     return -1;
341   *val = PALEXT(im)->pal[which];
342
343   return 0;
344 }
345
346 /*
347 =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
348
349 Retrieve a row of pixels.
350
351 =cut
352 */
353 static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) {
354   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
355     int palsize = PALEXT(im)->count;
356     i_color *pal = PALEXT(im)->pal;
357     i_palidx *data;
358     i_img_dim count, i;
359     if (r > im->xsize)
360       r = im->xsize;
361     data = ((i_palidx *)im->idata) + l + y * im->xsize;
362     count = r - l;
363     for (i = 0; i < count; ++i) {
364       i_palidx which = *data++;
365       if (which < palsize)
366         vals[i] = pal[which];
367     }
368     return count;
369   }
370   else {
371     return 0;
372   }
373 }
374
375 /*
376 =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
377
378 Write a line of color data to the image.
379
380 If any color value is not in the image when the image is converted to 
381 RGB.
382
383 =cut
384 */
385 static i_img_dim 
386 i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
387   i_img_dim count, i;
388   i_palidx *data;
389   i_palidx which;
390   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
391     if (r > im->xsize)
392       r = im->xsize;
393     data = ((i_palidx *)im->idata) + l + y * im->xsize;
394     count = r - l;
395     for (i = 0; i < count; ++i) {
396       if (i_findcolor(im, vals+i, &which)) {
397         ((i_palidx *)data)[i] = which;
398       }
399       else {
400         if (i_img_to_rgb_inplace(im)) {
401           return i+i_plin(im, l+i, r, y, vals+i);
402         }
403       }
404     }
405     return count;
406   }
407   else {
408     return 0;
409   }
410 }
411
412 /*
413 =item i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int chans, int chan_count)
414
415 =cut
416 */
417 static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, 
418               int const *chans, int chan_count) {
419   int ch;
420   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
421     int palsize = PALEXT(im)->count;
422     i_color *pal = PALEXT(im)->pal;
423     i_palidx *data;
424     i_img_dim count, i, w;
425     if (r > im->xsize)
426       r = im->xsize;
427     data = ((i_palidx *)im->idata) + l + y * im->xsize;
428     count = 0;
429     w = r - l;
430     if (chans) {
431       for (ch = 0; ch < chan_count; ++ch) {
432         if (chans[ch] < 0 || chans[ch] >= im->channels) {
433           i_push_errorf(0, "No channel %d in this image", chans[ch]);
434         }
435       }
436
437       for (i = 0; i < w; ++i) {
438         i_palidx which = *data++;
439         if (which < palsize) {
440           for (ch = 0; ch < chan_count; ++ch) {
441             *samps++ = pal[which].channel[chans[ch]];
442             ++count;
443           }
444         }
445       }
446     }
447     else {
448       if (chan_count <= 0 || chan_count > im->channels) {
449         i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", 
450                       chan_count);
451         return 0;
452       }
453       for (i = 0; i < w; ++i) {
454         i_palidx which = *data++;
455         if (which < palsize) {
456           for (ch = 0; ch < chan_count; ++ch) {
457             *samps++ = pal[which].channel[ch];
458             ++count;
459           }
460         }
461       }
462     }
463     return count;
464   }
465   else {
466     return 0;
467   }
468 }
469
470 /*
471 =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
472
473 =cut
474 */
475
476 static i_img_dim i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) {
477   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
478     i_palidx *data;
479     i_img_dim i, w;
480     if (r > im->xsize)
481       r = im->xsize;
482     data = ((i_palidx *)im->idata) + l + y * im->xsize;
483     w = r - l;
484     for (i = 0; i < w; ++i) {
485       *vals++ = *data++;
486     }
487     return i;
488   }
489   else {
490     return 0;
491   }
492 }
493
494 /*
495 =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
496
497 =cut
498 */
499
500 static i_img_dim i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) {
501   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
502     i_palidx *data;
503     i_img_dim i, w;
504     if (r > im->xsize)
505       r = im->xsize;
506     data = ((i_palidx *)im->idata) + l + y * im->xsize;
507     w = r - l;
508     for (i = 0; i < w; ++i) {
509       *data++ = *vals++;
510     }
511     return i;
512   }
513   else {
514     return 0;
515   }
516 }
517
518 /*
519 =item i_addcolors_p(i_img *im, const i_color *color, int count)
520
521 =cut
522 */
523 static int i_addcolors_p(i_img *im, const i_color *color, int count) {
524   if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
525     int result = PALEXT(im)->count;
526     int index = result;
527
528     PALEXT(im)->count += count;
529     while (count) {
530       PALEXT(im)->pal[index++] = *color++;
531       --count;
532     }
533
534     return result;
535   }
536   else
537     return -1;
538 }
539
540 /*
541 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
542
543 =cut
544 */
545 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
546   if (i >= 0 && i+count <= PALEXT(im)->count) {
547     while (count) {
548       *color++ = PALEXT(im)->pal[i++];
549       --count;
550     }
551     return 1;
552   }
553   else
554     return 0;
555 }
556
557 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
558   int ch;
559   for (ch = 0; ch < im->channels; ++ch) {
560     if (c1->channel[ch] != c2->channel[ch])
561       return 0;
562   }
563   return 1;
564 }
565
566 /*
567 =item i_colorcount_p(i_img *im)
568
569 =cut
570 */
571 static int i_colorcount_p(i_img *im) {
572   return PALEXT(im)->count;
573 }
574
575 /*
576 =item i_maxcolors_p(i_img *im)
577
578 =cut
579 */
580 static int i_maxcolors_p(i_img *im) {
581   return PALEXT(im)->alloc;
582 }
583
584 /*
585 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
586
587 =cut
588 */
589 static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
590   if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
591     while (count) {
592       PALEXT(im)->pal[index++] = *colors++;
593       --count;
594     }
595     return 1;
596   }
597
598   return 0;
599 }
600
601 /*
602 =item i_findcolor_p(i_img *im)
603
604 =cut
605 */
606 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
607   if (PALEXT(im)->count) {
608     int i;
609     /* often the same color comes up several times in a row */
610     if (PALEXT(im)->last_found >= 0) {
611       if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
612         *entry = PALEXT(im)->last_found;
613         return 1;
614       }
615     }
616     for (i = 0; i < PALEXT(im)->count; ++i) {
617       if (color_eq(im, color, PALEXT(im)->pal + i)) {
618         PALEXT(im)->last_found = *entry = i;
619         return 1;
620       }
621     }
622   }
623   return 0;
624 }
625
626 /*
627 =item i_psamp_p(im, l, r, y, samps, chans, chan_count)
628
629 Implement psamp() for paletted images.
630
631 Since writing samples doesn't really work as a concept for paletted
632 images, this is slow.
633
634 Also, writing samples may convert the image to a direct image in the
635 process, so use i_ppix/i_gpix instead of directly calling the paletted
636 handlers.
637
638 =cut
639 */
640
641 static i_img_dim 
642 i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
643           const i_sample_t *samps, const int *chans, int chan_count) {
644   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
645     i_img_dim count = 0;
646     int ch;
647
648     if (r > im->xsize)
649       r = im->xsize;
650       
651     if (chans) {
652       /* make sure we have good channel numbers */
653       for (ch = 0; ch < chan_count; ++ch) {
654         if (chans[ch] < 0 || chans[ch] >= im->channels) {
655           i_push_errorf(0, "No channel %d in this image", chans[ch]);
656           return -1;
657         }
658       }
659       while (l < r) {
660         i_color c;
661         
662         i_gpix(im, l, y, &c);
663         for (ch = 0; ch < chan_count; ++ch)
664           c.channel[chans[ch]] = *samps++;
665         i_ppix(im, l, y, &c);
666         count += chan_count;
667         ++l;
668       }
669     }
670     else {
671       if (chan_count <= 0 || chan_count > im->channels) {
672         i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", 
673                       chan_count);
674         return -1;
675       }
676
677       while (l < r) {
678         i_color c;
679         
680         i_gpix(im, l, y, &c);
681         for (ch = 0; ch < chan_count; ++ch)
682           c.channel[ch] = *samps++;
683         i_ppix(im, l, y, &c);
684         count += chan_count;
685         ++l;
686       }
687     }
688
689     return count;
690   }
691   else {
692     dIMCTXim(im);
693     i_push_error(0, "Image position outside of image");
694     return -1;
695   }
696 }
697
698 /*
699 =item i_psampf_p(im, l, r, y, samps, chans, chan_count)
700
701 Implement psampf() for paletted images.
702
703 Since writing samples doesn't really work as a concept for paletted
704 images, this is slow.
705
706 Also, writing samples may convert the image to a direct image in the
707 process, so use i_ppixf/i_gpixf instead of directly calling the paletted
708 handlers.
709
710 =cut
711 */
712
713 static i_img_dim 
714 i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
715           const i_fsample_t *samps, const int *chans, int chan_count) {
716   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
717     i_img_dim count = 0;
718     int ch;
719
720     if (r > im->xsize)
721       r = im->xsize;
722       
723     if (chans) {
724       /* make sure we have good channel numbers */
725       for (ch = 0; ch < chan_count; ++ch) {
726         if (chans[ch] < 0 || chans[ch] >= im->channels) {
727           i_push_errorf(0, "No channel %d in this image", chans[ch]);
728           return -1;
729         }
730       }
731       while (l < r) {
732         i_fcolor c;
733         
734         i_gpixf(im, l, y, &c);
735         for (ch = 0; ch < chan_count; ++ch)
736           c.channel[chans[ch]] = *samps++;
737         i_ppixf(im, l, y, &c);
738         count += chan_count;
739         ++l;
740       }
741     }
742     else {
743       if (chan_count <= 0 || chan_count > im->channels) {
744         i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", 
745                       chan_count);
746         return -1;
747       }
748
749       while (l < r) {
750         i_fcolor c;
751         
752         i_gpixf(im, l, y, &c);
753         for (ch = 0; ch < chan_count; ++ch)
754           c.channel[ch] = *samps++;
755         i_ppixf(im, l, y, &c);
756         count += chan_count;
757         ++l;
758       }
759     }
760
761     return count;
762   }
763   else {
764     dIMCTXim(im);
765     i_push_error(0, "Image position outside of image");
766     return -1;
767   }
768 }
769
770 /*
771 =back
772
773 =head1 AUTHOR
774
775 Tony Cook <tony@develop-help.com>
776
777 =head1 SEE ALSO
778
779 Imager(3)
780
781 =cut
782 */