palimg.c

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