Commit | Line | Data |
---|---|---|
faa9b3e7 TC |
1 | /* |
2 | =head1 NAME | |
3 | ||
4 | palimg.c - implements paletted images for Imager. | |
5 | ||
6 | =head1 SYNOPSIS | |
7 | ||
8 | =head1 DESCRIPTION | |
9 | ||
10 | Implements paletted images using the new image interface. | |
11 | ||
12 | =over | |
13 | ||
14 | =item IIM_base_8bit_pal | |
15 | ||
16 | Basic 8-bit/sample paletted image | |
17 | ||
18 | =cut | |
19 | */ | |
20 | ||
21 | #include "image.h" | |
22 | #include "imagei.h" | |
23 | ||
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); | |
18accb2a | 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); |
faa9b3e7 TC |
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); | |
38 | ||
39 | static void i_destroy_p(i_img *im); | |
40 | ||
41 | static i_img IIM_base_8bit_pal = | |
42 | { | |
43 | 0, /* channels set */ | |
44 | 0, 0, 0, /* xsize, ysize, bytes */ | |
9a88a5e6 | 45 | ~0U, /* ch_mask */ |
faa9b3e7 TC |
46 | i_8_bits, /* bits */ |
47 | i_palette_type, /* type */ | |
48 | 0, /* virtual */ | |
49 | NULL, /* idata */ | |
50 | { 0, 0, NULL }, /* tags */ | |
51 | NULL, /* ext_data */ | |
52 | ||
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 */ | |
63 | ||
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 */ | |
72 | ||
73 | i_destroy_p, /* i_f_destroy */ | |
74 | }; | |
75 | ||
76 | /* | |
77 | =item i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) | |
78 | ||
79 | Creates a new paletted image. | |
80 | ||
81 | Currently 0 < maxpal <= 256 | |
82 | ||
83 | =cut | |
84 | */ | |
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; | |
f0960b14 | 87 | int bytes, line_bytes; |
faa9b3e7 TC |
88 | |
89 | i_clear_error(); | |
d443d6be | 90 | if (maxpal < 1 || maxpal > 256) { |
faa9b3e7 TC |
91 | i_push_error(0, "Maximum of 256 palette entries"); |
92 | return NULL; | |
93 | } | |
94 | if (x < 1 || y < 1) { | |
95 | i_push_error(0, "Image sizes must be positive"); | |
96 | return NULL; | |
97 | } | |
98 | if (channels < 1 || channels > MAXCHANNELS) { | |
1501d9b3 | 99 | i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS); |
faa9b3e7 TC |
100 | return NULL; |
101 | } | |
653ea321 TC |
102 | bytes = sizeof(i_palidx) * x * y; |
103 | if (bytes / y / sizeof(i_palidx) != x) { | |
f0960b14 TC |
104 | i_push_error(0, "integer overflow calculating image allocation"); |
105 | return NULL; | |
106 | } | |
107 | ||
108 | /* basic assumption: we can always allocate a buffer representing a | |
109 | line from the image, otherwise we're going to have trouble | |
110 | working with the image */ | |
111 | line_bytes = sizeof(i_color) * x; | |
112 | if (line_bytes / x != sizeof(i_color)) { | |
113 | i_push_error(0, "integer overflow calculating scanline allocation"); | |
653ea321 TC |
114 | return NULL; |
115 | } | |
faa9b3e7 TC |
116 | |
117 | memcpy(im, &IIM_base_8bit_pal, sizeof(i_img)); | |
118 | palext = mymalloc(sizeof(i_img_pal_ext)); | |
119 | palext->pal = mymalloc(sizeof(i_color) * maxpal); | |
120 | palext->count = 0; | |
121 | palext->alloc = maxpal; | |
122 | palext->last_found = -1; | |
123 | im->ext_data = palext; | |
124 | i_tags_new(&im->tags); | |
653ea321 | 125 | im->bytes = bytes; |
faa9b3e7 TC |
126 | im->idata = mymalloc(im->bytes); |
127 | im->channels = channels; | |
705fd961 | 128 | memset(im->idata, 0, im->bytes); |
faa9b3e7 TC |
129 | im->xsize = x; |
130 | im->ysize = y; | |
131 | ||
132 | return im; | |
133 | } | |
134 | ||
135 | i_img *i_img_pal_new(int x, int y, int channels, int maxpal) { | |
7f882a01 AMH |
136 | i_img *im; |
137 | mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal)); | |
138 | im = mymalloc(sizeof(i_img)); | |
4dc7c46b TC |
139 | if (!i_img_pal_new_low(im, x, y, channels, maxpal)) { |
140 | myfree(im); | |
141 | im = NULL; | |
142 | } | |
143 | ||
144 | return im; | |
faa9b3e7 TC |
145 | } |
146 | ||
147 | /* | |
148 | =item i_img_rgb_convert(i_img *targ, i_img *src) | |
149 | ||
150 | Converts paletted data in src to RGB data in targ | |
151 | ||
152 | Internal function. | |
153 | ||
154 | src must be a paletted image and targ must be an RGB image with the | |
155 | same width, height and channels. | |
156 | ||
157 | =cut | |
158 | */ | |
159 | static void i_img_rgb_convert(i_img *targ, i_img *src) { | |
160 | i_color *row = mymalloc(sizeof(i_color) * targ->xsize); | |
161 | int y; | |
162 | for (y = 0; y < targ->ysize; ++y) { | |
163 | i_glin(src, 0, src->xsize, y, row); | |
164 | i_plin(targ, 0, src->xsize, y, row); | |
165 | } | |
166 | myfree(row); | |
167 | } | |
168 | ||
169 | /* | |
170 | =item i_img_to_rgb_inplace(im) | |
171 | ||
172 | Converts im from a paletted image to an RGB image. | |
173 | ||
174 | The conversion is done in place. | |
175 | ||
176 | The conversion cannot be done for virtual images. | |
177 | ||
178 | =cut | |
179 | */ | |
180 | int i_img_to_rgb_inplace(i_img *im) { | |
181 | i_img temp; | |
faa9b3e7 TC |
182 | |
183 | if (im->virtual) | |
184 | return 0; | |
185 | ||
186 | if (im->type == i_direct_type) | |
187 | return 1; /* trivial success */ | |
188 | ||
189 | i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels); | |
190 | i_img_rgb_convert(&temp, im); | |
191 | ||
192 | /* nasty hack */ | |
193 | (im->i_f_destroy)(im); | |
194 | myfree(im->idata); | |
195 | *im = temp; | |
196 | ||
197 | return 1; | |
198 | } | |
199 | ||
200 | /* | |
201 | =item i_img_to_pal(i_img *im, i_quantize *quant) | |
202 | ||
203 | Converts an RGB image to a paletted image | |
204 | ||
205 | =cut | |
206 | */ | |
207 | i_img *i_img_to_pal(i_img *src, i_quantize *quant) { | |
208 | i_palidx *result; | |
209 | i_img *im; | |
faa9b3e7 | 210 | |
1501d9b3 TC |
211 | i_clear_error(); |
212 | ||
faa9b3e7 TC |
213 | quant_makemap(quant, &src, 1); |
214 | result = quant_translate(quant, src); | |
215 | ||
1501d9b3 | 216 | if (result) { |
faa9b3e7 | 217 | |
1501d9b3 | 218 | im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size); |
faa9b3e7 | 219 | |
1501d9b3 TC |
220 | /* copy things over */ |
221 | memcpy(im->idata, result, im->bytes); | |
222 | PALEXT(im)->count = quant->mc_count; | |
223 | memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count); | |
224 | ||
225 | myfree(result); | |
226 | ||
227 | return im; | |
228 | } | |
229 | else { | |
230 | return NULL; | |
231 | } | |
faa9b3e7 TC |
232 | } |
233 | ||
234 | /* | |
235 | =item i_img_to_rgb(i_img *src) | |
236 | ||
237 | =cut | |
238 | */ | |
239 | i_img *i_img_to_rgb(i_img *src) { | |
240 | i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels); | |
241 | i_img_rgb_convert(im, src); | |
242 | ||
243 | return im; | |
244 | } | |
245 | ||
246 | /* | |
247 | =item i_destroy_p(i_img *im) | |
248 | ||
249 | Destroys data related to a paletted image. | |
250 | ||
251 | =cut | |
252 | */ | |
253 | static void i_destroy_p(i_img *im) { | |
254 | if (im) { | |
255 | i_img_pal_ext *palext = im->ext_data; | |
256 | if (palext) { | |
257 | if (palext->pal) | |
258 | myfree(palext->pal); | |
259 | myfree(palext); | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
264 | /* | |
265 | =item i_ppix_p(i_img *im, int x, int y, i_color *val) | |
266 | ||
267 | Write to a pixel in the image. | |
268 | ||
269 | Warning: converts the image to a RGB image if the color isn't already | |
270 | present in the image. | |
271 | ||
272 | =cut | |
273 | */ | |
63b018fd | 274 | static int i_ppix_p(i_img *im, int x, int y, i_color *val) { |
faa9b3e7 TC |
275 | i_palidx which; |
276 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) | |
277 | return -1; | |
278 | if (i_findcolor(im, val, &which)) { | |
279 | ((i_palidx *)im->idata)[x + y * im->xsize] = which; | |
280 | return 0; | |
281 | } | |
282 | else { | |
283 | if (i_img_to_rgb_inplace(im)) { | |
284 | return i_ppix(im, x, y, val); | |
285 | } | |
286 | else | |
287 | return -1; | |
288 | } | |
289 | } | |
290 | ||
291 | /* | |
292 | =item i_gpix(i_img *im, int x, int y, i_color *val) | |
293 | ||
294 | Retrieve a pixel, converting from a palette index to a color. | |
295 | ||
296 | =cut | |
297 | */ | |
63b018fd | 298 | static int i_gpix_p(i_img *im, int x, int y, i_color *val) { |
faa9b3e7 TC |
299 | i_palidx which; |
300 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { | |
301 | return -1; | |
302 | } | |
303 | which = ((i_palidx *)im->idata)[x + y * im->xsize]; | |
304 | if (which > PALEXT(im)->count) | |
305 | return -1; | |
306 | *val = PALEXT(im)->pal[which]; | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | /* | |
312 | =item i_glinp(i_img *im, int l, int r, int y, i_color *vals) | |
313 | ||
314 | Retrieve a row of pixels. | |
315 | ||
316 | =cut | |
317 | */ | |
63b018fd | 318 | static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) { |
faa9b3e7 TC |
319 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
320 | int palsize = PALEXT(im)->count; | |
321 | i_color *pal = PALEXT(im)->pal; | |
322 | i_palidx *data; | |
323 | int count, i; | |
324 | if (r > im->xsize) | |
325 | r = im->xsize; | |
326 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
327 | count = r - l; | |
328 | for (i = 0; i < count; ++i) { | |
329 | i_palidx which = *data++; | |
330 | if (which < palsize) | |
331 | vals[i] = pal[which]; | |
332 | } | |
333 | return count; | |
334 | } | |
335 | else { | |
336 | return 0; | |
337 | } | |
338 | } | |
339 | ||
340 | /* | |
341 | =item i_plin_p(i_img *im, int l, int r, int y, i_color *vals) | |
342 | ||
343 | Write a line of color data to the image. | |
344 | ||
345 | If any color value is not in the image when the image is converted to | |
346 | RGB. | |
347 | ||
348 | =cut | |
349 | */ | |
63b018fd | 350 | static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) { |
a659442a | 351 | int count, i; |
faa9b3e7 TC |
352 | i_palidx *data; |
353 | i_palidx which; | |
354 | if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { | |
355 | if (r > im->xsize) | |
356 | r = im->xsize; | |
357 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
358 | count = r - l; | |
359 | for (i = 0; i < count; ++i) { | |
360 | if (i_findcolor(im, vals+i, &which)) { | |
361 | ((i_palidx *)data)[i] = which; | |
362 | } | |
363 | else { | |
364 | if (i_img_to_rgb_inplace(im)) { | |
365 | return i+i_plin(im, l+i, r, y, vals+i); | |
366 | } | |
367 | } | |
368 | } | |
369 | return count; | |
370 | } | |
371 | else { | |
372 | return 0; | |
373 | } | |
374 | } | |
375 | ||
376 | /* | |
377 | =item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count) | |
378 | ||
379 | =cut | |
380 | */ | |
63b018fd | 381 | static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, |
18accb2a | 382 | int const *chans, int chan_count) { |
faa9b3e7 TC |
383 | int ch; |
384 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { | |
385 | int palsize = PALEXT(im)->count; | |
386 | i_color *pal = PALEXT(im)->pal; | |
387 | i_palidx *data; | |
388 | int count, i, w; | |
389 | if (r > im->xsize) | |
390 | r = im->xsize; | |
391 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
392 | count = 0; | |
393 | w = r - l; | |
394 | if (chans) { | |
395 | for (ch = 0; ch < chan_count; ++ch) { | |
396 | if (chans[ch] < 0 || chans[ch] >= im->channels) { | |
397 | i_push_errorf(0, "No channel %d in this image", chans[ch]); | |
398 | } | |
399 | } | |
400 | ||
401 | for (i = 0; i < w; ++i) { | |
402 | i_palidx which = *data++; | |
403 | if (which < palsize) { | |
404 | for (ch = 0; ch < chan_count; ++ch) { | |
405 | *samps++ = pal[which].channel[chans[ch]]; | |
406 | ++count; | |
407 | } | |
408 | } | |
409 | } | |
410 | } | |
411 | else { | |
412 | for (i = 0; i < w; ++i) { | |
413 | i_palidx which = *data++; | |
414 | if (which < palsize) { | |
415 | for (ch = 0; ch < chan_count; ++ch) { | |
416 | *samps++ = pal[which].channel[ch]; | |
417 | ++count; | |
418 | } | |
419 | } | |
420 | } | |
421 | } | |
422 | return count; | |
423 | } | |
424 | else { | |
425 | return 0; | |
426 | } | |
427 | } | |
428 | ||
429 | /* | |
430 | =item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) | |
431 | ||
432 | =cut | |
433 | */ | |
434 | ||
63b018fd | 435 | static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) { |
faa9b3e7 TC |
436 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
437 | i_palidx *data; | |
438 | int i, w; | |
439 | if (r > im->xsize) | |
440 | r = im->xsize; | |
441 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
442 | w = r - l; | |
443 | for (i = 0; i < w; ++i) { | |
444 | *vals++ = *data++; | |
445 | } | |
446 | return i; | |
447 | } | |
448 | else { | |
449 | return 0; | |
450 | } | |
451 | } | |
452 | ||
453 | /* | |
454 | =item i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) | |
455 | ||
456 | =cut | |
457 | */ | |
458 | ||
63b018fd | 459 | static int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) { |
faa9b3e7 TC |
460 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
461 | i_palidx *data; | |
462 | int i, w; | |
463 | if (r > im->xsize) | |
464 | r = im->xsize; | |
465 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
466 | w = r - l; | |
467 | for (i = 0; i < w; ++i) { | |
468 | *data++ = *vals++; | |
469 | } | |
470 | return i; | |
471 | } | |
472 | else { | |
473 | return 0; | |
474 | } | |
475 | } | |
476 | ||
477 | /* | |
478 | =item i_addcolors_p(i_img *im, i_color *color, int count) | |
479 | ||
480 | =cut | |
481 | */ | |
63b018fd | 482 | static int i_addcolors_p(i_img *im, i_color *color, int count) { |
faa9b3e7 TC |
483 | if (PALEXT(im)->count + count <= PALEXT(im)->alloc) { |
484 | int result = PALEXT(im)->count; | |
485 | int index = result; | |
486 | ||
487 | PALEXT(im)->count += count; | |
488 | while (count) { | |
489 | PALEXT(im)->pal[index++] = *color++; | |
490 | --count; | |
491 | } | |
492 | ||
493 | return result; | |
494 | } | |
495 | else | |
496 | return -1; | |
497 | } | |
498 | ||
499 | /* | |
500 | =item i_getcolors_p(i_img *im, int i, i_color *color, int count) | |
501 | ||
502 | =cut | |
503 | */ | |
63b018fd | 504 | static int i_getcolors_p(i_img *im, int i, i_color *color, int count) { |
faa9b3e7 TC |
505 | if (i >= 0 && i+count <= PALEXT(im)->count) { |
506 | while (count) { | |
507 | *color++ = PALEXT(im)->pal[i++]; | |
508 | --count; | |
509 | } | |
510 | return 1; | |
511 | } | |
512 | else | |
513 | return 0; | |
514 | } | |
515 | ||
516 | static int color_eq(i_img *im, i_color *c1, i_color *c2) { | |
517 | int ch; | |
518 | for (ch = 0; ch < im->channels; ++ch) { | |
519 | if (c1->channel[ch] != c2->channel[ch]) | |
520 | return 0; | |
521 | } | |
522 | return 1; | |
523 | } | |
524 | ||
525 | /* | |
526 | =item i_colorcount_p(i_img *im) | |
527 | ||
528 | =cut | |
529 | */ | |
63b018fd | 530 | static int i_colorcount_p(i_img *im) { |
faa9b3e7 TC |
531 | return PALEXT(im)->count; |
532 | } | |
533 | ||
534 | /* | |
535 | =item i_maxcolors_p(i_img *im) | |
536 | ||
537 | =cut | |
538 | */ | |
63b018fd | 539 | static int i_maxcolors_p(i_img *im) { |
faa9b3e7 TC |
540 | return PALEXT(im)->alloc; |
541 | } | |
542 | ||
543 | /* | |
544 | =item i_setcolors_p(i_img *im, int index, i_color *colors, int count) | |
545 | ||
546 | =cut | |
547 | */ | |
63b018fd | 548 | static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) { |
8efd1577 | 549 | if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) { |
faa9b3e7 TC |
550 | while (count) { |
551 | PALEXT(im)->pal[index++] = *colors++; | |
552 | --count; | |
553 | } | |
554 | return 1; | |
555 | } | |
556 | ||
557 | return 0; | |
558 | } | |
559 | ||
560 | /* | |
561 | =item i_findcolor_p(i_img *im) | |
562 | ||
563 | =cut | |
564 | */ | |
63b018fd | 565 | static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) { |
faa9b3e7 TC |
566 | if (PALEXT(im)->count) { |
567 | int i; | |
568 | /* often the same color comes up several times in a row */ | |
569 | if (PALEXT(im)->last_found >= 0) { | |
570 | if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) { | |
571 | *entry = PALEXT(im)->last_found; | |
572 | return 1; | |
573 | } | |
574 | } | |
575 | for (i = 0; i < PALEXT(im)->count; ++i) { | |
576 | if (color_eq(im, color, PALEXT(im)->pal + i)) { | |
577 | PALEXT(im)->last_found = *entry = i; | |
578 | return 1; | |
579 | } | |
580 | } | |
581 | } | |
582 | return 0; | |
583 | } | |
b8c2033e AMH |
584 | |
585 | /* | |
586 | =back | |
587 | ||
588 | =head1 AUTHOR | |
589 | ||
590 | Tony Cook <tony@develop-help.com> | |
591 | ||
592 | =head1 SEE ALSO | |
593 | ||
594 | Imager(3) | |
595 | ||
596 | =cut | |
597 | */ |