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