filters.im

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