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 im_img_pal_new(ctx, C<x>, C<y>, C<channels>, C<maxpal>)
90 X<im_img_pal_new API>X<i_img_pal_new API>
91 =category Image creation/destruction
92 =synopsis i_img *img = im_img_pal_new(aIMCTX, width, height, channels, max_palette_size)
93 =synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size)
95 Creates a new paletted image of the supplied dimensions.
97 C<maxpal> is the maximum palette size and should normally be 256.
99 Returns a new image or NULL on failure.
101 Also callable as C<i_img_pal_new(width, height, channels, max_palette_size)>.
106 im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int channels, int maxpal) {
108 i_img_pal_ext *palext;
109 size_t bytes, line_bytes;
112 if (maxpal < 1 || maxpal > 256) {
113 i_push_error(0, "Maximum of 256 palette entries");
116 if (x < 1 || y < 1) {
117 i_push_error(0, "Image sizes must be positive");
120 if (channels < 1 || channels > MAXCHANNELS) {
121 im_push_errorf(aIMCTX, 0, "Channels must be positive and <= %d", MAXCHANNELS);
124 bytes = sizeof(i_palidx) * x * y;
125 if (bytes / y / sizeof(i_palidx) != x) {
126 i_push_error(0, "integer overflow calculating image allocation");
130 /* basic assumption: we can always allocate a buffer representing a
131 line from the image, otherwise we're going to have trouble
132 working with the image */
133 line_bytes = sizeof(i_color) * x;
134 if (line_bytes / x != sizeof(i_color)) {
135 i_push_error(0, "integer overflow calculating scanline allocation");
140 memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
141 palext = mymalloc(sizeof(i_img_pal_ext));
142 palext->pal = mymalloc(sizeof(i_color) * maxpal);
144 palext->alloc = maxpal;
145 palext->last_found = -1;
146 im->ext_data = palext;
147 i_tags_new(&im->tags);
149 im->idata = mymalloc(im->bytes);
150 im->channels = channels;
151 memset(im->idata, 0, im->bytes);
161 =item i_img_rgb_convert(i_img *targ, i_img *src)
163 Converts paletted data in src to RGB data in targ
167 src must be a paletted image and targ must be an RGB image with the
168 same width, height and channels.
172 static void i_img_rgb_convert(i_img *targ, i_img *src) {
173 i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
175 for (y = 0; y < targ->ysize; ++y) {
176 i_glin(src, 0, src->xsize, y, row);
177 i_plin(targ, 0, src->xsize, y, row);
183 =item i_img_to_rgb_inplace(im)
185 Converts im from a paletted image to an RGB image.
187 The conversion is done in place.
189 The conversion cannot be done for virtual images.
194 i_img_to_rgb_inplace(i_img *im) {
201 if (im->type == i_direct_type)
202 return 1; /* trivial success */
204 i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
205 i_img_rgb_convert(&temp, im);
211 /* i_img_empty_ch() calls i_img_init() which takes a ref */
212 im_context_refdec(aIMCTX, "img_destroy");
218 =item i_img_to_pal(i_img *im, i_quantize *quant)
220 Converts an RGB image to a paletted image
224 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
231 i_quant_makemap(quant, &src, 1);
232 result = i_quant_translate(quant, src);
236 im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
238 /* copy things over */
239 memcpy(im->idata, result, im->bytes);
240 PALEXT(im)->count = quant->mc_count;
241 memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
253 =item i_img_to_rgb(i_img *src)
258 i_img_to_rgb(i_img *src) {
260 i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
261 i_img_rgb_convert(im, src);
267 =item i_destroy_p(i_img *im)
269 Destroys data related to a paletted image.
273 static void i_destroy_p(i_img *im) {
275 i_img_pal_ext *palext = im->ext_data;
285 =item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)
287 Write to a pixel in the image.
289 Warning: converts the image to a RGB image if the color isn't already
290 present in the image.
295 i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
296 const i_color *work_val = val;
299 const unsigned all_mask = ( 1 << im->channels ) - 1;
301 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
304 if ((im->ch_mask & all_mask) != all_mask) {
307 i_gpix(im, x, y, &workc);
308 for (ch = 0; ch < im->channels; ++ch) {
309 if (im->ch_mask & mask)
310 workc.channel[ch] = val->channel[ch];
316 if (i_findcolor(im, work_val, &which)) {
317 ((i_palidx *)im->idata)[x + y * im->xsize] = which;
322 im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n",
323 val->channel[0], val->channel[1], val->channel[2]));
324 if (i_img_to_rgb_inplace(im)) {
325 return i_ppix(im, x, y, val);
333 =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
335 Retrieve a pixel, converting from a palette index to a color.
339 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
341 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
344 which = ((i_palidx *)im->idata)[x + y * im->xsize];
345 if (which > PALEXT(im)->count)
347 *val = PALEXT(im)->pal[which];
353 =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
355 Retrieve a row of pixels.
359 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) {
360 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
361 int palsize = PALEXT(im)->count;
362 i_color *pal = PALEXT(im)->pal;
367 data = ((i_palidx *)im->idata) + l + y * im->xsize;
369 for (i = 0; i < count; ++i) {
370 i_palidx which = *data++;
372 vals[i] = pal[which];
382 =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
384 Write a line of color data to the image.
386 If any color value is not in the image when the image is converted to
392 i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
396 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
399 data = ((i_palidx *)im->idata) + l + y * im->xsize;
401 for (i = 0; i < count; ++i) {
402 if (i_findcolor(im, vals+i, &which)) {
403 ((i_palidx *)data)[i] = which;
406 if (i_img_to_rgb_inplace(im)) {
407 return i+i_plin(im, l+i, r, y, vals+i);
419 =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)
423 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,
424 int const *chans, int chan_count) {
426 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
427 int palsize = PALEXT(im)->count;
428 i_color *pal = PALEXT(im)->pal;
430 i_img_dim count, i, w;
433 data = ((i_palidx *)im->idata) + l + y * im->xsize;
437 for (ch = 0; ch < chan_count; ++ch) {
438 if (chans[ch] < 0 || chans[ch] >= im->channels) {
440 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
444 for (i = 0; i < w; ++i) {
445 i_palidx which = *data++;
446 if (which < palsize) {
447 for (ch = 0; ch < chan_count; ++ch) {
448 *samps++ = pal[which].channel[chans[ch]];
455 if (chan_count <= 0 || chan_count > im->channels) {
457 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
461 for (i = 0; i < w; ++i) {
462 i_palidx which = *data++;
463 if (which < palsize) {
464 for (ch = 0; ch < chan_count; ++ch) {
465 *samps++ = pal[which].channel[ch];
479 =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
484 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) {
485 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
490 data = ((i_palidx *)im->idata) + l + y * im->xsize;
492 for (i = 0; i < w; ++i) {
503 =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
508 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) {
509 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
514 data = ((i_palidx *)im->idata) + l + y * im->xsize;
516 for (i = 0; i < w; ++i) {
527 =item i_addcolors_p(i_img *im, const i_color *color, int count)
531 static int i_addcolors_p(i_img *im, const i_color *color, int count) {
532 if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
533 int result = PALEXT(im)->count;
536 PALEXT(im)->count += count;
538 PALEXT(im)->pal[index++] = *color++;
549 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
553 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
554 if (i >= 0 && i+count <= PALEXT(im)->count) {
556 *color++ = PALEXT(im)->pal[i++];
565 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
567 for (ch = 0; ch < im->channels; ++ch) {
568 if (c1->channel[ch] != c2->channel[ch])
575 =item i_colorcount_p(i_img *im)
579 static int i_colorcount_p(i_img *im) {
580 return PALEXT(im)->count;
584 =item i_maxcolors_p(i_img *im)
588 static int i_maxcolors_p(i_img *im) {
589 return PALEXT(im)->alloc;
593 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
597 static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
598 if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
600 PALEXT(im)->pal[index++] = *colors++;
610 =item i_findcolor_p(i_img *im)
614 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
615 if (PALEXT(im)->count) {
617 /* often the same color comes up several times in a row */
618 if (PALEXT(im)->last_found >= 0) {
619 if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
620 *entry = PALEXT(im)->last_found;
624 for (i = 0; i < PALEXT(im)->count; ++i) {
625 if (color_eq(im, color, PALEXT(im)->pal + i)) {
626 PALEXT(im)->last_found = *entry = i;
635 =item i_psamp_p(im, l, r, y, samps, chans, chan_count)
637 Implement psamp() for paletted images.
639 Since writing samples doesn't really work as a concept for paletted
640 images, this is slow.
642 Also, writing samples may convert the image to a direct image in the
643 process, so use i_ppix/i_gpix instead of directly calling the paletted
650 i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
651 const i_sample_t *samps, const int *chans, int chan_count) {
652 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
660 /* make sure we have good channel numbers */
661 for (ch = 0; ch < chan_count; ++ch) {
662 if (chans[ch] < 0 || chans[ch] >= im->channels) {
664 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
671 i_gpix(im, l, y, &c);
672 for (ch = 0; ch < chan_count; ++ch)
673 c.channel[chans[ch]] = *samps++;
674 i_ppix(im, l, y, &c);
680 if (chan_count <= 0 || chan_count > im->channels) {
682 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
690 i_gpix(im, l, y, &c);
691 for (ch = 0; ch < chan_count; ++ch)
692 c.channel[ch] = *samps++;
693 i_ppix(im, l, y, &c);
703 i_push_error(0, "Image position outside of image");
709 =item i_psampf_p(im, l, r, y, samps, chans, chan_count)
711 Implement psampf() for paletted images.
713 Since writing samples doesn't really work as a concept for paletted
714 images, this is slow.
716 Also, writing samples may convert the image to a direct image in the
717 process, so use i_ppixf/i_gpixf instead of directly calling the paletted
724 i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
725 const i_fsample_t *samps, const int *chans, int chan_count) {
726 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
734 /* make sure we have good channel numbers */
735 for (ch = 0; ch < chan_count; ++ch) {
736 if (chans[ch] < 0 || chans[ch] >= im->channels) {
738 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
745 i_gpixf(im, l, y, &c);
746 for (ch = 0; ch < chan_count; ++ch)
747 c.channel[chans[ch]] = *samps++;
748 i_ppixf(im, l, y, &c);
754 if (chan_count <= 0 || chan_count > im->channels) {
756 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
764 i_gpixf(im, l, y, &c);
765 for (ch = 0; ch < chan_count; ++ch)
766 c.channel[ch] = *samps++;
767 i_ppixf(im, l, y, &c);
777 i_push_error(0, "Image position outside of image");
787 Tony Cook <tony@develop-help.com>