4 palimg.c - implements paletted images for Imager.
10 Implements paletted images using the new image interface.
14 =item IIM_base_8bit_pal
16 Basic 8-bit/sample paletted image
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);
39 static void i_destroy_p(i_img *im);
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);
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);
45 static i_img IIM_base_8bit_pal =
48 0, 0, 0, /* xsize, ysize, bytes */
51 i_palette_type, /* type */
54 { 0, 0, NULL }, /* tags */
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 */
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 */
77 i_destroy_p, /* i_f_destroy */
80 NULL, /* i_f_psamp_bits */
87 =item i_img_pal_new(C<x>, C<y>, C<channels>, C<maxpal>)
89 =category Image creation/destruction
90 =synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size)
92 Creates a new paletted image of the supplied dimensions.
94 C<maxpal> is the maximum palette size and should normally be 256.
96 Returns a new image or NULL on failure.
101 i_img_pal_new(i_img_dim x, i_img_dim y, int channels, int maxpal) {
103 i_img_pal_ext *palext;
104 size_t bytes, line_bytes;
107 if (maxpal < 1 || maxpal > 256) {
108 i_push_error(0, "Maximum of 256 palette entries");
111 if (x < 1 || y < 1) {
112 i_push_error(0, "Image sizes must be positive");
115 if (channels < 1 || channels > MAXCHANNELS) {
116 i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS);
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");
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");
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);
139 palext->alloc = maxpal;
140 palext->last_found = -1;
141 im->ext_data = palext;
142 i_tags_new(&im->tags);
144 im->idata = mymalloc(im->bytes);
145 im->channels = channels;
146 memset(im->idata, 0, im->bytes);
156 =item i_img_rgb_convert(i_img *targ, i_img *src)
158 Converts paletted data in src to RGB data in targ
162 src must be a paletted image and targ must be an RGB image with the
163 same width, height and channels.
167 static void i_img_rgb_convert(i_img *targ, i_img *src) {
168 i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
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);
178 =item i_img_to_rgb_inplace(im)
180 Converts im from a paletted image to an RGB image.
182 The conversion is done in place.
184 The conversion cannot be done for virtual images.
188 int i_img_to_rgb_inplace(i_img *im) {
194 if (im->type == i_direct_type)
195 return 1; /* trivial success */
197 i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
198 i_img_rgb_convert(&temp, im);
201 (im->i_f_destroy)(im);
209 =item i_img_to_pal(i_img *im, i_quantize *quant)
211 Converts an RGB image to a paletted image
215 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
221 i_quant_makemap(quant, &src, 1);
222 result = i_quant_translate(quant, src);
226 im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
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);
243 =item i_img_to_rgb(i_img *src)
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);
255 =item i_destroy_p(i_img *im)
257 Destroys data related to a paletted image.
261 static void i_destroy_p(i_img *im) {
263 i_img_pal_ext *palext = im->ext_data;
273 =item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)
275 Write to a pixel in the image.
277 Warning: converts the image to a RGB image if the color isn't already
278 present in the image.
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;
287 const unsigned all_mask = ( 1 << im->channels ) - 1;
289 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
292 if ((im->ch_mask & all_mask) != all_mask) {
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];
304 if (i_findcolor(im, work_val, &which)) {
305 ((i_palidx *)im->idata)[x + y * im->xsize] = which;
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);
320 =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
322 Retrieve a pixel, converting from a palette index to a color.
326 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
328 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
331 which = ((i_palidx *)im->idata)[x + y * im->xsize];
332 if (which > PALEXT(im)->count)
334 *val = PALEXT(im)->pal[which];
340 =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
342 Retrieve a row of pixels.
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;
354 data = ((i_palidx *)im->idata) + l + y * im->xsize;
356 for (i = 0; i < count; ++i) {
357 i_palidx which = *data++;
359 vals[i] = pal[which];
369 =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
371 Write a line of color data to the image.
373 If any color value is not in the image when the image is converted to
379 i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
383 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
386 data = ((i_palidx *)im->idata) + l + y * im->xsize;
388 for (i = 0; i < count; ++i) {
389 if (i_findcolor(im, vals+i, &which)) {
390 ((i_palidx *)data)[i] = which;
393 if (i_img_to_rgb_inplace(im)) {
394 return i+i_plin(im, l+i, r, y, vals+i);
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)
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) {
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;
417 i_img_dim count, i, w;
420 data = ((i_palidx *)im->idata) + l + y * im->xsize;
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]);
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]];
441 if (chan_count <= 0 || chan_count > im->channels) {
442 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
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];
464 =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
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) {
475 data = ((i_palidx *)im->idata) + l + y * im->xsize;
477 for (i = 0; i < w; ++i) {
488 =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
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) {
499 data = ((i_palidx *)im->idata) + l + y * im->xsize;
501 for (i = 0; i < w; ++i) {
512 =item i_addcolors_p(i_img *im, const i_color *color, int count)
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;
521 PALEXT(im)->count += count;
523 PALEXT(im)->pal[index++] = *color++;
534 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
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) {
541 *color++ = PALEXT(im)->pal[i++];
550 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
552 for (ch = 0; ch < im->channels; ++ch) {
553 if (c1->channel[ch] != c2->channel[ch])
560 =item i_colorcount_p(i_img *im)
564 static int i_colorcount_p(i_img *im) {
565 return PALEXT(im)->count;
569 =item i_maxcolors_p(i_img *im)
573 static int i_maxcolors_p(i_img *im) {
574 return PALEXT(im)->alloc;
578 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
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) {
585 PALEXT(im)->pal[index++] = *colors++;
595 =item i_findcolor_p(i_img *im)
599 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
600 if (PALEXT(im)->count) {
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;
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;
620 =item i_psamp_p(im, l, r, y, samps, chans, chan_count)
622 Implement psamp() for paletted images.
624 Since writing samples doesn't really work as a concept for paletted
625 images, this is slow.
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
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) {
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]);
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);
664 if (chan_count <= 0 || chan_count > im->channels) {
665 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
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);
685 i_push_error(0, "Image position outside of image");
691 =item i_psampf_p(im, l, r, y, samps, chans, chan_count)
693 Implement psampf() for paletted images.
695 Since writing samples doesn't really work as a concept for paletted
696 images, this is slow.
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
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) {
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]);
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);
735 if (chan_count <= 0 || chan_count > im->channels) {
736 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
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);
756 i_push_error(0, "Image position outside of image");
766 Tony Cook <tony@develop-help.com>