Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / blenkernel / intern / icons_rasterize.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file \ingroup bke
18  */
19 #include "MEM_guardedalloc.h"
20
21 #include "BLI_utildefines.h"
22 #include "BLI_bitmap_draw_2d.h"
23 #include "BLI_math_geom.h"
24
25 #include "IMB_imbuf.h"
26 #include "IMB_imbuf_types.h"
27
28 #include "BKE_icons.h"
29
30 #include "BLI_strict_flags.h"
31
32 struct UserRasterInfo {
33         int pt[3][2];
34         const uint *color;
35         /* only for smooth shading */
36         struct {
37                 float pt_fl[3][2];
38                 uint color_u[3][4];
39         } smooth;
40         int   rect_size[2];
41         uint *rect;
42 };
43
44 static void tri_fill_flat(int x, int x_end, int y, void *user_data)
45 {
46         struct UserRasterInfo *data = user_data;
47         uint *p = &data->rect[(y * data->rect_size[1]) + x];
48         uint col = data->color[0];
49         while (x++ != x_end) {
50                 *p++ = col;
51         }
52 }
53
54 static void tri_fill_smooth(int x, int x_end, int y, void *user_data)
55 {
56         struct UserRasterInfo *data = user_data;
57         uint *p = &data->rect[(y * data->rect_size[1]) + x];
58         float pt_step_fl[2] = {(float)x, (float)y};
59         while (x++ != x_end) {
60                 float w[3];
61                 barycentric_weights_v2_clamped(UNPACK3(data->smooth.pt_fl), pt_step_fl, w);
62
63                 uint col_u[4] = {0, 0, 0, 0};
64                 for (uint corner = 0; corner < 3; corner++) {
65                         for (uint chan = 0; chan < 4; chan++) {
66                                 col_u[chan] += data->smooth.color_u[corner][chan] * (uint)(w[corner] * 255.0f);
67                         }
68                 }
69                 union {
70                         uint  as_u32;
71                         uchar as_bytes[4];
72                 } col;
73                 col.as_bytes[0] = (uchar)(col_u[0] / 255);
74                 col.as_bytes[1] = (uchar)(col_u[1] / 255);
75                 col.as_bytes[2] = (uchar)(col_u[2] / 255);
76                 col.as_bytes[3] = (uchar)(col_u[3] / 255);
77                 *p++ = col.as_u32;
78
79                 pt_step_fl[0] += 1.0f;
80         }
81 }
82
83 ImBuf *BKE_icon_geom_rasterize(
84         const struct Icon_Geom *geom,
85         const unsigned int size_x, const unsigned int size_y)
86 {
87         const int coords_len = geom->coords_len;
88
89         const uchar (*pos)[2] = geom->coords;
90         const uint   *col = (void *)geom->colors;
91
92         /* TODO(campbell): Currently rasterizes to fixed size, then scales.
93          * Should rasterize to double size for eg instead. */
94         const int rect_size[2] = {max_ii(256, (int)size_x * 2), max_ii(256, (int)size_y * 2)};
95
96         ImBuf *ibuf = IMB_allocImBuf((uint)rect_size[0], (uint)rect_size[1], 32, IB_rect);
97
98         struct UserRasterInfo data;
99
100         data.rect_size[0] = rect_size[0];
101         data.rect_size[1] = rect_size[1];
102
103         data.rect = ibuf->rect;
104
105         float scale[2];
106         const bool use_scale = (rect_size[0] != 256) || (rect_size[1] != 256);
107
108         if (use_scale) {
109                 scale[0] = ((float)rect_size[0] / 256.0f);
110                 scale[1] = ((float)rect_size[1] / 256.0f);
111         }
112
113         for (int t = 0; t < coords_len; t += 1, pos += 3, col += 3) {
114                 if (use_scale) {
115                         ARRAY_SET_ITEMS(data.pt[0], (int)(pos[0][0] * scale[0]), (int)(pos[0][1] * scale[1]));
116                         ARRAY_SET_ITEMS(data.pt[1], (int)(pos[1][0] * scale[0]), (int)(pos[1][1] * scale[1]));
117                         ARRAY_SET_ITEMS(data.pt[2], (int)(pos[2][0] * scale[0]), (int)(pos[2][1] * scale[1]));
118                 }
119                 else {
120                         ARRAY_SET_ITEMS(data.pt[0], UNPACK2(pos[0]));
121                         ARRAY_SET_ITEMS(data.pt[1], UNPACK2(pos[1]));
122                         ARRAY_SET_ITEMS(data.pt[2], UNPACK2(pos[2]));
123                 }
124                 data.color = col;
125                 if ((col[0] == col[1]) && (col[0] == col[2])) {
126                         BLI_bitmap_draw_2d_tri_v2i(UNPACK3(data.pt), tri_fill_flat, &data);
127                 }
128                 else {
129                         ARRAY_SET_ITEMS(data.smooth.pt_fl[0], UNPACK2_EX((float), data.pt[0], ));
130                         ARRAY_SET_ITEMS(data.smooth.pt_fl[1], UNPACK2_EX((float), data.pt[1], ));
131                         ARRAY_SET_ITEMS(data.smooth.pt_fl[2], UNPACK2_EX((float), data.pt[2], ));
132                         ARRAY_SET_ITEMS(data.smooth.color_u[0], UNPACK4_EX((uint), ((uchar *)(col + 0)), ));
133                         ARRAY_SET_ITEMS(data.smooth.color_u[1], UNPACK4_EX((uint), ((uchar *)(col + 1)), ));
134                         ARRAY_SET_ITEMS(data.smooth.color_u[2], UNPACK4_EX((uint), ((uchar *)(col + 2)), ));
135                         BLI_bitmap_draw_2d_tri_v2i(UNPACK3(data.pt), tri_fill_smooth, &data);
136                 }
137         }
138         IMB_scaleImBuf(ibuf, size_x, size_y);
139         return ibuf;
140 }