Pose brush: Smooth Iterations Brush Property
[blender.git] / source / blender / blenkernel / intern / brush.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
18  * \ingroup bke
19  */
20
21 #include "MEM_guardedalloc.h"
22
23 #include "DNA_brush_types.h"
24 #include "DNA_scene_types.h"
25 #include "DNA_object_types.h"
26 #include "DNA_gpencil_types.h"
27 #include "DNA_defaults.h"
28
29 #include "BLI_listbase.h"
30 #include "BLI_math.h"
31 #include "BLI_rand.h"
32
33 #include "BKE_brush.h"
34 #include "BKE_colortools.h"
35 #include "BKE_context.h"
36 #include "BKE_library.h"
37 #include "BKE_library_query.h"
38 #include "BKE_library_remap.h"
39 #include "BKE_main.h"
40 #include "BKE_material.h"
41 #include "BKE_paint.h"
42 #include "BKE_texture.h"
43 #include "BKE_icons.h"
44
45 #include "IMB_colormanagement.h"
46 #include "IMB_imbuf.h"
47 #include "IMB_imbuf_types.h"
48
49 #include "RE_render_ext.h" /* externtex */
50
51 static RNG *brush_rng;
52
53 void BKE_brush_system_init(void)
54 {
55   brush_rng = BLI_rng_new(0);
56   BLI_rng_srandom(brush_rng, 31415682);
57 }
58
59 void BKE_brush_system_exit(void)
60 {
61   if (brush_rng == NULL) {
62     return;
63   }
64   BLI_rng_free(brush_rng);
65   brush_rng = NULL;
66 }
67
68 static void brush_defaults(Brush *brush)
69 {
70
71   const Brush *brush_def = DNA_struct_default_get(Brush);
72
73 #define FROM_DEFAULT(member) memcpy(&brush->member, &brush_def->member, sizeof(brush->member))
74 #define FROM_DEFAULT_PTR(member) memcpy(brush->member, brush_def->member, sizeof(brush->member))
75
76   FROM_DEFAULT(blend);
77   FROM_DEFAULT(flag);
78   FROM_DEFAULT(weight);
79   FROM_DEFAULT(size);
80   FROM_DEFAULT(alpha);
81   FROM_DEFAULT(autosmooth_factor);
82   FROM_DEFAULT(topology_rake_factor);
83   FROM_DEFAULT(crease_pinch_factor);
84   FROM_DEFAULT(normal_radius_factor);
85   FROM_DEFAULT(sculpt_plane);
86   FROM_DEFAULT(plane_offset);
87   FROM_DEFAULT(clone.alpha);
88   FROM_DEFAULT(normal_weight);
89   FROM_DEFAULT(fill_threshold);
90   FROM_DEFAULT(flag);
91   FROM_DEFAULT(sampling_flag);
92   FROM_DEFAULT_PTR(rgb);
93   FROM_DEFAULT_PTR(secondary_rgb);
94   FROM_DEFAULT(spacing);
95   FROM_DEFAULT(smooth_stroke_radius);
96   FROM_DEFAULT(smooth_stroke_factor);
97   FROM_DEFAULT(rate);
98   FROM_DEFAULT(jitter);
99   FROM_DEFAULT(texture_sample_bias);
100   FROM_DEFAULT(texture_overlay_alpha);
101   FROM_DEFAULT(mask_overlay_alpha);
102   FROM_DEFAULT(cursor_overlay_alpha);
103   FROM_DEFAULT(overlay_flags);
104   FROM_DEFAULT_PTR(add_col);
105   FROM_DEFAULT_PTR(sub_col);
106   FROM_DEFAULT(stencil_pos);
107   FROM_DEFAULT(stencil_dimension);
108   FROM_DEFAULT(mtex);
109   FROM_DEFAULT(mask_mtex);
110
111 #undef FROM_DEFAULT
112 #undef FROM_DEFAULT_PTR
113 }
114
115 /* Datablock add/copy/free/make_local */
116
117 void BKE_brush_init(Brush *brush)
118 {
119   BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(brush, id));
120
121   MEMCPY_STRUCT_AFTER(brush, DNA_struct_default_get(Brush), id);
122
123   /* enable fake user by default */
124   id_fake_user_set(&brush->id);
125
126   /* the default alpha falloff curve */
127   BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
128 }
129
130 /**
131  * \note Resulting brush will have two users: one as a fake user,
132  * another is assumed to be used by the caller.
133  */
134 Brush *BKE_brush_add(Main *bmain, const char *name, const eObjectMode ob_mode)
135 {
136   Brush *brush;
137
138   brush = BKE_libblock_alloc(bmain, ID_BR, name, 0);
139
140   BKE_brush_init(brush);
141
142   brush->ob_mode = ob_mode;
143
144   return brush;
145 }
146
147 /* add grease pencil settings */
148 void BKE_brush_init_gpencil_settings(Brush *brush)
149 {
150   if (brush->gpencil_settings == NULL) {
151     brush->gpencil_settings = MEM_callocN(sizeof(BrushGpencilSettings), "BrushGpencilSettings");
152   }
153
154   brush->gpencil_settings->draw_smoothlvl = 1;
155   brush->gpencil_settings->flag = 0;
156   brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
157   brush->gpencil_settings->draw_sensitivity = 1.0f;
158   brush->gpencil_settings->draw_strength = 1.0f;
159   brush->gpencil_settings->draw_jitter = 0.0f;
160   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
161   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
162   brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
163
164   /* curves */
165   brush->gpencil_settings->curve_sensitivity = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
166   brush->gpencil_settings->curve_strength = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
167   brush->gpencil_settings->curve_jitter = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
168 }
169
170 /* add a new gp-brush */
171 Brush *BKE_brush_add_gpencil(Main *bmain, ToolSettings *ts, const char *name)
172 {
173   Brush *brush;
174   Paint *paint = &ts->gp_paint->paint;
175   brush = BKE_brush_add(bmain, name, OB_MODE_PAINT_GPENCIL);
176
177   BKE_paint_brush_set(paint, brush);
178   id_us_min(&brush->id);
179
180   brush->size = 3;
181
182   /* grease pencil basic settings */
183   BKE_brush_init_gpencil_settings(brush);
184
185   /* return brush */
186   return brush;
187 }
188
189 /* grease pencil cumapping->preset */
190 typedef enum eGPCurveMappingPreset {
191   GPCURVE_PRESET_PENCIL = 0,
192   GPCURVE_PRESET_INK = 1,
193   GPCURVE_PRESET_INKNOISE = 2,
194   GPCURVE_PRESET_MARKER = 3,
195 } eGPCurveMappingPreset;
196
197 static void brush_gpencil_curvemap_reset(CurveMap *cuma, int tot, int preset)
198 {
199   if (cuma->curve) {
200     MEM_freeN(cuma->curve);
201   }
202
203   cuma->totpoint = tot;
204   cuma->curve = MEM_callocN(cuma->totpoint * sizeof(CurveMapPoint), __func__);
205
206   switch (preset) {
207     case GPCURVE_PRESET_PENCIL:
208       cuma->curve[0].x = 0.0f;
209       cuma->curve[0].y = 0.0f;
210       cuma->curve[1].x = 0.75115f;
211       cuma->curve[1].y = 0.25f;
212       cuma->curve[2].x = 1.0f;
213       cuma->curve[2].y = 1.0f;
214       break;
215     case GPCURVE_PRESET_INK:
216       cuma->curve[0].x = 0.0f;
217       cuma->curve[0].y = 0.0f;
218       cuma->curve[1].x = 0.63448f;
219       cuma->curve[1].y = 0.375f;
220       cuma->curve[2].x = 1.0f;
221       cuma->curve[2].y = 1.0f;
222       break;
223     case GPCURVE_PRESET_INKNOISE:
224       cuma->curve[0].x = 0.0f;
225       cuma->curve[0].y = 0.0f;
226       cuma->curve[1].x = 0.63134f;
227       cuma->curve[1].y = 0.3625f;
228       cuma->curve[2].x = 1.0f;
229       cuma->curve[2].y = 1.0f;
230       break;
231     case GPCURVE_PRESET_MARKER:
232       cuma->curve[0].x = 0.0f;
233       cuma->curve[0].y = 0.0f;
234       cuma->curve[1].x = 0.38f;
235       cuma->curve[1].y = 0.22f;
236       cuma->curve[2].x = 0.65f;
237       cuma->curve[2].y = 0.68f;
238       cuma->curve[3].x = 1.0f;
239       cuma->curve[3].y = 1.0f;
240       break;
241   }
242
243   if (cuma->table) {
244     MEM_freeN(cuma->table);
245     cuma->table = NULL;
246   }
247 }
248
249 /* create a set of grease pencil presets. */
250 void BKE_brush_gpencil_presets(Main *bmain, ToolSettings *ts)
251 {
252 #define SMOOTH_STROKE_RADIUS 40
253 #define SMOOTH_STROKE_FACTOR 0.9f
254
255   Paint *paint = &ts->gp_paint->paint;
256
257   Brush *brush, *deft;
258   CurveMapping *custom_curve;
259
260   /* Airbrush brush. */
261   brush = BLI_findstring(&bmain->brushes, "Airbrush", offsetof(ID, name) + 2);
262   if (brush == NULL) {
263     brush = BKE_brush_add_gpencil(bmain, ts, "Airbrush");
264   }
265
266   brush->size = 300.0f;
267   brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
268
269   brush->gpencil_settings->draw_strength = 0.4f;
270   brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
271
272   brush->gpencil_settings->input_samples = 10;
273   brush->gpencil_settings->active_smooth = 0.98f;
274   brush->gpencil_settings->draw_angle = 0.0f;
275   brush->gpencil_settings->draw_angle_factor = 0.0f;
276   brush->gpencil_settings->gradient_f = 0.211f;
277   brush->gpencil_settings->gradient_s[0] = 1.0f;
278   brush->gpencil_settings->gradient_s[1] = 1.0f;
279
280   brush->gpencil_settings->draw_sensitivity = 1.0f;
281
282   brush->gpencil_tool = GPAINT_TOOL_DRAW;
283   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_AIRBRUSH;
284
285   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
286   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
287
288   /* Create and link Black Dots material to brush.
289    * This material is required because the brush uses the material to define how the stroke is
290    * drawn. */
291   Material *ma = BLI_findstring(&bmain->materials, "Black Dots", offsetof(ID, name) + 2);
292   if (ma == NULL) {
293     ma = BKE_material_add_gpencil(bmain, "Black Dots");
294   }
295   brush->gpencil_settings->material = ma;
296   /* Pin the matterial to the brush. */
297   brush->gpencil_settings->flag |= GP_BRUSH_MATERIAL_PINNED;
298
299   /* Ink Pen brush. */
300   brush = BLI_findstring(&bmain->brushes, "Ink Pen", offsetof(ID, name) + 2);
301   if (brush == NULL) {
302     brush = BKE_brush_add_gpencil(bmain, ts, "Ink Pen");
303   }
304   brush->size = 60.0f;
305   brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
306
307   brush->gpencil_settings->draw_strength = 1.0f;
308
309   brush->gpencil_settings->input_samples = 10;
310   brush->gpencil_settings->active_smooth = 0.7f;
311   brush->gpencil_settings->draw_angle = 0.0f;
312   brush->gpencil_settings->draw_angle_factor = 0.0f;
313   brush->gpencil_settings->gradient_f = 1.0f;
314   brush->gpencil_settings->gradient_s[0] = 1.0f;
315   brush->gpencil_settings->gradient_s[1] = 1.0f;
316
317   brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
318   brush->gpencil_settings->draw_smoothfac = 0.1f;
319   brush->gpencil_settings->draw_smoothlvl = 1;
320   brush->gpencil_settings->thick_smoothfac = 1.0f;
321   brush->gpencil_settings->thick_smoothlvl = 3;
322   brush->gpencil_settings->draw_subdivide = 0;
323   brush->gpencil_settings->draw_random_sub = 0.0f;
324   brush->gpencil_settings->simplify_f = 0.002f;
325
326   brush->gpencil_settings->draw_random_press = 0.0f;
327   brush->gpencil_settings->draw_jitter = 0.0f;
328   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
329   brush->gpencil_settings->draw_sensitivity = 1.0f;
330
331   /* Curve. */
332   custom_curve = brush->gpencil_settings->curve_sensitivity;
333   BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
334   BKE_curvemapping_initialize(custom_curve);
335   brush_gpencil_curvemap_reset(custom_curve->cm, 3, GPCURVE_PRESET_INK);
336
337   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_INK;
338   brush->gpencil_tool = GPAINT_TOOL_DRAW;
339
340   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
341   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
342
343   /* Ink Pen Rough brush. */
344   brush = BLI_findstring(&bmain->brushes, "Ink Pen Rough", offsetof(ID, name) + 2);
345   if (brush == NULL) {
346     brush = BKE_brush_add_gpencil(bmain, ts, "Ink Pen Rough");
347   }
348
349   brush->size = 60.0f;
350   brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
351
352   brush->gpencil_settings->draw_strength = 1.0f;
353
354   brush->gpencil_settings->input_samples = 10;
355   brush->gpencil_settings->active_smooth = 0.5f;
356   brush->gpencil_settings->draw_angle = 0.0f;
357   brush->gpencil_settings->draw_angle_factor = 0.0f;
358   brush->gpencil_settings->gradient_f = 1.0f;
359   brush->gpencil_settings->gradient_s[0] = 1.0f;
360   brush->gpencil_settings->gradient_s[1] = 1.0f;
361
362   brush->gpencil_settings->flag &= ~GP_BRUSH_GROUP_SETTINGS;
363   brush->gpencil_settings->draw_smoothfac = 0.0f;
364   brush->gpencil_settings->draw_smoothlvl = 2;
365   brush->gpencil_settings->thick_smoothfac = 0.0f;
366   brush->gpencil_settings->thick_smoothlvl = 2;
367   brush->gpencil_settings->draw_subdivide = 0;
368   brush->gpencil_settings->draw_random_sub = 0.0f;
369   brush->gpencil_settings->simplify_f = 0.000f;
370
371   brush->gpencil_settings->flag |= GP_BRUSH_GROUP_RANDOM;
372   brush->gpencil_settings->draw_random_press = 1.0f;
373   brush->gpencil_settings->draw_random_strength = 0.0f;
374   brush->gpencil_settings->draw_jitter = 0.0f;
375   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
376   brush->gpencil_settings->draw_sensitivity = 1.0f;
377
378   /* Curve. */
379   custom_curve = brush->gpencil_settings->curve_sensitivity;
380   BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
381   BKE_curvemapping_initialize(custom_curve);
382   brush_gpencil_curvemap_reset(custom_curve->cm, 3, GPCURVE_PRESET_INKNOISE);
383
384   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_INKNOISE;
385   brush->gpencil_tool = GPAINT_TOOL_DRAW;
386
387   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
388   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
389
390   /* Marker Bold brush. */
391   brush = BLI_findstring(&bmain->brushes, "Marker Bold", offsetof(ID, name) + 2);
392   if (brush == NULL) {
393     brush = BKE_brush_add_gpencil(bmain, ts, "Marker Bold");
394   }
395   brush->size = 150.0f;
396   brush->gpencil_settings->flag &= ~GP_BRUSH_USE_PRESSURE;
397
398   brush->gpencil_settings->draw_strength = 0.3f;
399
400   brush->gpencil_settings->input_samples = 10;
401   brush->gpencil_settings->active_smooth = 0.6f;
402   brush->gpencil_settings->draw_angle = 0.0f;
403   brush->gpencil_settings->draw_angle_factor = 0.0f;
404   brush->gpencil_settings->gradient_f = 1.0f;
405   brush->gpencil_settings->gradient_s[0] = 1.0f;
406   brush->gpencil_settings->gradient_s[1] = 1.0f;
407
408   brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
409   brush->gpencil_settings->draw_smoothfac = 0.1f;
410   brush->gpencil_settings->draw_smoothlvl = 1;
411   brush->gpencil_settings->thick_smoothfac = 1.0f;
412   brush->gpencil_settings->thick_smoothlvl = 3;
413   brush->gpencil_settings->draw_subdivide = 0;
414   brush->gpencil_settings->draw_random_sub = 0.0f;
415   brush->gpencil_settings->simplify_f = 0.002f;
416
417   brush->gpencil_settings->flag &= ~GP_BRUSH_GROUP_RANDOM;
418   brush->gpencil_settings->draw_random_press = 0.0f;
419   brush->gpencil_settings->draw_random_strength = 0.0f;
420   brush->gpencil_settings->draw_jitter = 0.0f;
421   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
422   brush->gpencil_settings->draw_sensitivity = 1.0f;
423
424   /* Curve. */
425   custom_curve = brush->gpencil_settings->curve_sensitivity;
426   BKE_curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
427   BKE_curvemapping_initialize(custom_curve);
428   brush_gpencil_curvemap_reset(custom_curve->cm, 4, GPCURVE_PRESET_MARKER);
429
430   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_MARKER;
431   brush->gpencil_tool = GPAINT_TOOL_DRAW;
432
433   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
434   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
435
436   /* Marker Chisel brush. */
437   brush = BLI_findstring(&bmain->brushes, "Marker Chisel", offsetof(ID, name) + 2);
438   if (brush == NULL) {
439     brush = BKE_brush_add_gpencil(bmain, ts, "Marker Chisel");
440   }
441   brush->size = 80.0f;
442   brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
443
444   brush->gpencil_settings->draw_strength = 1.0f;
445
446   brush->gpencil_settings->input_samples = 10;
447   brush->gpencil_settings->active_smooth = 0.5f;
448   brush->gpencil_settings->draw_angle = DEG2RAD(20.0f);
449   brush->gpencil_settings->draw_angle_factor = 1.0f;
450   brush->gpencil_settings->gradient_f = 1.0f;
451   brush->gpencil_settings->gradient_s[0] = 1.0f;
452   brush->gpencil_settings->gradient_s[1] = 1.0f;
453
454   brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
455   brush->gpencil_settings->draw_smoothfac = 0.0f;
456   brush->gpencil_settings->draw_smoothlvl = 1;
457   brush->gpencil_settings->thick_smoothfac = 1.0f;
458   brush->gpencil_settings->thick_smoothlvl = 3;
459   brush->gpencil_settings->draw_subdivide = 0;
460   brush->gpencil_settings->draw_random_sub = 0;
461   brush->gpencil_settings->simplify_f = 0.002f;
462
463   brush->gpencil_settings->flag &= ~GP_BRUSH_GROUP_RANDOM;
464   brush->gpencil_settings->draw_random_press = 0.0f;
465   brush->gpencil_settings->draw_jitter = 0.0f;
466   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
467   brush->gpencil_settings->draw_sensitivity = 1.0f;
468
469   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_CHISEL;
470   brush->gpencil_tool = GPAINT_TOOL_DRAW;
471
472   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
473   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
474
475   /* Pen brush. */
476   brush = BLI_findstring(&bmain->brushes, "Pen", offsetof(ID, name) + 2);
477   if (brush == NULL) {
478     brush = BKE_brush_add_gpencil(bmain, ts, "Pen");
479   }
480   brush->size = 30.0f;
481   brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
482
483   brush->gpencil_settings->draw_strength = 1.0f;
484   brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
485
486   brush->gpencil_settings->input_samples = 10;
487   brush->gpencil_settings->active_smooth = 0.3f;
488   brush->gpencil_settings->draw_angle = 0.0f;
489   brush->gpencil_settings->draw_angle_factor = 0.0f;
490   brush->gpencil_settings->gradient_f = 1.0f;
491   brush->gpencil_settings->gradient_s[0] = 1.0f;
492   brush->gpencil_settings->gradient_s[1] = 1.0f;
493
494   brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
495   brush->gpencil_settings->draw_smoothfac = 0.0f;
496   brush->gpencil_settings->draw_smoothlvl = 1;
497   brush->gpencil_settings->thick_smoothfac = 1.0f;
498   brush->gpencil_settings->thick_smoothlvl = 1;
499   brush->gpencil_settings->draw_subdivide = 1;
500   brush->gpencil_settings->draw_random_sub = 0.0f;
501   brush->gpencil_settings->simplify_f = 0.002f;
502
503   brush->gpencil_settings->draw_random_press = 0.0f;
504   brush->gpencil_settings->draw_random_strength = 0.0f;
505   brush->gpencil_settings->draw_jitter = 0.0f;
506   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
507   brush->gpencil_settings->draw_sensitivity = 1.0f;
508
509   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
510   brush->gpencil_tool = GPAINT_TOOL_DRAW;
511
512   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
513   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
514
515   /* Pencil Soft brush. */
516   brush = BLI_findstring(&bmain->brushes, "Pencil Soft", offsetof(ID, name) + 2);
517   if (brush == NULL) {
518     brush = BKE_brush_add_gpencil(bmain, ts, "Pencil Soft");
519   }
520
521   brush->size = 80.0f;
522   brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
523
524   brush->gpencil_settings->draw_strength = 0.4f;
525   brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
526
527   brush->gpencil_settings->input_samples = 10;
528   brush->gpencil_settings->active_smooth = 0.64f;
529   brush->gpencil_settings->draw_angle = 0.0f;
530   brush->gpencil_settings->draw_angle_factor = 0.0f;
531   brush->gpencil_settings->gradient_f = 0.8f;
532   brush->gpencil_settings->gradient_s[0] = 1.0f;
533   brush->gpencil_settings->gradient_s[1] = 1.0f;
534
535   brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
536   brush->gpencil_settings->draw_smoothfac = 0.0f;
537   brush->gpencil_settings->draw_smoothlvl = 1;
538   brush->gpencil_settings->thick_smoothfac = 1.0f;
539   brush->gpencil_settings->thick_smoothlvl = 3;
540   brush->gpencil_settings->draw_subdivide = 0;
541   brush->gpencil_settings->draw_random_sub = 0.0f;
542   brush->gpencil_settings->simplify_f = 0.000f;
543
544   brush->gpencil_settings->draw_random_press = 0.0f;
545   brush->gpencil_settings->draw_random_strength = 0.0f;
546   brush->gpencil_settings->draw_jitter = 0.0f;
547   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
548   brush->gpencil_settings->draw_sensitivity = 1.0f;
549
550   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PENCIL;
551   brush->gpencil_tool = GPAINT_TOOL_DRAW;
552
553   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
554   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
555
556   /* Pencil brush. */
557   brush = BLI_findstring(&bmain->brushes, "Pencil", offsetof(ID, name) + 2);
558   if (brush == NULL) {
559     brush = BKE_brush_add_gpencil(bmain, ts, "Pencil");
560   }
561   deft = brush; /* save default brush. */
562
563   brush->size = 25.0f;
564   brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
565
566   brush->gpencil_settings->draw_strength = 0.6f;
567   brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
568
569   brush->gpencil_settings->input_samples = 10;
570   brush->gpencil_settings->active_smooth = 0.55f;
571   brush->gpencil_settings->draw_angle = 0.0f;
572   brush->gpencil_settings->draw_angle_factor = 0.0f;
573   brush->gpencil_settings->gradient_f = 1.0f;
574   brush->gpencil_settings->gradient_s[0] = 1.0f;
575   brush->gpencil_settings->gradient_s[1] = 1.0f;
576
577   brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
578   brush->gpencil_settings->draw_smoothfac = 0.0f;
579   brush->gpencil_settings->draw_smoothlvl = 1;
580   brush->gpencil_settings->thick_smoothfac = 1.0f;
581   brush->gpencil_settings->thick_smoothlvl = 3;
582   brush->gpencil_settings->draw_subdivide = 0;
583   brush->gpencil_settings->draw_random_sub = 0.0f;
584   brush->gpencil_settings->simplify_f = 0.002f;
585
586   brush->gpencil_settings->draw_random_press = 0.0f;
587   brush->gpencil_settings->draw_jitter = 0.0f;
588   brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
589   brush->gpencil_settings->draw_sensitivity = 1.0f;
590
591   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PENCIL;
592   brush->gpencil_tool = GPAINT_TOOL_DRAW;
593
594   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
595   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
596
597   /* Fill brush. */
598   brush = BLI_findstring(&bmain->brushes, "Fill Area", offsetof(ID, name) + 2);
599   if (brush == NULL) {
600     brush = BKE_brush_add_gpencil(bmain, ts, "Fill Area");
601   }
602   brush->size = 20.0f;
603   brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
604
605   brush->gpencil_settings->fill_leak = 3;
606   brush->gpencil_settings->fill_threshold = 0.1f;
607   brush->gpencil_settings->fill_simplylvl = 1;
608   brush->gpencil_settings->fill_factor = 1;
609
610   brush->gpencil_settings->draw_strength = 1.0f;
611   brush->gpencil_settings->gradient_f = 1.0f;
612   brush->gpencil_settings->gradient_s[0] = 1.0f;
613   brush->gpencil_settings->gradient_s[1] = 1.0f;
614   brush->gpencil_settings->draw_smoothfac = 0.1f;
615   brush->gpencil_settings->draw_smoothlvl = 1;
616   brush->gpencil_settings->thick_smoothfac = 1.0f;
617   brush->gpencil_settings->thick_smoothlvl = 3;
618   brush->gpencil_settings->draw_subdivide = 1;
619
620   brush->gpencil_settings->draw_sensitivity = 1.0f;
621
622   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_FILL;
623   brush->gpencil_tool = GPAINT_TOOL_FILL;
624
625   brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
626   brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
627
628   /* Soft Eraser brush. */
629   brush = BLI_findstring(&bmain->brushes, "Eraser Soft", offsetof(ID, name) + 2);
630   if (brush == NULL) {
631     brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Soft");
632   }
633   brush->size = 30.0f;
634   brush->gpencil_settings->draw_strength = 0.5f;
635   brush->gpencil_settings->flag |= (GP_BRUSH_ENABLE_CURSOR | GP_BRUSH_DEFAULT_ERASER);
636   brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
637   brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
638   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_SOFT;
639   brush->gpencil_tool = GPAINT_TOOL_ERASE;
640   brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_SOFT;
641   brush->gpencil_settings->era_strength_f = 100.0f;
642   brush->gpencil_settings->era_thickness_f = 10.0f;
643
644   /* Hard Eraser brush. */
645   brush = BLI_findstring(&bmain->brushes, "Eraser Hard", offsetof(ID, name) + 2);
646   if (brush == NULL) {
647     brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Hard");
648   }
649   brush->size = 30.0f;
650   brush->gpencil_settings->draw_strength = 1.0f;
651   brush->gpencil_settings->flag |= (GP_BRUSH_ENABLE_CURSOR | GP_BRUSH_DEFAULT_ERASER);
652   brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_SOFT;
653   brush->gpencil_settings->era_strength_f = 100.0f;
654   brush->gpencil_settings->era_thickness_f = 50.0f;
655
656   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_HARD;
657   brush->gpencil_tool = GPAINT_TOOL_ERASE;
658
659   /* Point Eraser brush. */
660   brush = BLI_findstring(&bmain->brushes, "Eraser Point", offsetof(ID, name) + 2);
661   if (brush == NULL) {
662     brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Point");
663   }
664   brush->size = 30.0f;
665   brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
666   brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_HARD;
667
668   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_HARD;
669   brush->gpencil_tool = GPAINT_TOOL_ERASE;
670
671   /* Stroke Eraser brush. */
672   brush = BLI_findstring(&bmain->brushes, "Eraser Stroke", offsetof(ID, name) + 2);
673   if (brush == NULL) {
674     brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Stroke");
675   }
676   brush->size = 30.0f;
677   brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
678   brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_STROKE;
679
680   brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_STROKE;
681   brush->gpencil_tool = GPAINT_TOOL_ERASE;
682
683   /* set default brush. */
684   BKE_paint_brush_set(paint, deft);
685 }
686
687 struct Brush *BKE_brush_first_search(struct Main *bmain, const eObjectMode ob_mode)
688 {
689   Brush *brush;
690
691   for (brush = bmain->brushes.first; brush; brush = brush->id.next) {
692     if (brush->ob_mode & ob_mode) {
693       return brush;
694     }
695   }
696   return NULL;
697 }
698
699 /**
700  * Only copy internal data of Brush ID from source
701  * to already allocated/initialized destination.
702  * You probably never want to use that directly,
703  * use #BKE_id_copy or #BKE_id_copy_ex for typical needs.
704  *
705  * WARNING! This function will not handle ID user count!
706  *
707  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
708  */
709 void BKE_brush_copy_data(Main *UNUSED(bmain),
710                          Brush *brush_dst,
711                          const Brush *brush_src,
712                          const int flag)
713 {
714   if (brush_src->icon_imbuf) {
715     brush_dst->icon_imbuf = IMB_dupImBuf(brush_src->icon_imbuf);
716   }
717
718   if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
719     BKE_previewimg_id_copy(&brush_dst->id, &brush_src->id);
720   }
721   else {
722     brush_dst->preview = NULL;
723   }
724
725   brush_dst->curve = BKE_curvemapping_copy(brush_src->curve);
726   if (brush_src->gpencil_settings != NULL) {
727     brush_dst->gpencil_settings = MEM_dupallocN(brush_src->gpencil_settings);
728     brush_dst->gpencil_settings->curve_sensitivity = BKE_curvemapping_copy(
729         brush_src->gpencil_settings->curve_sensitivity);
730     brush_dst->gpencil_settings->curve_strength = BKE_curvemapping_copy(
731         brush_src->gpencil_settings->curve_strength);
732     brush_dst->gpencil_settings->curve_jitter = BKE_curvemapping_copy(
733         brush_src->gpencil_settings->curve_jitter);
734   }
735
736   /* enable fake user by default */
737   id_fake_user_set(&brush_dst->id);
738 }
739
740 Brush *BKE_brush_copy(Main *bmain, const Brush *brush)
741 {
742   Brush *brush_copy;
743   BKE_id_copy(bmain, &brush->id, (ID **)&brush_copy);
744   return brush_copy;
745 }
746
747 /** Free (or release) any data used by this brush (does not free the brush itself). */
748 void BKE_brush_free(Brush *brush)
749 {
750   if (brush->icon_imbuf) {
751     IMB_freeImBuf(brush->icon_imbuf);
752   }
753   BKE_curvemapping_free(brush->curve);
754
755   if (brush->gpencil_settings != NULL) {
756     BKE_curvemapping_free(brush->gpencil_settings->curve_sensitivity);
757     BKE_curvemapping_free(brush->gpencil_settings->curve_strength);
758     BKE_curvemapping_free(brush->gpencil_settings->curve_jitter);
759     MEM_SAFE_FREE(brush->gpencil_settings);
760   }
761
762   MEM_SAFE_FREE(brush->gradient);
763
764   BKE_previewimg_free(&(brush->preview));
765 }
766
767 void BKE_brush_make_local(Main *bmain, Brush *brush, const bool lib_local)
768 {
769   bool is_local = false, is_lib = false;
770
771   /* - only lib users: do nothing (unless force_local is set)
772    * - only local users: set flag
773    * - mixed: make copy
774    */
775
776   if (!ID_IS_LINKED(brush)) {
777     return;
778   }
779
780   if (brush->clone.image) {
781     /* Special case: ima always local immediately. Clone image should only have one user anyway. */
782     id_make_local(bmain, &brush->clone.image->id, false, false);
783   }
784
785   BKE_library_ID_test_usages(bmain, brush, &is_local, &is_lib);
786
787   if (lib_local || is_local) {
788     if (!is_lib) {
789       id_clear_lib_data(bmain, &brush->id);
790       BKE_id_expand_local(bmain, &brush->id);
791
792       /* enable fake user by default */
793       id_fake_user_set(&brush->id);
794     }
795     else {
796       Brush *brush_new = BKE_brush_copy(bmain, brush); /* Ensures FAKE_USER is set */
797
798       brush_new->id.us = 0;
799
800       /* setting newid is mandatory for complex make_lib_local logic... */
801       ID_NEW_SET(brush, brush_new);
802
803       if (!lib_local) {
804         BKE_libblock_remap(bmain, brush, brush_new, ID_REMAP_SKIP_INDIRECT_USAGE);
805       }
806     }
807   }
808 }
809
810 void BKE_brush_debug_print_state(Brush *br)
811 {
812   /* create a fake brush and set it to the defaults */
813   Brush def = {{NULL}};
814   brush_defaults(&def);
815
816 #define BR_TEST(field, t) \
817   if (br->field != def.field) \
818   printf("br->" #field " = %" #t ";\n", br->field)
819
820 #define BR_TEST_FLAG(_f) \
821   if ((br->flag & _f) && !(def.flag & _f)) \
822     printf("br->flag |= " #_f ";\n"); \
823   else if (!(br->flag & _f) && (def.flag & _f)) \
824   printf("br->flag &= ~" #_f ";\n")
825
826 #define BR_TEST_FLAG_OVERLAY(_f) \
827   if ((br->overlay_flags & _f) && !(def.overlay_flags & _f)) \
828     printf("br->overlay_flags |= " #_f ";\n"); \
829   else if (!(br->overlay_flags & _f) && (def.overlay_flags & _f)) \
830   printf("br->overlay_flags &= ~" #_f ";\n")
831
832   /* print out any non-default brush state */
833   BR_TEST(normal_weight, f);
834
835   BR_TEST(blend, d);
836   BR_TEST(size, d);
837
838   /* br->flag */
839   BR_TEST_FLAG(BRUSH_AIRBRUSH);
840   BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
841   BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
842   BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
843   BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
844   BR_TEST_FLAG(BRUSH_ANCHORED);
845   BR_TEST_FLAG(BRUSH_DIR_IN);
846   BR_TEST_FLAG(BRUSH_SPACE);
847   BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
848   BR_TEST_FLAG(BRUSH_PERSISTENT);
849   BR_TEST_FLAG(BRUSH_ACCUMULATE);
850   BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
851   BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
852   BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
853   BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
854   BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
855   BR_TEST_FLAG(BRUSH_LOCK_SIZE);
856   BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
857   BR_TEST_FLAG(BRUSH_DRAG_DOT);
858   BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
859   BR_TEST_FLAG(BRUSH_PLANE_TRIM);
860   BR_TEST_FLAG(BRUSH_FRONTFACE);
861   BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
862
863   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR);
864   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY);
865   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY);
866   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE);
867   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE);
868   BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE);
869
870   BR_TEST(jitter, f);
871   BR_TEST(spacing, d);
872   BR_TEST(smooth_stroke_radius, d);
873   BR_TEST(smooth_stroke_factor, f);
874   BR_TEST(rate, f);
875
876   BR_TEST(alpha, f);
877
878   BR_TEST(sculpt_plane, d);
879
880   BR_TEST(plane_offset, f);
881
882   BR_TEST(autosmooth_factor, f);
883
884   BR_TEST(topology_rake_factor, f);
885
886   BR_TEST(crease_pinch_factor, f);
887
888   BR_TEST(plane_trim, f);
889
890   BR_TEST(texture_sample_bias, f);
891   BR_TEST(texture_overlay_alpha, d);
892
893   BR_TEST(add_col[0], f);
894   BR_TEST(add_col[1], f);
895   BR_TEST(add_col[2], f);
896   BR_TEST(sub_col[0], f);
897   BR_TEST(sub_col[1], f);
898   BR_TEST(sub_col[2], f);
899
900   printf("\n");
901
902 #undef BR_TEST
903 #undef BR_TEST_FLAG
904 }
905
906 void BKE_brush_sculpt_reset(Brush *br)
907 {
908   /* enable this to see any non-default
909    * settings used by a brush: */
910   // BKE_brush_debug_print_state(br);
911
912   brush_defaults(br);
913   BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
914
915   /* Use the curve presets by default */
916   br->curve_preset = BRUSH_CURVE_SMOOTH;
917
918   /* Note that sculpt defaults where set when 0.5 was the default (now it's 1.0)
919    * assign this so logic below can remain the same. */
920   br->alpha = 0.5f;
921
922   /* Brush settings */
923   switch (br->sculpt_tool) {
924     case SCULPT_TOOL_DRAW_SHARP:
925       br->flag |= BRUSH_DIR_IN;
926       br->curve_preset = BRUSH_CURVE_POW4;
927       br->spacing = 5;
928       break;
929     case SCULPT_TOOL_CLAY:
930       br->spacing = 6;
931       br->normal_radius_factor = 0.75f;
932       break;
933     case SCULPT_TOOL_CLAY_STRIPS:
934       br->flag |= BRUSH_ACCUMULATE | BRUSH_SIZE_PRESSURE;
935       br->flag &= ~BRUSH_SPACE_ATTEN;
936       br->alpha = 0.6f;
937       br->normal_radius_factor = 1.55f;
938       br->curve_preset = BRUSH_CURVE_SPHERE;
939       br->spacing = 6;
940       break;
941     case SCULPT_TOOL_MULTIPLANE_SCRAPE:
942       br->flag2 |= BRUSH_MULTIPLANE_SCRAPE_DYNAMIC | BRUSH_MULTIPLANE_SCRAPE_PLANES_PREVIEW;
943       br->alpha = 0.7f;
944       br->normal_radius_factor = 0.70f;
945       br->multiplane_scrape_angle = 60;
946       br->curve_preset = BRUSH_CURVE_SMOOTH;
947       br->spacing = 5;
948       break;
949     case SCULPT_TOOL_CREASE:
950       br->flag |= BRUSH_DIR_IN;
951       br->alpha = 0.25;
952       break;
953     case SCULPT_TOOL_SCRAPE:
954       br->alpha = 1.0f;
955       br->spacing = 7;
956       br->flag |= BRUSH_ACCUMULATE;
957       break;
958     case SCULPT_TOOL_ROTATE:
959       br->alpha = 1.0;
960       break;
961     case SCULPT_TOOL_SMOOTH:
962       br->flag &= ~BRUSH_SPACE_ATTEN;
963       br->spacing = 5;
964       br->alpha = 0.7f;
965       break;
966     case SCULPT_TOOL_SNAKE_HOOK:
967       br->alpha = 1.0f;
968       br->rake_factor = 1.0f;
969       break;
970     case SCULPT_TOOL_THUMB:
971       br->size = 75;
972       br->flag &= ~BRUSH_ALPHA_PRESSURE;
973       br->flag &= ~BRUSH_SPACE;
974       br->flag &= ~BRUSH_SPACE_ATTEN;
975       break;
976     case SCULPT_TOOL_ELASTIC_DEFORM:
977       br->elastic_deform_volume_preservation = 0.4f;
978       br->elastic_deform_type = BRUSH_ELASTIC_DEFORM_GRAB_TRISCALE;
979       br->flag &= ~BRUSH_ALPHA_PRESSURE;
980       br->flag &= ~BRUSH_SPACE;
981       br->flag &= ~BRUSH_SPACE_ATTEN;
982       break;
983     case SCULPT_TOOL_POSE:
984       br->pose_smooth_iterations = 4;
985       br->flag &= ~BRUSH_ALPHA_PRESSURE;
986       br->flag &= ~BRUSH_SPACE;
987       br->flag &= ~BRUSH_SPACE_ATTEN;
988       break;
989     case SCULPT_TOOL_GRAB:
990       br->alpha = 0.4f;
991       br->size = 75;
992       br->flag &= ~BRUSH_ALPHA_PRESSURE;
993       br->flag &= ~BRUSH_SPACE;
994       br->flag &= ~BRUSH_SPACE_ATTEN;
995       break;
996     default:
997       break;
998   }
999
1000   /* Cursor colors */
1001   switch (br->sculpt_tool) {
1002     case SCULPT_TOOL_DRAW:
1003     case SCULPT_TOOL_DRAW_SHARP:
1004     case SCULPT_TOOL_CLAY:
1005     case SCULPT_TOOL_CLAY_STRIPS:
1006     case SCULPT_TOOL_LAYER:
1007     case SCULPT_TOOL_INFLATE:
1008     case SCULPT_TOOL_BLOB:
1009     case SCULPT_TOOL_CREASE:
1010       br->add_col[0] = 0.5f;
1011       br->add_col[1] = 0.7f;
1012       br->add_col[2] = 0.875f;
1013       br->sub_col[0] = 0.5f;
1014       br->sub_col[1] = 0.7f;
1015       br->sub_col[2] = 0.875f;
1016       break;
1017
1018     case SCULPT_TOOL_SMOOTH:
1019     case SCULPT_TOOL_FLATTEN:
1020     case SCULPT_TOOL_FILL:
1021     case SCULPT_TOOL_SCRAPE:
1022     case SCULPT_TOOL_MULTIPLANE_SCRAPE:
1023       br->add_col[0] = 1.0f;
1024       br->add_col[1] = 0.39f;
1025       br->add_col[2] = 0.39f;
1026       br->sub_col[0] = 1.0f;
1027       br->sub_col[1] = 0.39f;
1028       br->sub_col[2] = 0.39f;
1029       break;
1030
1031     case SCULPT_TOOL_PINCH:
1032     case SCULPT_TOOL_GRAB:
1033     case SCULPT_TOOL_SNAKE_HOOK:
1034     case SCULPT_TOOL_THUMB:
1035     case SCULPT_TOOL_NUDGE:
1036     case SCULPT_TOOL_ROTATE:
1037     case SCULPT_TOOL_ELASTIC_DEFORM:
1038     case SCULPT_TOOL_POSE:
1039       br->add_col[0] = 1.0f;
1040       br->add_col[1] = 1.0f;
1041       br->add_col[2] = 0.39f;
1042       br->sub_col[0] = 1.0f;
1043       br->sub_col[1] = 1.0f;
1044       br->sub_col[2] = 0.39f;
1045       break;
1046
1047     case SCULPT_TOOL_SIMPLIFY:
1048     case SCULPT_TOOL_MASK:
1049       br->add_col[0] = 0.750000;
1050       br->add_col[1] = 0.750000;
1051       br->add_col[2] = 0.750000;
1052       br->sub_col[0] = 0.750000;
1053       br->sub_col[1] = 0.750000;
1054       br->sub_col[2] = 0.750000;
1055       break;
1056     default:
1057       break;
1058   }
1059 }
1060
1061 /**
1062  * Library Operations
1063  */
1064 void BKE_brush_curve_preset(Brush *b, eCurveMappingPreset preset)
1065 {
1066   CurveMap *cm = NULL;
1067
1068   if (!b->curve) {
1069     b->curve = BKE_curvemapping_add(1, 0, 0, 1, 1);
1070   }
1071
1072   cm = b->curve->cm;
1073   cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1074
1075   b->curve->preset = preset;
1076   BKE_curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
1077   BKE_curvemapping_changed(b->curve, false);
1078 }
1079
1080 /* Generic texture sampler for 3D painting systems. point has to be either in
1081  * region space mouse coordinates, or 3d world coordinates for 3D mapping.
1082  *
1083  * rgba outputs straight alpha. */
1084 float BKE_brush_sample_tex_3d(const Scene *scene,
1085                               const Brush *br,
1086                               const float point[3],
1087                               float rgba[4],
1088                               const int thread,
1089                               struct ImagePool *pool)
1090 {
1091   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1092   const MTex *mtex = &br->mtex;
1093   float intensity = 1.0;
1094   bool hasrgb = false;
1095
1096   if (!mtex->tex) {
1097     intensity = 1;
1098   }
1099   else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
1100     /* Get strength by feeding the vertex
1101      * location directly into a texture */
1102     hasrgb = externtex(
1103         mtex, point, &intensity, rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1104   }
1105   else if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
1106     float rotation = -mtex->rot;
1107     float point_2d[2] = {point[0], point[1]};
1108     float x, y;
1109     float co[3];
1110
1111     x = point_2d[0] - br->stencil_pos[0];
1112     y = point_2d[1] - br->stencil_pos[1];
1113
1114     if (rotation > 0.001f || rotation < -0.001f) {
1115       const float angle = atan2f(y, x) + rotation;
1116       const float flen = sqrtf(x * x + y * y);
1117
1118       x = flen * cosf(angle);
1119       y = flen * sinf(angle);
1120     }
1121
1122     if (fabsf(x) > br->stencil_dimension[0] || fabsf(y) > br->stencil_dimension[1]) {
1123       zero_v4(rgba);
1124       return 0.0f;
1125     }
1126     x /= (br->stencil_dimension[0]);
1127     y /= (br->stencil_dimension[1]);
1128
1129     co[0] = x;
1130     co[1] = y;
1131     co[2] = 0.0f;
1132
1133     hasrgb = externtex(
1134         mtex, co, &intensity, rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1135   }
1136   else {
1137     float rotation = -mtex->rot;
1138     float point_2d[2] = {point[0], point[1]};
1139     float x = 0.0f, y = 0.0f; /* Quite warnings */
1140     float invradius = 1.0f;   /* Quite warnings */
1141     float co[3];
1142
1143     if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
1144       /* keep coordinates relative to mouse */
1145
1146       rotation += ups->brush_rotation;
1147
1148       x = point_2d[0] - ups->tex_mouse[0];
1149       y = point_2d[1] - ups->tex_mouse[1];
1150
1151       /* use pressure adjusted size for fixed mode */
1152       invradius = 1.0f / ups->pixel_radius;
1153     }
1154     else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
1155       /* leave the coordinates relative to the screen */
1156
1157       /* use unadjusted size for tiled mode */
1158       invradius = 1.0f / BKE_brush_size_get(scene, br);
1159
1160       x = point_2d[0];
1161       y = point_2d[1];
1162     }
1163     else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
1164       rotation += ups->brush_rotation;
1165       /* these contain a random coordinate */
1166       x = point_2d[0] - ups->tex_mouse[0];
1167       y = point_2d[1] - ups->tex_mouse[1];
1168
1169       invradius = 1.0f / ups->pixel_radius;
1170     }
1171
1172     x *= invradius;
1173     y *= invradius;
1174
1175     /* it is probably worth optimizing for those cases where
1176      * the texture is not rotated by skipping the calls to
1177      * atan2, sqrtf, sin, and cos. */
1178     if (rotation > 0.001f || rotation < -0.001f) {
1179       const float angle = atan2f(y, x) + rotation;
1180       const float flen = sqrtf(x * x + y * y);
1181
1182       x = flen * cosf(angle);
1183       y = flen * sinf(angle);
1184     }
1185
1186     co[0] = x;
1187     co[1] = y;
1188     co[2] = 0.0f;
1189
1190     hasrgb = externtex(
1191         mtex, co, &intensity, rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1192   }
1193
1194   intensity += br->texture_sample_bias;
1195
1196   if (!hasrgb) {
1197     rgba[0] = intensity;
1198     rgba[1] = intensity;
1199     rgba[2] = intensity;
1200     rgba[3] = 1.0f;
1201   }
1202   /* For consistency, sampling always returns color in linear space */
1203   else if (ups->do_linear_conversion) {
1204     IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, ups->colorspace);
1205   }
1206
1207   return intensity;
1208 }
1209
1210 float BKE_brush_sample_masktex(
1211     const Scene *scene, Brush *br, const float point[2], const int thread, struct ImagePool *pool)
1212 {
1213   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1214   MTex *mtex = &br->mask_mtex;
1215   float rgba[4], intensity;
1216
1217   if (!mtex->tex) {
1218     return 1.0f;
1219   }
1220   if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
1221     float rotation = -mtex->rot;
1222     float point_2d[2] = {point[0], point[1]};
1223     float x, y;
1224     float co[3];
1225
1226     x = point_2d[0] - br->mask_stencil_pos[0];
1227     y = point_2d[1] - br->mask_stencil_pos[1];
1228
1229     if (rotation > 0.001f || rotation < -0.001f) {
1230       const float angle = atan2f(y, x) + rotation;
1231       const float flen = sqrtf(x * x + y * y);
1232
1233       x = flen * cosf(angle);
1234       y = flen * sinf(angle);
1235     }
1236
1237     if (fabsf(x) > br->mask_stencil_dimension[0] || fabsf(y) > br->mask_stencil_dimension[1]) {
1238       zero_v4(rgba);
1239       return 0.0f;
1240     }
1241     x /= (br->mask_stencil_dimension[0]);
1242     y /= (br->mask_stencil_dimension[1]);
1243
1244     co[0] = x;
1245     co[1] = y;
1246     co[2] = 0.0f;
1247
1248     externtex(
1249         mtex, co, &intensity, rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1250   }
1251   else {
1252     float rotation = -mtex->rot;
1253     float point_2d[2] = {point[0], point[1]};
1254     float x = 0.0f, y = 0.0f; /* Quite warnings */
1255     float invradius = 1.0f;   /* Quite warnings */
1256     float co[3];
1257
1258     if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
1259       /* keep coordinates relative to mouse */
1260
1261       rotation += ups->brush_rotation_sec;
1262
1263       x = point_2d[0] - ups->mask_tex_mouse[0];
1264       y = point_2d[1] - ups->mask_tex_mouse[1];
1265
1266       /* use pressure adjusted size for fixed mode */
1267       invradius = 1.0f / ups->pixel_radius;
1268     }
1269     else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
1270       /* leave the coordinates relative to the screen */
1271
1272       /* use unadjusted size for tiled mode */
1273       invradius = 1.0f / BKE_brush_size_get(scene, br);
1274
1275       x = point_2d[0];
1276       y = point_2d[1];
1277     }
1278     else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
1279       rotation += ups->brush_rotation_sec;
1280       /* these contain a random coordinate */
1281       x = point_2d[0] - ups->mask_tex_mouse[0];
1282       y = point_2d[1] - ups->mask_tex_mouse[1];
1283
1284       invradius = 1.0f / ups->pixel_radius;
1285     }
1286
1287     x *= invradius;
1288     y *= invradius;
1289
1290     /* it is probably worth optimizing for those cases where
1291      * the texture is not rotated by skipping the calls to
1292      * atan2, sqrtf, sin, and cos. */
1293     if (rotation > 0.001f || rotation < -0.001f) {
1294       const float angle = atan2f(y, x) + rotation;
1295       const float flen = sqrtf(x * x + y * y);
1296
1297       x = flen * cosf(angle);
1298       y = flen * sinf(angle);
1299     }
1300
1301     co[0] = x;
1302     co[1] = y;
1303     co[2] = 0.0f;
1304
1305     externtex(
1306         mtex, co, &intensity, rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1307   }
1308
1309   CLAMP(intensity, 0.0f, 1.0f);
1310
1311   switch (br->mask_pressure) {
1312     case BRUSH_MASK_PRESSURE_CUTOFF:
1313       intensity = ((1.0f - intensity) < ups->size_pressure_value) ? 1.0f : 0.0f;
1314       break;
1315     case BRUSH_MASK_PRESSURE_RAMP:
1316       intensity = ups->size_pressure_value + intensity * (1.0f - ups->size_pressure_value);
1317       break;
1318     default:
1319       break;
1320   }
1321
1322   return intensity;
1323 }
1324
1325 /* Unified Size / Strength / Color */
1326
1327 /* XXX: be careful about setting size and unprojected radius
1328  * because they depend on one another
1329  * these functions do not set the other corresponding value
1330  * this can lead to odd behavior if size and unprojected
1331  * radius become inconsistent.
1332  * the biggest problem is that it isn't possible to change
1333  * unprojected radius because a view context is not
1334  * available.  my usual solution to this is to use the
1335  * ratio of change of the size to change the unprojected
1336  * radius.  Not completely convinced that is correct.
1337  * In any case, a better solution is needed to prevent
1338  * inconsistency. */
1339
1340 const float *BKE_brush_color_get(const struct Scene *scene, const struct Brush *brush)
1341 {
1342   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1343   return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->rgb : brush->rgb;
1344 }
1345
1346 const float *BKE_brush_secondary_color_get(const struct Scene *scene, const struct Brush *brush)
1347 {
1348   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1349   return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->secondary_rgb : brush->secondary_rgb;
1350 }
1351
1352 void BKE_brush_color_set(struct Scene *scene, struct Brush *brush, const float color[3])
1353 {
1354   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1355
1356   if (ups->flag & UNIFIED_PAINT_COLOR) {
1357     copy_v3_v3(ups->rgb, color);
1358   }
1359   else {
1360     copy_v3_v3(brush->rgb, color);
1361   }
1362 }
1363
1364 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
1365 {
1366   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1367
1368   /* make sure range is sane */
1369   CLAMP(size, 1, MAX_BRUSH_PIXEL_RADIUS);
1370
1371   if (ups->flag & UNIFIED_PAINT_SIZE) {
1372     ups->size = size;
1373   }
1374   else {
1375     brush->size = size;
1376   }
1377 }
1378
1379 int BKE_brush_size_get(const Scene *scene, const Brush *brush)
1380 {
1381   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1382   int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
1383
1384   return size;
1385 }
1386
1387 bool BKE_brush_use_locked_size(const Scene *scene, const Brush *brush)
1388 {
1389   const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1390
1391   return (us_flag & UNIFIED_PAINT_SIZE) ? (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
1392                                           (brush->flag & BRUSH_LOCK_SIZE);
1393 }
1394
1395 bool BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush)
1396 {
1397   const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1398
1399   return (us_flag & UNIFIED_PAINT_SIZE) ? (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
1400                                           (brush->flag & BRUSH_SIZE_PRESSURE);
1401 }
1402
1403 bool BKE_brush_use_alpha_pressure(const Scene *scene, const Brush *brush)
1404 {
1405   const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1406
1407   return (us_flag & UNIFIED_PAINT_ALPHA) ? (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
1408                                            (brush->flag & BRUSH_ALPHA_PRESSURE);
1409 }
1410
1411 bool BKE_brush_sculpt_has_secondary_color(const Brush *brush)
1412 {
1413   return ELEM(brush->sculpt_tool,
1414               SCULPT_TOOL_BLOB,
1415               SCULPT_TOOL_DRAW,
1416               SCULPT_TOOL_DRAW_SHARP,
1417               SCULPT_TOOL_INFLATE,
1418               SCULPT_TOOL_CLAY,
1419               SCULPT_TOOL_CLAY_STRIPS,
1420               SCULPT_TOOL_PINCH,
1421               SCULPT_TOOL_CREASE,
1422               SCULPT_TOOL_LAYER,
1423               SCULPT_TOOL_FLATTEN,
1424               SCULPT_TOOL_FILL,
1425               SCULPT_TOOL_SCRAPE,
1426               SCULPT_TOOL_MASK);
1427 }
1428
1429 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
1430 {
1431   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1432
1433   if (ups->flag & UNIFIED_PAINT_SIZE) {
1434     ups->unprojected_radius = unprojected_radius;
1435   }
1436   else {
1437     brush->unprojected_radius = unprojected_radius;
1438   }
1439 }
1440
1441 float BKE_brush_unprojected_radius_get(const Scene *scene, const Brush *brush)
1442 {
1443   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1444
1445   return (ups->flag & UNIFIED_PAINT_SIZE) ? ups->unprojected_radius : brush->unprojected_radius;
1446 }
1447
1448 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
1449 {
1450   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1451
1452   if (ups->flag & UNIFIED_PAINT_ALPHA) {
1453     ups->alpha = alpha;
1454   }
1455   else {
1456     brush->alpha = alpha;
1457   }
1458 }
1459
1460 float BKE_brush_alpha_get(const Scene *scene, const Brush *brush)
1461 {
1462   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1463
1464   return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
1465 }
1466
1467 float BKE_brush_weight_get(const Scene *scene, const Brush *brush)
1468 {
1469   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1470
1471   return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
1472 }
1473
1474 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
1475 {
1476   UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1477
1478   if (ups->flag & UNIFIED_PAINT_WEIGHT) {
1479     ups->weight = value;
1480   }
1481   else {
1482     brush->weight = value;
1483   }
1484 }
1485
1486 /* scale unprojected radius to reflect a change in the brush's 2D size */
1487 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
1488                                         int new_brush_size,
1489                                         int old_brush_size)
1490 {
1491   float scale = new_brush_size;
1492   /* avoid division by zero */
1493   if (old_brush_size != 0) {
1494     scale /= (float)old_brush_size;
1495   }
1496   (*unprojected_radius) *= scale;
1497 }
1498
1499 /* scale brush size to reflect a change in the brush's unprojected radius */
1500 void BKE_brush_scale_size(int *r_brush_size,
1501                           float new_unprojected_radius,
1502                           float old_unprojected_radius)
1503 {
1504   float scale = new_unprojected_radius;
1505   /* avoid division by zero */
1506   if (old_unprojected_radius != 0) {
1507     scale /= new_unprojected_radius;
1508   }
1509   (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
1510 }
1511
1512 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
1513 {
1514   float rand_pos[2];
1515   float spread;
1516   int diameter;
1517
1518   do {
1519     rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
1520     rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
1521   } while (len_squared_v2(rand_pos) > SQUARE(0.5f));
1522
1523   if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
1524     diameter = 2 * brush->jitter_absolute;
1525     spread = 1.0;
1526   }
1527   else {
1528     diameter = 2 * BKE_brush_size_get(scene, brush);
1529     spread = brush->jitter;
1530   }
1531   /* find random position within a circle of diameter 1 */
1532   jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
1533   jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
1534 }
1535
1536 void BKE_brush_randomize_texture_coords(UnifiedPaintSettings *ups, bool mask)
1537 {
1538   /* we multiply with brush radius as an optimization for the brush
1539    * texture sampling functions */
1540   if (mask) {
1541     ups->mask_tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1542     ups->mask_tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1543   }
1544   else {
1545     ups->tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1546     ups->tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1547   }
1548 }
1549
1550 /* Uses the brush curve control to find a strength value */
1551 float BKE_brush_curve_strength(const Brush *br, float p, const float len)
1552 {
1553   float strength = 1.0f;
1554
1555   if (p >= len) {
1556     return 0;
1557   }
1558   else {
1559     p = p / len;
1560     p = 1.0f - p;
1561   }
1562
1563   switch (br->curve_preset) {
1564     case BRUSH_CURVE_CUSTOM:
1565       strength = BKE_curvemapping_evaluateF(br->curve, 0, 1.0f - p);
1566       break;
1567     case BRUSH_CURVE_SHARP:
1568       strength = p * p;
1569       break;
1570     case BRUSH_CURVE_SMOOTH:
1571       strength = 3.0f * p * p - 2.0f * p * p * p;
1572       break;
1573     case BRUSH_CURVE_ROOT:
1574       strength = sqrtf(p);
1575       break;
1576     case BRUSH_CURVE_LIN:
1577       strength = p;
1578       break;
1579     case BRUSH_CURVE_CONSTANT:
1580       strength = 1.0f;
1581       break;
1582     case BRUSH_CURVE_SPHERE:
1583       strength = sqrtf(2 * p - p * p);
1584       break;
1585     case BRUSH_CURVE_POW4:
1586       strength = p * p * p * p;
1587       break;
1588     case BRUSH_CURVE_INVSQUARE:
1589       strength = p * (2.0f - p);
1590       break;
1591   }
1592
1593   return strength;
1594 }
1595
1596 /* Uses the brush curve control to find a strength value between 0 and 1 */
1597 float BKE_brush_curve_strength_clamped(Brush *br, float p, const float len)
1598 {
1599   float strength = BKE_brush_curve_strength(br, p, len);
1600
1601   CLAMP(strength, 0.0f, 1.0f);
1602
1603   return strength;
1604 }
1605
1606 /* TODO: should probably be unified with BrushPainter stuff? */
1607 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side, bool use_secondary)
1608 {
1609   unsigned int *texcache = NULL;
1610   MTex *mtex = (use_secondary) ? &br->mask_mtex : &br->mtex;
1611   float intensity;
1612   float rgba[4];
1613   int ix, iy;
1614   int side = half_side * 2;
1615
1616   if (mtex->tex) {
1617     float x, y, step = 2.0 / side, co[3];
1618
1619     texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1620
1621     /* do normalized canonical view coords for texture */
1622     for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
1623       for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
1624         co[0] = x;
1625         co[1] = y;
1626         co[2] = 0.0f;
1627
1628         /* This is copied from displace modifier code */
1629         /* TODO(sergey): brush are always caching with CM enabled for now. */
1630         externtex(mtex, co, &intensity, rgba, rgba + 1, rgba + 2, rgba + 3, 0, NULL, false, false);
1631
1632         ((char *)texcache)[(iy * side + ix) * 4] = ((char *)texcache)[(iy * side + ix) * 4 + 1] =
1633             ((char *)texcache)[(iy * side + ix) * 4 + 2] = ((
1634                 char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(intensity * 255.0f);
1635       }
1636     }
1637   }
1638
1639   return texcache;
1640 }
1641
1642 /**** Radial Control ****/
1643 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary, bool display_gradient)
1644 {
1645   ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1646   unsigned int *texcache;
1647   int side = 512;
1648   int half = side / 2;
1649   int i, j;
1650
1651   BKE_curvemapping_initialize(br->curve);
1652   texcache = BKE_brush_gen_texture_cache(br, half, secondary);
1653   im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1654   im->x = im->y = side;
1655
1656   if (display_gradient || texcache) {
1657     for (i = 0; i < side; i++) {
1658       for (j = 0; j < side; j++) {
1659         float magn = sqrtf(pow2f(i - half) + pow2f(j - half));
1660         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamped(br, magn, half);
1661       }
1662     }
1663   }
1664
1665   if (texcache) {
1666     /* Modulate curve with texture */
1667     for (i = 0; i < side; i++) {
1668       for (j = 0; j < side; j++) {
1669         const int col = texcache[i * side + j];
1670         im->rect_float[i * side + j] *= (((char *)&col)[0] + ((char *)&col)[1] +
1671                                          ((char *)&col)[2]) /
1672                                         3.0f / 255.0f;
1673       }
1674     }
1675     MEM_freeN(texcache);
1676   }
1677
1678   return im;
1679 }