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