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
21 #define IMAGER_NO_CONTEXT
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);
41 static void i_destroy_p(i_img *im);
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);
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);
47 static i_img IIM_base_8bit_pal =
50 0, 0, 0, /* xsize, ysize, bytes */
53 i_palette_type, /* type */
56 { 0, 0, NULL }, /* tags */
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 */
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 */
79 i_destroy_p, /* i_f_destroy */
82 NULL, /* i_f_psamp_bits */
89 =item i_img_pal_new(C<x>, C<y>, C<channels>, C<maxpal>)
91 =category Image creation/destruction
92 =synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size)
94 Creates a new paletted image of the supplied dimensions.
96 C<maxpal> is the maximum palette size and should normally be 256.
98 Returns a new image or NULL on failure.
103 im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int channels, int maxpal) {
105 i_img_pal_ext *palext;
106 size_t bytes, line_bytes;
109 if (maxpal < 1 || maxpal > 256) {
110 i_push_error(0, "Maximum of 256 palette entries");
113 if (x < 1 || y < 1) {
114 i_push_error(0, "Image sizes must be positive");
117 if (channels < 1 || channels > MAXCHANNELS) {
118 im_push_errorf(aIMCTX, 0, "Channels must be positive and <= %d", MAXCHANNELS);
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");
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");
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);
141 palext->alloc = maxpal;
142 palext->last_found = -1;
143 im->ext_data = palext;
144 i_tags_new(&im->tags);
146 im->idata = mymalloc(im->bytes);
147 im->channels = channels;
148 memset(im->idata, 0, im->bytes);
158 =item i_img_rgb_convert(i_img *targ, i_img *src)
160 Converts paletted data in src to RGB data in targ
164 src must be a paletted image and targ must be an RGB image with the
165 same width, height and channels.
169 static void i_img_rgb_convert(i_img *targ, i_img *src) {
170 i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
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);
180 =item i_img_to_rgb_inplace(im)
182 Converts im from a paletted image to an RGB image.
184 The conversion is done in place.
186 The conversion cannot be done for virtual images.
191 i_img_to_rgb_inplace(i_img *im) {
198 if (im->type == i_direct_type)
199 return 1; /* trivial success */
201 i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
202 i_img_rgb_convert(&temp, im);
205 (im->i_f_destroy)(im);
213 =item i_img_to_pal(i_img *im, i_quantize *quant)
215 Converts an RGB image to a paletted image
219 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
226 i_quant_makemap(quant, &src, 1);
227 result = i_quant_translate(quant, src);
231 im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
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);
248 =item i_img_to_rgb(i_img *src)
253 i_img_to_rgb(i_img *src) {
255 i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
256 i_img_rgb_convert(im, src);
262 =item i_destroy_p(i_img *im)
264 Destroys data related to a paletted image.
268 static void i_destroy_p(i_img *im) {
270 i_img_pal_ext *palext = im->ext_data;
280 =item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)
282 Write to a pixel in the image.
284 Warning: converts the image to a RGB image if the color isn't already
285 present in the image.
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;
294 const unsigned all_mask = ( 1 << im->channels ) - 1;
296 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
299 if ((im->ch_mask & all_mask) != all_mask) {
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];
311 if (i_findcolor(im, work_val, &which)) {
312 ((i_palidx *)im->idata)[x + y * im->xsize] = which;
317 im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n",
318 val->channel[0], val->channel[1], val->channel[2]));
319 if (i_img_to_rgb_inplace(im)) {
320 return i_ppix(im, x, y, val);
328 =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
330 Retrieve a pixel, converting from a palette index to a color.
334 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
336 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
339 which = ((i_palidx *)im->idata)[x + y * im->xsize];
340 if (which > PALEXT(im)->count)
342 *val = PALEXT(im)->pal[which];
348 =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
350 Retrieve a row of pixels.
354 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) {
355 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
356 int palsize = PALEXT(im)->count;
357 i_color *pal = PALEXT(im)->pal;
362 data = ((i_palidx *)im->idata) + l + y * im->xsize;
364 for (i = 0; i < count; ++i) {
365 i_palidx which = *data++;
367 vals[i] = pal[which];
377 =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
379 Write a line of color data to the image.
381 If any color value is not in the image when the image is converted to
387 i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
391 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
394 data = ((i_palidx *)im->idata) + l + y * im->xsize;
396 for (i = 0; i < count; ++i) {
397 if (i_findcolor(im, vals+i, &which)) {
398 ((i_palidx *)data)[i] = which;
401 if (i_img_to_rgb_inplace(im)) {
402 return i+i_plin(im, l+i, r, y, vals+i);
414 =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)
418 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,
419 int const *chans, int chan_count) {
421 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
422 int palsize = PALEXT(im)->count;
423 i_color *pal = PALEXT(im)->pal;
425 i_img_dim count, i, w;
428 data = ((i_palidx *)im->idata) + l + y * im->xsize;
432 for (ch = 0; ch < chan_count; ++ch) {
433 if (chans[ch] < 0 || chans[ch] >= im->channels) {
435 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
439 for (i = 0; i < w; ++i) {
440 i_palidx which = *data++;
441 if (which < palsize) {
442 for (ch = 0; ch < chan_count; ++ch) {
443 *samps++ = pal[which].channel[chans[ch]];
450 if (chan_count <= 0 || chan_count > im->channels) {
452 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
456 for (i = 0; i < w; ++i) {
457 i_palidx which = *data++;
458 if (which < palsize) {
459 for (ch = 0; ch < chan_count; ++ch) {
460 *samps++ = pal[which].channel[ch];
474 =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
479 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) {
480 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
485 data = ((i_palidx *)im->idata) + l + y * im->xsize;
487 for (i = 0; i < w; ++i) {
498 =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
503 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) {
504 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
509 data = ((i_palidx *)im->idata) + l + y * im->xsize;
511 for (i = 0; i < w; ++i) {
522 =item i_addcolors_p(i_img *im, const i_color *color, int count)
526 static int i_addcolors_p(i_img *im, const i_color *color, int count) {
527 if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
528 int result = PALEXT(im)->count;
531 PALEXT(im)->count += count;
533 PALEXT(im)->pal[index++] = *color++;
544 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
548 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
549 if (i >= 0 && i+count <= PALEXT(im)->count) {
551 *color++ = PALEXT(im)->pal[i++];
560 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
562 for (ch = 0; ch < im->channels; ++ch) {
563 if (c1->channel[ch] != c2->channel[ch])
570 =item i_colorcount_p(i_img *im)
574 static int i_colorcount_p(i_img *im) {
575 return PALEXT(im)->count;
579 =item i_maxcolors_p(i_img *im)
583 static int i_maxcolors_p(i_img *im) {
584 return PALEXT(im)->alloc;
588 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
592 static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
593 if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
595 PALEXT(im)->pal[index++] = *colors++;
605 =item i_findcolor_p(i_img *im)
609 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
610 if (PALEXT(im)->count) {
612 /* often the same color comes up several times in a row */
613 if (PALEXT(im)->last_found >= 0) {
614 if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
615 *entry = PALEXT(im)->last_found;
619 for (i = 0; i < PALEXT(im)->count; ++i) {
620 if (color_eq(im, color, PALEXT(im)->pal + i)) {
621 PALEXT(im)->last_found = *entry = i;
630 =item i_psamp_p(im, l, r, y, samps, chans, chan_count)
632 Implement psamp() for paletted images.
634 Since writing samples doesn't really work as a concept for paletted
635 images, this is slow.
637 Also, writing samples may convert the image to a direct image in the
638 process, so use i_ppix/i_gpix instead of directly calling the paletted
645 i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
646 const i_sample_t *samps, const int *chans, int chan_count) {
647 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
655 /* make sure we have good channel numbers */
656 for (ch = 0; ch < chan_count; ++ch) {
657 if (chans[ch] < 0 || chans[ch] >= im->channels) {
659 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
666 i_gpix(im, l, y, &c);
667 for (ch = 0; ch < chan_count; ++ch)
668 c.channel[chans[ch]] = *samps++;
669 i_ppix(im, l, y, &c);
675 if (chan_count <= 0 || chan_count > im->channels) {
677 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
685 i_gpix(im, l, y, &c);
686 for (ch = 0; ch < chan_count; ++ch)
687 c.channel[ch] = *samps++;
688 i_ppix(im, l, y, &c);
698 i_push_error(0, "Image position outside of image");
704 =item i_psampf_p(im, l, r, y, samps, chans, chan_count)
706 Implement psampf() for paletted images.
708 Since writing samples doesn't really work as a concept for paletted
709 images, this is slow.
711 Also, writing samples may convert the image to a direct image in the
712 process, so use i_ppixf/i_gpixf instead of directly calling the paletted
719 i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
720 const i_fsample_t *samps, const int *chans, int chan_count) {
721 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
729 /* make sure we have good channel numbers */
730 for (ch = 0; ch < chan_count; ++ch) {
731 if (chans[ch] < 0 || chans[ch] >= im->channels) {
733 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
740 i_gpixf(im, l, y, &c);
741 for (ch = 0; ch < chan_count; ++ch)
742 c.channel[chans[ch]] = *samps++;
743 i_ppixf(im, l, y, &c);
749 if (chan_count <= 0 || chan_count > im->channels) {
751 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
759 i_gpixf(im, l, y, &c);
760 for (ch = 0; ch < chan_count; ++ch)
761 c.channel[ch] = *samps++;
762 i_ppixf(im, l, y, &c);
772 i_push_error(0, "Image position outside of image");
782 Tony Cook <tony@develop-help.com>