convert.im

filter: Statementstmt 91.7% 111 / 121 Branchbran 81.5% 106 / 130 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.4% 217 / 251 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« limits.c ↑ Summary testout/t1000files_probe.pl »
1 /*
2 =head1 NAME
3  
4 convert.im - image conversions
5  
6 =head1 SYNOPSIS
7  
8 out = i_convert(srcimage, coeff, outchans, inchans)
9  
10 =head1 DESCRIPTION
11  
12 Converts images from one format to another, typically in this case for
13 converting from RGBA to greyscale and back.
14  
15 =over
16  
17 =cut
18 */
19  
20 #define IMAGER_NO_CONTEXT
21 #include "imager.h"
22  
23 struct chan_copy {
24 /* channels to copy */
25 int copy_count;
26 int from[MAXCHANNELS];
27 int to[MAXCHANNELS];
28  
29 /* channels to zero */
30 int zero_count;
31 int zero[MAXCHANNELS];
32  
33 /* channels to set to maxsample */
34 int one_count;
35 int one[MAXCHANNELS];
36 };
37  
38 static int
39 is_channel_copy(i_img *im, const double *coeff,
40 int outchan, int inchan,
41 struct chan_copy *info);
42  
43 static i_img *
44 convert_via_copy(i_img *im, i_img *src, struct chan_copy *info);
45  
46 /*
47 =item i_convert(src, coeff, outchan, inchan)
48  
49 Converts the image src into another image.
50  
51 coeff contains the co-efficients of an outchan x inchan matrix, for
52 each output pixel:
53  
54 coeff[0], coeff[1] ...
55 im[x,y] = [ coeff[inchan], coeff[inchan+1]... ] * [ src[x,y], 1]
56 ... coeff[inchan*outchan-1]
57  
58 If im has the wrong number of channels or is the wrong size then
59 i_convert() will re-create it.
60  
61 Now handles images with more than 8-bits/sample.
62  
63 =cut
64 */
65  
66 i_img *
67 28 i_convert(i_img *src, const double *coeff, int outchan, int inchan) {
68 double work[MAXCHANNELS];
69 i_img_dim x, y;
70 int i, j;
71 int ilimit;
72 28 i_img *im = NULL;
73 28 dIMCTXim(src);
74  
75 28 im_log((aIMCTX,1,"i_convert(im %p, src %p, coeff %p,outchan %d, inchan %d)\n",
76 im, src, coeff, outchan, inchan));
77
78 28 im_clear_error(aIMCTX);
79  
80 28 ilimit = inchan;
81 28 ▶ if (ilimit > src->channels)
Branch: 1 branch
BranchTrueFalseTotal
if (ilimit > src->channels) 8 20 28
82 8 ilimit = src->channels;
83 28 ▶ if (outchan > MAXCHANNELS) {
Branch: 1 branch
BranchTrueFalseTotal
if (outchan > MAXCHANNELS) { 0 28 28
84 0 im_push_error(aIMCTX, 0, "cannot have outchan > MAXCHANNELS");
85 0 return 0;
86 }
87  
88 28 ▶ if (src->type == i_direct_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->type == i_direct_type) { 25 3 28
89 struct chan_copy info;
90 25 im = i_sametype_chans(src, src->xsize, src->ysize, outchan);
91 25 ▶ if (!im)
Branch: 1 branch
BranchTrueFalseTotal
if (!im) 0 25 25
92 18 return 0;
93
94 25 ▶ if (is_channel_copy(src, coeff, outchan, inchan, &info)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_channel_copy(src, coeff, outchan, inchan, &info)) { 18 7 25
95 18 return convert_via_copy(im, src, &info);
96 }
97 else {
98 7 ▶#code src->bits <= i_8_bits
Branch: 1 branch
BranchTrueFalseTotal
#code src->bits <= i_8_bits 4 3 7
99 IM_COLOR *vals;
100
101 /* we can always allocate a single scanline of i_color */
102 7 vals = mymalloc(sizeof(IM_COLOR) * src->xsize); /* checked 04Jul05 tonyc */
103 622 ▶ for (y = 0; y < src->ysize; ++y) {
Branch: 2 branches
BranchTrueFalseTotal
for (y = 0; y < src->ysize; ++y) { 435 4 439
for (y = 0; y < src->ysize; ++y) { 180 3 183
104 615 IM_GLIN(src, 0, src->xsize, y, vals);
105 81145 ▶ for (x = 0; x < src->xsize; ++x) {
Branch: 2 branches
BranchTrueFalseTotal
for (x = 0; x < src->xsize; ++x) { 57530 435 57965
for (x = 0; x < src->xsize; ++x) { 23000 180 23180
106 161060 ▶ for (j = 0; j < outchan; ++j) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < outchan; ++j) { 57530 57530 115060
for (j = 0; j < outchan; ++j) { 23000 23000 46000
107 80530 work[j] = 0;
108 322120 ▶ for (i = 0; i < ilimit; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < ilimit; ++i) { 172590 57530 230120
for (i = 0; i < ilimit; ++i) { 69000 23000 92000
109 241590 work[j] += coeff[i+inchan*j] * vals[x].channel[i];
110 }
111 80530 ▶ if (i < inchan) {
Branch: 2 branches
BranchTrueFalseTotal
if (i < inchan) { 0 57530 57530
if (i < inchan) { 100 22900 23000
112 100 work[j] += coeff[i+inchan*j] * IM_SAMPLE_MAX;
113 }
114 }
115 161060 ▶ for (j = 0; j < outchan; ++j) {
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < outchan; ++j) { 57530 57530 115060
for (j = 0; j < outchan; ++j) { 23000 23000 46000
116 80530 ▶ if (work[j] < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (work[j] < 0) 0 57530 57530
if (work[j] < 0) 0 23000 23000
117 0 vals[x].channel[j] = 0;
118 80530 ▶ else if (work[j] >= IM_SAMPLE_MAX)
Branch: 2 branches
BranchTrueFalseTotal
else if (work[j] >= IM_SAMPLE_MAX) 0 57530 57530
else if (work[j] >= IM_SAMPLE_MAX) 0 23000 23000
119 0 vals[x].channel[j] = IM_SAMPLE_MAX;
120 else
121 80530 vals[x].channel[j] = work[j];
122 }
123 }
124 615 IM_PLIN(im, 0, src->xsize, y, vals);
125 }
126 7 myfree(vals);
127 #/code
128 }
129 }
130 else {
131 int count;
132 int outcount;
133 int index;
134 i_color *colors;
135 i_palidx *vals;
136  
137 3 im = im_img_pal_new(aIMCTX, src->xsize, src->ysize, outchan,
138 3 ▶ i_maxcolors(src));
Branch: 1 branch
BranchTrueFalseTotal
i_maxcolors(src)); 3 0 3
139 3 ▶ if (!im)
Branch: 1 branch
BranchTrueFalseTotal
if (!im) 0 3 3
140 0 return 0;
141  
142 /* just translate the color table */
143 3 ▶ count = i_colorcount(src);
Branch: 1 branch
BranchTrueFalseTotal
count = i_colorcount(src); 3 0 3
144 3 ▶ outcount = i_colorcount(im);
Branch: 1 branch
BranchTrueFalseTotal
outcount = i_colorcount(im); 3 0 3
145 /* color table allocated for image, so it must fit */
146 3 colors = mymalloc(count * sizeof(i_color)); /* check 04Jul05 tonyc */
147 3 ▶ i_getcolors(src, 0, colors, count);
Branch: 1 branch
BranchTrueFalseTotal
i_getcolors(src, 0, colors, count); 3 0 3
148 9 ▶ for (index = 0; index < count; ++index) {
Branch: 1 branch
BranchTrueFalseTotal
for (index = 0; index < count; ++index) { 6 3 9
149 25 ▶ for (j = 0; j < outchan; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < outchan; ++j) { 19 6 25
150 19 work[j] = 0;
151 52 ▶ for (i = 0; i < ilimit; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ilimit; ++i) { 33 19 52
152 33 work[j] += coeff[i+inchan*j] * colors[index].channel[i];
153 }
154 19 ▶ if (i < inchan) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < inchan) { 7 12 19
155 7 work[j] += coeff[i+inchan*j] * 255.9;
156 }
157 }
158 25 ▶ for (j = 0; j < outchan; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < outchan; ++j) { 19 6 25
159 19 ▶ if (work[j] < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (work[j] < 0) 0 19 19
160 0 colors[index].channel[j] = 0;
161 19 ▶ else if (work[j] >= 255)
Branch: 1 branch
BranchTrueFalseTotal
else if (work[j] >= 255) 6 13 19
162 6 colors[index].channel[j] = 255;
163 else
164 13 colors[index].channel[j] = work[j];
165 }
166 }
167 3 ▶ if (count < outcount) {
Branch: 1 branch
BranchTrueFalseTotal
if (count < outcount) { 0 3 3
168 0 ▶ i_setcolors(im, 0, colors, count);
Branch: 1 branch
BranchTrueFalseTotal
i_setcolors(im, 0, colors, count); 0 0 0
169 }
170 else {
171 3 ▶ i_setcolors(im, 0, colors, outcount);
Branch: 1 branch
BranchTrueFalseTotal
i_setcolors(im, 0, colors, outcount); 3 0 3
172 3 ▶ i_addcolors(im, colors, count-outcount);
Branch: 1 branch
BranchTrueFalseTotal
i_addcolors(im, colors, count-outcount); 3 0 3
173 }
174 /* and copy the indicies */
175 /* i_palidx is always unsigned char and will never be bigger than short
176 and since a line of 4-byte i_colors can fit then a line of i_palidx
177 will fit */
178 3 vals = mymalloc(sizeof(i_palidx) * im->xsize); /* checked 4jul05 tonyc */
179 324 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 321 3 324
180 321 ▶ i_gpal(src, 0, im->xsize, y, vals);
Branch: 1 branch
BranchTrueFalseTotal
i_gpal(src, 0, im->xsize, y, vals); 321 0 321
181 321 ▶ i_ppal(im, 0, im->xsize, y, vals);
Branch: 1 branch
BranchTrueFalseTotal
i_ppal(im, 0, im->xsize, y, vals); 321 0 321
182 }
183 3 myfree(vals);
184 3 myfree(colors);
185 }
186  
187 10 return im;
188 }
189  
190 /*
191 =item is_channel_copy(coeff, outchan, inchan, chan_copy_info)
192  
193 Test if the coefficients represent just copying channels around, and
194 initialize lists of the channels to copy, zero or set to max.
195  
196 =cut
197 */
198  
199 static
200 25 int is_channel_copy(i_img *im, const double *coeff, int outchan, int inchan,
201 struct chan_copy *info) {
202 int srcchan[MAXCHANNELS];
203 int onechan[MAXCHANNELS];
204 int i, j;
205 25 int ilimit = im->channels > inchan ? inchan : im->channels;
206  
207 82 ▶ for (j = 0; j < outchan; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < outchan; ++j) { 57 25 82
208 57 srcchan[j] = -1;
209 57 onechan[j] = 0;
210 }
211  
212 75 ▶ for (j = 0; j < outchan; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < outchan; ++j) { 57 18 75
213 167 ▶ for (i = 0; i < ilimit; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ilimit; ++i) { 117 50 167
214 117 ▶ if (coeff[i+inchan*j] == 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (coeff[i+inchan*j] == 1.0) { 43 74 117
215 43 ▶ if (srcchan[j] != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (srcchan[j] != -1) { 0 43 43
216 /* from two or more channels, not a copy */
217 0 return 0;
218 }
219 43 srcchan[j] = i;
220 }
221 74 ▶ else if (coeff[i+inchan*j]) {
Branch: 1 branch
BranchTrueFalseTotal
else if (coeff[i+inchan*j]) { 7 67 74
222 /* some other non-zero value, not a copy */
223 7 return 0;
224 }
225 }
226 50 ▶ if (i < inchan) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < inchan) { 16 34 50
227 16 ▶ if (coeff[i+inchan*j] == 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (coeff[i+inchan*j] == 1.0) { 5 11 16
228 5 ▶ if (srcchan[j] != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (srcchan[j] != -1) { 0 5 5
229 /* can't do both */
230 0 return 0;
231 }
232 5 onechan[j] = 1;
233 }
234 11 ▶ else if (coeff[i+inchan*j]) {
Branch: 1 branch
BranchTrueFalseTotal
else if (coeff[i+inchan*j]) { 0 11 11
235 /* some other non-zero value, not a copy */
236 0 return 0;
237 }
238 }
239 }
240  
241 /* build our working data structures */
242 18 info->copy_count = info->zero_count = info->one_count = 0;
243 68 ▶ for (j = 0; j < outchan; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < outchan; ++j) { 50 18 68
244 50 ▶ if (srcchan[j] != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (srcchan[j] != -1) { 43 7 50
245 43 info->from[info->copy_count] = srcchan[j];
246 43 info->to[info->copy_count] = j;
247 43 ++info->copy_count;
248 }
249 7 ▶ else if (onechan[j]) {
Branch: 1 branch
BranchTrueFalseTotal
else if (onechan[j]) { 5 2 7
250 5 info->one[info->one_count] = j;
251 5 ++info->one_count;
252 }
253 else {
254 2 info->zero[info->zero_count] = j;
255 2 ++info->zero_count;
256 }
257 }
258  
259 #if 0
260 {
261 for (i = 0; i < info->copy_count; ++i) {
262 printf("From %d to %d\n", info->from[i], info->to[i]);
263 }
264 for (i = 0; i < info->one_count; ++i) {
265 printf("One %d\n", info->one[i]);
266 }
267 for (i = 0; i < info->zero_count; ++i) {
268 printf("Zero %d\n", info->zero[i]);
269 }
270 fflush(stdout);
271 }
272 #endif
273  
274 18 return 1;
275 }
276  
277 /*
278 =item convert_via_copy(im, src, chan_copy_info)
279  
280 Perform a convert that only requires channel copies.
281  
282 =cut
283 */
284  
285 static i_img *
286 18 convert_via_copy(i_img *im, i_img *src, struct chan_copy *info) {
287 18 ▶#code src->bits <= i_8_bits
Branch: 1 branch
BranchTrueFalseTotal
#code src->bits <= i_8_bits 13 5 18
288 18 IM_COLOR *in_line = mymalloc(sizeof(IM_COLOR) * src->xsize);
289 18 IM_COLOR *out_line = mymalloc(sizeof(IM_COLOR) * src->xsize);
290 i_img_dim x, y;
291 int i;
292 IM_COLOR *inp, *outp;
293  
294 1549 ▶ for (y = 0; y < src->ysize; ++y) {
Branch: 2 branches
BranchTrueFalseTotal
for (y = 0; y < src->ysize; ++y) { 1131 13 1144
for (y = 0; y < src->ysize; ++y) { 400 5 405
295 1531 IM_GLIN(src, 0, src->xsize, y, in_line);
296  
297 1531 inp = in_line;
298 1531 outp = out_line;
299 250637 ▶ for (x = 0; x < src->xsize; ++x) {
Branch: 2 branches
BranchTrueFalseTotal
for (x = 0; x < src->xsize; ++x) { 185706 1131 186837
for (x = 0; x < src->xsize; ++x) { 63400 400 63800
300 587388 ▶ for (i = 0; i < info->copy_count; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < info->copy_count; ++i) { 313082 185706 498788
for (i = 0; i < info->copy_count; ++i) { 25200 63400 88600
301 338282 outp->channel[info->to[i]] = inp->channel[info->from[i]];
302 }
303 438166 ▶ for (i = 0; i < info->one_count; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < info->one_count; ++i) { 149060 185706 334766
for (i = 0; i < info->one_count; ++i) { 40000 63400 103400
304 189060 outp->channel[info->one[i]] = IM_SAMPLE_MAX;
305 }
306 329106 ▶ for (i = 0; i < info->zero_count; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < info->zero_count; ++i) { 0 185706 185706
for (i = 0; i < info->zero_count; ++i) { 80000 63400 143400
307 80000 outp->channel[info->zero[i]] = 0;
308 }
309 249106 ++inp;
310 249106 ++outp;
311 }
312
313 1531 IM_PLIN(im, 0, src->xsize, y, out_line);
314 }
315
316 18 myfree(in_line);
317 18 myfree(out_line);
318 #/code
319
320 18 return im;
321 }
322  
323 /*
324 =back
325  
326 =head1 SEE ALSO
327  
328 Imager(3)
329  
330 =head1 AUTHOR
331  
332 Tony Cook <tony@develop-help.com>
333  
334 =cut
335 */
« limits.c ↑ Summary testout/t1000files_probe.pl »