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