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 | ||
92bda632 TC |
21 | #include "imager.h" |
22 | #include "imageri.h" | |
faa9b3e7 TC |
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 | /* | |
92bda632 | 77 | =item i_img_pal_new_low(im, x, y, channels, maxpal) |
faa9b3e7 TC |
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 | ||
92bda632 TC |
135 | /* |
136 | =item i_img_pal_new(x, y, channels, maxpal) | |
137 | ||
138 | =category Image creation | |
139 | ||
140 | Creates a new paletted image of the supplied dimensions. | |
141 | ||
142 | Returns a new image or NULL on failure. | |
143 | ||
144 | =cut | |
145 | */ | |
146 | ||
faa9b3e7 | 147 | i_img *i_img_pal_new(int x, int y, int channels, int maxpal) { |
7f882a01 AMH |
148 | i_img *im; |
149 | mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal)); | |
150 | im = mymalloc(sizeof(i_img)); | |
4dc7c46b TC |
151 | if (!i_img_pal_new_low(im, x, y, channels, maxpal)) { |
152 | myfree(im); | |
153 | im = NULL; | |
154 | } | |
155 | ||
156 | return im; | |
faa9b3e7 TC |
157 | } |
158 | ||
159 | /* | |
160 | =item i_img_rgb_convert(i_img *targ, i_img *src) | |
161 | ||
162 | Converts paletted data in src to RGB data in targ | |
163 | ||
164 | Internal function. | |
165 | ||
166 | src must be a paletted image and targ must be an RGB image with the | |
167 | same width, height and channels. | |
168 | ||
169 | =cut | |
170 | */ | |
171 | static void i_img_rgb_convert(i_img *targ, i_img *src) { | |
172 | i_color *row = mymalloc(sizeof(i_color) * targ->xsize); | |
173 | int y; | |
174 | for (y = 0; y < targ->ysize; ++y) { | |
175 | i_glin(src, 0, src->xsize, y, row); | |
176 | i_plin(targ, 0, src->xsize, y, row); | |
177 | } | |
178 | myfree(row); | |
179 | } | |
180 | ||
181 | /* | |
182 | =item i_img_to_rgb_inplace(im) | |
183 | ||
184 | Converts im from a paletted image to an RGB image. | |
185 | ||
186 | The conversion is done in place. | |
187 | ||
188 | The conversion cannot be done for virtual images. | |
189 | ||
190 | =cut | |
191 | */ | |
192 | int i_img_to_rgb_inplace(i_img *im) { | |
193 | i_img temp; | |
faa9b3e7 TC |
194 | |
195 | if (im->virtual) | |
196 | return 0; | |
197 | ||
198 | if (im->type == i_direct_type) | |
199 | return 1; /* trivial success */ | |
200 | ||
201 | i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels); | |
202 | i_img_rgb_convert(&temp, im); | |
203 | ||
204 | /* nasty hack */ | |
205 | (im->i_f_destroy)(im); | |
206 | myfree(im->idata); | |
207 | *im = temp; | |
208 | ||
209 | return 1; | |
210 | } | |
211 | ||
212 | /* | |
213 | =item i_img_to_pal(i_img *im, i_quantize *quant) | |
214 | ||
215 | Converts an RGB image to a paletted image | |
216 | ||
217 | =cut | |
218 | */ | |
219 | i_img *i_img_to_pal(i_img *src, i_quantize *quant) { | |
220 | i_palidx *result; | |
221 | i_img *im; | |
faa9b3e7 | 222 | |
1501d9b3 TC |
223 | i_clear_error(); |
224 | ||
92bda632 TC |
225 | i_quant_makemap(quant, &src, 1); |
226 | result = i_quant_translate(quant, src); | |
faa9b3e7 | 227 | |
1501d9b3 | 228 | if (result) { |
faa9b3e7 | 229 | |
1501d9b3 | 230 | im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size); |
faa9b3e7 | 231 | |
1501d9b3 TC |
232 | /* copy things over */ |
233 | memcpy(im->idata, result, im->bytes); | |
234 | PALEXT(im)->count = quant->mc_count; | |
235 | memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count); | |
236 | ||
237 | myfree(result); | |
238 | ||
239 | return im; | |
240 | } | |
241 | else { | |
242 | return NULL; | |
243 | } | |
faa9b3e7 TC |
244 | } |
245 | ||
246 | /* | |
247 | =item i_img_to_rgb(i_img *src) | |
248 | ||
249 | =cut | |
250 | */ | |
251 | i_img *i_img_to_rgb(i_img *src) { | |
252 | i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels); | |
253 | i_img_rgb_convert(im, src); | |
254 | ||
255 | return im; | |
256 | } | |
257 | ||
258 | /* | |
259 | =item i_destroy_p(i_img *im) | |
260 | ||
261 | Destroys data related to a paletted image. | |
262 | ||
263 | =cut | |
264 | */ | |
265 | static void i_destroy_p(i_img *im) { | |
266 | if (im) { | |
267 | i_img_pal_ext *palext = im->ext_data; | |
268 | if (palext) { | |
269 | if (palext->pal) | |
270 | myfree(palext->pal); | |
271 | myfree(palext); | |
272 | } | |
273 | } | |
274 | } | |
275 | ||
276 | /* | |
277 | =item i_ppix_p(i_img *im, int x, int y, i_color *val) | |
278 | ||
279 | Write to a pixel in the image. | |
280 | ||
281 | Warning: converts the image to a RGB image if the color isn't already | |
282 | present in the image. | |
283 | ||
284 | =cut | |
285 | */ | |
63b018fd | 286 | static int i_ppix_p(i_img *im, int x, int y, i_color *val) { |
faa9b3e7 TC |
287 | i_palidx which; |
288 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) | |
289 | return -1; | |
290 | if (i_findcolor(im, val, &which)) { | |
291 | ((i_palidx *)im->idata)[x + y * im->xsize] = which; | |
292 | return 0; | |
293 | } | |
294 | else { | |
295 | if (i_img_to_rgb_inplace(im)) { | |
296 | return i_ppix(im, x, y, val); | |
297 | } | |
298 | else | |
299 | return -1; | |
300 | } | |
301 | } | |
302 | ||
303 | /* | |
92bda632 | 304 | =item i_gpix_p(i_img *im, int x, int y, i_color *val) |
faa9b3e7 TC |
305 | |
306 | Retrieve a pixel, converting from a palette index to a color. | |
307 | ||
308 | =cut | |
309 | */ | |
63b018fd | 310 | static int i_gpix_p(i_img *im, int x, int y, i_color *val) { |
faa9b3e7 TC |
311 | i_palidx which; |
312 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { | |
313 | return -1; | |
314 | } | |
315 | which = ((i_palidx *)im->idata)[x + y * im->xsize]; | |
316 | if (which > PALEXT(im)->count) | |
317 | return -1; | |
318 | *val = PALEXT(im)->pal[which]; | |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
323 | /* | |
324 | =item i_glinp(i_img *im, int l, int r, int y, i_color *vals) | |
325 | ||
326 | Retrieve a row of pixels. | |
327 | ||
328 | =cut | |
329 | */ | |
63b018fd | 330 | static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) { |
faa9b3e7 TC |
331 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
332 | int palsize = PALEXT(im)->count; | |
333 | i_color *pal = PALEXT(im)->pal; | |
334 | i_palidx *data; | |
335 | int count, i; | |
336 | if (r > im->xsize) | |
337 | r = im->xsize; | |
338 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
339 | count = r - l; | |
340 | for (i = 0; i < count; ++i) { | |
341 | i_palidx which = *data++; | |
342 | if (which < palsize) | |
343 | vals[i] = pal[which]; | |
344 | } | |
345 | return count; | |
346 | } | |
347 | else { | |
348 | return 0; | |
349 | } | |
350 | } | |
351 | ||
352 | /* | |
353 | =item i_plin_p(i_img *im, int l, int r, int y, i_color *vals) | |
354 | ||
355 | Write a line of color data to the image. | |
356 | ||
357 | If any color value is not in the image when the image is converted to | |
358 | RGB. | |
359 | ||
360 | =cut | |
361 | */ | |
63b018fd | 362 | static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) { |
a659442a | 363 | int count, i; |
faa9b3e7 TC |
364 | i_palidx *data; |
365 | i_palidx which; | |
366 | if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { | |
367 | if (r > im->xsize) | |
368 | r = im->xsize; | |
369 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
370 | count = r - l; | |
371 | for (i = 0; i < count; ++i) { | |
372 | if (i_findcolor(im, vals+i, &which)) { | |
373 | ((i_palidx *)data)[i] = which; | |
374 | } | |
375 | else { | |
376 | if (i_img_to_rgb_inplace(im)) { | |
377 | return i+i_plin(im, l+i, r, y, vals+i); | |
378 | } | |
379 | } | |
380 | } | |
381 | return count; | |
382 | } | |
383 | else { | |
384 | return 0; | |
385 | } | |
386 | } | |
387 | ||
388 | /* | |
389 | =item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count) | |
390 | ||
391 | =cut | |
392 | */ | |
63b018fd | 393 | static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, |
18accb2a | 394 | int const *chans, int chan_count) { |
faa9b3e7 TC |
395 | int ch; |
396 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { | |
397 | int palsize = PALEXT(im)->count; | |
398 | i_color *pal = PALEXT(im)->pal; | |
399 | i_palidx *data; | |
400 | int count, i, w; | |
401 | if (r > im->xsize) | |
402 | r = im->xsize; | |
403 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
404 | count = 0; | |
405 | w = r - l; | |
406 | if (chans) { | |
407 | for (ch = 0; ch < chan_count; ++ch) { | |
408 | if (chans[ch] < 0 || chans[ch] >= im->channels) { | |
409 | i_push_errorf(0, "No channel %d in this image", chans[ch]); | |
410 | } | |
411 | } | |
412 | ||
413 | for (i = 0; i < w; ++i) { | |
414 | i_palidx which = *data++; | |
415 | if (which < palsize) { | |
416 | for (ch = 0; ch < chan_count; ++ch) { | |
417 | *samps++ = pal[which].channel[chans[ch]]; | |
418 | ++count; | |
419 | } | |
420 | } | |
421 | } | |
422 | } | |
423 | else { | |
424 | for (i = 0; i < w; ++i) { | |
425 | i_palidx which = *data++; | |
426 | if (which < palsize) { | |
427 | for (ch = 0; ch < chan_count; ++ch) { | |
428 | *samps++ = pal[which].channel[ch]; | |
429 | ++count; | |
430 | } | |
431 | } | |
432 | } | |
433 | } | |
434 | return count; | |
435 | } | |
436 | else { | |
437 | return 0; | |
438 | } | |
439 | } | |
440 | ||
441 | /* | |
442 | =item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) | |
443 | ||
444 | =cut | |
445 | */ | |
446 | ||
63b018fd | 447 | static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) { |
faa9b3e7 TC |
448 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
449 | i_palidx *data; | |
450 | int i, w; | |
451 | if (r > im->xsize) | |
452 | r = im->xsize; | |
453 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
454 | w = r - l; | |
455 | for (i = 0; i < w; ++i) { | |
456 | *vals++ = *data++; | |
457 | } | |
458 | return i; | |
459 | } | |
460 | else { | |
461 | return 0; | |
462 | } | |
463 | } | |
464 | ||
465 | /* | |
466 | =item i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) | |
467 | ||
468 | =cut | |
469 | */ | |
470 | ||
63b018fd | 471 | static int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) { |
faa9b3e7 TC |
472 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
473 | i_palidx *data; | |
474 | int i, w; | |
475 | if (r > im->xsize) | |
476 | r = im->xsize; | |
477 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
478 | w = r - l; | |
479 | for (i = 0; i < w; ++i) { | |
480 | *data++ = *vals++; | |
481 | } | |
482 | return i; | |
483 | } | |
484 | else { | |
485 | return 0; | |
486 | } | |
487 | } | |
488 | ||
489 | /* | |
490 | =item i_addcolors_p(i_img *im, i_color *color, int count) | |
491 | ||
492 | =cut | |
493 | */ | |
63b018fd | 494 | static int i_addcolors_p(i_img *im, i_color *color, int count) { |
faa9b3e7 TC |
495 | if (PALEXT(im)->count + count <= PALEXT(im)->alloc) { |
496 | int result = PALEXT(im)->count; | |
497 | int index = result; | |
498 | ||
499 | PALEXT(im)->count += count; | |
500 | while (count) { | |
501 | PALEXT(im)->pal[index++] = *color++; | |
502 | --count; | |
503 | } | |
504 | ||
505 | return result; | |
506 | } | |
507 | else | |
508 | return -1; | |
509 | } | |
510 | ||
511 | /* | |
512 | =item i_getcolors_p(i_img *im, int i, i_color *color, int count) | |
513 | ||
514 | =cut | |
515 | */ | |
63b018fd | 516 | static int i_getcolors_p(i_img *im, int i, i_color *color, int count) { |
faa9b3e7 TC |
517 | if (i >= 0 && i+count <= PALEXT(im)->count) { |
518 | while (count) { | |
519 | *color++ = PALEXT(im)->pal[i++]; | |
520 | --count; | |
521 | } | |
522 | return 1; | |
523 | } | |
524 | else | |
525 | return 0; | |
526 | } | |
527 | ||
528 | static int color_eq(i_img *im, i_color *c1, i_color *c2) { | |
529 | int ch; | |
530 | for (ch = 0; ch < im->channels; ++ch) { | |
531 | if (c1->channel[ch] != c2->channel[ch]) | |
532 | return 0; | |
533 | } | |
534 | return 1; | |
535 | } | |
536 | ||
537 | /* | |
538 | =item i_colorcount_p(i_img *im) | |
539 | ||
540 | =cut | |
541 | */ | |
63b018fd | 542 | static int i_colorcount_p(i_img *im) { |
faa9b3e7 TC |
543 | return PALEXT(im)->count; |
544 | } | |
545 | ||
546 | /* | |
547 | =item i_maxcolors_p(i_img *im) | |
548 | ||
549 | =cut | |
550 | */ | |
63b018fd | 551 | static int i_maxcolors_p(i_img *im) { |
faa9b3e7 TC |
552 | return PALEXT(im)->alloc; |
553 | } | |
554 | ||
555 | /* | |
556 | =item i_setcolors_p(i_img *im, int index, i_color *colors, int count) | |
557 | ||
558 | =cut | |
559 | */ | |
63b018fd | 560 | static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) { |
8efd1577 | 561 | if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) { |
faa9b3e7 TC |
562 | while (count) { |
563 | PALEXT(im)->pal[index++] = *colors++; | |
564 | --count; | |
565 | } | |
566 | return 1; | |
567 | } | |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
572 | /* | |
573 | =item i_findcolor_p(i_img *im) | |
574 | ||
575 | =cut | |
576 | */ | |
63b018fd | 577 | static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) { |
faa9b3e7 TC |
578 | if (PALEXT(im)->count) { |
579 | int i; | |
580 | /* often the same color comes up several times in a row */ | |
581 | if (PALEXT(im)->last_found >= 0) { | |
582 | if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) { | |
583 | *entry = PALEXT(im)->last_found; | |
584 | return 1; | |
585 | } | |
586 | } | |
587 | for (i = 0; i < PALEXT(im)->count; ++i) { | |
588 | if (color_eq(im, color, PALEXT(im)->pal + i)) { | |
589 | PALEXT(im)->last_found = *entry = i; | |
590 | return 1; | |
591 | } | |
592 | } | |
593 | } | |
594 | return 0; | |
595 | } | |
b8c2033e AMH |
596 | |
597 | /* | |
598 | =back | |
599 | ||
600 | =head1 AUTHOR | |
601 | ||
602 | Tony Cook <tony@develop-help.com> | |
603 | ||
604 | =head1 SEE ALSO | |
605 | ||
606 | Imager(3) | |
607 | ||
608 | =cut | |
609 | */ |