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