Update Portuguese translation
[gegl.git] / opencl / c2g.cl
blob6140c91fba0894f97abc9da63aeaef9c3ddd0d37
1 #define ANGLE_PRIME 95273
2 #define RADIUS_PRIME 29537
4 void sample_min_max(const __global float4 *src_buf,
5 int src_width,
6 int src_height,
7 const __global float *radiuses,
8 const __global float *lut_cos,
9 const __global float *lut_sin,
10 int x,
11 int y,
12 int radius,
13 int samples,
14 float4 *min,
15 float4 *max,
16 int j,
17 int iterations)
19 float4 best_min;
20 float4 best_max;
21 float4 center_pix = *(src_buf + src_width * y + x);
22 int i;
24 best_min = center_pix;
25 best_max = center_pix;
27 int angle_no = (src_width * y + x) * (iterations) *
28 samples + j * samples;
29 int radius_no = angle_no;
30 angle_no %= ANGLE_PRIME;
31 radius_no %= RADIUS_PRIME;
32 for(i=0; i<samples; i++)
34 int angle;
35 float rmag;
36 /* if we've sampled outside the valid image
37 area, we grab another sample instead, this
38 should potentially work better than mirroring
39 or extending the image */
41 angle = angle_no++;
42 rmag = radiuses[radius_no++] * radius;
44 if( angle_no >= ANGLE_PRIME)
45 angle_no = 0;
46 if( radius_no >= RADIUS_PRIME)
47 radius_no = 0;
49 int u = x + rmag * lut_cos[angle];
50 int v = y + rmag * lut_sin[angle];
52 if(u>=src_width || u <0 || v>=src_height || v<0)
54 //--i;
55 continue;
57 float4 pixel = *(src_buf + (src_width * v + u));
58 if(pixel.w<=0.0f)
60 //--i;
61 continue;
64 best_min = pixel < best_min ? pixel : best_min;
65 best_max = pixel > best_max ? pixel : best_max;
68 (*min).xyz = best_min.xyz;
69 (*max).xyz = best_max.xyz;
72 void compute_envelopes(const __global float4 *src_buf,
73 int src_width,
74 int src_height,
75 const __global float *radiuses,
76 const __global float *lut_cos,
77 const __global float *lut_sin,
78 int x,
79 int y,
80 int radius,
81 int samples,
82 int iterations,
83 float4 *min_envelope,
84 float4 *max_envelope)
86 float4 range_sum = 0;
87 float4 relative_brightness_sum = 0;
88 float4 pixel = *(src_buf + src_width * y + x);
90 int i;
91 for(i =0; i<iterations; i++)
93 float4 min,max;
94 float4 range, relative_brightness;
96 sample_min_max(src_buf, src_width, src_height,
97 radiuses, lut_cos, lut_sin, x, y,
98 radius,samples,&min,&max,i,iterations);
99 range = max - min;
100 relative_brightness = range <= 0.0f ?
101 0.5f : (pixel - min) / range;
102 relative_brightness_sum += relative_brightness;
103 range_sum += range;
106 float4 relative_brightness = relative_brightness_sum / (float4)(iterations);
107 float4 range = range_sum / (float4)(iterations);
109 if(max_envelope)
110 *max_envelope = pixel + (1.0f - relative_brightness) * range;
112 if(min_envelope)
113 *min_envelope = pixel - relative_brightness * range;
116 __kernel void c2g(const __global float4 *src_buf,
117 int src_width,
118 int src_height,
119 const __global float *radiuses,
120 const __global float *lut_cos,
121 const __global float *lut_sin,
122 __global float2 *dst_buf,
123 int radius,
124 int samples,
125 int iterations)
127 int gidx = get_global_id(0);
128 int gidy = get_global_id(1);
130 int x = gidx + radius;
131 int y = gidy + radius;
133 int src_offset = (src_width * y + x);
134 int dst_offset = gidx + get_global_size(0) * gidy;
135 float4 min,max;
137 compute_envelopes(src_buf, src_width, src_height,
138 radiuses, lut_cos, lut_sin, x, y,
139 radius, samples, iterations, &min, &max);
141 float4 pixel = *(src_buf + src_offset);
143 float nominator=0, denominator=0;
144 float4 t1 = (pixel - min) * (pixel - min);
145 float4 t2 = (pixel - max) * (pixel - max);
147 nominator = t1.x + t1.y + t1.z;
148 denominator = t2.x + t2.y + t2.z;
150 nominator = sqrt(nominator);
151 denominator = sqrt(denominator);
152 denominator+= nominator + denominator;
154 dst_buf[dst_offset].x = (denominator > 0.000f)
155 ? (nominator / denominator) : 0.5f;
156 dst_buf[dst_offset].y = src_buf[src_offset].w;