Update Portuguese translation
[gegl.git] / opencl / pixelize.cl
blob54ea858310f403a28862e39108a80e36793561d9
1 /* This file is an image processing operation for GEGL
3 * GEGL is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 3 of the License, or (at your option) any later version.
8 * GEGL is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
16 * Copyright 2013 Victor Oliveira <victormatheus@gmail.com>
17 * Copyright 2013 Carlos Zubieta <czubieta.dev@gmail.com>
20 int
21 block_index (int pos,
22 int size)
24 return pos < 0 ? ((pos + 1) / size - 1) : (pos / size);
27 __kernel void calc_block_color(__global float4 *in,
28 __global float4 *out,
29 int xsize,
30 int ysize,
31 int roi_x,
32 int roi_y,
33 int4 bbox,
34 int line_width,
35 int block_count_x )
37 int gidx = get_global_id(0);
38 int gidy = get_global_id(1);
40 int cx = block_index (roi_x, xsize) + gidx;
41 int cy = block_index (roi_y, ysize) + gidy;
43 int px0 = max (bbox.s0, cx * xsize) - roi_x + xsize;
44 int py0 = max (bbox.s1, cy * ysize) - roi_y + ysize;
46 int px1 = min (bbox.s2, cx * xsize + xsize) - roi_x + xsize;
47 int py1 = min (bbox.s3, cy * ysize + ysize) - roi_y + ysize;
49 int i, j;
51 float4 col = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
53 int real_xsize = px1 - px0;
54 int real_ysize = py1 - py0;
56 float weight = 1.0f / (real_xsize * real_ysize);
58 for (j = py0; j < py1; ++j)
60 for (i = px0; i < px1; ++i)
62 col += in[j * line_width + i];
65 out[gidy * block_count_x + gidx] = col * weight;
69 #define NORM_MANHATTAN 0
70 #define NORM_EUCLIDEAN 1
71 #define NORM_INFINITY 2
72 #define SQR(x) ((x)*(x))
74 __kernel void kernel_pixelize(__global float4 *in,
75 __global float4 *out,
76 int xsize,
77 int ysize,
78 float xratio,
79 float yratio,
80 int roi_x,
81 int roi_y,
82 float4 bg_color,
83 int norm,
84 int block_count_x)
86 int gidx = get_global_id(0);
87 int gidy = get_global_id(1);
89 int src_width = get_global_size(0);
90 int cx = block_index (gidx + roi_x, xsize) - block_index (roi_x, xsize);
91 int cy = block_index (gidy + roi_y, ysize) - block_index (roi_y, ysize);
93 float4 grid_color = in[cx + cy * block_count_x];
94 float4 out_color = bg_color;
96 int x_pos = gidx + roi_x;
97 int y_pos = gidy + roi_y;
99 int rect_shape_width = ceil (xsize * xratio);
100 int rect_shape_height = ceil (ysize * yratio);
102 int off_shape_x = floor ((xsize - xratio * xsize) / 2.0f);
103 int off_shape_y = floor ((ysize - yratio * ysize) / 2.0f);
105 int start_x = block_index (x_pos, xsize) * xsize - roi_x;
106 int start_y = block_index (y_pos, ysize) * ysize - roi_y;
108 float shape_area = rect_shape_width * rect_shape_height;
110 float center_x = start_x + off_shape_x + (float)(rect_shape_width) / 2.0f;
111 float center_y = start_y + off_shape_y + (float)(rect_shape_height) / 2.0f;
113 if (norm == NORM_MANHATTAN &&
114 (fabs (gidx - center_x) * rect_shape_height +
115 fabs (gidy - center_y) * rect_shape_width
116 < shape_area))
117 out_color = grid_color;
119 if (norm == NORM_EUCLIDEAN &&
120 SQR ((gidx - center_x) / (float)rect_shape_width) +
121 SQR ((gidy - center_y) / (float)rect_shape_height) <= 1.0f)
122 out_color = grid_color;
124 if (norm == NORM_INFINITY &&
125 (gidx >= start_x + off_shape_x &&
126 gidy >= start_y + off_shape_y &&
127 gidx < start_x + off_shape_x + rect_shape_width &&
128 gidy < start_y + off_shape_y + rect_shape_height))
129 out_color = grid_color;
131 out[gidx + gidy * src_width] = out_color;