Blender V5.3
icons_rasterize.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BLI_math_color_c.hh"
11#include "BLI_math_geom_c.hh"
12#include "BLI_utildefines.hh"
13
14#include "IMB_imbuf.hh"
15#include "IMB_imbuf_types.hh"
16
17#include "BKE_icons.hh"
18
19#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
20
21namespace blender {
22
24 int pt[3][2];
25 const uint *color;
26 /* only for smooth shading */
27 struct {
28 float pt_fl[3][2];
29 uint color_u[3][4];
31 int rect_size[2];
33};
34
35static void tri_fill_flat(int x, int x_end, int y, void *user_data)
36{
37 UserRasterInfo *data = static_cast<UserRasterInfo *>(user_data);
38 uint *p = &data->rect[(y * data->rect_size[1]) + x];
39 uint col = data->color[0];
40 while (x++ != x_end) {
41 *p++ = col;
42 }
43}
44
45static void tri_fill_smooth(int x, int x_end, int y, void *user_data)
46{
47 UserRasterInfo *data = static_cast<UserRasterInfo *>(user_data);
48 uint *p = &data->rect[(y * data->rect_size[1]) + x];
49 float pt_step_fl[2] = {float(x), float(y)};
50 while (x++ != x_end) {
51 float w[3];
52 barycentric_weights_v2_clamped(UNPACK3(data->smooth.pt_fl), pt_step_fl, w);
53
54 uint col_u[4] = {0, 0, 0, 0};
55 for (uint corner = 0; corner < 3; corner++) {
56 for (uint chan = 0; chan < 4; chan++) {
57 col_u[chan] += data->smooth.color_u[corner][chan] * uint(w[corner] * 255.0f);
58 }
59 }
60 union {
61 uint as_u32;
62 uchar as_bytes[4];
63 } col;
64 col.as_bytes[0] = uchar(col_u[0] / 255);
65 col.as_bytes[1] = uchar(col_u[1] / 255);
66 col.as_bytes[2] = uchar(col_u[2] / 255);
67 col.as_bytes[3] = uchar(col_u[3] / 255);
68 *p++ = col.as_u32;
69
70 pt_step_fl[0] += 1.0f;
71 }
72}
73
74ImBuf *BKE_icon_geom_rasterize(const Icon_Geom *geom, const uint size_x, const uint size_y)
75{
76 const int coords_len = geom->coords_len;
77
78 const uchar(*pos)[2] = geom->coords;
79 const uint *col = static_cast<const uint *>(static_cast<void *>(geom->colors));
80
81 /* TODO(@ideasman42): Currently rasterizes to fixed size, then scales.
82 * Should rasterize to double size for eg instead. */
83 const int rect_size[2] = {max_ii(256, int(size_x) * 2), max_ii(256, int(size_y) * 2)};
84
86
88
89 data.rect_size[0] = rect_size[0];
90 data.rect_size[1] = rect_size[1];
91
92 data.rect = reinterpret_cast<uint *>(ibuf->byte_data_for_write());
93
94 float scale[2];
95 const bool use_scale = (rect_size[0] != 256) || (rect_size[1] != 256);
96
97 if (use_scale) {
98 scale[0] = float(rect_size[0]) / 256.0f;
99 scale[1] = float(rect_size[1]) / 256.0f;
100 }
101
102 for (int t = 0; t < coords_len; t += 1, pos += 3, col += 3) {
103 if (use_scale) {
104 ARRAY_SET_ITEMS(data.pt[0], int(pos[0][0] * scale[0]), int(pos[0][1] * scale[1]));
105 ARRAY_SET_ITEMS(data.pt[1], int(pos[1][0] * scale[0]), int(pos[1][1] * scale[1]));
106 ARRAY_SET_ITEMS(data.pt[2], int(pos[2][0] * scale[0]), int(pos[2][1] * scale[1]));
107 }
108 else {
109 ARRAY_SET_ITEMS(data.pt[0], UNPACK2(pos[0]));
110 ARRAY_SET_ITEMS(data.pt[1], UNPACK2(pos[1]));
111 ARRAY_SET_ITEMS(data.pt[2], UNPACK2(pos[2]));
112 }
113 data.color = col;
114 if ((col[0] == col[1]) && (col[0] == col[2])) {
116 }
117 else {
118 ARRAY_SET_ITEMS(data.smooth.pt_fl[0], UNPACK2_EX((float), data.pt[0], ));
119 ARRAY_SET_ITEMS(data.smooth.pt_fl[1], UNPACK2_EX((float), data.pt[1], ));
120 ARRAY_SET_ITEMS(data.smooth.pt_fl[2], UNPACK2_EX((float), data.pt[2], ));
121 ARRAY_SET_ITEMS(data.smooth.color_u[0], UNPACK4_EX((uint), ((uchar *)(col + 0)), ));
122 ARRAY_SET_ITEMS(data.smooth.color_u[1], UNPACK4_EX((uint), ((uchar *)(col + 1)), ));
123 ARRAY_SET_ITEMS(data.smooth.color_u[2], UNPACK4_EX((uint), ((uchar *)(col + 2)), ));
125 }
126 }
127 IMB_scale(ibuf, size_x, size_y, IMBScaleFilter::Box, false);
128 return ibuf;
129}
130
132{
133 const int length = 3 * geom->coords_len;
134
135 for (int i = 0; i < length; i++) {
136 float rgb[3], hsl[3];
137
138 rgb_uchar_to_float(rgb, geom->colors[i]);
139 rgb_to_hsl_v(rgb, hsl);
140 hsl_to_rgb(hsl[0], hsl[1], 1.0f - hsl[2], &rgb[0], &rgb[1], &rgb[2]);
141 rgb_float_to_uchar(geom->colors[i], rgb);
142 }
143}
144
145} // namespace blender
#define UNPACK2(a)
#define ARRAY_SET_ITEMS(...)
#define UNPACK2_EX(pre, a, post)
#define UNPACK4_EX(pre, a, post)
#define UNPACK3(a)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
float length(VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
ImBuf * BKE_icon_geom_rasterize(const Icon_Geom *geom, unsigned int size_x, unsigned int size_y)
unsigned int uint
Definition BKE_text.h:55
void BLI_bitmap_draw_2d_tri_v2i(const int p1[2], const int p2[2], const int p3[2], void(*callback)(int x, int x_end, int y, void *), void *user_data)
void rgb_to_hsl_v(const float rgb[3], float r_hsl[3])
static void tri_fill_smooth(int x, int x_end, int y, void *user_data)
MINLINE int max_ii(int a, int b)
void BKE_icon_geom_invert_lightness(Icon_Geom *geom)
MINLINE void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
void barycentric_weights_v2_clamped(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
void hsl_to_rgb(float h, float s, float l, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:42
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static void tri_fill_flat(int x, int x_end, int y, void *user_data)
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
ccl_device_inline int rect_size(const int4 rect)
Definition rect.h:56
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
unsigned char(* colors)[4]
Definition BKE_icons.hh:70
unsigned char(* coords)[2]
Definition BKE_icons.hh:69
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60