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, int x, int y, i_color *val);
26 static int i_gpix_p(i_img *im, int x, int y, i_color *val);
27 static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals);
28 static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals);
29 static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int const *chans, int chan_count);
30 static int i_gpal_p(i_img *pm, int l, int r, int y, i_palidx *vals);
31 static int i_ppal_p(i_img *pm, int l, int r, int y, i_palidx *vals);
32 static int i_addcolors_p(i_img *im, 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, i_color *color, i_palidx *entry);
37 static int i_setcolors_p(i_img *im, int index, i_color *color, int count);
39 static void i_destroy_p(i_img *im);
41 static i_img IIM_base_8bit_pal =
44 0, 0, 0, /* xsize, ysize, bytes */
47 i_palette_type, /* type */
50 { 0, 0, NULL }, /* tags */
53 i_ppix_p, /* i_f_ppix */
54 i_ppixf_fp, /* i_f_ppixf */
55 i_plin_p, /* i_f_plin */
56 i_plinf_fp, /* i_f_plinf */
57 i_gpix_p, /* i_f_gpix */
58 i_gpixf_fp, /* i_f_gpixf */
59 i_glin_p, /* i_f_glin */
60 i_glinf_fp, /* i_f_glinf */
61 i_gsamp_p, /* i_f_gsamp */
62 i_gsampf_fp, /* i_f_gsampf */
64 i_gpal_p, /* i_f_gpal */
65 i_ppal_p, /* i_f_ppal */
66 i_addcolors_p, /* i_f_addcolors */
67 i_getcolors_p, /* i_f_getcolors */
68 i_colorcount_p, /* i_f_colorcount */
69 i_maxcolors_p, /* i_f_maxcolors */
70 i_findcolor_p, /* i_f_findcolor */
71 i_setcolors_p, /* i_f_setcolors */
73 i_destroy_p, /* i_f_destroy */
77 =item i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal)
79 Creates a new paletted image.
81 Currently 0 < maxpal <= 256
85 i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
86 i_img_pal_ext *palext;
90 if (maxpal < 0 || maxpal > 256) {
91 i_push_error(0, "Maximum of 256 palette entries");
95 i_push_error(0, "Image sizes must be positive");
98 if (channels < 1 || channels > MAXCHANNELS) {
99 i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS);
102 bytes = sizeof(i_palidx) * x * y;
103 if (bytes / y / sizeof(i_palidx) != x) {
104 i_push_errorf(0, "integer overflow calculating image allocation");
108 memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
109 palext = mymalloc(sizeof(i_img_pal_ext));
110 palext->pal = mymalloc(sizeof(i_color) * maxpal);
112 palext->alloc = maxpal;
113 palext->last_found = -1;
114 im->ext_data = palext;
115 i_tags_new(&im->tags);
117 im->idata = mymalloc(im->bytes);
118 im->channels = channels;
119 memset(im->idata, 0, im->bytes);
126 i_img *i_img_pal_new(int x, int y, int channels, int maxpal) {
128 mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal));
129 im = mymalloc(sizeof(i_img));
130 if (!i_img_pal_new_low(im, x, y, channels, maxpal)) {
139 =item i_img_rgb_convert(i_img *targ, i_img *src)
141 Converts paletted data in src to RGB data in targ
145 src must be a paletted image and targ must be an RGB image with the
146 same width, height and channels.
150 static void i_img_rgb_convert(i_img *targ, i_img *src) {
151 i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
153 for (y = 0; y < targ->ysize; ++y) {
154 i_glin(src, 0, src->xsize, y, row);
155 i_plin(targ, 0, src->xsize, y, row);
161 =item i_img_to_rgb_inplace(im)
163 Converts im from a paletted image to an RGB image.
165 The conversion is done in place.
167 The conversion cannot be done for virtual images.
171 int i_img_to_rgb_inplace(i_img *im) {
179 if (im->type == i_direct_type)
180 return 1; /* trivial success */
182 i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
183 i_img_rgb_convert(&temp, im);
186 (im->i_f_destroy)(im);
194 =item i_img_to_pal(i_img *im, i_quantize *quant)
196 Converts an RGB image to a paletted image
200 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
206 quant_makemap(quant, &src, 1);
207 result = quant_translate(quant, src);
211 im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
213 /* copy things over */
214 memcpy(im->idata, result, im->bytes);
215 PALEXT(im)->count = quant->mc_count;
216 memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
228 =item i_img_to_rgb(i_img *src)
232 i_img *i_img_to_rgb(i_img *src) {
233 i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
234 i_img_rgb_convert(im, src);
240 =item i_destroy_p(i_img *im)
242 Destroys data related to a paletted image.
246 static void i_destroy_p(i_img *im) {
248 i_img_pal_ext *palext = im->ext_data;
258 =item i_ppix_p(i_img *im, int x, int y, i_color *val)
260 Write to a pixel in the image.
262 Warning: converts the image to a RGB image if the color isn't already
263 present in the image.
267 static int i_ppix_p(i_img *im, int x, int y, i_color *val) {
269 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
271 if (i_findcolor(im, val, &which)) {
272 ((i_palidx *)im->idata)[x + y * im->xsize] = which;
276 if (i_img_to_rgb_inplace(im)) {
277 return i_ppix(im, x, y, val);
285 =item i_gpix(i_img *im, int x, int y, i_color *val)
287 Retrieve a pixel, converting from a palette index to a color.
291 static int i_gpix_p(i_img *im, int x, int y, i_color *val) {
293 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
296 which = ((i_palidx *)im->idata)[x + y * im->xsize];
297 if (which > PALEXT(im)->count)
299 *val = PALEXT(im)->pal[which];
305 =item i_glinp(i_img *im, int l, int r, int y, i_color *vals)
307 Retrieve a row of pixels.
311 static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) {
312 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
313 int palsize = PALEXT(im)->count;
314 i_color *pal = PALEXT(im)->pal;
319 data = ((i_palidx *)im->idata) + l + y * im->xsize;
321 for (i = 0; i < count; ++i) {
322 i_palidx which = *data++;
324 vals[i] = pal[which];
334 =item i_plin_p(i_img *im, int l, int r, int y, i_color *vals)
336 Write a line of color data to the image.
338 If any color value is not in the image when the image is converted to
343 static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) {
347 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
350 data = ((i_palidx *)im->idata) + l + y * im->xsize;
352 for (i = 0; i < count; ++i) {
353 if (i_findcolor(im, vals+i, &which)) {
354 ((i_palidx *)data)[i] = which;
357 if (i_img_to_rgb_inplace(im)) {
358 return i+i_plin(im, l+i, r, y, vals+i);
370 =item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count)
374 static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps,
375 int const *chans, int chan_count) {
377 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
378 int palsize = PALEXT(im)->count;
379 i_color *pal = PALEXT(im)->pal;
384 data = ((i_palidx *)im->idata) + l + y * im->xsize;
388 for (ch = 0; ch < chan_count; ++ch) {
389 if (chans[ch] < 0 || chans[ch] >= im->channels) {
390 i_push_errorf(0, "No channel %d in this image", chans[ch]);
394 for (i = 0; i < w; ++i) {
395 i_palidx which = *data++;
396 if (which < palsize) {
397 for (ch = 0; ch < chan_count; ++ch) {
398 *samps++ = pal[which].channel[chans[ch]];
405 for (i = 0; i < w; ++i) {
406 i_palidx which = *data++;
407 if (which < palsize) {
408 for (ch = 0; ch < chan_count; ++ch) {
409 *samps++ = pal[which].channel[ch];
423 =item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals)
428 static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
429 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
434 data = ((i_palidx *)im->idata) + l + y * im->xsize;
436 for (i = 0; i < w; ++i) {
447 =item i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals)
452 static int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
453 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
458 data = ((i_palidx *)im->idata) + l + y * im->xsize;
460 for (i = 0; i < w; ++i) {
471 =item i_addcolors_p(i_img *im, i_color *color, int count)
475 static int i_addcolors_p(i_img *im, i_color *color, int count) {
476 if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
477 int result = PALEXT(im)->count;
480 PALEXT(im)->count += count;
482 PALEXT(im)->pal[index++] = *color++;
493 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
497 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
498 if (i >= 0 && i+count <= PALEXT(im)->count) {
500 *color++ = PALEXT(im)->pal[i++];
509 static int color_eq(i_img *im, i_color *c1, i_color *c2) {
511 for (ch = 0; ch < im->channels; ++ch) {
512 if (c1->channel[ch] != c2->channel[ch])
519 =item i_colorcount_p(i_img *im)
523 static int i_colorcount_p(i_img *im) {
524 return PALEXT(im)->count;
528 =item i_maxcolors_p(i_img *im)
532 static int i_maxcolors_p(i_img *im) {
533 return PALEXT(im)->alloc;
537 =item i_setcolors_p(i_img *im, int index, i_color *colors, int count)
541 static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
542 if (index >= 0 && count >= 1 && index + count < PALEXT(im)->count) {
544 PALEXT(im)->pal[index++] = *colors++;
554 =item i_findcolor_p(i_img *im)
558 static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) {
559 if (PALEXT(im)->count) {
561 /* often the same color comes up several times in a row */
562 if (PALEXT(im)->last_found >= 0) {
563 if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
564 *entry = PALEXT(im)->last_found;
568 for (i = 0; i < PALEXT(im)->count; ++i) {
569 if (color_eq(im, color, PALEXT(im)->pal + i)) {
570 PALEXT(im)->last_found = *entry = i;
583 Tony Cook <tony@develop-help.com>