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)) | |
8d14daab TC |
25 | static int i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val); |
26 | static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val); | |
27 | static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals); | |
28 | static i_img_dim i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals); | |
29 | static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int const *chans, int chan_count); | |
30 | static i_img_dim i_gpal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals); | |
31 | static i_img_dim i_ppal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals); | |
97ac0a96 | 32 | static int i_addcolors_p(i_img *im, const i_color *color, int count); |
faa9b3e7 TC |
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); | |
97ac0a96 TC |
36 | static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry); |
37 | static int i_setcolors_p(i_img *im, int index, const i_color *color, int count); | |
faa9b3e7 TC |
38 | |
39 | static void i_destroy_p(i_img *im); | |
836d9f54 TC |
40 | static i_img_dim |
41 | i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_sample_t *samps, const int *chans, int chan_count); | |
42 | static i_img_dim | |
43 | i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fsample_t *samps, const int *chans, int chan_count); | |
faa9b3e7 TC |
44 | |
45 | static i_img IIM_base_8bit_pal = | |
46 | { | |
47 | 0, /* channels set */ | |
48 | 0, 0, 0, /* xsize, ysize, bytes */ | |
9a88a5e6 | 49 | ~0U, /* ch_mask */ |
faa9b3e7 TC |
50 | i_8_bits, /* bits */ |
51 | i_palette_type, /* type */ | |
52 | 0, /* virtual */ | |
53 | NULL, /* idata */ | |
54 | { 0, 0, NULL }, /* tags */ | |
55 | NULL, /* ext_data */ | |
56 | ||
57 | i_ppix_p, /* i_f_ppix */ | |
58 | i_ppixf_fp, /* i_f_ppixf */ | |
59 | i_plin_p, /* i_f_plin */ | |
60 | i_plinf_fp, /* i_f_plinf */ | |
61 | i_gpix_p, /* i_f_gpix */ | |
62 | i_gpixf_fp, /* i_f_gpixf */ | |
63 | i_glin_p, /* i_f_glin */ | |
64 | i_glinf_fp, /* i_f_glinf */ | |
65 | i_gsamp_p, /* i_f_gsamp */ | |
66 | i_gsampf_fp, /* i_f_gsampf */ | |
67 | ||
68 | i_gpal_p, /* i_f_gpal */ | |
69 | i_ppal_p, /* i_f_ppal */ | |
70 | i_addcolors_p, /* i_f_addcolors */ | |
71 | i_getcolors_p, /* i_f_getcolors */ | |
72 | i_colorcount_p, /* i_f_colorcount */ | |
73 | i_maxcolors_p, /* i_f_maxcolors */ | |
74 | i_findcolor_p, /* i_f_findcolor */ | |
75 | i_setcolors_p, /* i_f_setcolors */ | |
76 | ||
77 | i_destroy_p, /* i_f_destroy */ | |
bd8052a6 TC |
78 | |
79 | i_gsamp_bits_fb, | |
80 | NULL, /* i_f_psamp_bits */ | |
836d9f54 TC |
81 | |
82 | i_psamp_p, | |
83 | i_psampf_p | |
faa9b3e7 TC |
84 | }; |
85 | ||
86 | /* | |
d03fd5a4 TC |
87 | =item i_img_pal_new(C<x>, C<y>, C<channels>, C<maxpal>) |
88 | ||
bd8052a6 TC |
89 | =category Image creation/destruction |
90 | =synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size) | |
91 | ||
92 | Creates a new paletted image of the supplied dimensions. | |
faa9b3e7 | 93 | |
5715f7c3 | 94 | C<maxpal> is the maximum palette size and should normally be 256. |
faa9b3e7 | 95 | |
bd8052a6 | 96 | Returns a new image or NULL on failure. |
faa9b3e7 TC |
97 | |
98 | =cut | |
99 | */ | |
bd8052a6 | 100 | i_img * |
d03fd5a4 | 101 | i_img_pal_new(i_img_dim x, i_img_dim y, int channels, int maxpal) { |
bd8052a6 | 102 | i_img *im; |
faa9b3e7 | 103 | i_img_pal_ext *palext; |
8d14daab | 104 | size_t bytes, line_bytes; |
faa9b3e7 TC |
105 | |
106 | i_clear_error(); | |
d443d6be | 107 | if (maxpal < 1 || maxpal > 256) { |
faa9b3e7 TC |
108 | i_push_error(0, "Maximum of 256 palette entries"); |
109 | return NULL; | |
110 | } | |
111 | if (x < 1 || y < 1) { | |
112 | i_push_error(0, "Image sizes must be positive"); | |
113 | return NULL; | |
114 | } | |
115 | if (channels < 1 || channels > MAXCHANNELS) { | |
d03fd5a4 | 116 | i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS); |
faa9b3e7 TC |
117 | return NULL; |
118 | } | |
653ea321 TC |
119 | bytes = sizeof(i_palidx) * x * y; |
120 | if (bytes / y / sizeof(i_palidx) != x) { | |
f0960b14 TC |
121 | i_push_error(0, "integer overflow calculating image allocation"); |
122 | return NULL; | |
123 | } | |
124 | ||
125 | /* basic assumption: we can always allocate a buffer representing a | |
126 | line from the image, otherwise we're going to have trouble | |
127 | working with the image */ | |
128 | line_bytes = sizeof(i_color) * x; | |
129 | if (line_bytes / x != sizeof(i_color)) { | |
130 | i_push_error(0, "integer overflow calculating scanline allocation"); | |
653ea321 TC |
131 | return NULL; |
132 | } | |
faa9b3e7 | 133 | |
bd8052a6 | 134 | im = i_img_alloc(); |
faa9b3e7 TC |
135 | memcpy(im, &IIM_base_8bit_pal, sizeof(i_img)); |
136 | palext = mymalloc(sizeof(i_img_pal_ext)); | |
137 | palext->pal = mymalloc(sizeof(i_color) * maxpal); | |
138 | palext->count = 0; | |
139 | palext->alloc = maxpal; | |
140 | palext->last_found = -1; | |
141 | im->ext_data = palext; | |
142 | i_tags_new(&im->tags); | |
653ea321 | 143 | im->bytes = bytes; |
faa9b3e7 TC |
144 | im->idata = mymalloc(im->bytes); |
145 | im->channels = channels; | |
705fd961 | 146 | memset(im->idata, 0, im->bytes); |
faa9b3e7 TC |
147 | im->xsize = x; |
148 | im->ysize = y; | |
4dc7c46b | 149 | |
bd8052a6 TC |
150 | i_img_init(im); |
151 | ||
4dc7c46b | 152 | return im; |
faa9b3e7 TC |
153 | } |
154 | ||
155 | /* | |
156 | =item i_img_rgb_convert(i_img *targ, i_img *src) | |
157 | ||
158 | Converts paletted data in src to RGB data in targ | |
159 | ||
160 | Internal function. | |
161 | ||
162 | src must be a paletted image and targ must be an RGB image with the | |
163 | same width, height and channels. | |
164 | ||
165 | =cut | |
166 | */ | |
167 | static void i_img_rgb_convert(i_img *targ, i_img *src) { | |
168 | i_color *row = mymalloc(sizeof(i_color) * targ->xsize); | |
8d14daab | 169 | i_img_dim y; |
faa9b3e7 TC |
170 | for (y = 0; y < targ->ysize; ++y) { |
171 | i_glin(src, 0, src->xsize, y, row); | |
172 | i_plin(targ, 0, src->xsize, y, row); | |
173 | } | |
174 | myfree(row); | |
175 | } | |
176 | ||
177 | /* | |
178 | =item i_img_to_rgb_inplace(im) | |
179 | ||
180 | Converts im from a paletted image to an RGB image. | |
181 | ||
182 | The conversion is done in place. | |
183 | ||
184 | The conversion cannot be done for virtual images. | |
185 | ||
186 | =cut | |
187 | */ | |
d03fd5a4 | 188 | int i_img_to_rgb_inplace(i_img *im) { |
faa9b3e7 | 189 | i_img temp; |
faa9b3e7 TC |
190 | |
191 | if (im->virtual) | |
192 | return 0; | |
193 | ||
194 | if (im->type == i_direct_type) | |
195 | return 1; /* trivial success */ | |
196 | ||
197 | i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels); | |
198 | i_img_rgb_convert(&temp, im); | |
199 | ||
200 | /* nasty hack */ | |
201 | (im->i_f_destroy)(im); | |
202 | myfree(im->idata); | |
203 | *im = temp; | |
204 | ||
205 | return 1; | |
206 | } | |
207 | ||
208 | /* | |
209 | =item i_img_to_pal(i_img *im, i_quantize *quant) | |
210 | ||
211 | Converts an RGB image to a paletted image | |
212 | ||
213 | =cut | |
214 | */ | |
215 | i_img *i_img_to_pal(i_img *src, i_quantize *quant) { | |
216 | i_palidx *result; | |
217 | i_img *im; | |
faa9b3e7 | 218 | |
1501d9b3 TC |
219 | i_clear_error(); |
220 | ||
92bda632 TC |
221 | i_quant_makemap(quant, &src, 1); |
222 | result = i_quant_translate(quant, src); | |
faa9b3e7 | 223 | |
1501d9b3 | 224 | if (result) { |
faa9b3e7 | 225 | |
1501d9b3 | 226 | im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size); |
faa9b3e7 | 227 | |
1501d9b3 TC |
228 | /* copy things over */ |
229 | memcpy(im->idata, result, im->bytes); | |
230 | PALEXT(im)->count = quant->mc_count; | |
231 | memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count); | |
232 | ||
233 | myfree(result); | |
234 | ||
235 | return im; | |
236 | } | |
237 | else { | |
238 | return NULL; | |
239 | } | |
faa9b3e7 TC |
240 | } |
241 | ||
242 | /* | |
243 | =item i_img_to_rgb(i_img *src) | |
244 | ||
245 | =cut | |
246 | */ | |
d03fd5a4 | 247 | i_img *i_img_to_rgb(i_img *src) { |
faa9b3e7 TC |
248 | i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels); |
249 | i_img_rgb_convert(im, src); | |
250 | ||
251 | return im; | |
252 | } | |
253 | ||
254 | /* | |
255 | =item i_destroy_p(i_img *im) | |
256 | ||
257 | Destroys data related to a paletted image. | |
258 | ||
259 | =cut | |
260 | */ | |
261 | static void i_destroy_p(i_img *im) { | |
262 | if (im) { | |
263 | i_img_pal_ext *palext = im->ext_data; | |
264 | if (palext) { | |
265 | if (palext->pal) | |
266 | myfree(palext->pal); | |
267 | myfree(palext); | |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | /* | |
8d14daab | 273 | =item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) |
faa9b3e7 TC |
274 | |
275 | Write to a pixel in the image. | |
276 | ||
277 | Warning: converts the image to a RGB image if the color isn't already | |
278 | present in the image. | |
279 | ||
280 | =cut | |
281 | */ | |
97ac0a96 | 282 | static int |
8d14daab | 283 | i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) { |
836d9f54 TC |
284 | const i_color *work_val = val; |
285 | i_color workc; | |
faa9b3e7 | 286 | i_palidx which; |
836d9f54 TC |
287 | const unsigned all_mask = ( 1 << im->channels ) - 1; |
288 | ||
faa9b3e7 TC |
289 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) |
290 | return -1; | |
836d9f54 TC |
291 | |
292 | if ((im->ch_mask & all_mask) != all_mask) { | |
293 | unsigned mask = 1; | |
294 | int ch; | |
295 | i_gpix(im, x, y, &workc); | |
296 | for (ch = 0; ch < im->channels; ++ch) { | |
297 | if (im->ch_mask & mask) | |
298 | workc.channel[ch] = val->channel[ch]; | |
299 | mask <<= 1; | |
300 | } | |
301 | work_val = &workc; | |
302 | } | |
303 | ||
304 | if (i_findcolor(im, work_val, &which)) { | |
faa9b3e7 TC |
305 | ((i_palidx *)im->idata)[x + y * im->xsize] = which; |
306 | return 0; | |
307 | } | |
308 | else { | |
d03fd5a4 | 309 | mm_log((1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n", |
836d9f54 | 310 | val->channel[0], val->channel[1], val->channel[2])); |
faa9b3e7 TC |
311 | if (i_img_to_rgb_inplace(im)) { |
312 | return i_ppix(im, x, y, val); | |
313 | } | |
314 | else | |
315 | return -1; | |
316 | } | |
317 | } | |
318 | ||
319 | /* | |
8d14daab | 320 | =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) |
faa9b3e7 TC |
321 | |
322 | Retrieve a pixel, converting from a palette index to a color. | |
323 | ||
324 | =cut | |
325 | */ | |
8d14daab | 326 | static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) { |
faa9b3e7 TC |
327 | i_palidx which; |
328 | if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) { | |
329 | return -1; | |
330 | } | |
331 | which = ((i_palidx *)im->idata)[x + y * im->xsize]; | |
332 | if (which > PALEXT(im)->count) | |
333 | return -1; | |
334 | *val = PALEXT(im)->pal[which]; | |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
339 | /* | |
8d14daab | 340 | =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) |
faa9b3e7 TC |
341 | |
342 | Retrieve a row of pixels. | |
343 | ||
344 | =cut | |
345 | */ | |
8d14daab | 346 | static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) { |
faa9b3e7 TC |
347 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
348 | int palsize = PALEXT(im)->count; | |
349 | i_color *pal = PALEXT(im)->pal; | |
350 | i_palidx *data; | |
8d14daab | 351 | i_img_dim count, i; |
faa9b3e7 TC |
352 | if (r > im->xsize) |
353 | r = im->xsize; | |
354 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
355 | count = r - l; | |
356 | for (i = 0; i < count; ++i) { | |
357 | i_palidx which = *data++; | |
358 | if (which < palsize) | |
359 | vals[i] = pal[which]; | |
360 | } | |
361 | return count; | |
362 | } | |
363 | else { | |
364 | return 0; | |
365 | } | |
366 | } | |
367 | ||
368 | /* | |
8d14daab | 369 | =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) |
faa9b3e7 TC |
370 | |
371 | Write a line of color data to the image. | |
372 | ||
373 | If any color value is not in the image when the image is converted to | |
374 | RGB. | |
375 | ||
376 | =cut | |
377 | */ | |
8d14daab TC |
378 | static i_img_dim |
379 | i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) { | |
380 | i_img_dim count, i; | |
faa9b3e7 TC |
381 | i_palidx *data; |
382 | i_palidx which; | |
383 | if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { | |
384 | if (r > im->xsize) | |
385 | r = im->xsize; | |
386 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
387 | count = r - l; | |
388 | for (i = 0; i < count; ++i) { | |
389 | if (i_findcolor(im, vals+i, &which)) { | |
390 | ((i_palidx *)data)[i] = which; | |
391 | } | |
392 | else { | |
393 | if (i_img_to_rgb_inplace(im)) { | |
394 | return i+i_plin(im, l+i, r, y, vals+i); | |
395 | } | |
396 | } | |
397 | } | |
398 | return count; | |
399 | } | |
400 | else { | |
401 | return 0; | |
402 | } | |
403 | } | |
404 | ||
405 | /* | |
8d14daab | 406 | =item i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int chans, int chan_count) |
faa9b3e7 TC |
407 | |
408 | =cut | |
409 | */ | |
8d14daab | 410 | static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, |
18accb2a | 411 | int const *chans, int chan_count) { |
faa9b3e7 TC |
412 | int ch; |
413 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { | |
414 | int palsize = PALEXT(im)->count; | |
415 | i_color *pal = PALEXT(im)->pal; | |
416 | i_palidx *data; | |
8d14daab | 417 | i_img_dim count, i, w; |
faa9b3e7 TC |
418 | if (r > im->xsize) |
419 | r = im->xsize; | |
420 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
421 | count = 0; | |
422 | w = r - l; | |
423 | if (chans) { | |
424 | for (ch = 0; ch < chan_count; ++ch) { | |
425 | if (chans[ch] < 0 || chans[ch] >= im->channels) { | |
d03fd5a4 | 426 | i_push_errorf(0, "No channel %d in this image", chans[ch]); |
faa9b3e7 TC |
427 | } |
428 | } | |
429 | ||
430 | for (i = 0; i < w; ++i) { | |
431 | i_palidx which = *data++; | |
432 | if (which < palsize) { | |
433 | for (ch = 0; ch < chan_count; ++ch) { | |
434 | *samps++ = pal[which].channel[chans[ch]]; | |
435 | ++count; | |
436 | } | |
437 | } | |
438 | } | |
439 | } | |
440 | else { | |
c7481ae1 | 441 | if (chan_count <= 0 || chan_count > im->channels) { |
d03fd5a4 | 442 | i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", |
c7481ae1 TC |
443 | chan_count); |
444 | return 0; | |
445 | } | |
faa9b3e7 TC |
446 | for (i = 0; i < w; ++i) { |
447 | i_palidx which = *data++; | |
448 | if (which < palsize) { | |
449 | for (ch = 0; ch < chan_count; ++ch) { | |
450 | *samps++ = pal[which].channel[ch]; | |
451 | ++count; | |
452 | } | |
453 | } | |
454 | } | |
455 | } | |
456 | return count; | |
457 | } | |
458 | else { | |
459 | return 0; | |
460 | } | |
461 | } | |
462 | ||
463 | /* | |
8d14daab | 464 | =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) |
faa9b3e7 TC |
465 | |
466 | =cut | |
467 | */ | |
468 | ||
8d14daab | 469 | static i_img_dim i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) { |
faa9b3e7 TC |
470 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
471 | i_palidx *data; | |
8d14daab | 472 | i_img_dim i, w; |
faa9b3e7 TC |
473 | if (r > im->xsize) |
474 | r = im->xsize; | |
475 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
476 | w = r - l; | |
477 | for (i = 0; i < w; ++i) { | |
478 | *vals++ = *data++; | |
479 | } | |
480 | return i; | |
481 | } | |
482 | else { | |
483 | return 0; | |
484 | } | |
485 | } | |
486 | ||
487 | /* | |
8d14daab | 488 | =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) |
faa9b3e7 TC |
489 | |
490 | =cut | |
491 | */ | |
492 | ||
8d14daab | 493 | static i_img_dim i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) { |
faa9b3e7 TC |
494 | if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) { |
495 | i_palidx *data; | |
8d14daab | 496 | i_img_dim i, w; |
faa9b3e7 TC |
497 | if (r > im->xsize) |
498 | r = im->xsize; | |
499 | data = ((i_palidx *)im->idata) + l + y * im->xsize; | |
500 | w = r - l; | |
501 | for (i = 0; i < w; ++i) { | |
502 | *data++ = *vals++; | |
503 | } | |
504 | return i; | |
505 | } | |
506 | else { | |
507 | return 0; | |
508 | } | |
509 | } | |
510 | ||
511 | /* | |
97ac0a96 | 512 | =item i_addcolors_p(i_img *im, const i_color *color, int count) |
faa9b3e7 TC |
513 | |
514 | =cut | |
515 | */ | |
97ac0a96 | 516 | static int i_addcolors_p(i_img *im, const i_color *color, int count) { |
faa9b3e7 TC |
517 | if (PALEXT(im)->count + count <= PALEXT(im)->alloc) { |
518 | int result = PALEXT(im)->count; | |
519 | int index = result; | |
520 | ||
521 | PALEXT(im)->count += count; | |
522 | while (count) { | |
523 | PALEXT(im)->pal[index++] = *color++; | |
524 | --count; | |
525 | } | |
526 | ||
527 | return result; | |
528 | } | |
529 | else | |
530 | return -1; | |
531 | } | |
532 | ||
533 | /* | |
534 | =item i_getcolors_p(i_img *im, int i, i_color *color, int count) | |
535 | ||
536 | =cut | |
537 | */ | |
63b018fd | 538 | static int i_getcolors_p(i_img *im, int i, i_color *color, int count) { |
faa9b3e7 TC |
539 | if (i >= 0 && i+count <= PALEXT(im)->count) { |
540 | while (count) { | |
541 | *color++ = PALEXT(im)->pal[i++]; | |
542 | --count; | |
543 | } | |
544 | return 1; | |
545 | } | |
546 | else | |
547 | return 0; | |
548 | } | |
549 | ||
97ac0a96 | 550 | static int color_eq(i_img *im, const i_color *c1, const i_color *c2) { |
faa9b3e7 TC |
551 | int ch; |
552 | for (ch = 0; ch < im->channels; ++ch) { | |
553 | if (c1->channel[ch] != c2->channel[ch]) | |
554 | return 0; | |
555 | } | |
556 | return 1; | |
557 | } | |
558 | ||
559 | /* | |
560 | =item i_colorcount_p(i_img *im) | |
561 | ||
562 | =cut | |
563 | */ | |
63b018fd | 564 | static int i_colorcount_p(i_img *im) { |
faa9b3e7 TC |
565 | return PALEXT(im)->count; |
566 | } | |
567 | ||
568 | /* | |
569 | =item i_maxcolors_p(i_img *im) | |
570 | ||
571 | =cut | |
572 | */ | |
63b018fd | 573 | static int i_maxcolors_p(i_img *im) { |
faa9b3e7 TC |
574 | return PALEXT(im)->alloc; |
575 | } | |
576 | ||
577 | /* | |
97ac0a96 | 578 | =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count) |
faa9b3e7 TC |
579 | |
580 | =cut | |
581 | */ | |
97ac0a96 | 582 | static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) { |
8efd1577 | 583 | if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) { |
faa9b3e7 TC |
584 | while (count) { |
585 | PALEXT(im)->pal[index++] = *colors++; | |
586 | --count; | |
587 | } | |
588 | return 1; | |
589 | } | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
594 | /* | |
595 | =item i_findcolor_p(i_img *im) | |
596 | ||
597 | =cut | |
598 | */ | |
97ac0a96 | 599 | static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) { |
faa9b3e7 TC |
600 | if (PALEXT(im)->count) { |
601 | int i; | |
602 | /* often the same color comes up several times in a row */ | |
603 | if (PALEXT(im)->last_found >= 0) { | |
604 | if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) { | |
605 | *entry = PALEXT(im)->last_found; | |
606 | return 1; | |
607 | } | |
608 | } | |
609 | for (i = 0; i < PALEXT(im)->count; ++i) { | |
610 | if (color_eq(im, color, PALEXT(im)->pal + i)) { | |
611 | PALEXT(im)->last_found = *entry = i; | |
612 | return 1; | |
613 | } | |
614 | } | |
615 | } | |
616 | return 0; | |
617 | } | |
b8c2033e | 618 | |
836d9f54 TC |
619 | /* |
620 | =item i_psamp_p(im, l, r, y, samps, chans, chan_count) | |
621 | ||
622 | Implement psamp() for paletted images. | |
623 | ||
624 | Since writing samples doesn't really work as a concept for paletted | |
625 | images, this is slow. | |
626 | ||
627 | Also, writing samples may convert the image to a direct image in the | |
628 | process, so use i_ppix/i_gpix instead of directly calling the paletted | |
629 | handlers. | |
630 | ||
631 | =cut | |
632 | */ | |
633 | ||
634 | static i_img_dim | |
635 | i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, | |
636 | const i_sample_t *samps, const int *chans, int chan_count) { | |
637 | if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { | |
638 | i_img_dim count = 0; | |
639 | int ch; | |
640 | ||
641 | if (r > im->xsize) | |
642 | r = im->xsize; | |
643 | ||
644 | if (chans) { | |
645 | /* make sure we have good channel numbers */ | |
646 | for (ch = 0; ch < chan_count; ++ch) { | |
647 | if (chans[ch] < 0 || chans[ch] >= im->channels) { | |
d03fd5a4 | 648 | i_push_errorf(0, "No channel %d in this image", chans[ch]); |
836d9f54 TC |
649 | return -1; |
650 | } | |
651 | } | |
652 | while (l < r) { | |
653 | i_color c; | |
654 | ||
655 | i_gpix(im, l, y, &c); | |
656 | for (ch = 0; ch < chan_count; ++ch) | |
657 | c.channel[chans[ch]] = *samps++; | |
658 | i_ppix(im, l, y, &c); | |
659 | count += chan_count; | |
660 | ++l; | |
661 | } | |
662 | } | |
663 | else { | |
664 | if (chan_count <= 0 || chan_count > im->channels) { | |
d03fd5a4 | 665 | i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", |
836d9f54 TC |
666 | chan_count); |
667 | return -1; | |
668 | } | |
669 | ||
670 | while (l < r) { | |
671 | i_color c; | |
672 | ||
673 | i_gpix(im, l, y, &c); | |
674 | for (ch = 0; ch < chan_count; ++ch) | |
675 | c.channel[ch] = *samps++; | |
676 | i_ppix(im, l, y, &c); | |
677 | count += chan_count; | |
678 | ++l; | |
679 | } | |
680 | } | |
681 | ||
682 | return count; | |
683 | } | |
684 | else { | |
685 | i_push_error(0, "Image position outside of image"); | |
686 | return -1; | |
687 | } | |
688 | } | |
689 | ||
690 | /* | |
691 | =item i_psampf_p(im, l, r, y, samps, chans, chan_count) | |
692 | ||
693 | Implement psampf() for paletted images. | |
694 | ||
695 | Since writing samples doesn't really work as a concept for paletted | |
696 | images, this is slow. | |
697 | ||
698 | Also, writing samples may convert the image to a direct image in the | |
699 | process, so use i_ppixf/i_gpixf instead of directly calling the paletted | |
700 | handlers. | |
701 | ||
702 | =cut | |
703 | */ | |
704 | ||
705 | static i_img_dim | |
706 | i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, | |
707 | const i_fsample_t *samps, const int *chans, int chan_count) { | |
708 | if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) { | |
709 | i_img_dim count = 0; | |
710 | int ch; | |
711 | ||
712 | if (r > im->xsize) | |
713 | r = im->xsize; | |
714 | ||
715 | if (chans) { | |
716 | /* make sure we have good channel numbers */ | |
717 | for (ch = 0; ch < chan_count; ++ch) { | |
718 | if (chans[ch] < 0 || chans[ch] >= im->channels) { | |
d03fd5a4 | 719 | i_push_errorf(0, "No channel %d in this image", chans[ch]); |
836d9f54 TC |
720 | return -1; |
721 | } | |
722 | } | |
723 | while (l < r) { | |
724 | i_fcolor c; | |
725 | ||
726 | i_gpixf(im, l, y, &c); | |
727 | for (ch = 0; ch < chan_count; ++ch) | |
728 | c.channel[chans[ch]] = *samps++; | |
729 | i_ppixf(im, l, y, &c); | |
730 | count += chan_count; | |
731 | ++l; | |
732 | } | |
733 | } | |
734 | else { | |
735 | if (chan_count <= 0 || chan_count > im->channels) { | |
d03fd5a4 | 736 | i_push_errorf(0, "chan_count %d out of range, must be >0, <= channels", |
836d9f54 TC |
737 | chan_count); |
738 | return -1; | |
739 | } | |
740 | ||
741 | while (l < r) { | |
742 | i_fcolor c; | |
743 | ||
744 | i_gpixf(im, l, y, &c); | |
745 | for (ch = 0; ch < chan_count; ++ch) | |
746 | c.channel[ch] = *samps++; | |
747 | i_ppixf(im, l, y, &c); | |
748 | count += chan_count; | |
749 | ++l; | |
750 | } | |
751 | } | |
752 | ||
753 | return count; | |
754 | } | |
755 | else { | |
756 | i_push_error(0, "Image position outside of image"); | |
757 | return -1; | |
758 | } | |
759 | } | |
760 | ||
b8c2033e AMH |
761 | /* |
762 | =back | |
763 | ||
764 | =head1 AUTHOR | |
765 | ||
766 | Tony Cook <tony@develop-help.com> | |
767 | ||
768 | =head1 SEE ALSO | |
769 | ||
770 | Imager(3) | |
771 | ||
772 | =cut | |
773 | */ |