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); | |
29 | static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int *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); | |
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 */ | |
45 | ~0, /* ch_mask */ | |
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; | |
87 | ||
88 | i_clear_error(); | |
89 | if (maxpal < 0 || maxpal > 256) { | |
90 | i_push_error(0, "Maximum of 256 palette entries"); | |
91 | return NULL; | |
92 | } | |
93 | if (x < 1 || y < 1) { | |
94 | i_push_error(0, "Image sizes must be positive"); | |
95 | return NULL; | |
96 | } | |
97 | if (channels < 1 || channels > MAXCHANNELS) { | |
98 | i_push_errorf(0, "Channels must be postive and <= %d", MAXCHANNELS); | |
99 | return NULL; | |
100 | } | |
101 | ||
102 | memcpy(im, &IIM_base_8bit_pal, sizeof(i_img)); | |
103 | palext = mymalloc(sizeof(i_img_pal_ext)); | |
104 | palext->pal = mymalloc(sizeof(i_color) * maxpal); | |
105 | palext->count = 0; | |
106 | palext->alloc = maxpal; | |
107 | palext->last_found = -1; | |
108 | im->ext_data = palext; | |
109 | i_tags_new(&im->tags); | |
110 | im->bytes = sizeof(i_palidx) * x * y; | |
111 | im->idata = mymalloc(im->bytes); | |
112 | im->channels = channels; | |
113 | im->xsize = x; | |
114 | im->ysize = y; | |
115 | ||
116 | return im; | |
117 | } | |
118 | ||
119 | i_img *i_img_pal_new(int x, int y, int channels, int maxpal) { | |
120 | i_img *im = mymalloc(sizeof(i_img)); | |
121 | ||
122 | return i_img_pal_new_low(im, x, y, channels, maxpal); | |
123 | } | |
124 | ||
125 | /* | |
126 | =item i_img_rgb_convert(i_img *targ, i_img *src) | |
127 | ||
128 | Converts paletted data in src to RGB data in targ | |
129 | ||
130 | Internal function. | |
131 | ||
132 | src must be a paletted image and targ must be an RGB image with the | |
133 | same width, height and channels. | |
134 | ||
135 | =cut | |
136 | */ | |
137 | static void i_img_rgb_convert(i_img *targ, i_img *src) { | |
138 | i_color *row = mymalloc(sizeof(i_color) * targ->xsize); | |
139 | int y; | |
140 | for (y = 0; y < targ->ysize; ++y) { | |
141 | i_glin(src, 0, src->xsize, y, row); | |
142 | i_plin(targ, 0, src->xsize, y, row); | |
143 | } | |
144 | myfree(row); | |
145 | } | |
146 | ||
147 | /* | |
148 | =item i_img_to_rgb_inplace(im) | |
149 | ||
150 | Converts im from a paletted image to an RGB image. | |
151 | ||
152 | The conversion is done in place. | |
153 | ||
154 | The conversion cannot be done for virtual images. | |
155 | ||
156 | =cut | |
157 | */ | |
158 | int i_img_to_rgb_inplace(i_img *im) { | |
159 | i_img temp; | |
160 | i_color *pal; | |
161 | int palsize; | |
162 | ||
163 | if (im->virtual) | |
164 | return 0; | |
165 | ||
166 | if (im->type == i_direct_type) | |
167 | return 1; /* trivial success */ | |
168 | ||
169 | i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels); | |
170 | i_img_rgb_convert(&temp, im); | |
171 | ||
172 | /* nasty hack */ | |
173 | (im->i_f_destroy)(im); | |
174 | myfree(im->idata); | |
175 | *im = temp; | |
176 | ||
177 | return 1; | |
178 | } | |
179 | ||
180 | /* | |
181 | =item i_img_to_pal(i_img *im, i_quantize *quant) | |
182 | ||
183 | Converts an RGB image to a paletted image | |
184 | ||
185 | =cut | |
186 | */ | |
187 | i_img *i_img_to_pal(i_img *src, i_quantize *quant) { | |
188 | i_palidx *result; | |
189 | i_img *im; | |
190 | ||
191 | im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size); | |
192 | ||
193 | quant_makemap(quant, &src, 1); | |
194 | result = quant_translate(quant, src); | |
195 | ||
196 | /* copy things over */ | |
197 | memcpy(im->idata, result, im->bytes); | |
198 | PALEXT(im)->count = quant->mc_count; | |
199 | memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count); | |
200 | ||
201 | myfree(result); | |
202 | ||
203 | return im; | |
204 | } | |
205 | ||
206 | /* | |
207 | =item i_img_to_rgb(i_img *src) | |
208 | ||
209 | =cut | |
210 | */ | |
211 | i_img *i_img_to_rgb(i_img *src) { | |
212 | i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels); | |
213 | i_img_rgb_convert(im, src); | |
214 | ||
215 | return im; | |
216 | } | |
217 | ||
218 | /* | |
219 | =item i_destroy_p(i_img *im) | |
220 | ||
221 | Destroys data related to a paletted image. | |
222 | ||
223 | =cut | |
224 | */ | |
225 | static void i_destroy_p(i_img *im) { | |
226 | if (im) { | |
227 | i_img_pal_ext *palext = im->ext_data; | |
228 | if (palext) { | |
229 | if (palext->pal) | |
230 | myfree(palext->pal); | |
231 | myfree(palext); | |
232 | } | |
233 | } | |
234 | } | |
235 | ||
236 | /* | |
237 | =item i_ppix_p(i_img *im, int x, int y, i_color *val) | |
238 | ||
239 | Write to a pixel in the image. | |
240 | ||
241 | Warning: converts the image to a RGB image if the color isn't already | |
242 | present in the image. | |
243 | ||
244 | =cut | |
245 | */ | |
246 | int i_ppix_p(i_img *im, int x, int y, i_color *val) { | |
247 | i_palidx which; | |
248 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) | |
249 | return -1; | |
250 | if (i_findcolor(im, val, &which)) { | |
251 | ((i_palidx *)im->idata)[x + y * im->xsize] = which; | |
252 | return 0; | |
253 | } | |
254 | else { | |
255 | if (i_img_to_rgb_inplace(im)) { | |
256 | return i_ppix(im, x, y, val); | |
257 | } | |
258 | else | |
259 | return -1; | |
260 | } | |
261 | } | |
262 | ||
263 | /* | |
264 | =item i_gpix(i_img *im, int x, int y, i_color *val) | |
265 | ||
266 | Retrieve a pixel, converting from a palette index to a color. | |
267 | ||
268 | =cut | |
269 | */ | |
270 | int i_gpix_p(i_img *im, int x, int y, i_color *val) { | |
271 | i_palidx which; | |
272 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { | |
273 | return -1; | |
274 | } | |
275 | which = ((i_palidx *)im->idata)[x + y * im->xsize]; | |
276 | if (which > PALEXT(im)->count) | |
277 | return -1; | |
278 | *val = PALEXT(im)->pal[which]; | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | /* | |
284 | =item i_glinp(i_img *im, int l, int r, int y, i_color *vals) | |
285 | ||
286 | Retrieve a row of pixels. | |
287 | ||
288 | =cut | |
289 | */ | |
290 | int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) { | |
291 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { | |
292 | int palsize = PALEXT(im)->count; | |
293 | i_color *pal = PALEXT(im)->pal; | |
294 | i_palidx *data; | |
295 | int count, i; | |
296 | if (r > im->xsize) | |
297 | r = im->xsize; | |
298 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
299 | count = r - l; | |
300 | for (i = 0; i < count; ++i) { | |
301 | i_palidx which = *data++; | |
302 | if (which < palsize) | |
303 | vals[i] = pal[which]; | |
304 | } | |
305 | return count; | |
306 | } | |
307 | else { | |
308 | return 0; | |
309 | } | |
310 | } | |
311 | ||
312 | /* | |
313 | =item i_plin_p(i_img *im, int l, int r, int y, i_color *vals) | |
314 | ||
315 | Write a line of color data to the image. | |
316 | ||
317 | If any color value is not in the image when the image is converted to | |
318 | RGB. | |
319 | ||
320 | =cut | |
321 | */ | |
322 | int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) { | |
323 | int ch, count, i; | |
324 | i_palidx *data; | |
325 | i_palidx which; | |
326 | if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { | |
327 | if (r > im->xsize) | |
328 | r = im->xsize; | |
329 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
330 | count = r - l; | |
331 | for (i = 0; i < count; ++i) { | |
332 | if (i_findcolor(im, vals+i, &which)) { | |
333 | ((i_palidx *)data)[i] = which; | |
334 | } | |
335 | else { | |
336 | if (i_img_to_rgb_inplace(im)) { | |
337 | return i+i_plin(im, l+i, r, y, vals+i); | |
338 | } | |
339 | } | |
340 | } | |
341 | return count; | |
342 | } | |
343 | else { | |
344 | return 0; | |
345 | } | |
346 | } | |
347 | ||
348 | /* | |
349 | =item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count) | |
350 | ||
351 | =cut | |
352 | */ | |
353 | int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, | |
354 | int *chans, int chan_count) { | |
355 | int ch; | |
356 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { | |
357 | int palsize = PALEXT(im)->count; | |
358 | i_color *pal = PALEXT(im)->pal; | |
359 | i_palidx *data; | |
360 | int count, i, w; | |
361 | if (r > im->xsize) | |
362 | r = im->xsize; | |
363 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
364 | count = 0; | |
365 | w = r - l; | |
366 | if (chans) { | |
367 | for (ch = 0; ch < chan_count; ++ch) { | |
368 | if (chans[ch] < 0 || chans[ch] >= im->channels) { | |
369 | i_push_errorf(0, "No channel %d in this image", chans[ch]); | |
370 | } | |
371 | } | |
372 | ||
373 | for (i = 0; i < w; ++i) { | |
374 | i_palidx which = *data++; | |
375 | if (which < palsize) { | |
376 | for (ch = 0; ch < chan_count; ++ch) { | |
377 | *samps++ = pal[which].channel[chans[ch]]; | |
378 | ++count; | |
379 | } | |
380 | } | |
381 | } | |
382 | } | |
383 | else { | |
384 | for (i = 0; i < w; ++i) { | |
385 | i_palidx which = *data++; | |
386 | if (which < palsize) { | |
387 | for (ch = 0; ch < chan_count; ++ch) { | |
388 | *samps++ = pal[which].channel[ch]; | |
389 | ++count; | |
390 | } | |
391 | } | |
392 | } | |
393 | } | |
394 | return count; | |
395 | } | |
396 | else { | |
397 | return 0; | |
398 | } | |
399 | } | |
400 | ||
401 | /* | |
402 | =item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) | |
403 | ||
404 | =cut | |
405 | */ | |
406 | ||
407 | int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) { | |
408 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { | |
409 | i_palidx *data; | |
410 | int i, w; | |
411 | if (r > im->xsize) | |
412 | r = im->xsize; | |
413 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
414 | w = r - l; | |
415 | for (i = 0; i < w; ++i) { | |
416 | *vals++ = *data++; | |
417 | } | |
418 | return i; | |
419 | } | |
420 | else { | |
421 | return 0; | |
422 | } | |
423 | } | |
424 | ||
425 | /* | |
426 | =item i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) | |
427 | ||
428 | =cut | |
429 | */ | |
430 | ||
431 | int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) { | |
432 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { | |
433 | i_palidx *data; | |
434 | int i, w; | |
435 | if (r > im->xsize) | |
436 | r = im->xsize; | |
437 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
438 | w = r - l; | |
439 | for (i = 0; i < w; ++i) { | |
440 | *data++ = *vals++; | |
441 | } | |
442 | return i; | |
443 | } | |
444 | else { | |
445 | return 0; | |
446 | } | |
447 | } | |
448 | ||
449 | /* | |
450 | =item i_addcolors_p(i_img *im, i_color *color, int count) | |
451 | ||
452 | =cut | |
453 | */ | |
454 | int i_addcolors_p(i_img *im, i_color *color, int count) { | |
455 | if (PALEXT(im)->count + count <= PALEXT(im)->alloc) { | |
456 | int result = PALEXT(im)->count; | |
457 | int index = result; | |
458 | ||
459 | PALEXT(im)->count += count; | |
460 | while (count) { | |
461 | PALEXT(im)->pal[index++] = *color++; | |
462 | --count; | |
463 | } | |
464 | ||
465 | return result; | |
466 | } | |
467 | else | |
468 | return -1; | |
469 | } | |
470 | ||
471 | /* | |
472 | =item i_getcolors_p(i_img *im, int i, i_color *color, int count) | |
473 | ||
474 | =cut | |
475 | */ | |
476 | int i_getcolors_p(i_img *im, int i, i_color *color, int count) { | |
477 | if (i >= 0 && i+count <= PALEXT(im)->count) { | |
478 | while (count) { | |
479 | *color++ = PALEXT(im)->pal[i++]; | |
480 | --count; | |
481 | } | |
482 | return 1; | |
483 | } | |
484 | else | |
485 | return 0; | |
486 | } | |
487 | ||
488 | static int color_eq(i_img *im, i_color *c1, i_color *c2) { | |
489 | int ch; | |
490 | for (ch = 0; ch < im->channels; ++ch) { | |
491 | if (c1->channel[ch] != c2->channel[ch]) | |
492 | return 0; | |
493 | } | |
494 | return 1; | |
495 | } | |
496 | ||
497 | /* | |
498 | =item i_colorcount_p(i_img *im) | |
499 | ||
500 | =cut | |
501 | */ | |
502 | int i_colorcount_p(i_img *im) { | |
503 | return PALEXT(im)->count; | |
504 | } | |
505 | ||
506 | /* | |
507 | =item i_maxcolors_p(i_img *im) | |
508 | ||
509 | =cut | |
510 | */ | |
511 | int i_maxcolors_p(i_img *im) { | |
512 | return PALEXT(im)->alloc; | |
513 | } | |
514 | ||
515 | /* | |
516 | =item i_setcolors_p(i_img *im, int index, i_color *colors, int count) | |
517 | ||
518 | =cut | |
519 | */ | |
520 | int i_setcolors_p(i_img *im, int index, i_color *colors, int count) { | |
521 | if (index >= 0 && count >= 1 && index + count < PALEXT(im)->count) { | |
522 | while (count) { | |
523 | PALEXT(im)->pal[index++] = *colors++; | |
524 | --count; | |
525 | } | |
526 | return 1; | |
527 | } | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
532 | /* | |
533 | =item i_findcolor_p(i_img *im) | |
534 | ||
535 | =cut | |
536 | */ | |
537 | int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) { | |
538 | if (PALEXT(im)->count) { | |
539 | int i; | |
540 | /* often the same color comes up several times in a row */ | |
541 | if (PALEXT(im)->last_found >= 0) { | |
542 | if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) { | |
543 | *entry = PALEXT(im)->last_found; | |
544 | return 1; | |
545 | } | |
546 | } | |
547 | for (i = 0; i < PALEXT(im)->count; ++i) { | |
548 | if (color_eq(im, color, PALEXT(im)->pal + i)) { | |
549 | PALEXT(im)->last_found = *entry = i; | |
550 | return 1; | |
551 | } | |
552 | } | |
553 | } | |
554 | return 0; | |
555 | } |