filters.im

filter: Statementstmt 83.9% 763 / 909 Branchbran 79.6% 379 / 476 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.4% 1142 / 1385 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« bmp.c ↑ Summary draw.c »
1 #define IMAGER_NO_CONTEXT
2 #include "imager.h"
3 #include "imageri.h"
4 #include <stdlib.h>
5 #include <math.h>
6  
7  
8 /*
9 =head1 NAME
10  
11 filters.im - implements filters that operate on images
12  
13 =head1 SYNOPSIS
14  
15
16 i_contrast(im, 0.8);
17 i_hardinvert(im);
18 i_hardinvertall(im);
19 i_unsharp_mask(im, 2.0, 1.0);
20 ... and more
21  
22 =head1 DESCRIPTION
23  
24 filters.c implements basic filters for Imager. These filters
25 should be accessible from the filter interface as defined in
26 the pod for Imager.
27  
28 =head1 FUNCTION REFERENCE
29  
30 Some of these functions are internal.
31  
32 =over
33  
34 =cut
35 */
36  
37  
38  
39  
40 /*
41 =item saturate(in)
42  
43 Clamps the input value between 0 and 255. (internal)
44  
45 in - input integer
46  
47 =cut
48 */
49  
50 static
51 unsigned char
52 315000 saturate(int in) {
53 315000 ▶ if (in>255) { return 255; }
Branch: 1 branch
BranchTrueFalseTotal
if (in>255) { return 255; } 13591 301409 315000
54 301409 ▶ else if (in>0) return in;
Branch: 1 branch
BranchTrueFalseTotal
else if (in>0) return in; 192224 109185 301409
55 109185 return 0;
56 }
57  
58  
59  
60 /*
61 =item i_contrast(im, intensity)
62  
63 Scales the pixel values by the amount specified.
64  
65 im - image object
66 intensity - scalefactor
67  
68 =cut
69 */
70  
71 void
72 1 i_contrast(i_img *im, float intensity) {
73 i_img_dim x, y;
74 int ch;
75 unsigned int new_color;
76 i_color rcolor;
77 1 dIMCTXim(im);
78
79 1 im_log((aIMCTX, 1,"i_contrast(im %p, intensity %f)\n", im, intensity));
80
81 1 ▶ if(intensity < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if(intensity < 0) return; 0 1 1
82
83 22651 ▶ for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 22500 150 22650
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 150 1 151
84 22500 i_gpix(im, x, y, &rcolor);
85
86 90000 ▶ for(ch = 0; ch < im->channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < im->channels; ch++) { 67500 22500 90000
87 67500 new_color = (unsigned int) rcolor.channel[ch];
88 67500 new_color *= intensity;
89
90 67500 ▶ if(new_color > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if(new_color > 255) { 0 67500 67500
91 0 new_color = 255;
92 }
93 67500 rcolor.channel[ch] = (unsigned char) new_color;
94 }
95 22500 i_ppix(im, x, y, &rcolor);
96 }
97 }
98  
99  
100 static int
101 5 s_hardinvert_low(i_img *im, int all) {
102 i_img_dim x, y;
103 int ch;
104 5 ▶ int invert_channels = all ? im->channels : i_img_color_channels(im);
Branch: 1 branch
BranchTrueFalseTotal
int invert_channels = all ? im->channels : i_img_color_channels(im); 2 3 5
105 5 dIMCTXim(im);
106  
107 5 im_log((aIMCTX,1,"i_hardinvert)low(im %p, all %d)\n", im, all));
108
109 5 ▶#code im->bits <= 8
Branch: 1 branch
BranchTrueFalseTotal
#code im->bits <= 8 3 2 5
110 IM_COLOR *row, *entry;
111
112 /* always rooms to allocate a single line of i_color */
113 5 row = mymalloc(sizeof(IM_COLOR) * im->xsize); /* checked 17feb2005 tonyc */
114  
115 159 ▶ for(y = 0; y < im->ysize; y++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) { 152 3 155
for(y = 0; y < im->ysize; y++) { 2 2 4
116 154 IM_GLIN(im, 0, im->xsize, y, row);
117 154 entry = row;
118 22658 ▶ for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(x = 0; x < im->xsize; x++) { 22502 152 22654
for(x = 0; x < im->xsize; x++) { 2 2 4
119 90018 ▶ for(ch = 0; ch < invert_channels; ch++) {
Branch: 2 branches
BranchTrueFalseTotal
for(ch = 0; ch < invert_channels; ch++) { 67507 22502 90009
for(ch = 0; ch < invert_channels; ch++) { 7 2 9
120 67514 entry->channel[ch] = IM_SAMPLE_MAX - entry->channel[ch];
121 }
122 22504 ++entry;
123 }
124 154 IM_PLIN(im, 0, im->xsize, y, row);
125 }
126 5 myfree(row);
127 #/code
128  
129 5 return 1;
130 }
131  
132 /*
133 =item i_hardinvert(im)
134  
135 Inverts the color channels of the input image.
136  
137 im - image object
138  
139 =cut
140 */
141  
142 void
143 3 i_hardinvert(i_img *im) {
144 3 s_hardinvert_low(im, 0);
145 3 }
146  
147 /*
148 =item i_hardinvertall(im)
149  
150 Inverts all channels of the input image.
151  
152 im - image object
153  
154 =cut
155 */
156  
157 void
158 2 i_hardinvertall(i_img *im) {
159 2 s_hardinvert_low(im, 1);
160 2 }
161  
162 /*
163 =item i_noise(im, amount, type)
164  
165 Adjusts the sample values randomly by the amount specified.
166  
167 If type is 0, adjust all channels in a pixel by the same (random)
168 amount amount, if non-zero adjust each sample independently.
169  
170 im - image object
171 amount - deviation in pixel values
172 type - noise individual for each channel if true
173  
174 =cut
175 */
176  
177 #ifdef WIN32
178 /* random() is non-ASCII, even if it is better than rand() */
179 #define random() rand()
180 #endif
181  
182 void
183 1 i_noise(i_img *im, float amount, unsigned char type) {
184 i_img_dim x, y;
185 int ch;
186 int new_color;
187 1 float damount = amount * 2;
188 i_color rcolor;
189 1 int color_inc = 0;
190 1 dIMCTXim(im);
191
192 1 im_log((aIMCTX, 1,"i_noise(im %p, intensity %.2f\n", im, amount));
193
194 1 ▶ if(amount < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if(amount < 0) return; 0 1 1
195
196 22651 ▶ for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 22500 150 22650
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 150 1 151
197 22500 i_gpix(im, x, y, &rcolor);
198
199 22500 ▶ if(type == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(type == 0) { 22500 0 22500
200 22500 color_inc = (amount - (damount * ((double)random() / RAND_MAX)));
201 }
202
203 90000 ▶ for(ch = 0; ch < im->channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < im->channels; ch++) { 67500 22500 90000
204 67500 new_color = (int) rcolor.channel[ch];
205
206 67500 ▶ if(type != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(type != 0) { 0 67500 67500
207 0 new_color += (amount - (damount * ((double)random() / RAND_MAX)));
208 } else {
209 67500 new_color += color_inc;
210 }
211
212 67500 ▶ if(new_color < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(new_color < 0) { 17838 49662 67500
213 17838 new_color = 0;
214 }
215 67500 ▶ if(new_color > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if(new_color > 255) { 0 67500 67500
216 0 new_color = 255;
217 }
218
219 67500 rcolor.channel[ch] = (unsigned char) new_color;
220 }
221
222 22500 i_ppix(im, x, y, &rcolor);
223 }
224 }
225  
226 /*
227 =item i_bumpmap(im, bump, channel, light_x, light_y, st)
228  
229 Makes a bumpmap on image im using the bump image as the elevation map.
230  
231 im - target image
232 bump - image that contains the elevation info
233 channel - to take the elevation information from
234 light_x - x coordinate of light source
235 light_y - y coordinate of light source
236 st - length of shadow
237  
238 =cut
239 */
240  
241 void
242 1 i_bumpmap(i_img *im, i_img *bump, int channel, i_img_dim light_x, i_img_dim light_y, i_img_dim st) {
243 i_img_dim x, y;
244 int ch;
245 i_img_dim mx, my;
246 i_color x1_color, y1_color, x2_color, y2_color, dst_color;
247 double nX, nY;
248 double tX, tY, tZ;
249 double aX, aY, aL;
250 double fZ;
251 unsigned char px1, px2, py1, py2;
252 1 dIMCTXim(im);
253 i_img new_im;
254  
255 1 im_log((aIMCTX, 1, "i_bumpmap(im %p, add_im %p, channel %d, light(" i_DFp "), st %" i_DF ")\n",
256 im, bump, channel, i_DFcp(light_x, light_y), i_DFc(st)));
257  
258  
259 1 ▶ if(channel >= bump->channels) {
Branch: 1 branch
BranchTrueFalseTotal
if(channel >= bump->channels) { 0 1 1
260 0 im_log((aIMCTX, 1, "i_bumpmap: channel = %d while bump image only has %d channels\n", channel, bump->channels));
261 0 return;
262 }
263  
264 1 mx = (bump->xsize <= im->xsize) ? bump->xsize : im->xsize;
265 1 my = (bump->ysize <= im->ysize) ? bump->ysize : im->ysize;
266  
267 1 i_img_empty_ch(&new_im, im->xsize, im->ysize, im->channels);
268
269 1 ▶ aX = (light_x > (mx >> 1)) ? light_x : mx - light_x;
Branch: 1 branch
BranchTrueFalseTotal
aX = (light_x > (mx >> 1)) ? light_x : mx - light_x; 1 0 1
270 1 ▶ aY = (light_y > (my >> 1)) ? light_y : my - light_y;
Branch: 1 branch
BranchTrueFalseTotal
aY = (light_y > (my >> 1)) ? light_y : my - light_y; 1 0 1
271  
272 1 aL = sqrt((aX * aX) + (aY * aY));
273  
274 149 ▶ for(y = 1; y < my - 1; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 1; y < my - 1; y++) { 148 1 149
275 22052 ▶ for(x = 1; x < mx - 1; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 1; x < mx - 1; x++) { 21904 148 22052
276 21904 i_gpix(bump, x + st, y, &x1_color);
277 21904 i_gpix(bump, x, y + st, &y1_color);
278 21904 i_gpix(bump, x - st, y, &x2_color);
279 21904 i_gpix(bump, x, y - st, &y2_color);
280  
281 21904 i_gpix(im, x, y, &dst_color);
282  
283 21904 px1 = x1_color.channel[channel];
284 21904 py1 = y1_color.channel[channel];
285 21904 px2 = x2_color.channel[channel];
286 21904 py2 = y2_color.channel[channel];
287  
288 21904 nX = px1 - px2;
289 21904 nY = py1 - py2;
290  
291 21904 nX += 128;
292 21904 nY += 128;
293  
294 21904 fZ = (sqrt((nX * nX) + (nY * nY)) / aL);
295
296 21904 tX = i_abs(x - light_x) / aL;
297 21904 tY = i_abs(y - light_y) / aL;
298  
299 21904 tZ = 1 - (sqrt((tX * tX) + (tY * tY)) * fZ);
300
301 21904 ▶ if(tZ < 0) tZ = 0;
Branch: 1 branch
BranchTrueFalseTotal
if(tZ < 0) tZ = 0; 180 21724 21904
302 21904 ▶ if(tZ > 2) tZ = 2;
Branch: 1 branch
BranchTrueFalseTotal
if(tZ > 2) tZ = 2; 0 21904 21904
303  
304 87616 ▶ for(ch = 0; ch < im->channels; ch++)
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < im->channels; ch++) 65712 21904 87616
305 65712 dst_color.channel[ch] = (unsigned char) (float)(dst_color.channel[ch] * tZ);
306
307 21904 i_ppix(&new_im, x, y, &dst_color);
308 }
309 }
310  
311 1 i_copyto(im, &new_im, 0, 0, im->xsize, im->ysize, 0, 0);
312
313 1 i_img_exorcise(&new_im);
314 }
315  
316  
317  
318  
319 typedef struct {
320 double x,y,z;
321 } fvec;
322  
323  
324 static
325 float
326 67501 dotp(fvec *a, fvec *b) {
327 67501 return a->x*b->x+a->y*b->y+a->z*b->z;
328 }
329  
330 static
331 void
332 22501 normalize(fvec *a) {
333 22501 double d = sqrt(dotp(a,a));
334 22501 a->x /= d;
335 22501 a->y /= d;
336 22501 a->z /= d;
337 22501 }
338  
339  
340 /*
341 positive directions:
342
343 x - right,
344 y - down
345 z - out of the plane
346
347 I = Ia + Ip*( cd*Scol(N.L) + cs*(R.V)^n )
348
349 Here, the variables are:
350
351 * Ia - ambient colour
352 * Ip - intensity of the point light source
353 * cd - diffuse coefficient
354 * Scol - surface colour
355 * cs - specular coefficient
356 * n - objects shinyness
357 * N - normal vector
358 * L - lighting vector
359 * R - reflection vector
360 * V - vision vector
361  
362 static void fvec_dump(fvec *x) {
363 printf("(%.2f %.2f %.2f)", x->x, x->y, x->z);
364 }
365 */
366  
367 /* XXX: Should these return a code for success? */
368  
369  
370  
371  
372 /*
373 =item i_bumpmap_complex(im, bump, channel, tx, ty, Lx, Ly, Lz, Ip, cd, cs, n, Ia, Il, Is)
374  
375 Makes a bumpmap on image im using the bump image as the elevation map.
376  
377 im - target image
378 bump - image that contains the elevation info
379 channel - to take the elevation information from
380 tx - shift in x direction of where to start applying bumpmap
381 ty - shift in y direction of where to start applying bumpmap
382 Lx - x position/direction of light
383 Ly - y position/direction of light
384 Lz - z position/direction of light
385 Ip - light intensity
386 cd - diffuse coefficient
387 cs - specular coefficient
388 n - surface shinyness
389 Ia - ambient colour
390 Il - light colour
391 Is - specular colour
392  
393 if z<0 then the L is taken to be the direction the light is shining in. Otherwise
394 the L is taken to be the position of the Light, Relative to the image.
395  
396 =cut
397 */
398  
399  
400 void
401 1 i_bumpmap_complex(i_img *im,
402 i_img *bump,
403 int channel,
404 i_img_dim tx,
405 i_img_dim ty,
406 double Lx,
407 double Ly,
408 double Lz,
409 float cd,
410 float cs,
411 float n,
412 i_color *Ia,
413 i_color *Il,
414 i_color *Is) {
415 i_img new_im;
416
417 i_img_dim x, y;
418 int ch;
419 i_img_dim mx, Mx, my, My;
420
421 float cdc[MAXCHANNELS];
422 float csc[MAXCHANNELS];
423  
424 i_color x1_color, y1_color, x2_color, y2_color;
425  
426 i_color Scol; /* Surface colour */
427  
428 fvec L; /* Light vector */
429 fvec N; /* surface normal */
430 fvec R; /* Reflection vector */
431 fvec V; /* Vision vector */
432  
433 1 dIMCTXim(im);
434  
435 1 im_log((aIMCTX, 1, "i_bumpmap_complex(im %p, bump %p, channel %d, t(" i_DFp
436 "), Lx %.2f, Ly %.2f, Lz %.2f, cd %.2f, cs %.2f, n %.2f, Ia %p, Il %p, Is %p)\n",
437 im, bump, channel, i_DFcp(tx, ty), Lx, Ly, Lz, cd, cs, n, Ia, Il, Is));
438
439 1 ▶ if (channel >= bump->channels) {
Branch: 1 branch
BranchTrueFalseTotal
if (channel >= bump->channels) { 0 1 1
440 0 im_log((aIMCTX, 1, "i_bumpmap_complex: channel = %d while bump image only has %d channels\n", channel, bump->channels));
441 0 return;
442 }
443  
444 4 ▶ for(ch=0; ch<im->channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<im->channels; ch++) { 3 1 4
445 3 cdc[ch] = (float)Il->channel[ch]*cd/255.f;
446 3 csc[ch] = (float)Is->channel[ch]*cs/255.f;
447 }
448  
449 1 mx = 1;
450 1 my = 1;
451 1 Mx = bump->xsize-1;
452 1 My = bump->ysize-1;
453
454 1 V.x = 0;
455 1 V.y = 0;
456 1 V.z = 1;
457
458 1 ▶ if (Lz < 0) { /* Light specifies a direction vector, reverse it to get the vector from surface to light */
Branch: 1 branch
BranchTrueFalseTotal
if (Lz < 0) { /* Light specifies a direction vector, reverse it to get the vector from surface to light */ 1 0 1
459 1 L.x = -Lx;
460 1 L.y = -Ly;
461 1 L.z = -Lz;
462 1 normalize(&L);
463 } else { /* Light is the position of the light source */
464 0 L.x = -0.2;
465 0 L.y = -0.4;
466 0 L.z = 1;
467 0 normalize(&L);
468 }
469  
470 1 i_img_empty_ch(&new_im, im->xsize, im->ysize, im->channels);
471  
472 151 ▶ for(y = 0; y < im->ysize; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) { 150 1 151
473 22650 ▶ for(x = 0; x < im->xsize; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < im->xsize; x++) { 22500 150 22650
474 double dp1, dp2;
475 22500 double dx = 0, dy = 0;
476  
477 /* Calculate surface normal */
478 22500 ▶ if (mx<x && x<Mx && my<y && y<My) {
Branch: 4 branches
BranchTrueFalseTotal
if (mx<x && x<Mx && my<y && y<My) { 22200 300 22500
if (mx<x && x<Mx && my<y && y<My) { 22050 150 22200
if (mx<x && x<Mx && my<y && y<My) { 21756 294 22050
if (mx<x && x<Mx && my<y && y<My) { 21609 147 21756
479 21609 i_gpix(bump, x + 1, y, &x1_color);
480 21609 i_gpix(bump, x - 1, y, &x2_color);
481 21609 i_gpix(bump, x, y + 1, &y1_color);
482 21609 i_gpix(bump, x, y - 1, &y2_color);
483 21609 dx = x2_color.channel[channel] - x1_color.channel[channel];
484 21609 dy = y2_color.channel[channel] - y1_color.channel[channel];
485 } else {
486 891 dx = 0;
487 891 dy = 0;
488 }
489 22500 N.x = -dx * 0.015;
490 22500 N.y = -dy * 0.015;
491 22500 N.z = 1;
492 22500 normalize(&N);
493  
494 /* Calculate Light vector if needed */
495 22500 ▶ if (Lz>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if (Lz>=0) { 0 22500 22500
496 0 L.x = Lx - x;
497 0 L.y = Ly - y;
498 0 L.z = Lz;
499 0 normalize(&L);
500 }
501
502 22500 dp1 = dotp(&L,&N);
503 22500 R.x = -L.x + 2*dp1*N.x;
504 22500 R.y = -L.y + 2*dp1*N.y;
505 22500 R.z = -L.z + 2*dp1*N.z;
506
507 22500 dp2 = dotp(&R,&V);
508  
509 22500 ▶ dp1 = dp1<0 ?0 : dp1;
Branch: 1 branch
BranchTrueFalseTotal
dp1 = dp1<0 ?0 : dp1; 181 22319 22500
510 22500 ▶ dp2 = pow(dp2<0 ?0 : dp2,n);
Branch: 1 branch
BranchTrueFalseTotal
dp2 = pow(dp2<0 ?0 : dp2,n); 484 22016 22500
511  
512 22500 i_gpix(im, x, y, &Scol);
513  
514 90000 ▶ for(ch = 0; ch < im->channels; ch++)
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < im->channels; ch++) 67500 22500 90000
515 67500 Scol.channel[ch] =
516 67500 saturate( Ia->channel[ch] + cdc[ch]*Scol.channel[ch]*dp1 + csc[ch]*dp2 );
517
518 22500 i_ppix(&new_im, x, y, &Scol);
519 }
520 }
521
522 1 i_copyto(im, &new_im, 0, 0, im->xsize, im->ysize, 0, 0);
523 1 i_img_exorcise(&new_im);
524 }
525  
526  
527 /*
528 =item i_postlevels(im, levels)
529  
530 Quantizes Images to fewer levels.
531  
532 im - target image
533 levels - number of levels
534  
535 =cut
536 */
537  
538 void
539 1 i_postlevels(i_img *im, int levels) {
540 i_img_dim x, y;
541 int ch;
542 float pv;
543 int rv;
544 float av;
545  
546 i_color rcolor;
547  
548 1 rv = (int) ((float)(256 / levels));
549 1 av = (float)levels;
550  
551 22651 ▶ for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 22500 150 22650
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 150 1 151
552 22500 i_gpix(im, x, y, &rcolor);
553  
554 90000 ▶ for(ch = 0; ch < im->channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < im->channels; ch++) { 67500 22500 90000
555 67500 pv = (((float)rcolor.channel[ch] / 255)) * av;
556 67500 pv = (int) ((int)pv * rv);
557  
558 67500 ▶ if(pv < 0) pv = 0;
Branch: 1 branch
BranchTrueFalseTotal
if(pv < 0) pv = 0; 0 67500 67500
559 67500 ▶ else if(pv > 255) pv = 255;
Branch: 1 branch
BranchTrueFalseTotal
else if(pv > 255) pv = 255; 0 67500 67500
560  
561 67500 rcolor.channel[ch] = (unsigned char) pv;
562 }
563 22500 i_ppix(im, x, y, &rcolor);
564 }
565 1 }
566  
567  
568 /*
569 =item i_mosaic(im, size)
570  
571 Makes an image looks like a mosaic with tilesize of size
572  
573 im - target image
574 size - size of tiles
575  
576 =cut
577 */
578  
579 void
580 1 i_mosaic(i_img *im, i_img_dim size) {
581 i_img_dim x, y;
582 int ch, z;
583 i_img_dim lx, ly;
584 long sqrsize;
585  
586 i_color rcolor;
587 long col[256];
588
589 1 sqrsize = size * size;
590
591 381 ▶ for(y = 0; y < im->ysize; y += size) for(x = 0; x < im->xsize; x += size) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y += size) for(x = 0; x < im->xsize; x += size) { 361 19 380
for(y = 0; y < im->ysize; y += size) for(x = 0; x < im->xsize; x += size) { 19 1 20
592 92777 ▶ for(z = 0; z < 256; z++) col[z] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for(z = 0; z < 256; z++) col[z] = 0; 92416 361 92777
593
594 3249 ▶ for(lx = 0; lx < size; lx++) {
Branch: 1 branch
BranchTrueFalseTotal
for(lx = 0; lx < size; lx++) { 2888 361 3249
595 25992 ▶ for(ly = 0; ly < size; ly++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ly = 0; ly < size; ly++) { 23104 2888 25992
596 23104 i_gpix(im, (x + lx), (y + ly), &rcolor);
597
598 92416 ▶ for(ch = 0; ch < im->channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < im->channels; ch++) { 69312 23104 92416
599 69312 col[ch] += rcolor.channel[ch];
600 }
601 }
602 }
603
604 1444 ▶ for(ch = 0; ch < im->channels; ch++)
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch < im->channels; ch++) 1083 361 1444
605 1083 rcolor.channel[ch] = (int) ((float)col[ch] / sqrsize);
606
607
608 3249 ▶ for(lx = 0; lx < size; lx++)
Branch: 1 branch
BranchTrueFalseTotal
for(lx = 0; lx < size; lx++) 2888 361 3249
609 25992 ▶ for(ly = 0; ly < size; ly++)
Branch: 1 branch
BranchTrueFalseTotal
for(ly = 0; ly < size; ly++) 23104 2888 25992
610 23104 i_ppix(im, (x + lx), (y + ly), &rcolor);
611
612 }
613 1 }
614  
615  
616 /*
617 =item i_watermark(im, wmark, tx, ty, pixdiff)
618  
619 Applies a watermark to the target image
620  
621 im - target image
622 wmark - watermark image
623 tx - x coordinate of where watermark should be applied
624 ty - y coordinate of where watermark should be applied
625 pixdiff - the magnitude of the watermark, controls how visible it is
626  
627 =cut
628 */
629  
630 void
631 1 i_watermark(i_img *im, i_img *wmark, i_img_dim tx, i_img_dim ty, int pixdiff) {
632 i_img_dim vx, vy;
633 int ch;
634 i_color val, wval;
635  
636 1 i_img_dim mx = wmark->xsize;
637 1 i_img_dim my = wmark->ysize;
638  
639 22651 ▶ for(vx=0;vx<mx;vx++) for(vy=0;vy<my;vy++) {
Branch: 2 branches
BranchTrueFalseTotal
for(vx=0;vx<mx;vx++) for(vy=0;vy<my;vy++) { 22500 150 22650
for(vx=0;vx<mx;vx++) for(vy=0;vy<my;vy++) { 150 1 151
640
641 22500 i_gpix(im, tx+vx, ty+vy,&val );
642 22500 i_gpix(wmark, vx, vy, &wval);
643
644 90000 ▶ for(ch=0;ch<im->channels;ch++)
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) 67500 22500 90000
645 67500 val.channel[ch] = saturate( val.channel[ch] + (pixdiff* (wval.channel[0]-128) )/128 );
646
647 22500 i_ppix(im,tx+vx,ty+vy,&val);
648 }
649 1 }
650  
651 /*
652 =item i_autolevels_mono(im, lsat, usat)
653  
654 Do autolevels, but monochromatically.
655  
656 =cut
657 */
658  
659 void
660 3 i_autolevels_mono(i_img *im, float lsat, float usat) {
661 i_img_dim i, x, y, hist[256];
662 i_img_dim sum_lum, min_lum, max_lum;
663 i_img_dim upper_accum, lower_accum;
664 i_color *row;
665 3 dIMCTXim(im);
666 3 ▶ int adapt_channels = im->channels == 4 ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
int adapt_channels = im->channels == 4 ? 2 : 1; 0 3 3
667 3 int color_channels = i_img_color_channels(im);
668 3 i_img_dim color_samples = im->xsize * color_channels;
669
670  
671 3 im_log((aIMCTX, 1,"i_autolevels_mono(im %p, lsat %f,usat %f)\n", im, lsat,usat));
672  
673 /* build the histogram in 8-bits, unless the image has a very small
674 range it should make little difference to the result */
675 3 sum_lum = 0;
676 771 ▶ for (i = 0; i < 256; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) 768 3 771
677 768 hist[i] = 0;
678  
679 3 row = mymalloc(im->xsize * sizeof(i_color));
680 /* create histogram for each channel */
681 173 ▶ for (y = 0; y < im->ysize; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; y++) { 170 3 173
682 170 i_color *p = row;
683 170 i_glin(im, 0, im->xsize, y, row);
684 170 ▶ if (im->channels > 2)
Branch: 1 branch
BranchTrueFalseTotal
if (im->channels > 2) 170 0 170
685 170 i_adapt_colors(adapt_channels, im->channels, row, im->xsize);
686 22870 ▶ for (x = 0; x < im->xsize; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < im->xsize; x++) { 22700 170 22870
687 22700 hist[p->channel[0]]++;
688 22700 ++p;
689 }
690 }
691 3 myfree(row);
692  
693 771 ▶ for(i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < 256; i++) { 768 3 771
694 768 sum_lum += hist[i];
695 }
696
697 3 min_lum = 0;
698 3 lower_accum = 0;
699 771 ▶ for (i = 0; i < 256; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; ++i) { 768 3 771
700 768 ▶ if (lower_accum < sum_lum * lsat)
Branch: 1 branch
BranchTrueFalseTotal
if (lower_accum < sum_lum * lsat) 131 637 768
701 131 min_lum = i;
702 768 lower_accum += hist[i];
703 }
704  
705 3 max_lum = 255;
706 3 upper_accum = 0;
707 771 ▶ for(i = 255; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 255; i >= 0; i--) { 768 3 771
708 768 ▶ if (upper_accum < sum_lum * usat)
Branch: 1 branch
BranchTrueFalseTotal
if (upper_accum < sum_lum * usat) 205 563 768
709 205 max_lum = i;
710 768 upper_accum += hist[i];
711 }
712  
713 3 ▶#code im->bits <= 8
Branch: 1 branch
BranchTrueFalseTotal
#code im->bits <= 8 2 1 3
714 3 IM_SAMPLE_T *srow = mymalloc(color_samples * sizeof(IM_SAMPLE_T));
715 #ifdef IM_EIGHT_BIT
716 2 IM_WORK_T low = min_lum;
717 i_sample_t lookup[256];
718 #else
719 1 IM_WORK_T low = min_lum / 255.0 * IM_SAMPLE_MAX;
720 #endif
721 3 double scale = 255.0 / (max_lum - min_lum);
722  
723 #ifdef IM_EIGHT_BIT
724 514 ▶ for (i = 0; i < 256; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; ++i) { 512 2 514
725 512 IM_WORK_T tmp = (i - low) * scale;
726 512 ▶ lookup[i] = IM_LIMIT(tmp);
Branch: 1 branch
BranchTrueFalseTotal
lookup[i] = IM_LIMIT(tmp); 448 64 512
727 }
728 #endif
729  
730 173 ▶ for(y = 0; y < im->ysize; y++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) { 160 2 162
for(y = 0; y < im->ysize; y++) { 10 1 11
731 170 IM_GSAMP(im, 0, im->xsize, y, srow, NULL, color_channels);
732 68270 ▶ for(i = 0; i < color_samples; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for(i = 0; i < color_samples; ++i) { 67800 160 67960
for(i = 0; i < color_samples; ++i) { 300 10 310
733 #ifdef IM_EIGHT_BIT
734 67800 srow[i] = lookup[srow[i]];
735 #else
736 300 IM_WORK_T tmp = (srow[i] - low) * scale;
737 300 ▶ srow[i] = IM_LIMIT(tmp);
Branch: 2 branches
BranchTrueFalseTotal
srow[i] = IM_LIMIT(tmp); 0 300 300
srow[i] = IM_LIMIT(tmp); 0 300 300
738 #endif
739 }
740 170 IM_PSAMP(im, 0, im->xsize, y, srow, NULL, color_channels);
741 }
742 3 myfree(srow);
743 #/code
744 3 }
745  
746  
747 /*
748 =item i_autolevels(im, lsat, usat, skew)
749  
750 Scales and translates each color such that it fills the range completely.
751 Skew is not implemented yet - purpose is to control the color skew that can
752 occur when changing the contrast.
753  
754 im - target image
755 lsat - fraction of pixels that will be truncated at the lower end of the spectrum
756 usat - fraction of pixels that will be truncated at the higher end of the spectrum
757 skew - not used yet
758  
759 Note: this code calculates levels and adjusts each channel separately,
760 which will typically cause a color shift.
761  
762 =cut
763 */
764  
765 void
766 1 i_autolevels(i_img *im, float lsat, float usat, float skew) {
767 i_color val;
768 i_img_dim i, x, y, rhist[256], ghist[256], bhist[256];
769 i_img_dim rsum, rmin, rmax;
770 i_img_dim gsum, gmin, gmax;
771 i_img_dim bsum, bmin, bmax;
772 i_img_dim rcl, rcu, gcl, gcu, bcl, bcu;
773 1 dIMCTXim(im);
774  
775 1 im_log((aIMCTX, 1,"i_autolevels(im %p, lsat %f,usat %f,skew %f)\n", im, lsat,usat,skew));
776  
777 1 rsum=gsum=bsum=0;
778 257 ▶ for(i=0;i<256;i++) rhist[i]=ghist[i]=bhist[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<256;i++) rhist[i]=ghist[i]=bhist[i] = 0; 256 1 257
779 /* create histogram for each channel */
780 22651 ▶ for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 22500 150 22650
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 150 1 151
781 22500 i_gpix(im, x, y, &val);
782 22500 rhist[val.channel[0]]++;
783 22500 ghist[val.channel[1]]++;
784 22500 bhist[val.channel[2]]++;
785 }
786  
787 257 ▶ for(i=0;i<256;i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<256;i++) { 256 1 257
788 256 rsum+=rhist[i];
789 256 gsum+=ghist[i];
790 256 bsum+=bhist[i];
791 }
792
793 1 rmin = gmin = bmin = 0;
794 1 rmax = gmax = bmax = 255;
795
796 1 rcu = rcl = gcu = gcl = bcu = bcl = 0;
797
798 257 ▶ for(i=0; i<256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<256; i++) { 256 1 257
799 256 ▶ rcl += rhist[i]; if ( (rcl<rsum*lsat) ) rmin=i;
Branch: 1 branch
BranchTrueFalseTotal
rcl += rhist[i]; if ( (rcl<rsum*lsat) ) rmin=i; 0 256 256
800 256 ▶ rcu += rhist[255-i]; if ( (rcu<rsum*usat) ) rmax=255-i;
Branch: 1 branch
BranchTrueFalseTotal
rcu += rhist[255-i]; if ( (rcu<rsum*usat) ) rmax=255-i; 2 254 256
801  
802 256 ▶ gcl += ghist[i]; if ( (gcl<gsum*lsat) ) gmin=i;
Branch: 1 branch
BranchTrueFalseTotal
gcl += ghist[i]; if ( (gcl<gsum*lsat) ) gmin=i; 0 256 256
803 256 ▶ gcu += ghist[255-i]; if ( (gcu<gsum*usat) ) gmax=255-i;
Branch: 1 branch
BranchTrueFalseTotal
gcu += ghist[255-i]; if ( (gcu<gsum*usat) ) gmax=255-i; 2 254 256
804  
805 256 ▶ bcl += bhist[i]; if ( (bcl<bsum*lsat) ) bmin=i;
Branch: 1 branch
BranchTrueFalseTotal
bcl += bhist[i]; if ( (bcl<bsum*lsat) ) bmin=i; 0 256 256
806 256 ▶ bcu += bhist[255-i]; if ( (bcu<bsum*usat) ) bmax=255-i;
Branch: 1 branch
BranchTrueFalseTotal
bcu += bhist[255-i]; if ( (bcu<bsum*usat) ) bmax=255-i; 2 254 256
807 }
808  
809 22651 ▶ for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 22500 150 22650
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 150 1 151
810 22500 i_gpix(im, x, y, &val);
811 22500 val.channel[0]=saturate((val.channel[0]-rmin)*255/(rmax-rmin));
812 22500 val.channel[1]=saturate((val.channel[1]-gmin)*255/(gmax-gmin));
813 22500 val.channel[2]=saturate((val.channel[2]-bmin)*255/(bmax-bmin));
814 22500 i_ppix(im, x, y, &val);
815 }
816 1 }
817  
818 /*
819 =item Noise(x,y)
820  
821 Pseudo noise utility function used to generate perlin noise. (internal)
822  
823 x - x coordinate
824 y - y coordinate
825  
826 =cut
827 */
828  
829 static
830 double
831 8100000 Noise(i_img_dim x, i_img_dim y) {
832 8100000 i_img_dim n = x + y * 57;
833 8100000 n = (n<<13) ^ n;
834 8100000 return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
835 }
836  
837 /*
838 =item SmoothedNoise1(x,y)
839  
840 Pseudo noise utility function used to generate perlin noise. (internal)
841  
842 x - x coordinate
843 y - y coordinate
844  
845 =cut
846 */
847  
848 static
849 double
850 900000 SmoothedNoise1(double x, double y) {
851 900000 double corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16;
852 900000 double sides = ( Noise(x-1, y) +Noise(x+1, y) +Noise(x, y-1) +Noise(x, y+1) ) / 8;
853 900000 double center = Noise(x, y) / 4;
854 900000 return corners + sides + center;
855 }
856  
857  
858 /*
859 =item G_Interpolate(a, b, x)
860  
861 Utility function used to generate perlin noise. (internal)
862  
863 =cut
864 */
865  
866 static
867 double
868 675000 C_Interpolate(double a, double b, double x) {
869 /* float ft = x * 3.1415927; */
870 675000 double ft = x * PI;
871 675000 double f = (1 - cos(ft)) * .5;
872 675000 return a*(1-f) + b*f;
873 }
874  
875  
876 /*
877 =item InterpolatedNoise(x, y)
878  
879 Utility function used to generate perlin noise. (internal)
880  
881 =cut
882 */
883  
884 static
885 double
886 225000 InterpolatedNoise(double x, double y) {
887  
888 225000 i_img_dim integer_X = x;
889 225000 double fractional_X = x - integer_X;
890 225000 i_img_dim integer_Y = y;
891 225000 double fractional_Y = y - integer_Y;
892  
893 225000 double v1 = SmoothedNoise1(integer_X, integer_Y);
894 225000 double v2 = SmoothedNoise1(integer_X + 1, integer_Y);
895 225000 double v3 = SmoothedNoise1(integer_X, integer_Y + 1);
896 225000 double v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1);
897  
898 225000 double i1 = C_Interpolate(v1 , v2 , fractional_X);
899 225000 double i2 = C_Interpolate(v3 , v4 , fractional_X);
900  
901 225000 return C_Interpolate(i1 , i2 , fractional_Y);
902 }
903  
904  
905  
906 /*
907 =item PerlinNoise_2D(x, y)
908  
909 Utility function used to generate perlin noise. (internal)
910  
911 =cut
912 */
913  
914 static
915 float
916 45000 PerlinNoise_2D(float x, float y) {
917 int i;
918 double frequency;
919 double amplitude;
920 45000 double total = 0;
921 45000 int Number_Of_Octaves=6;
922 45000 int n = Number_Of_Octaves - 1;
923  
924 270000 ▶ for(i=0;i<n;i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<n;i++) { 225000 45000 270000
925 225000 frequency = 2.0 * i;
926 225000 amplitude = PI;
927 225000 total = total + InterpolatedNoise(x * frequency, y * frequency) * amplitude;
928 }
929  
930 45000 return total;
931 }
932  
933  
934 /*
935 =item i_radnoise(im, xo, yo, rscale, ascale)
936  
937 Perlin-like radial noise.
938  
939 im - target image
940 xo - x coordinate of center
941 yo - y coordinate of center
942 rscale - radial scale
943 ascale - angular scale
944  
945 =cut
946 */
947  
948 void
949 1 i_radnoise(i_img *im, i_img_dim xo, i_img_dim yo, double rscale, double ascale) {
950 i_img_dim x, y;
951 int ch;
952 i_color val;
953 unsigned char v;
954 double xc, yc, r;
955 double a;
956
957 22651 ▶ for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 22500 150 22650
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 150 1 151
958 22500 xc = (double)x-xo+0.5;
959 22500 yc = (double)y-yo+0.5;
960 22500 r = rscale*sqrt(xc*xc+yc*yc)+1.2;
961 22500 a = (PI+atan2(yc,xc))*ascale;
962 22500 v = saturate(128+100*(PerlinNoise_2D(a,r)));
963 /* v=saturate(120+12*PerlinNoise_2D(xo+(float)x/scale,yo+(float)y/scale)); Good soft marble */
964 90000 ▶ for(ch=0; ch<im->channels; ch++) val.channel[ch]=v;
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<im->channels; ch++) val.channel[ch]=v; 67500 22500 90000
965 22500 i_ppix(im, x, y, &val);
966 }
967 1 }
968  
969  
970 /*
971 =item i_turbnoise(im, xo, yo, scale)
972  
973 Perlin-like 2d noise noise.
974  
975 im - target image
976 xo - x coordinate translation
977 yo - y coordinate translation
978 scale - scale of noise
979  
980 =cut
981 */
982  
983 void
984 1 i_turbnoise(i_img *im, double xo, double yo, double scale) {
985 i_img_dim x,y;
986 int ch;
987 unsigned char v;
988 i_color val;
989  
990 22651 ▶ for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 22500 150 22650
for(y = 0; y < im->ysize; y++) for(x = 0; x < im->xsize; x++) { 150 1 151
991 /* v=saturate(125*(1.0+PerlinNoise_2D(xo+(float)x/scale,yo+(float)y/scale))); */
992 22500 v = saturate(120*(1.0+sin(xo+(double)x/scale+PerlinNoise_2D(xo+(double)x/scale,yo+(float)y/scale))));
993 90000 ▶ for(ch=0; ch<im->channels; ch++) val.channel[ch] = v;
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<im->channels; ch++) val.channel[ch] = v; 67500 22500 90000
994 22500 i_ppix(im, x, y, &val);
995 }
996 1 }
997  
998  
999  
1000 /*
1001 =item i_gradgen(im, num, xo, yo, ival, dmeasure)
1002  
1003 Gradient generating function.
1004  
1005 im - target image
1006 num - number of points given
1007 xo - array of x coordinates
1008 yo - array of y coordinates
1009 ival - array of i_color objects
1010 dmeasure - distance measure to be used.
1011 0 = Euclidean
1012 1 = Euclidean squared
1013 2 = Manhattan distance
1014  
1015 =cut
1016 */
1017  
1018  
1019 void
1020 1 i_gradgen(i_img *im, int num, i_img_dim *xo, i_img_dim *yo, i_color *ival, int dmeasure) {
1021
1022 i_color val;
1023 int p, ch;
1024 i_img_dim x, y;
1025 1 int channels = im->channels;
1026 1 i_img_dim xsize = im->xsize;
1027 1 i_img_dim ysize = im->ysize;
1028 size_t bytes;
1029  
1030 double *fdist;
1031 1 dIMCTXim(im);
1032  
1033 1 im_log((aIMCTX, 1,"i_gradgen(im %p, num %d, xo %p, yo %p, ival %p, dmeasure %d)\n", im, num, xo, yo, ival, dmeasure));
1034
1035 4 ▶ for(p = 0; p<num; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = 0; p<num; p++) { 3 1 4
1036 3 im_log((aIMCTX,1,"i_gradgen: p%d(" i_DFp ")\n", p, i_DFcp(xo[p], yo[p])));
1037 3 ICL_info(&ival[p]);
1038 }
1039  
1040 /* on the systems I have sizeof(float) == sizeof(int) and thus
1041 this would be same size as the arrays xo and yo point at, but this
1042 may not be true for other systems
1043  
1044 since the arrays here are caller controlled, I assume that on
1045 overflow is a programming error rather than an end-user error, so
1046 calling exit() is justified.
1047 */
1048 1 bytes = sizeof(double) * num;
1049 1 ▶ if (bytes / num != sizeof(double)) {
Branch: 1 branch
BranchTrueFalseTotal
if (bytes / num != sizeof(double)) { 0 1 1
1050 0 fprintf(stderr, "integer overflow calculating memory allocation");
1051 0 exit(1);
1052 }
1053 1 fdist = mymalloc( bytes ); /* checked 14jul05 tonyc */
1054
1055 22651 ▶ for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) { 22500 150 22650
for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) { 150 1 151
1056 22500 double cs = 0;
1057 22500 double csd = 0;
1058 90000 ▶ for(p = 0; p<num; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = 0; p<num; p++) { 67500 22500 90000
1059 67500 i_img_dim xd = x-xo[p];
1060 67500 i_img_dim yd = y-yo[p];
1061 67500 switch (dmeasure) {
1062 0 case 0: /* euclidean */
1063 0 fdist[p] = sqrt(xd*xd + yd*yd); /* euclidean distance */
1064 0 break;
1065 67500 case 1: /* euclidean squared */
1066 67500 fdist[p] = xd*xd + yd*yd; /* euclidean distance */
1067 67500 break;
1068 0 case 2: /* euclidean squared */
1069 0 fdist[p] = i_max(xd*xd, yd*yd); /* manhattan distance */
1070 0 break;
1071 0 default:
1072 0 im_fatal(aIMCTX, 3,"i_gradgen: Unknown distance measure\n");
1073 }
1074 67500 cs += fdist[p];
1075 }
1076
1077 22500 csd = 1/((num-1)*cs);
1078  
1079 90000 ▶ for(p = 0; p<num; p++) fdist[p] = (cs-fdist[p])*csd;
Branch: 1 branch
BranchTrueFalseTotal
for(p = 0; p<num; p++) fdist[p] = (cs-fdist[p])*csd; 67500 22500 90000
1080
1081 90000 ▶ for(ch = 0; ch<channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch<channels; ch++) { 67500 22500 90000
1082 67500 int tres = 0;
1083 270000 ▶ for(p = 0; p<num; p++) tres += ival[p].channel[ch] * fdist[p];
Branch: 1 branch
BranchTrueFalseTotal
for(p = 0; p<num; p++) tres += ival[p].channel[ch] * fdist[p]; 202500 67500 270000
1084 67500 val.channel[ch] = saturate(tres);
1085 }
1086 22500 i_ppix(im, x, y, &val);
1087 }
1088 1 myfree(fdist);
1089
1090 1 }
1091  
1092 void
1093 1 i_nearest_color_foo(i_img *im, int num, i_img_dim *xo, i_img_dim *yo, i_color *ival, int dmeasure) {
1094  
1095 int p;
1096 i_img_dim x, y;
1097 1 i_img_dim xsize = im->xsize;
1098 1 i_img_dim ysize = im->ysize;
1099 1 dIMCTXim(im);
1100  
1101 1 im_log((aIMCTX,1,"i_gradgen(im %p, num %d, xo %p, yo %p, ival %p, dmeasure %d)\n", im, num, xo, yo, ival, dmeasure));
1102
1103 4 ▶ for(p = 0; p<num; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = 0; p<num; p++) { 3 1 4
1104 3 im_log((aIMCTX, 1,"i_gradgen: p%d(" i_DFp ")\n", p, i_DFcp(xo[p], yo[p])));
1105 3 ICL_info(&ival[p]);
1106 }
1107  
1108 22651 ▶ for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) { 22500 150 22650
for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) { 150 1 151
1109 22500 int midx = 0;
1110 22500 double mindist = 0;
1111 22500 double curdist = 0;
1112  
1113 22500 i_img_dim xd = x-xo[0];
1114 22500 i_img_dim yd = y-yo[0];
1115  
1116 22500 switch (dmeasure) {
1117 0 case 0: /* euclidean */
1118 0 mindist = sqrt(xd*xd + yd*yd); /* euclidean distance */
1119 0 break;
1120 22500 case 1: /* euclidean squared */
1121 22500 mindist = xd*xd + yd*yd; /* euclidean distance */
1122 22500 break;
1123 0 case 2: /* euclidean squared */
1124 0 mindist = i_max(xd*xd, yd*yd); /* manhattan distance */
1125 0 break;
1126 0 default:
1127 0 im_fatal(aIMCTX, 3,"i_nearest_color: Unknown distance measure\n");
1128 }
1129  
1130 67500 ▶ for(p = 1; p<num; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = 1; p<num; p++) { 45000 22500 67500
1131 45000 xd = x-xo[p];
1132 45000 yd = y-yo[p];
1133 45000 switch (dmeasure) {
1134 0 case 0: /* euclidean */
1135 0 curdist = sqrt(xd*xd + yd*yd); /* euclidean distance */
1136 0 break;
1137 45000 case 1: /* euclidean squared */
1138 45000 curdist = xd*xd + yd*yd; /* euclidean distance */
1139 45000 break;
1140 0 case 2: /* euclidean squared */
1141 0 curdist = i_max(xd*xd, yd*yd); /* manhattan distance */
1142 0 break;
1143 0 default:
1144 0 im_fatal(aIMCTX, 3,"i_nearest_color: Unknown distance measure\n");
1145 }
1146 45000 ▶ if (curdist < mindist) {
Branch: 1 branch
BranchTrueFalseTotal
if (curdist < mindist) { 23182 21818 45000
1147 23182 mindist = curdist;
1148 23182 midx = p;
1149 }
1150 }
1151 22500 i_ppix(im, x, y, &ival[midx]);
1152 }
1153 1 }
1154  
1155 /*
1156 =item i_nearest_color(im, num, xo, yo, oval, dmeasure)
1157  
1158 This wasn't document - quoth Addi:
1159  
1160 An arty type of filter
1161  
1162 FIXME: check IRC logs for actual text.
1163  
1164 Inputs:
1165  
1166 =over
1167  
1168 =item *
1169  
1170 i_img *im - image to render on.
1171  
1172 =item *
1173  
1174 int num - number of points/colors in xo, yo, oval
1175  
1176 =item *
1177  
1178 i_img_dim *xo - array of I<num> x positions
1179  
1180 =item *
1181  
1182 i_img_dim *yo - array of I<num> y positions
1183  
1184 =item *
1185  
1186 i_color *oval - array of I<num> colors
1187  
1188 xo, yo, oval correspond to each other, the point xo[i], yo[i] has a
1189 color something like oval[i], at least closer to that color than other
1190 points.
1191  
1192 =item *
1193  
1194 int dmeasure - how we measure the distance from some point P(x,y) to
1195 any (xo[i], yo[i]).
1196  
1197 Valid values are:
1198  
1199 =over
1200  
1201 =item 0
1202  
1203 euclidean distance: sqrt((x2-x1)**2 + (y2-y1)**2)
1204  
1205 =item 1
1206  
1207 square of euclidean distance: ((x2-x1)**2 + (y2-y1)**2)
1208  
1209 =item 2
1210  
1211 manhattan distance: max((y2-y1)**2, (x2-x1)**2)
1212  
1213 =back
1214  
1215 An invalid value causes an error exit (the program is aborted).
1216  
1217 =back
1218  
1219 =cut
1220 */
1221  
1222 int
1223 1 i_nearest_color(i_img *im, int num, i_img_dim *xo, i_img_dim *yo, i_color *oval, int dmeasure) {
1224 i_color *ival;
1225 float *tval;
1226 double c1, c2;
1227 i_color val;
1228 int p, ch;
1229 i_img_dim x, y;
1230 1 i_img_dim xsize = im->xsize;
1231 1 i_img_dim ysize = im->ysize;
1232 int *cmatch;
1233 size_t ival_bytes, tval_bytes;
1234 1 dIMCTXim(im);
1235  
1236 1 im_log((aIMCTX, 1,"i_nearest_color(im %p, num %d, xo %p, yo %p, oval %p, dmeasure %d)\n", im, num, xo, yo, oval, dmeasure));
1237  
1238 1 i_clear_error();
1239  
1240 1 ▶ if (num <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (num <= 0) { 0 1 1
1241 0 i_push_error(0, "no points supplied to nearest_color filter");
1242 0 return 0;
1243 }
1244  
1245 1 ▶ if (dmeasure < 0 || dmeasure > i_dmeasure_limit) {
Branch: 2 branches
BranchTrueFalseTotal
if (dmeasure < 0 || dmeasure > i_dmeasure_limit) { 1 0 1
if (dmeasure < 0 || dmeasure > i_dmeasure_limit) { 0 1 1
1246 0 i_push_error(0, "distance measure invalid");
1247 0 return 0;
1248 }
1249  
1250 1 tval_bytes = sizeof(float) * (size_t)num * (size_t)im->channels;
1251 1 ▶ if (tval_bytes / (size_t)num != sizeof(float) * (size_t)im->channels) {
Branch: 1 branch
BranchTrueFalseTotal
if (tval_bytes / (size_t)num != sizeof(float) * (size_t)im->channels) { 0 1 1
1252 0 i_push_error(0, "integer overflow calculating memory allocation");
1253 0 return 0;
1254 }
1255 1 ival_bytes = sizeof(i_color) * (size_t)num;
1256 1 ▶ if (ival_bytes / sizeof(i_color) != (size_t)num) {
Branch: 1 branch
BranchTrueFalseTotal
if (ival_bytes / sizeof(i_color) != (size_t)num) { 0 1 1
1257 0 i_push_error(0, "integer overflow calculating memory allocation");
1258 0 return 0;
1259 }
1260 1 tval = mymalloc( tval_bytes ); /* checked 17feb2005 tonyc */
1261 1 ival = mymalloc( ival_bytes ); /* checked 17feb2005 tonyc */
1262 1 cmatch = mymalloc( sizeof(int)*num ); /* checked 17feb2005 tonyc */
1263  
1264 4 ▶ for(p = 0; p<num; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = 0; p<num; p++) { 3 1 4
1265 12 ▶ for(ch = 0; ch<im->channels; ch++) tval[ p * im->channels + ch] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch<im->channels; ch++) tval[ p * im->channels + ch] = 0; 9 3 12
1266 3 cmatch[p] = 0;
1267 }
1268  
1269
1270 22651 ▶ for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) { 22500 150 22650
for(y = 0; y<ysize; y++) for(x = 0; x<xsize; x++) { 150 1 151
1271 22500 int midx = 0;
1272 22500 double mindist = 0;
1273 22500 double curdist = 0;
1274
1275 22500 i_img_dim xd = x-xo[0];
1276 22500 i_img_dim yd = y-yo[0];
1277  
1278 22500 switch (dmeasure) {
1279 0 case 0: /* euclidean */
1280 0 mindist = sqrt(xd*xd + yd*yd); /* euclidean distance */
1281 0 break;
1282 22500 case 1: /* euclidean squared */
1283 22500 mindist = xd*xd + yd*yd; /* euclidean distance */
1284 22500 break;
1285 0 case 2: /* manhatten distance */
1286 0 mindist = i_max(xd*xd, yd*yd); /* manhattan distance */
1287 0 break;
1288 0 default:
1289 0 im_fatal(aIMCTX, 3,"i_nearest_color: Unknown distance measure\n");
1290 }
1291
1292 67500 ▶ for(p = 1; p<num; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = 1; p<num; p++) { 45000 22500 67500
1293 45000 xd = x-xo[p];
1294 45000 yd = y-yo[p];
1295 45000 switch (dmeasure) {
1296 0 case 0: /* euclidean */
1297 0 curdist = sqrt(xd*xd + yd*yd); /* euclidean distance */
1298 0 break;
1299 45000 case 1: /* euclidean squared */
1300 45000 curdist = xd*xd + yd*yd; /* euclidean distance */
1301 45000 break;
1302 0 case 2: /* euclidean squared */
1303 0 curdist = i_max(xd*xd, yd*yd); /* manhattan distance */
1304 0 break;
1305 0 default:
1306 0 im_fatal(aIMCTX, 3,"i_nearest_color: Unknown distance measure\n");
1307 }
1308 45000 ▶ if (curdist < mindist) {
Branch: 1 branch
BranchTrueFalseTotal
if (curdist < mindist) { 23182 21818 45000
1309 23182 mindist = curdist;
1310 23182 midx = p;
1311 }
1312 }
1313  
1314 22500 cmatch[midx]++;
1315 22500 i_gpix(im, x, y, &val);
1316 22500 c2 = 1.0/(float)(cmatch[midx]);
1317 22500 c1 = 1.0-c2;
1318
1319 90000 ▶ for(ch = 0; ch<im->channels; ch++)
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch<im->channels; ch++) 67500 22500 90000
1320 67500 tval[midx*im->channels + ch] =
1321 67500 c1*tval[midx*im->channels + ch] + c2 * (float) val.channel[ch];
1322
1323 }
1324  
1325 4 ▶ for(p = 0; p<num; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(p = 0; p<num; p++) { 3 1 4
1326 12 ▶ for(ch = 0; ch<im->channels; ch++)
Branch: 1 branch
BranchTrueFalseTotal
for(ch = 0; ch<im->channels; ch++) 9 3 12
1327 9 ival[p].channel[ch] = tval[p*im->channels + ch];
1328  
1329 /* avoid uninitialized value messages from valgrind */
1330 6 ▶ while (ch < MAXCHANNELS)
Branch: 1 branch
BranchTrueFalseTotal
while (ch < MAXCHANNELS) 3 3 6
1331 3 ival[p].channel[ch++] = 0;
1332 }
1333  
1334 1 i_nearest_color_foo(im, num, xo, yo, ival, dmeasure);
1335  
1336 1 myfree(cmatch);
1337 1 myfree(ival);
1338 1 myfree(tval);
1339  
1340 1 return 1;
1341 }
1342  
1343 /*
1344 =item i_unsharp_mask(im, stddev, scale)
1345  
1346 Perform an usharp mask, which is defined as subtracting the blurred
1347 image from double the original.
1348  
1349 =cut
1350 */
1351  
1352 void
1353 1 i_unsharp_mask(i_img *im, double stddev, double scale) {
1354 i_img *copy;
1355 i_img_dim x, y;
1356 int ch;
1357  
1358 1 ▶ if (scale < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scale < 0) 0 1 1
1359 0 return;
1360 /* it really shouldn't ever be more than 1.0, but maybe ... */
1361 1 ▶ if (scale > 100)
Branch: 1 branch
BranchTrueFalseTotal
if (scale > 100) 0 1 1
1362 0 scale = 100;
1363  
1364 1 copy = i_copy(im);
1365 1 i_gaussian(copy, stddev);
1366 1 ▶ if (im->bits == i_8_bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->bits == i_8_bits) { 1 0 1
1367 1 i_color *blur = mymalloc(im->xsize * sizeof(i_color)); /* checked 17feb2005 tonyc */
1368 1 i_color *out = mymalloc(im->xsize * sizeof(i_color)); /* checked 17feb2005 tonyc */
1369  
1370 151 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 150 1 151
1371 150 i_glin(copy, 0, copy->xsize, y, blur);
1372 150 i_glin(im, 0, im->xsize, y, out);
1373 22650 ▶ for (x = 0; x < im->xsize; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < im->xsize; ++x) { 22500 150 22650
1374 90000 ▶ for (ch = 0; ch < im->channels; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) { 67500 22500 90000
1375 /*int temp = out[x].channel[ch] +
1376 scale * (out[x].channel[ch] - blur[x].channel[ch]);*/
1377 67500 int temp = out[x].channel[ch] * 2 - blur[x].channel[ch];
1378 67500 ▶ if (temp < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (temp < 0) 4602 62898 67500
1379 4602 temp = 0;
1380 62898 ▶ else if (temp > 255)
Branch: 1 branch
BranchTrueFalseTotal
else if (temp > 255) 3167 59731 62898
1381 3167 temp = 255;
1382 67500 out[x].channel[ch] = temp;
1383 }
1384 }
1385 150 i_plin(im, 0, im->xsize, y, out);
1386 }
1387  
1388 1 myfree(blur);
1389 1 myfree(out);
1390 }
1391 else {
1392 0 i_fcolor *blur = mymalloc(im->xsize * sizeof(i_fcolor)); /* checked 17feb2005 tonyc */
1393 0 i_fcolor *out = mymalloc(im->xsize * sizeof(i_fcolor)); /* checked 17feb2005 tonyc */
1394  
1395 0 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 0 0 0
1396 0 i_glinf(copy, 0, copy->xsize, y, blur);
1397 0 i_glinf(im, 0, im->xsize, y, out);
1398 0 ▶ for (x = 0; x < im->xsize; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < im->xsize; ++x) { 0 0 0
1399 0 ▶ for (ch = 0; ch < im->channels; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < im->channels; ++ch) { 0 0 0
1400 0 double temp = out[x].channel[ch] +
1401 0 scale * (out[x].channel[ch] - blur[x].channel[ch]);
1402 0 ▶ if (temp < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (temp < 0) 0 0 0
1403 0 temp = 0;
1404 0 ▶ else if (temp > 1.0)
Branch: 1 branch
BranchTrueFalseTotal
else if (temp > 1.0) 0 0 0
1405 0 temp = 1.0;
1406 0 out[x].channel[ch] = temp;
1407 }
1408 }
1409 0 i_plinf(im, 0, im->xsize, y, out);
1410 }
1411  
1412 0 myfree(blur);
1413 0 myfree(out);
1414 }
1415 1 i_img_destroy(copy);
1416 }
1417  
1418 /*
1419 =item i_diff_image(im1, im2, mindist)
1420  
1421 Creates a new image that is transparent, except where the pixel in im2
1422 is different from im1, where it is the pixel from im2.
1423  
1424 The samples must differ by at least mindiff to be considered different.
1425  
1426 =cut
1427 */
1428  
1429 i_img *
1430 5 i_diff_image(i_img *im1, i_img *im2, double mindist) {
1431 i_img *out;
1432 int outchans, diffchans;
1433 i_img_dim xsize, ysize;
1434 5 dIMCTXim(im1);
1435  
1436 5 i_clear_error();
1437 5 ▶ if (im1->channels != im2->channels) {
Branch: 1 branch
BranchTrueFalseTotal
if (im1->channels != im2->channels) { 0 5 5
1438 0 i_push_error(0, "different number of channels");
1439 0 return NULL;
1440 }
1441  
1442 5 outchans = diffchans = im1->channels;
1443 5 ▶ if (outchans == 1 || outchans == 3)
Branch: 2 branches
BranchTrueFalseTotal
if (outchans == 1 || outchans == 3) 5 0 5
if (outchans == 1 || outchans == 3) 5 0 5
1444 5 ++outchans;
1445  
1446 5 xsize = i_min(im1->xsize, im2->xsize);
1447 5 ysize = i_min(im1->ysize, im2->ysize);
1448  
1449 5 out = i_sametype_chans(im1, xsize, ysize, outchans);
1450 5 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 0 5 5
1451 0 return NULL;
1452  
1453 8 ▶ if (im1->bits == i_8_bits && im2->bits == i_8_bits) {
Branch: 2 branches
BranchTrueFalseTotal
if (im1->bits == i_8_bits && im2->bits == i_8_bits) { 3 2 5
if (im1->bits == i_8_bits && im2->bits == i_8_bits) { 3 0 3
1454 3 i_color *line1 = mymalloc(xsize * sizeof(*line1)); /* checked 17feb2005 tonyc */
1455 3 i_color *line2 = mymalloc(xsize * sizeof(*line1)); /* checked 17feb2005 tonyc */
1456 i_color empty;
1457 i_img_dim x, y;
1458 int ch;
1459 3 int imindist = (int)mindist;
1460  
1461 15 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) 12 3 15
1462 12 empty.channel[ch] = 0;
1463  
1464 157 ▶ for (y = 0; y < ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < ysize; ++y) { 154 3 157
1465 154 i_glin(im1, 0, xsize, y, line1);
1466 154 i_glin(im2, 0, xsize, y, line2);
1467 154 ▶ if (outchans != diffchans) {
Branch: 1 branch
BranchTrueFalseTotal
if (outchans != diffchans) { 154 0 154
1468 /* give the output an alpha channel since it doesn't have one */
1469 22666 ▶ for (x = 0; x < xsize; ++x)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < xsize; ++x) 22512 154 22666
1470 22512 line2[x].channel[diffchans] = 255;
1471 }
1472 22666 ▶ for (x = 0; x < xsize; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < xsize; ++x) { 22512 154 22666
1473 22512 int diff = 0;
1474 88719 ▶ for (ch = 0; ch < diffchans; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < diffchans; ++ch) { 66651 22068 88719
1475 66651 ▶ if (line1[x].channel[ch] != line2[x].channel[ch]
Branch: 1 branch
BranchTrueFalseTotal
if (line1[x].channel[ch] != line2[x].channel[ch] 445 66206 66651
1476 445 ▶ && abs(line1[x].channel[ch] - line2[x].channel[ch]) > imindist) {
Branch: 1 branch
BranchTrueFalseTotal
&& abs(line1[x].channel[ch] - line2[x].channel[ch]) > imindist) { 444 1 445
1477 444 diff = 1;
1478 444 break;
1479 }
1480 }
1481 22512 ▶ if (!diff)
Branch: 1 branch
BranchTrueFalseTotal
if (!diff) 22068 444 22512
1482 22068 line2[x] = empty;
1483 }
1484 154 i_plin(out, 0, xsize, y, line2);
1485 }
1486 3 myfree(line1);
1487 3 myfree(line2);
1488 }
1489 else {
1490 2 i_fcolor *line1 = mymalloc(xsize * sizeof(*line1)); /* checked 17feb2005 tonyc */
1491 2 i_fcolor *line2 = mymalloc(xsize * sizeof(*line2)); /* checked 17feb2005 tonyc */
1492 i_fcolor empty;
1493 i_img_dim x, y;
1494 int ch;
1495 2 double dist = mindist / 255.0;
1496  
1497 10 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) 8 2 10
1498 8 empty.channel[ch] = 0;
1499  
1500 6 ▶ for (y = 0; y < ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < ysize; ++y) { 4 2 6
1501 4 i_glinf(im1, 0, xsize, y, line1);
1502 4 i_glinf(im2, 0, xsize, y, line2);
1503 4 ▶ if (outchans != diffchans) {
Branch: 1 branch
BranchTrueFalseTotal
if (outchans != diffchans) { 4 0 4
1504 /* give the output an alpha channel since it doesn't have one */
1505 16 ▶ for (x = 0; x < xsize; ++x)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < xsize; ++x) 12 4 16
1506 12 line2[x].channel[diffchans] = 1.0;
1507 }
1508 16 ▶ for (x = 0; x < xsize; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < xsize; ++x) { 12 4 16
1509 12 int diff = 0;
1510 42 ▶ for (ch = 0; ch < diffchans; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < diffchans; ++ch) { 33 9 42
1511 33 ▶ if (line1[x].channel[ch] != line2[x].channel[ch]
Branch: 1 branch
BranchTrueFalseTotal
if (line1[x].channel[ch] != line2[x].channel[ch] 4 29 33
1512 4 ▶ && fabs(line1[x].channel[ch] - line2[x].channel[ch]) > dist) {
Branch: 1 branch
BranchTrueFalseTotal
&& fabs(line1[x].channel[ch] - line2[x].channel[ch]) > dist) { 3 1 4
1513 3 diff = 1;
1514 3 break;
1515 }
1516 }
1517 12 ▶ if (!diff)
Branch: 1 branch
BranchTrueFalseTotal
if (!diff) 9 3 12
1518 9 line2[x] = empty;
1519 }
1520 4 i_plinf(out, 0, xsize, y, line2);
1521 }
1522 2 myfree(line1);
1523 2 myfree(line2);
1524 }
1525  
1526 5 return out;
1527 }
1528  
1529 /*
1530 =item i_rgbdiff_image(im1, im2)
1531  
1532 Creates a new image that is black, except where the pixel in im2 is
1533 different from im1, where it is the arithmetical difference to im2 per
1534 color.
1535  
1536 =cut
1537 */
1538  
1539 i_img *
1540 1 i_rgbdiff_image(i_img *im1, i_img *im2) {
1541 i_img *out;
1542 int outchans, diffchans;
1543 i_img_dim xsize, ysize;
1544 1 dIMCTXim(im1);
1545  
1546 1 i_clear_error();
1547 1 ▶ if (im1->channels != im2->channels) {
Branch: 1 branch
BranchTrueFalseTotal
if (im1->channels != im2->channels) { 0 1 1
1548 0 i_push_error(0, "different number of channels");
1549 0 return NULL;
1550 }
1551  
1552 1 outchans = diffchans = im1->channels;
1553 1 ▶ if (outchans == 2 || outchans == 4)
Branch: 2 branches
BranchTrueFalseTotal
if (outchans == 2 || outchans == 4) 1 0 1
if (outchans == 2 || outchans == 4) 1 0 1
1554 1 --outchans;
1555  
1556 1 xsize = i_min(im1->xsize, im2->xsize);
1557 1 ysize = i_min(im1->ysize, im2->ysize);
1558  
1559 1 out = i_sametype_chans(im1, xsize, ysize, outchans);
1560 1 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 0 1 1
1561 0 return NULL;
1562  
1563 2 ▶#code im1->bits == i_8_bits && im2->bits == i_8_bits
Branch: 2 branches
BranchTrueFalseTotal
#code im1->bits == i_8_bits && im2->bits == i_8_bits 1 0 1
#code im1->bits == i_8_bits && im2->bits == i_8_bits 1 0 1
1564 1 IM_COLOR *line1 = mymalloc(xsize * sizeof(*line1));
1565 1 IM_COLOR *line2 = mymalloc(xsize * sizeof(*line1));
1566 i_img_dim x, y;
1567 int ch;
1568  
1569 3 ▶ for (y = 0; y < ysize; ++y) {
Branch: 2 branches
BranchTrueFalseTotal
for (y = 0; y < ysize; ++y) { 2 1 3
for (y = 0; y < ysize; ++y) { 0 0 0
1570 2 IM_GLIN(im1, 0, xsize, y, line1);
1571 2 IM_GLIN(im2, 0, xsize, y, line2);
1572 8 ▶ for (x = 0; x < xsize; ++x) {
Branch: 2 branches
BranchTrueFalseTotal
for (x = 0; x < xsize; ++x) { 6 2 8
for (x = 0; x < xsize; ++x) { 0 0 0
1573 24 ▶ for (ch = 0; ch < outchans; ++ch) {
Branch: 2 branches
BranchTrueFalseTotal
for (ch = 0; ch < outchans; ++ch) { 18 6 24
for (ch = 0; ch < outchans; ++ch) { 0 0 0
1574 18 line2[x].channel[ch] = IM_ABS(line1[x].channel[ch] - line2[x].channel[ch]);
1575 }
1576 }
1577 2 IM_PLIN(out, 0, xsize, y, line2);
1578 }
1579 1 myfree(line1);
1580 1 myfree(line2);
1581 #/code
1582  
1583 1 return out;
1584 }
1585  
1586 struct fount_state;
1587 static double linear_fount_f(double x, double y, struct fount_state *state);
1588 static double bilinear_fount_f(double x, double y, struct fount_state *state);
1589 static double radial_fount_f(double x, double y, struct fount_state *state);
1590 static double square_fount_f(double x, double y, struct fount_state *state);
1591 static double revolution_fount_f(double x, double y,
1592 struct fount_state *state);
1593 static double conical_fount_f(double x, double y, struct fount_state *state);
1594  
1595 typedef double (*fount_func)(double, double, struct fount_state *);
1596 static fount_func fount_funcs[] =
1597 {
1598 linear_fount_f,
1599 bilinear_fount_f,
1600 radial_fount_f,
1601 square_fount_f,
1602 revolution_fount_f,
1603 conical_fount_f,
1604 };
1605  
1606 static double linear_interp(double pos, i_fountain_seg *seg);
1607 static double sine_interp(double pos, i_fountain_seg *seg);
1608 static double sphereup_interp(double pos, i_fountain_seg *seg);
1609 static double spheredown_interp(double pos, i_fountain_seg *seg);
1610 typedef double (*fount_interp)(double pos, i_fountain_seg *seg);
1611 static fount_interp fount_interps[] =
1612 {
1613 linear_interp,
1614 linear_interp,
1615 sine_interp,
1616 sphereup_interp,
1617 spheredown_interp,
1618 };
1619  
1620 static void direct_cinterp(i_fcolor *out, double pos, i_fountain_seg *seg);
1621 static void hue_up_cinterp(i_fcolor *out, double pos, i_fountain_seg *seg);
1622 static void hue_down_cinterp(i_fcolor *out, double pos, i_fountain_seg *seg);
1623 typedef void (*fount_cinterp)(i_fcolor *out, double pos, i_fountain_seg *seg);
1624 static fount_cinterp fount_cinterps[] =
1625 {
1626 direct_cinterp,
1627 hue_up_cinterp,
1628 hue_down_cinterp,
1629 };
1630  
1631 typedef double (*fount_repeat)(double v);
1632 static double fount_r_none(double v);
1633 static double fount_r_sawtooth(double v);
1634 static double fount_r_triangle(double v);
1635 static double fount_r_saw_both(double v);
1636 static double fount_r_tri_both(double v);
1637 static fount_repeat fount_repeats[] =
1638 {
1639 fount_r_none,
1640 fount_r_sawtooth,
1641 fount_r_triangle,
1642 fount_r_saw_both,
1643 fount_r_tri_both,
1644 };
1645  
1646 static int simple_ssample(i_fcolor *out, double x, double y,
1647 struct fount_state *state);
1648 static int random_ssample(i_fcolor *out, double x, double y,
1649 struct fount_state *state);
1650 static int circle_ssample(i_fcolor *out, double x, double y,
1651 struct fount_state *state);
1652 typedef int (*fount_ssample)(i_fcolor *out, double x, double y,
1653 struct fount_state *state);
1654 static fount_ssample fount_ssamples[] =
1655 {
1656 NULL,
1657 simple_ssample,
1658 random_ssample,
1659 circle_ssample,
1660 };
1661  
1662 static int
1663 fount_getat(i_fcolor *out, double x, double y, struct fount_state *state);
1664  
1665 /*
1666 Keep state information used by each type of fountain fill
1667 */
1668 struct fount_state {
1669 /* precalculated for the equation of the line perpendicular to the line AB */
1670 double lA, lB, lC;
1671 double AB;
1672 double sqrtA2B2;
1673 double mult;
1674 double cos;
1675 double sin;
1676 double theta;
1677 i_img_dim xa, ya;
1678 void *ssample_data;
1679 fount_func ffunc;
1680 fount_repeat rpfunc;
1681 fount_ssample ssfunc;
1682 double parm;
1683 i_fountain_seg *segs;
1684 int count;
1685 };
1686  
1687 static void
1688 fount_init_state(struct fount_state *state, double xa, double ya,
1689 double xb, double yb, i_fountain_type type,
1690 i_fountain_repeat repeat, int super_sample,
1691 double ssample_param, int count, i_fountain_seg *segs);
1692  
1693 static void
1694 fount_finish_state(struct fount_state *state);
1695  
1696 #define EPSILON (1e-6)
1697  
1698 /*
1699 =item i_fountain(im, xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, count, segs)
1700  
1701 Draws a fountain fill using A(xa, ya) and B(xb, yb) as reference points.
1702  
1703 I<type> controls how the reference points are used:
1704  
1705 =over
1706  
1707 =item i_ft_linear
1708  
1709 linear, where A is 0 and B is 1.
1710  
1711 =item i_ft_bilinear
1712  
1713 linear in both directions from A.
1714  
1715 =item i_ft_radial
1716  
1717 circular, where A is the centre of the fill, and B is a point
1718 on the radius.
1719  
1720 =item i_ft_radial_square
1721  
1722 where A is the centre of the fill and B is the centre of
1723 one side of the square.
1724  
1725 =item i_ft_revolution
1726  
1727 where A is the centre of the fill and B defines the 0/1.0
1728 angle of the fill.
1729  
1730 =item i_ft_conical
1731  
1732 similar to i_ft_revolution, except that the revolution goes in both
1733 directions
1734  
1735 =back
1736  
1737 I<repeat> can be one of:
1738  
1739 =over
1740  
1741 =item i_fr_none
1742  
1743 values < 0 are treated as zero, values > 1 are treated as 1.
1744  
1745 =item i_fr_sawtooth
1746  
1747 negative values are treated as 0, positive values are modulo 1.0
1748  
1749 =item i_fr_triangle
1750  
1751 negative values are treated as zero, if (int)value is odd then the value is treated as 1-(value
1752 mod 1.0), otherwise the same as for sawtooth.
1753  
1754 =item i_fr_saw_both
1755  
1756 like i_fr_sawtooth, except that the sawtooth pattern repeats into
1757 negative values.
1758  
1759 =item i_fr_tri_both
1760  
1761 Like i_fr_triangle, except that negative values are handled as their
1762 absolute values.
1763  
1764 =back
1765  
1766 If combine is non-zero then non-opaque values are combined with the
1767 underlying color.
1768  
1769 I<super_sample> controls super sampling, if any. At some point I'll
1770 probably add a adaptive super-sampler. Current possible values are:
1771  
1772 =over
1773  
1774 =item i_fts_none
1775  
1776 No super-sampling is done.
1777  
1778 =item i_fts_grid
1779  
1780 A square grid of points withing the pixel are sampled.
1781  
1782 =item i_fts_random
1783  
1784 Random points within the pixel are sampled.
1785  
1786 =item i_fts_circle
1787  
1788 Points on the radius of a circle are sampled. This produces fairly
1789 good results, but is fairly slow since sin() and cos() are evaluated
1790 for each point.
1791  
1792 =back
1793  
1794 I<ssample_param> is intended to be roughly the number of points
1795 sampled within the pixel.
1796  
1797 I<count> and I<segs> define the segments of the fill.
1798  
1799 =cut
1800  
1801 */
1802  
1803 int
1804 9 i_fountain(i_img *im, double xa, double ya, double xb, double yb,
1805 i_fountain_type type, i_fountain_repeat repeat,
1806 int combine, int super_sample, double ssample_param,
1807 int count, i_fountain_seg *segs) {
1808 struct fount_state state;
1809 i_img_dim x, y;
1810 9 i_fcolor *line = NULL;
1811 9 i_fcolor *work = NULL;
1812 size_t line_bytes;
1813 9 i_fill_combine_f combine_func = NULL;
1814 9 i_fill_combinef_f combinef_func = NULL;
1815 9 dIMCTXim(im);
1816  
1817 9 i_clear_error();
1818  
1819 /* i_fountain() allocates floating colors even for 8-bit images,
1820 so we need to do this check */
1821 9 line_bytes = sizeof(i_fcolor) * (size_t)im->xsize;
1822 9 ▶ if (line_bytes / sizeof(i_fcolor) != (size_t)im->xsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (line_bytes / sizeof(i_fcolor) != (size_t)im->xsize) { 0 9 9
1823 0 i_push_error(0, "integer overflow calculating memory allocation");
1824 0 return 0;
1825 }
1826
1827 9 line = mymalloc(line_bytes); /* checked 17feb2005 tonyc */
1828  
1829 9 i_get_combine(combine, &combine_func, &combinef_func);
1830 9 ▶ if (combinef_func) {
Branch: 1 branch
BranchTrueFalseTotal
if (combinef_func) { 2 7 9
1831 2 work = mymalloc(line_bytes); /* checked 17feb2005 tonyc */
1832 }
1833  
1834 9 fount_init_state(&state, xa, ya, xb, yb, type, repeat,
1835 super_sample, ssample_param, count, segs);
1836  
1837 1109 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 1100 9 1109
1838 1100 i_glinf(im, 0, im->xsize, y, line);
1839 146100 ▶ for (x = 0; x < im->xsize; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < im->xsize; ++x) { 145000 1100 146100
1840 i_fcolor c;
1841 int got_one;
1842 145000 ▶ if (super_sample == i_fts_none)
Branch: 1 branch
BranchTrueFalseTotal
if (super_sample == i_fts_none) 77500 67500 145000
1843 77500 got_one = fount_getat(&c, x, y, &state);
1844 else
1845 67500 got_one = state.ssfunc(&c, x, y, &state);
1846 145000 ▶ if (got_one) {
Branch: 1 branch
BranchTrueFalseTotal
if (got_one) { 145000 0 145000
1847 145000 ▶ if (combinef_func)
Branch: 1 branch
BranchTrueFalseTotal
if (combinef_func) 45000 100000 145000
1848 45000 work[x] = c;
1849 else
1850 100000 line[x] = c;
1851 }
1852 }
1853 1100 ▶ if (combinef_func)
Branch: 1 branch
BranchTrueFalseTotal
if (combinef_func) 300 800 1100
1854 300 combinef_func(line, work, im->channels, im->xsize);
1855 1100 i_plinf(im, 0, im->xsize, y, line);
1856 }
1857 9 fount_finish_state(&state);
1858 9 myfree(work);
1859 9 myfree(line);
1860  
1861 9 return 1;
1862 }
1863  
1864 typedef struct {
1865 i_fill_t base;
1866 struct fount_state state;
1867 } i_fill_fountain_t;
1868  
1869 static void
1870 fill_fountf(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width, int channels,
1871 i_fcolor *data);
1872 static void
1873 fount_fill_destroy(i_fill_t *fill);
1874  
1875 static i_fill_t
1876 fount_fill_proto =
1877 {
1878 NULL,
1879 fill_fountf,
1880 fount_fill_destroy,
1881 NULL,
1882 NULL
1883 };
1884  
1885 /*
1886 =item i_new_fill_fount(C<xa>, C<ya>, C<xb>, C<yb>, C<type>, C<repeat>, C<combine>, C<super_sample>, C<ssample_param>, C<count>, C<segs>)
1887  
1888 =category Fills
1889 =synopsis fill = i_new_fill_fount(0, 0, 100, 100, i_ft_linear, i_ft_linear,
1890 =synopsis i_fr_triangle, 0, i_fts_grid, 9, 1, segs);
1891  
1892  
1893 Creates a new general fill which fills with a fountain fill.
1894  
1895 =cut
1896 */
1897  
1898 i_fill_t *
1899 5 i_new_fill_fount(double xa, double ya, double xb, double yb,
1900 i_fountain_type type, i_fountain_repeat repeat,
1901 int combine, int super_sample, double ssample_param,
1902 int count, i_fountain_seg *segs) {
1903 5 i_fill_fountain_t *fill = mymalloc(sizeof(i_fill_fountain_t));
1904 5 memset(fill, 0, sizeof(*fill));
1905 5 fill->base = fount_fill_proto;
1906 5 ▶ if (combine)
Branch: 1 branch
BranchTrueFalseTotal
if (combine) 0 5 5
1907 0 i_get_combine(combine, &fill->base.combine, &fill->base.combinef);
1908 else {
1909 5 fill->base.combine = NULL;
1910 5 fill->base.combinef = NULL;
1911 }
1912 5 fount_init_state(&fill->state, xa, ya, xb, yb, type, repeat,
1913 super_sample, ssample_param, count, segs);
1914  
1915 5 return &fill->base;
1916 }
1917  
1918 /*
1919 =back
1920  
1921 =head1 INTERNAL FUNCTIONS
1922  
1923 =over
1924  
1925 =item fount_init_state(...)
1926  
1927 Used by both the fountain fill filter and the fountain fill.
1928  
1929 =cut
1930 */
1931  
1932 static void
1933 14 fount_init_state(struct fount_state *state, double xa, double ya,
1934 double xb, double yb, i_fountain_type type,
1935 i_fountain_repeat repeat, int super_sample,
1936 double ssample_param, int count, i_fountain_seg *segs) {
1937 int i, j;
1938 size_t bytes;
1939 14 i_fountain_seg *my_segs = mymalloc(sizeof(i_fountain_seg) * count); /* checked 2jul06 - duplicating original */
1940 /*int have_alpha = im->channels == 2 || im->channels == 4;*/
1941
1942 14 memset(state, 0, sizeof(*state));
1943 /* we keep a local copy that we can adjust for speed */
1944 31 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 17 14 31
1945 17 i_fountain_seg *seg = my_segs + i;
1946  
1947 17 *seg = segs[i];
1948 17 ▶ if (seg->type < 0 || seg->type >= i_fst_end)
Branch: 1 branch
BranchTrueFalseTotal
if (seg->type < 0 || seg->type >= i_fst_end) 1 16 17
1949 1 seg->type = i_fst_linear;
1950 17 ▶ if (seg->color < 0 || seg->color >= i_fc_end)
Branch: 1 branch
BranchTrueFalseTotal
if (seg->color < 0 || seg->color >= i_fc_end) 0 17 17
1951 0 seg->color = i_fc_direct;
1952 17 ▶ if (seg->color == i_fc_hue_up || seg->color == i_fc_hue_down) {
Branch: 2 branches
BranchTrueFalseTotal
if (seg->color == i_fc_hue_up || seg->color == i_fc_hue_down) { 15 2 17
if (seg->color == i_fc_hue_up || seg->color == i_fc_hue_down) { 1 14 15
1953 /* so we don't have to translate to HSV on each request, do it here */
1954 9 ▶ for (j = 0; j < 2; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 2; ++j) { 6 3 9
1955 6 i_rgb_to_hsvf(seg->c+j);
1956 }
1957 3 ▶ if (seg->color == i_fc_hue_up) {
Branch: 1 branch
BranchTrueFalseTotal
if (seg->color == i_fc_hue_up) { 2 1 3
1958 2 ▶ if (seg->c[1].channel[0] <= seg->c[0].channel[0])
Branch: 1 branch
BranchTrueFalseTotal
if (seg->c[1].channel[0] <= seg->c[0].channel[0]) 2 0 2
1959 2 seg->c[1].channel[0] += 1.0;
1960 }
1961 else {
1962 1 ▶ if (seg->c[0].channel[0] <= seg->c[0].channel[1])
Branch: 1 branch
BranchTrueFalseTotal
if (seg->c[0].channel[0] <= seg->c[0].channel[1]) 1 0 1
1963 1 seg->c[0].channel[0] += 1.0;
1964 }
1965 }
1966 /*printf("start %g mid %g end %g c0(%g,%g,%g,%g) c1(%g,%g,%g,%g) type %d color %d\n",
1967 seg->start, seg->middle, seg->end, seg->c[0].channel[0],
1968 seg->c[0].channel[1], seg->c[0].channel[2], seg->c[0].channel[3],
1969 seg->c[1].channel[0], seg->c[1].channel[1], seg->c[1].channel[2],
1970 seg->c[1].channel[3], seg->type, seg->color);*/
1971
1972 }
1973  
1974 /* initialize each engine */
1975 /* these are so common ... */
1976 14 state->lA = xb - xa;
1977 14 state->lB = yb - ya;
1978 14 state->AB = sqrt(state->lA * state->lA + state->lB * state->lB);
1979 14 state->xa = xa;
1980 14 state->ya = ya;
1981 14 switch (type) {
1982 0 default:
1983 0 type = i_ft_linear; /* make the invalid value valid */
1984 /* FALLTHROUGH */
1985 10 case i_ft_linear:
1986 case i_ft_bilinear:
1987 10 state->lC = ya * ya - ya * yb + xa * xa - xa * xb;
1988 10 state->mult = 1;
1989 10 state->mult = 1/linear_fount_f(xb, yb, state);
1990 10 break;
1991  
1992 2 case i_ft_radial:
1993 2 state->mult = 1.0 / sqrt((double)(xb-xa)*(xb-xa)
1994 2 + (double)(yb-ya)*(yb-ya));
1995 2 break;
1996  
1997 1 case i_ft_radial_square:
1998 1 state->cos = state->lA / state->AB;
1999 1 state->sin = state->lB / state->AB;
2000 1 state->mult = 1.0 / state->AB;
2001 1 break;
2002  
2003 1 case i_ft_revolution:
2004 1 state->theta = atan2(yb-ya, xb-xa);
2005 1 state->mult = 1.0 / (PI * 2);
2006 1 break;
2007  
2008 0 case i_ft_conical:
2009 0 state->theta = atan2(yb-ya, xb-xa);
2010 0 state->mult = 1.0 / PI;
2011 0 break;
2012 }
2013 14 state->ffunc = fount_funcs[type];
2014 14 ▶ if (super_sample < 0
Branch: 1 branch
BranchTrueFalseTotal
if (super_sample < 0 14 0 14
2015 14 ▶ || super_sample >= (int)(sizeof(fount_ssamples)/sizeof(*fount_ssamples))) {
Branch: 1 branch
BranchTrueFalseTotal
|| super_sample >= (int)(sizeof(fount_ssamples)/sizeof(*fount_ssamples))) { 0 14 14
2016 0 super_sample = 0;
2017 }
2018 14 state->ssample_data = NULL;
2019 14 switch (super_sample) {
2020 2 case i_fts_grid:
2021 2 ssample_param = floor(0.5 + sqrt(ssample_param));
2022 2 ▶ if (ssample_param > 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssample_param > 1000) { 0 2 2
2023 0 ssample_param = 1000;
2024 }
2025 2 state->ssample_data = mymalloc(sizeof(i_fcolor) * ssample_param * ssample_param); /* checked 1jul06 tonyc */
2026 2 break;
2027  
2028 1 case i_fts_random:
2029 case i_fts_circle:
2030 1 ssample_param = floor(0.5+ssample_param);
2031 1 ▶ if (ssample_param > 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssample_param > 1000) { 0 1 1
2032 0 ssample_param = 1000;
2033 }
2034 1 bytes = sizeof(i_fcolor) * ssample_param;
2035 1 state->ssample_data = mymalloc(bytes);
2036 1 break;
2037 }
2038 14 state->parm = ssample_param;
2039 14 state->ssfunc = fount_ssamples[super_sample];
2040 14 ▶ if (repeat < 0 || repeat >= (sizeof(fount_repeats)/sizeof(*fount_repeats)))
Branch: 1 branch
BranchTrueFalseTotal
if (repeat < 0 || repeat >= (sizeof(fount_repeats)/sizeof(*fount_repeats))) 0 14 14
2041 0 repeat = 0;
2042 14 state->rpfunc = fount_repeats[repeat];
2043 14 state->segs = my_segs;
2044 14 state->count = count;
2045 14 }
2046  
2047 static void
2048 14 fount_finish_state(struct fount_state *state) {
2049 14 ▶ if (state->ssample_data)
Branch: 1 branch
BranchTrueFalseTotal
if (state->ssample_data) 3 11 14
2050 3 myfree(state->ssample_data);
2051 14 myfree(state->segs);
2052 14 }
2053  
2054  
2055 /*
2056 =item fount_getat(out, x, y, ffunc, rpfunc, state, segs, count)
2057  
2058 Evaluates the fountain fill at the given point.
2059  
2060 This is called by both the non-super-sampling and super-sampling code.
2061  
2062 You might think that it would make sense to sample the fill parameter
2063 instead, and combine those, but this breaks badly.
2064  
2065 =cut
2066 */
2067  
2068 static int
2069 635417 fount_getat(i_fcolor *out, double x, double y, struct fount_state *state) {
2070 635417 double v = (state->rpfunc)((state->ffunc)(x, y, state));
2071 int i;
2072  
2073 635417 i = 0;
2074 635417 while (i < state->count
2075 736658 ▶ && (v < state->segs[i].start || v > state->segs[i].end)) {
Branch: 3 branches
BranchTrueFalseTotal
&& (v < state->segs[i].start || v > state->segs[i].end)) { 736658 0 736658
&& (v < state->segs[i].start || v > state->segs[i].end)) { 0 736658 736658
&& (v < state->segs[i].start || v > state->segs[i].end)) { 101241 635417 736658
2076 101241 ++i;
2077 }
2078 635417 ▶ if (i < state->count) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < state->count) { 635417 0 635417
2079 635417 v = (fount_interps[state->segs[i].type])(v, state->segs+i);
2080 635417 (fount_cinterps[state->segs[i].color])(out, v, state->segs+i);
2081 635417 return 1;
2082 }
2083 else
2084 0 return 0;
2085 }
2086  
2087 /*
2088 =item linear_fount_f(x, y, state)
2089  
2090 Calculate the fill parameter for a linear fountain fill.
2091  
2092 Uses the point to line distance function, with some precalculation
2093 done in i_fountain().
2094  
2095 =cut
2096 */
2097 static double
2098 521941 linear_fount_f(double x, double y, struct fount_state *state) {
2099 521941 return (state->lA * x + state->lB * y + state->lC) / state->AB * state->mult;
2100 }
2101  
2102 /*
2103 =item bilinear_fount_f(x, y, state)
2104  
2105 Calculate the fill parameter for a bi-linear fountain fill.
2106  
2107 =cut
2108 */
2109 static double
2110 0 bilinear_fount_f(double x, double y, struct fount_state *state) {
2111 0 return fabs((state->lA * x + state->lB * y + state->lC) / state->AB * state->mult);
2112 }
2113  
2114 /*
2115 =item radial_fount_f(x, y, state)
2116  
2117 Calculate the fill parameter for a radial fountain fill.
2118  
2119 Simply uses the distance function.
2120  
2121 =cut
2122 */
2123 static double
2124 13486 radial_fount_f(double x, double y, struct fount_state *state) {
2125 13486 return sqrt((double)(state->xa-x)*(state->xa-x)
2126 13486 + (double)(state->ya-y)*(state->ya-y)) * state->mult;
2127 }
2128  
2129 /*
2130 =item square_fount_f(x, y, state)
2131  
2132 Calculate the fill parameter for a square fountain fill.
2133  
2134 Works by rotating the reference co-ordinate around the centre of the
2135 square.
2136  
2137 =cut
2138 */
2139 static double
2140 90000 square_fount_f(double x, double y, struct fount_state *state) {
2141 i_img_dim xc, yc; /* centred on A */
2142 double xt, yt; /* rotated by theta */
2143 90000 xc = x - state->xa;
2144 90000 yc = y - state->ya;
2145 90000 xt = fabs(xc * state->cos + yc * state->sin);
2146 90000 yt = fabs(-xc * state->sin + yc * state->cos);
2147 90000 ▶ return (xt > yt ? xt : yt) * state->mult;
Branch: 1 branch
BranchTrueFalseTotal
return (xt > yt ? xt : yt) * state->mult; 44902 45098 90000
2148 }
2149  
2150 /*
2151 =item revolution_fount_f(x, y, state)
2152  
2153 Calculates the fill parameter for the revolution fountain fill.
2154  
2155 =cut
2156 */
2157 static double
2158 10000 revolution_fount_f(double x, double y, struct fount_state *state) {
2159 10000 double angle = atan2(y - state->ya, x - state->xa);
2160
2161 10000 angle -= state->theta;
2162 10000 ▶ if (angle < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (angle < 0) { 2500 7500 10000
2163 2500 angle = fmod(angle+ PI * 4, PI*2);
2164 }
2165  
2166 10000 return angle * state->mult;
2167 }
2168  
2169 /*
2170 =item conical_fount_f(x, y, state)
2171  
2172 Calculates the fill parameter for the conical fountain fill.
2173  
2174 =cut
2175 */
2176 static double
2177 0 conical_fount_f(double x, double y, struct fount_state *state) {
2178 0 double angle = atan2(y - state->ya, x - state->xa);
2179
2180 0 angle -= state->theta;
2181 0 ▶ if (angle < -PI)
Branch: 1 branch
BranchTrueFalseTotal
if (angle < -PI) 0 0 0
2182 0 angle += PI * 2;
2183 0 ▶ else if (angle > PI)
Branch: 1 branch
BranchTrueFalseTotal
else if (angle > PI) 0 0 0
2184 0 angle -= PI * 2;
2185  
2186 0 return fabs(angle) * state->mult;
2187 }
2188  
2189 /*
2190 =item linear_interp(pos, seg)
2191  
2192 Calculates linear interpolation on the fill parameter. Breaks the
2193 segment into 2 regions based in the I<middle> value.
2194  
2195 =cut
2196 */
2197 static double
2198 635417 linear_interp(double pos, i_fountain_seg *seg) {
2199 635417 ▶ if (pos < seg->middle) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos < seg->middle) { 380737 254680 635417
2200 380737 double len = seg->middle - seg->start;
2201 380737 ▶ if (len < EPSILON)
Branch: 1 branch
BranchTrueFalseTotal
if (len < EPSILON) 0 380737 380737
2202 0 return 0.0;
2203 else
2204 380737 return (pos - seg->start) / len / 2;
2205 }
2206 else {
2207 254680 double len = seg->end - seg->middle;
2208 254680 ▶ if (len < EPSILON)
Branch: 1 branch
BranchTrueFalseTotal
if (len < EPSILON) 0 254680 254680
2209 0 return 1.0;
2210 else
2211 254680 return 0.5 + (pos - seg->middle) / len / 2;
2212 }
2213 }
2214  
2215 /*
2216 =item sine_interp(pos, seg)
2217  
2218 Calculates sine function interpolation on the fill parameter.
2219  
2220 =cut
2221 */
2222 static double
2223 0 sine_interp(double pos, i_fountain_seg *seg) {
2224 /* I wonder if there's a simple way to smooth the transition for this */
2225 0 double work = linear_interp(pos, seg);
2226  
2227 0 return (1-cos(work * PI))/2;
2228 }
2229  
2230 /*
2231 =item sphereup_interp(pos, seg)
2232  
2233 Calculates spherical interpolation on the fill parameter, with the cusp
2234 at the low-end.
2235  
2236 =cut
2237 */
2238 static double
2239 0 sphereup_interp(double pos, i_fountain_seg *seg) {
2240 0 double work = linear_interp(pos, seg);
2241  
2242 0 return sqrt(1.0 - (1-work) * (1-work));
2243 }
2244  
2245 /*
2246 =item spheredown_interp(pos, seg)
2247  
2248 Calculates spherical interpolation on the fill parameter, with the cusp
2249 at the high-end.
2250  
2251 =cut
2252 */
2253 static double
2254 18864 spheredown_interp(double pos, i_fountain_seg *seg) {
2255 18864 double work = linear_interp(pos, seg);
2256  
2257 18864 return 1-sqrt(1.0 - work * work);
2258 }
2259  
2260 /*
2261 =item direct_cinterp(out, pos, seg)
2262  
2263 Calculates the fountain color based on direct scaling of the channels
2264 of the color channels.
2265  
2266 =cut
2267 */
2268 static void
2269 594053 direct_cinterp(i_fcolor *out, double pos, i_fountain_seg *seg) {
2270 int ch;
2271 2970265 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 2376212 594053 2970265
2272 2376212 out->channel[ch] = seg->c[0].channel[ch] * (1 - pos)
2273 2376212 + seg->c[1].channel[ch] * pos;
2274 }
2275 594053 }
2276  
2277 /*
2278 =item hue_up_cinterp(put, pos, seg)
2279  
2280 Calculates the fountain color based on scaling a HSV value. The hue
2281 increases as the fill parameter increases.
2282  
2283 =cut
2284 */
2285 static void
2286 30189 hue_up_cinterp(i_fcolor *out, double pos, i_fountain_seg *seg) {
2287 int ch;
2288 150945 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 120756 30189 150945
2289 120756 out->channel[ch] = seg->c[0].channel[ch] * (1 - pos)
2290 120756 + seg->c[1].channel[ch] * pos;
2291 }
2292 30189 i_hsv_to_rgbf(out);
2293 30189 }
2294  
2295 /*
2296 =item hue_down_cinterp(put, pos, seg)
2297  
2298 Calculates the fountain color based on scaling a HSV value. The hue
2299 decreases as the fill parameter increases.
2300  
2301 =cut
2302 */
2303 static void
2304 11175 hue_down_cinterp(i_fcolor *out, double pos, i_fountain_seg *seg) {
2305 int ch;
2306 55875 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 44700 11175 55875
2307 44700 out->channel[ch] = seg->c[0].channel[ch] * (1 - pos)
2308 44700 + seg->c[1].channel[ch] * pos;
2309 }
2310 11175 i_hsv_to_rgbf(out);
2311 11175 }
2312  
2313 /*
2314 =item simple_ssample(out, parm, x, y, state, ffunc, rpfunc, segs, count)
2315  
2316 Simple grid-based super-sampling.
2317  
2318 =cut
2319 */
2320 static int
2321 45000 simple_ssample(i_fcolor *out, double x, double y, struct fount_state *state) {
2322 45000 i_fcolor *work = state->ssample_data;
2323 i_img_dim dx, dy;
2324 45000 int grid = state->parm;
2325 45000 double base = -0.5 + 0.5 / grid;
2326 45000 double step = 1.0 / grid;
2327 int ch, i;
2328 45000 int samp_count = 0;
2329  
2330 135000 ▶ for (dx = 0; dx < grid; ++dx) {
Branch: 1 branch
BranchTrueFalseTotal
for (dx = 0; dx < grid; ++dx) { 90000 45000 135000
2331 270000 ▶ for (dy = 0; dy < grid; ++dy) {
Branch: 1 branch
BranchTrueFalseTotal
for (dy = 0; dy < grid; ++dy) { 180000 90000 270000
2332 180000 ▶ if (fount_getat(work+samp_count, x + base + step * dx,
Branch: 1 branch
BranchTrueFalseTotal
if (fount_getat(work+samp_count, x + base + step * dx, 180000 0 180000
2333 180000 y + base + step * dy, state)) {
2334 180000 ++samp_count;
2335 }
2336 }
2337 }
2338 225000 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 180000 45000 225000
2339 180000 out->channel[ch] = 0;
2340 900000 ▶ for (i = 0; i < samp_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < samp_count; ++i) { 720000 180000 900000
2341 720000 out->channel[ch] += work[i].channel[ch];
2342 }
2343 /* we divide by 4 rather than samp_count since if there's only one valid
2344 sample it should be mostly transparent */
2345 180000 out->channel[ch] /= grid * grid;
2346 }
2347 45000 return samp_count;
2348 }
2349  
2350 /*
2351 =item random_ssample(out, parm, x, y, state, ffunc, rpfunc, segs, count)
2352  
2353 Random super-sampling.
2354  
2355 =cut
2356 */
2357 static int
2358 0 random_ssample(i_fcolor *out, double x, double y,
2359 struct fount_state *state) {
2360 0 i_fcolor *work = state->ssample_data;
2361 int i, ch;
2362 0 int maxsamples = state->parm;
2363 0 double rand_scale = 1.0 / RAND_MAX;
2364 0 int samp_count = 0;
2365 0 ▶ for (i = 0; i < maxsamples; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < maxsamples; ++i) { 0 0 0
2366 0 ▶ if (fount_getat(work+samp_count, x - 0.5 + rand() * rand_scale,
Branch: 1 branch
BranchTrueFalseTotal
if (fount_getat(work+samp_count, x - 0.5 + rand() * rand_scale, 0 0 0
2367 0 y - 0.5 + rand() * rand_scale, state)) {
2368 0 ++samp_count;
2369 }
2370 }
2371 0 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 0 0 0
2372 0 out->channel[ch] = 0;
2373 0 ▶ for (i = 0; i < samp_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < samp_count; ++i) { 0 0 0
2374 0 out->channel[ch] += work[i].channel[ch];
2375 }
2376 /* we divide by maxsamples rather than samp_count since if there's
2377 only one valid sample it should be mostly transparent */
2378 0 out->channel[ch] /= maxsamples;
2379 }
2380 0 return samp_count;
2381 }
2382  
2383 /*
2384 =item circle_ssample(out, parm, x, y, state, ffunc, rpfunc, segs, count)
2385  
2386 Super-sampling around the circumference of a circle.
2387  
2388 I considered saving the sin()/cos() values and transforming step-size
2389 around the circle, but that's inaccurate, though it may not matter
2390 much.
2391  
2392 =cut
2393 */
2394 static int
2395 22500 circle_ssample(i_fcolor *out, double x, double y,
2396 struct fount_state *state) {
2397 22500 i_fcolor *work = state->ssample_data;
2398 int i, ch;
2399 22500 int maxsamples = state->parm;
2400 22500 double angle = 2 * PI / maxsamples;
2401 22500 double radius = 0.3; /* semi-random */
2402 22500 int samp_count = 0;
2403 382500 ▶ for (i = 0; i < maxsamples; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < maxsamples; ++i) { 360000 22500 382500
2404 360000 ▶ if (fount_getat(work+samp_count, x + radius * cos(angle * i),
Branch: 1 branch
BranchTrueFalseTotal
if (fount_getat(work+samp_count, x + radius * cos(angle * i), 360000 0 360000
2405 360000 y + radius * sin(angle * i), state)) {
2406 360000 ++samp_count;
2407 }
2408 }
2409 112500 ▶ for (ch = 0; ch < MAXCHANNELS; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < MAXCHANNELS; ++ch) { 90000 22500 112500
2410 90000 out->channel[ch] = 0;
2411 1530000 ▶ for (i = 0; i < samp_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < samp_count; ++i) { 1440000 90000 1530000
2412 1440000 out->channel[ch] += work[i].channel[ch];
2413 }
2414 /* we divide by maxsamples rather than samp_count since if there's
2415 only one valid sample it should be mostly transparent */
2416 90000 out->channel[ch] /= maxsamples;
2417 }
2418 22500 return samp_count;
2419 }
2420  
2421 /*
2422 =item fount_r_none(v)
2423  
2424 Implements no repeats. Simply clamps the fill value.
2425  
2426 =cut
2427 */
2428 static double
2429 265417 fount_r_none(double v) {
2430 265417 ▶ return v < 0 ? 0 : v > 1 ? 1 : v;
Branch: 2 branches
BranchTrueFalseTotal
return v < 0 ? 0 : v > 1 ? 1 : v; 46335 219082 265417
return v < 0 ? 0 : v > 1 ? 1 : v; 38789 180293 219082
2431 }
2432  
2433 /*
2434 =item fount_r_sawtooth(v)
2435  
2436 Implements sawtooth repeats. Clamps negative values and uses fmod()
2437 on others.
2438  
2439 =cut
2440 */
2441 static double
2442 10000 fount_r_sawtooth(double v) {
2443 10000 ▶ return v < 0 ? 0 : fmod(v, 1.0);
Branch: 1 branch
BranchTrueFalseTotal
return v < 0 ? 0 : fmod(v, 1.0); 0 10000 10000
2444 }
2445  
2446 /*
2447 =item fount_r_triangle(v)
2448  
2449 Implements triangle repeats. Clamps negative values, uses fmod to get
2450 a range 0 through 2 and then adjusts values > 1.
2451  
2452 =cut
2453 */
2454 static double
2455 360000 fount_r_triangle(double v) {
2456 360000 ▶ if (v < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) 179064 180936 360000
2457 179064 return 0;
2458 else {
2459 180936 v = fmod(v, 2.0);
2460 180936 ▶ return v > 1.0 ? 2.0 - v : v;
Branch: 1 branch
BranchTrueFalseTotal
return v > 1.0 ? 2.0 - v : v; 67602 113334 180936
2461 }
2462 }
2463  
2464 /*
2465 =item fount_r_saw_both(v)
2466  
2467 Implements sawtooth repeats in the both postive and negative directions.
2468  
2469 Adjusts the value to be postive and then just uses fmod().
2470  
2471 =cut
2472 */
2473 static double
2474 0 fount_r_saw_both(double v) {
2475 0 ▶ if (v < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) 0 0 0
2476 0 v += 1+(int)(-v);
2477 0 return fmod(v, 1.0);
2478 }
2479  
2480 /*
2481 =item fount_r_tri_both(v)
2482  
2483 Implements triangle repeats in the both postive and negative directions.
2484  
2485 Uses fmod on the absolute value, and then adjusts values > 1.
2486  
2487 =cut
2488 */
2489 static double
2490 0 fount_r_tri_both(double v) {
2491 0 v = fmod(fabs(v), 2.0);
2492 0 ▶ return v > 1.0 ? 2.0 - v : v;
Branch: 1 branch
BranchTrueFalseTotal
return v > 1.0 ? 2.0 - v : v; 0 0 0
2493 }
2494  
2495 /*
2496 =item fill_fountf(fill, x, y, width, channels, data)
2497  
2498 The fill function for fountain fills.
2499  
2500 =cut
2501 */
2502 static void
2503 292 fill_fountf(i_fill_t *fill, i_img_dim x, i_img_dim y, i_img_dim width,
2504 int channels, i_fcolor *data) {
2505 292 i_fill_fountain_t *f = (i_fill_fountain_t *)fill;
2506  
2507 (void)channels;
2508  
2509 18209 ▶ while (width--) {
Branch: 1 branch
BranchTrueFalseTotal
while (width--) { 17917 292 18209
2510 i_fcolor c;
2511 int got_one;
2512
2513 17917 ▶ if (f->state.ssfunc)
Branch: 1 branch
BranchTrueFalseTotal
if (f->state.ssfunc) 0 17917 17917
2514 0 got_one = f->state.ssfunc(&c, x, y, &f->state);
2515 else
2516 17917 got_one = fount_getat(&c, x, y, &f->state);
2517  
2518 17917 ▶ if (got_one)
Branch: 1 branch
BranchTrueFalseTotal
if (got_one) 17917 0 17917
2519 17917 *data++ = c;
2520
2521 17917 ++x;
2522 }
2523 292 }
2524  
2525 /*
2526 =item fount_fill_destroy(fill)
2527  
2528 =cut
2529 */
2530 static void
2531 5 fount_fill_destroy(i_fill_t *fill) {
2532 5 i_fill_fountain_t *f = (i_fill_fountain_t *)fill;
2533 5 fount_finish_state(&f->state);
2534 5 }
2535  
2536 /*
2537 =back
2538  
2539 =head1 AUTHOR
2540  
2541 Arnar M. Hrafnkelsson <addi@umich.edu>
2542  
2543 Tony Cook <tony@develop-help.com> (i_fountain())
2544  
2545 =head1 SEE ALSO
2546  
2547 Imager(3)
2548  
2549 =cut
2550 */
« bmp.c ↑ Summary draw.c »