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 i_push_errorf(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;
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);
327 =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
329 Retrieve a pixel, converting from a palette index to a color.
333 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
335 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
338 which = ((i_palidx *)im->idata)[x + y * im->xsize];
339 if (which > PALEXT(im)->count)
341 *val = PALEXT(im)->pal[which];
347 =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
349 Retrieve a row of pixels.
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;
361 data = ((i_palidx *)im->idata) + l + y * im->xsize;
363 for (i = 0; i < count; ++i) {
364 i_palidx which = *data++;
366 vals[i] = pal[which];
376 =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
378 Write a line of color data to the image.
380 If any color value is not in the image when the image is converted to
386 i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
390 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
393 data = ((i_palidx *)im->idata) + l + y * im->xsize;
395 for (i = 0; i < count; ++i) {
396 if (i_findcolor(im, vals+i, &which)) {
397 ((i_palidx *)data)[i] = which;
400 if (i_img_to_rgb_inplace(im)) {
401 return i+i_plin(im, l+i, r, y, vals+i);
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)
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) {
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;
424 i_img_dim count, i, w;
427 data = ((i_palidx *)im->idata) + l + y * im->xsize;
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]);
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]];
448 if (chan_count <= 0 || chan_count > im->channels) {
449 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
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];
471 =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
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) {
482 data = ((i_palidx *)im->idata) + l + y * im->xsize;
484 for (i = 0; i < w; ++i) {
495 =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
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) {
506 data = ((i_palidx *)im->idata) + l + y * im->xsize;
508 for (i = 0; i < w; ++i) {
519 =item i_addcolors_p(i_img *im, const i_color *color, int count)
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;
528 PALEXT(im)->count += count;
530 PALEXT(im)->pal[index++] = *color++;
541 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
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) {
548 *color++ = PALEXT(im)->pal[i++];
557 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
559 for (ch = 0; ch < im->channels; ++ch) {
560 if (c1->channel[ch] != c2->channel[ch])
567 =item i_colorcount_p(i_img *im)
571 static int i_colorcount_p(i_img *im) {
572 return PALEXT(im)->count;
576 =item i_maxcolors_p(i_img *im)
580 static int i_maxcolors_p(i_img *im) {
581 return PALEXT(im)->alloc;
585 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
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) {
592 PALEXT(im)->pal[index++] = *colors++;
602 =item i_findcolor_p(i_img *im)
606 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
607 if (PALEXT(im)->count) {
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;
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;
627 =item i_psamp_p(im, l, r, y, samps, chans, chan_count)
629 Implement psamp() for paletted images.
631 Since writing samples doesn't really work as a concept for paletted
632 images, this is slow.
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
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) {
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]);
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);
671 if (chan_count <= 0 || chan_count > im->channels) {
672 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
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);
693 i_push_error(0, "Image position outside of image");
699 =item i_psampf_p(im, l, r, y, samps, chans, chan_count)
701 Implement psampf() for paletted images.
703 Since writing samples doesn't really work as a concept for paletted
704 images, this is slow.
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
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) {
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]);
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);
743 if (chan_count <= 0 || chan_count > im->channels) {
744 i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels",
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);
765 i_push_error(0, "Image position outside of image");
775 Tony Cook <tony@develop-help.com>