Blender  V3.3
icons_rasterize.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
6 #include "MEM_guardedalloc.h"
7 
8 #include "BLI_bitmap_draw_2d.h"
9 #include "BLI_math_geom.h"
10 #include "BLI_utildefines.h"
11 
12 #include "IMB_imbuf.h"
13 #include "IMB_imbuf_types.h"
14 
15 #include "BKE_icons.h"
16 
17 #include "BLI_strict_flags.h"
18 
20  int pt[3][2];
21  const uint *color;
22  /* only for smooth shading */
23  struct {
24  float pt_fl[3][2];
25  uint color_u[3][4];
26  } smooth;
27  int rect_size[2];
29 };
30 
31 static void tri_fill_flat(int x, int x_end, int y, void *user_data)
32 {
33  struct UserRasterInfo *data = user_data;
34  uint *p = &data->rect[(y * data->rect_size[1]) + x];
35  uint col = data->color[0];
36  while (x++ != x_end) {
37  *p++ = col;
38  }
39 }
40 
41 static void tri_fill_smooth(int x, int x_end, int y, void *user_data)
42 {
43  struct UserRasterInfo *data = user_data;
44  uint *p = &data->rect[(y * data->rect_size[1]) + x];
45  float pt_step_fl[2] = {(float)x, (float)y};
46  while (x++ != x_end) {
47  float w[3];
48  barycentric_weights_v2_clamped(UNPACK3(data->smooth.pt_fl), pt_step_fl, w);
49 
50  uint col_u[4] = {0, 0, 0, 0};
51  for (uint corner = 0; corner < 3; corner++) {
52  for (uint chan = 0; chan < 4; chan++) {
53  col_u[chan] += data->smooth.color_u[corner][chan] * (uint)(w[corner] * 255.0f);
54  }
55  }
56  union {
57  uint as_u32;
58  uchar as_bytes[4];
59  } col;
60  col.as_bytes[0] = (uchar)(col_u[0] / 255);
61  col.as_bytes[1] = (uchar)(col_u[1] / 255);
62  col.as_bytes[2] = (uchar)(col_u[2] / 255);
63  col.as_bytes[3] = (uchar)(col_u[3] / 255);
64  *p++ = col.as_u32;
65 
66  pt_step_fl[0] += 1.0f;
67  }
68 }
69 
71  const unsigned int size_x,
72  const unsigned int size_y)
73 {
74  const int coords_len = geom->coords_len;
75 
76  const uchar(*pos)[2] = geom->coords;
77  const uint *col = (void *)geom->colors;
78 
79  /* TODO(campbell): Currently rasterizes to fixed size, then scales.
80  * Should rasterize to double size for eg instead. */
81  const int rect_size[2] = {max_ii(256, (int)size_x * 2), max_ii(256, (int)size_y * 2)};
82 
83  ImBuf *ibuf = IMB_allocImBuf((uint)rect_size[0], (uint)rect_size[1], 32, IB_rect);
84 
85  struct UserRasterInfo data;
86 
87  data.rect_size[0] = rect_size[0];
88  data.rect_size[1] = rect_size[1];
89 
90  data.rect = ibuf->rect;
91 
92  float scale[2];
93  const bool use_scale = (rect_size[0] != 256) || (rect_size[1] != 256);
94 
95  if (use_scale) {
96  scale[0] = ((float)rect_size[0] / 256.0f);
97  scale[1] = ((float)rect_size[1] / 256.0f);
98  }
99 
100  for (int t = 0; t < coords_len; t += 1, pos += 3, col += 3) {
101  if (use_scale) {
102  ARRAY_SET_ITEMS(data.pt[0], (int)(pos[0][0] * scale[0]), (int)(pos[0][1] * scale[1]));
103  ARRAY_SET_ITEMS(data.pt[1], (int)(pos[1][0] * scale[0]), (int)(pos[1][1] * scale[1]));
104  ARRAY_SET_ITEMS(data.pt[2], (int)(pos[2][0] * scale[0]), (int)(pos[2][1] * scale[1]));
105  }
106  else {
107  ARRAY_SET_ITEMS(data.pt[0], UNPACK2(pos[0]));
108  ARRAY_SET_ITEMS(data.pt[1], UNPACK2(pos[1]));
109  ARRAY_SET_ITEMS(data.pt[2], UNPACK2(pos[2]));
110  }
111  data.color = col;
112  if ((col[0] == col[1]) && (col[0] == col[2])) {
114  }
115  else {
116  ARRAY_SET_ITEMS(data.smooth.pt_fl[0], UNPACK2_EX((float), data.pt[0], ));
117  ARRAY_SET_ITEMS(data.smooth.pt_fl[1], UNPACK2_EX((float), data.pt[1], ));
118  ARRAY_SET_ITEMS(data.smooth.pt_fl[2], UNPACK2_EX((float), data.pt[2], ));
119  ARRAY_SET_ITEMS(data.smooth.color_u[0], UNPACK4_EX((uint), ((uchar *)(col + 0)), ));
120  ARRAY_SET_ITEMS(data.smooth.color_u[1], UNPACK4_EX((uint), ((uchar *)(col + 1)), ));
121  ARRAY_SET_ITEMS(data.smooth.color_u[2], UNPACK4_EX((uint), ((uchar *)(col + 2)), ));
123  }
124  }
125  IMB_scaleImBuf(ibuf, size_x, size_y);
126  return ibuf;
127 }
128 
130 {
131  const int length = 3 * geom->coords_len;
132 
133  for (int i = 0; i < length; i++) {
134  float rgb[3], hsl[3];
135 
136  rgb_uchar_to_float(rgb, geom->colors[i]);
137  rgb_to_hsl_v(rgb, hsl);
138  hsl_to_rgb(hsl[0], hsl[1], 1.0f - hsl[2], &rgb[0], &rgb[1], &rgb[2]);
139  rgb_float_to_uchar(geom->colors[i], rgb);
140  }
141 }
typedef float(TangentPoint)[2]
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)
MINLINE int max_ii(int a, int b)
void hsl_to_rgb(float h, float s, float l, float *r_r, float *r_g, float *r_b)
Definition: math_color.c:30
void rgb_to_hsl_v(const float rgb[3], float r_hsl[3])
Definition: math_color.c:292
void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
Definition: math_color.c:376
void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
Definition: math_color.c:391
void barycentric_weights_v2_clamped(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
Definition: math_geom.c:3723
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNPACK2(a)
#define ARRAY_SET_ITEMS(...)
#define UNPACK2_EX(pre, a, post)
#define UNPACK4_EX(pre, a, post)
#define UNPACK3(a)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
bool IMB_scaleImBuf(struct ImBuf *ibuf, unsigned int newx, unsigned int newy)
Definition: scaling.c:1644
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Definition: allocimbuf.c:500
Contains defines and structs used throughout the imbuf module.
@ IB_rect
Read Guarded memory(de)allocation.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
void * user_data
uint pos
uint col
static void tri_fill_flat(int x, int x_end, int y, void *user_data)
ImBuf * BKE_icon_geom_rasterize(const struct Icon_Geom *geom, const unsigned int size_x, const unsigned int size_y)
static void tri_fill_smooth(int x, int x_end, int y, void *user_data)
void BKE_icon_geom_invert_lightness(struct Icon_Geom *geom)
T length(const vec_base< T, Size > &a)
static const pxr::TfToken rgb("rgb", pxr::TfToken::Immortal)
ccl_device_inline int rect_size(int4 rect)
Definition: rect.h:55
int coords_len
Definition: BKE_icons.h:62
unsigned char(* colors)[4]
Definition: BKE_icons.h:65
unsigned char(* coords)[2]
Definition: BKE_icons.h:64
unsigned int * rect
const uint * color
float pt_fl[3][2]
uint color_u[3][4]
struct UserRasterInfo::@91 smooth