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