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