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