palimg.c

filter: Statementstmt 88.2% 262 / 297 Branchbran 78.3% 185 / 236 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.8% 447 / 533 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« img8.c ↑ Summary imgdouble.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  
220 7 ▶ if (i_img_virtual(im))
Branch: 1 branch
BranchTrueFalseTotal
if (i_img_virtual(im)) 0 7 7
221 0 return 0;
222  
223 7 ▶ if (im->type == i_direct_type)
Branch: 1 branch
BranchTrueFalseTotal
if (im->type == i_direct_type) 0 7 7
224 0 return 1; /* trivial success */
225  
226 7 i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
227 7 i_img_rgb_convert(&temp, im);
228  
229 /* nasty hack */
230 7 i_img_exorcise(im);
231 7 *im = temp;
232  
233 7 return 1;
234 }
235  
236 /*
237 =item i_img_to_pal(i_img *im, i_quantize *quant)
238  
239 Converts an RGB image to a paletted image
240  
241 =cut
242 */
243 18 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
244 i_palidx *result;
245 i_img *im;
246 18 dIMCTXim(src);
247  
248 18 i_clear_error();
249
250 18 i_quant_makemap(quant, &src, 1);
251 18 result = i_quant_translate(quant, src);
252  
253 18 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 17 1 18
254  
255 17 im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
256  
257 /* copy things over */
258 17 memcpy(im->idata, result, im->bytes);
259 17 PALEXT(im)->count = quant->mc_count;
260 17 memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
261
262 17 myfree(result);
263  
264 17 return im;
265 }
266 else {
267 1 return NULL;
268 }
269 }
270  
271 /*
272 =item i_img_to_rgb(i_img *src)
273  
274 =cut
275 */
276 i_img *
277 1 i_img_to_rgb(i_img *src) {
278 1 dIMCTXim(src);
279 1 i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
280 1 i_img_rgb_convert(im, src);
281  
282 1 return im;
283 }
284  
285 /*
286 =item i_destroy_p(i_img *im)
287  
288 Destroys data related to a paletted image.
289  
290 =cut
291 */
292 163 static void i_destroy_p(i_img *im) {
293 163 ▶ if (im) {
Branch: 1 branch
BranchTrueFalseTotal
if (im) { 163 0 163
294 163 i_img_pal_ext *palext = im->ext_data;
295 163 ▶ if (palext) {
Branch: 1 branch
BranchTrueFalseTotal
if (palext) { 163 0 163
296 163 ▶ if (palext->pal)
Branch: 1 branch
BranchTrueFalseTotal
if (palext->pal) 163 0 163
297 163 myfree(palext->pal);
298 163 myfree(palext);
299 }
300 }
301 163 }
302  
303 /*
304 =item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)
305  
306 Write to a pixel in the image.
307  
308 Warning: converts the image to a RGB image if the color isn't already
309 present in the image.
310  
311 =cut
312 */
313 static int
314 11876 i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
315 11876 const i_color *work_val = val;
316 i_color workc;
317 i_palidx which;
318 11876 const unsigned all_mask = ( 1 << im->channels ) - 1;
319  
320 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
321 8 return -1;
322  
323 11868 ▶ if ((im->ch_mask & all_mask) != all_mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ((im->ch_mask & all_mask) != all_mask) { 4 11864 11868
324 4 unsigned mask = 1;
325 int ch;
326 4 i_gpix(im, x, y, &workc);
327 16 ▶ for (ch = 0; ch < im->channels; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) { 12 4 16
328 12 ▶ if (im->ch_mask & mask)
Branch: 1 branch
BranchTrueFalseTotal
if (im->ch_mask & mask) 8 4 12
329 8 workc.channel[ch] = val->channel[ch];
330 12 mask <<= 1;
331 }
332 4 work_val = &workc;
333 }
334  
335 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
336 11866 ((i_palidx *)im->idata)[x + y * im->xsize] = which;
337 11866 return 0;
338 }
339 else {
340 2 dIMCTXim(im);
341 2 im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n",
342 val->channel[0], val->channel[1], val->channel[2]));
343 2 ▶ if (i_img_to_rgb_inplace(im)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_img_to_rgb_inplace(im)) { 2 0 2
344 2 return i_ppix(im, x, y, val);
345 }
346 else
347 0 return -1;
348 }
349 }
350  
351 /*
352 =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
353  
354 Retrieve a pixel, converting from a palette index to a color.
355  
356 =cut
357 */
358 682418 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
359 i_palidx which;
360 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
361 8 return -1;
362 }
363 682410 which = ((i_palidx *)im->idata)[x + y * im->xsize];
364 /* unallocated entries are black */
365 682410 *val = PALEXT(im)->pal[which];
366 682410 ▶ if (which >= PALEXT(im)->count)
Branch: 1 branch
BranchTrueFalseTotal
if (which >= PALEXT(im)->count) 0 682410 682410
367 0 return -1;
368  
369 682410 return 0;
370 }
371  
372 /*
373 =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
374  
375 Retrieve a row of pixels.
376  
377 =cut
378 */
379 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) {
380 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
381 700 i_color *pal = PALEXT(im)->pal;
382 i_palidx *data;
383 i_img_dim count, i;
384 700 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 700 700
385 0 r = im->xsize;
386 700 data = ((i_palidx *)im->idata) + l + y * im->xsize;
387 700 count = r - l;
388 121557 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 120857 700 121557
389 120857 i_palidx which = *data++;
390 120857 vals[i] = pal[which];
391 }
392 700 return count;
393 }
394 else {
395 0 return 0;
396 }
397 }
398  
399 /*
400 =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
401  
402 Write a line of color data to the image.
403  
404 If any color value is not in the image when the image is converted to
405 RGB.
406  
407 =cut
408 */
409 static i_img_dim
410 439 i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
411 i_img_dim count, i;
412 i_palidx *data;
413 i_palidx which;
414 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
415 439 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 439 439
416 0 r = im->xsize;
417 439 data = ((i_palidx *)im->idata) + l + y * im->xsize;
418 439 count = r - l;
419 25983 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 25549 434 25983
420 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
421 25544 ((i_palidx *)data)[i] = which;
422 }
423 else {
424 5 ▶ if (i_img_to_rgb_inplace(im)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_img_to_rgb_inplace(im)) { 5 0 5
425 5 return i+i_plin(im, l+i, r, y, vals+i);
426 }
427 }
428 }
429 434 return count;
430 }
431 else {
432 0 return 0;
433 }
434 }
435  
436 /*
437 =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)
438  
439 =cut
440 */
441 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,
442 int const *chans, int chan_count) {
443 int ch;
444 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
445 208 i_color *pal = PALEXT(im)->pal;
446 i_palidx *data;
447 i_img_dim count, i, w;
448 208 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 208 208
449 0 r = im->xsize;
450 208 data = ((i_palidx *)im->idata) + l + y * im->xsize;
451 208 count = 0;
452 208 w = r - l;
453 208 ▶ if (chans) {
Branch: 1 branch
BranchTrueFalseTotal
if (chans) { 6 202 208
454 24 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 18 6 24
455 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
456 0 dIMCTXim(im);
457 0 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
458 }
459 }
460  
461 426 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 420 6 426
462 420 i_palidx which = *data++;
463 1680 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 1260 420 1680
464 1260 *samps++ = pal[which].channel[chans[ch]];
465 1260 ++count;
466 }
467 }
468 }
469 else {
470 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
471 0 dIMCTXim(im);
472 0 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
473 chan_count);
474 0 return 0;
475 }
476 17229 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 17027 202 17229
477 17027 i_palidx which = *data++;
478 68108 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 51081 17027 68108
479 51081 *samps++ = pal[which].channel[ch];
480 51081 ++count;
481 }
482 }
483 }
484 208 return count;
485 }
486 else {
487 0 return 0;
488 }
489 }
490  
491 /*
492 =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
493  
494 =cut
495 */
496  
497 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) {
498 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
499 i_palidx *data;
500 i_img_dim i, w;
501 14708 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 14708 14708
502 0 r = im->xsize;
503 14708 data = ((i_palidx *)im->idata) + l + y * im->xsize;
504 14708 w = r - l;
505 626539 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 611831 14708 626539
506 611831 *vals++ = *data++;
507 }
508 14708 return i;
509 }
510 else {
511 0 return 0;
512 }
513 }
514  
515 /*
516 =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
517  
518 =cut
519 */
520  
521 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) {
522 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
523 i_palidx *data;
524 i_img_dim i, w;
525 92290 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 0 92290 92290
526 0 r = im->xsize;
527 92290 data = ((i_palidx *)im->idata) + l + y * im->xsize;
528 92290 w = r - l;
529 959759 ▶ for (i = 0; i < w; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w; ++i) { 867469 92290 959759
530 867469 *data++ = *vals++;
531 }
532 92290 return i;
533 }
534 else {
535 0 return 0;
536 }
537 }
538  
539 /*
540 =item i_addcolors_p(i_img *im, const i_color *color, int count)
541  
542 =cut
543 */
544 8800 static int i_addcolors_p(i_img *im, const i_color *color, int count) {
545 8800 ▶ if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
Branch: 1 branch
BranchTrueFalseTotal
if (PALEXT(im)->count + count <= PALEXT(im)->alloc) { 8800 0 8800
546 8800 int result = PALEXT(im)->count;
547 8800 int index = result;
548  
549 8800 PALEXT(im)->count += count;
550 17691 ▶ while (count) {
Branch: 1 branch
BranchTrueFalseTotal
while (count) { 8891 8800 17691
551 8891 PALEXT(im)->pal[index++] = *color++;
552 8891 --count;
553 }
554  
555 8800 return result;
556 }
557 else
558 0 return -1;
559 }
560  
561 /*
562 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
563  
564 =cut
565 */
566 2770 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
567 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
568 5960 ▶ while (count) {
Branch: 1 branch
BranchTrueFalseTotal
while (count) { 3190 2770 5960
569 3190 *color++ = PALEXT(im)->pal[i++];
570 3190 --count;
571 }
572 2770 return 1;
573 }
574 else
575 0 return 0;
576 }
577  
578 74848 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
579 int ch;
580 188050 ▶ for (ch = 0; ch < im->channels; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) { 150609 37441 188050
581 150609 ▶ if (c1->channel[ch] != c2->channel[ch])
Branch: 1 branch
BranchTrueFalseTotal
if (c1->channel[ch] != c2->channel[ch]) 37407 113202 150609
582 37407 return 0;
583 }
584 37441 return 1;
585 }
586  
587 /*
588 =item i_colorcount_p(i_img *im)
589  
590 =cut
591 */
592 1576 static int i_colorcount_p(i_img *im) {
593 1576 return PALEXT(im)->count;
594 }
595  
596 /*
597 =item i_maxcolors_p(i_img *im)
598  
599 =cut
600 */
601 23 static int i_maxcolors_p(i_img *im) {
602 23 return PALEXT(im)->alloc;
603 }
604  
605 /*
606 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
607  
608 =cut
609 */
610 12 static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
611 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
612 13 ▶ while (count) {
Branch: 1 branch
BranchTrueFalseTotal
while (count) { 8 5 13
613 8 PALEXT(im)->pal[index++] = *colors++;
614 8 --count;
615 }
616 5 return 1;
617 }
618  
619 7 return 0;
620 }
621  
622 /*
623 =item i_findcolor_p(i_img *im)
624  
625 =cut
626 */
627 37451 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
628 37451 ▶ if (PALEXT(im)->count) {
Branch: 1 branch
BranchTrueFalseTotal
if (PALEXT(im)->count) { 37451 0 37451
629 int i;
630 /* often the same color comes up several times in a row */
631 37451 ▶ if (PALEXT(im)->last_found >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PALEXT(im)->last_found >= 0) { 37425 26 37451
632 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
633 12748 *entry = PALEXT(im)->last_found;
634 12748 return 1;
635 }
636 }
637 37433 ▶ for (i = 0; i < PALEXT(im)->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PALEXT(im)->count; ++i) { 37423 10 37433
638 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
639 24693 PALEXT(im)->last_found = *entry = i;
640 24693 return 1;
641 }
642 }
643 }
644 10 return 0;
645 }
646  
647 /*
648 =item i_psamp_p(im, l, r, y, samps, chans, chan_count)
649  
650 Implement psamp() for paletted images.
651  
652 Since writing samples doesn't really work as a concept for paletted
653 images, this is slow.
654  
655 Also, writing samples may convert the image to a direct image in the
656 process, so use i_ppix/i_gpix instead of directly calling the paletted
657 handlers.
658  
659 =cut
660 */
661  
662 static i_img_dim
663 11 i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
664 const i_sample_t *samps, const int *chans, int chan_count) {
665 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
666 8 i_img_dim count = 0;
667 int ch;
668  
669 8 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 1 7 8
670 1 r = im->xsize;
671
672 8 ▶ if (chans) {
Branch: 1 branch
BranchTrueFalseTotal
if (chans) { 6 2 8
673 /* make sure we have good channel numbers */
674 21 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 17 4 21
675 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
676 2 dIMCTXim(im);
677 2 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
678 2 return -1;
679 }
680 }
681 19 ▶ while (l < r) {
Branch: 1 branch
BranchTrueFalseTotal
while (l < r) { 15 4 19
682 i_color c;
683
684 15 i_gpix(im, l, y, &c);
685 58 ▶ for (ch = 0; ch < chan_count; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) 43 15 58
686 43 c.channel[chans[ch]] = *samps++;
687 15 i_ppix(im, l, y, &c);
688 15 count += chan_count;
689 15 ++l;
690 }
691 }
692 else {
693 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
694 0 dIMCTXim(im);
695 0 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
696 chan_count);
697 0 return -1;
698 }
699  
700 4 ▶ while (l < r) {
Branch: 1 branch
BranchTrueFalseTotal
while (l < r) { 2 2 4
701 i_color c;
702
703 2 i_gpix(im, l, y, &c);
704 8 ▶ for (ch = 0; ch < chan_count; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) 6 2 8
705 6 c.channel[ch] = *samps++;
706 2 i_ppix(im, l, y, &c);
707 2 count += chan_count;
708 2 ++l;
709 }
710 }
711  
712 6 return count;
713 }
714 else {
715 3 dIMCTXim(im);
716 3 i_push_error(0, "Image position outside of image");
717 3 return -1;
718 }
719 }
720  
721 /*
722 =item i_psampf_p(im, l, r, y, samps, chans, chan_count)
723  
724 Implement psampf() for paletted images.
725  
726 Since writing samples doesn't really work as a concept for paletted
727 images, this is slow.
728  
729 Also, writing samples may convert the image to a direct image in the
730 process, so use i_ppixf/i_gpixf instead of directly calling the paletted
731 handlers.
732  
733 =cut
734 */
735  
736 static i_img_dim
737 12 i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
738 const i_fsample_t *samps, const int *chans, int chan_count) {
739 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
740 8 i_img_dim count = 0;
741 int ch;
742  
743 8 ▶ if (r > im->xsize)
Branch: 1 branch
BranchTrueFalseTotal
if (r > im->xsize) 1 7 8
744 1 r = im->xsize;
745
746 8 ▶ if (chans) {
Branch: 1 branch
BranchTrueFalseTotal
if (chans) { 6 2 8
747 /* make sure we have good channel numbers */
748 21 ▶ for (ch = 0; ch < chan_count; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) { 17 4 21
749 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
750 2 dIMCTXim(im);
751 2 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
752 2 return -1;
753 }
754 }
755 19 ▶ while (l < r) {
Branch: 1 branch
BranchTrueFalseTotal
while (l < r) { 15 4 19
756 i_fcolor c;
757
758 15 i_gpixf(im, l, y, &c);
759 58 ▶ for (ch = 0; ch < chan_count; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) 43 15 58
760 43 c.channel[chans[ch]] = *samps++;
761 15 i_ppixf(im, l, y, &c);
762 15 count += chan_count;
763 15 ++l;
764 }
765 }
766 else {
767 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
768 0 dIMCTXim(im);
769 0 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
770 chan_count);
771 0 return -1;
772 }
773  
774 4 ▶ while (l < r) {
Branch: 1 branch
BranchTrueFalseTotal
while (l < r) { 2 2 4
775 i_fcolor c;
776
777 2 i_gpixf(im, l, y, &c);
778 8 ▶ for (ch = 0; ch < chan_count; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < chan_count; ++ch) 6 2 8
779 6 c.channel[ch] = *samps++;
780 2 i_ppixf(im, l, y, &c);
781 2 count += chan_count;
782 2 ++l;
783 }
784 }
785  
786 6 return count;
787 }
788 else {
789 4 dIMCTXim(im);
790 4 i_push_error(0, "Image position outside of image");
791 4 return -1;
792 }
793 }
794  
795 /*
796 =back
797  
798 =head1 AUTHOR
799  
800 Tony Cook <tony@develop-help.com>
801  
802 =head1 SEE ALSO
803  
804 Imager(3)
805  
806 =cut
807 */
« img8.c ↑ Summary imgdouble.c »