diff options
author | Antony Riakiotakis <kalast@gmail.com> | 2015-01-29 21:23:45 +0300 |
---|---|---|
committer | Antony Riakiotakis <kalast@gmail.com> | 2015-01-29 21:23:57 +0300 |
commit | ebc064f5c0c9bc5a5f67c82ea564bf170d135bd0 (patch) | |
tree | 10d09df96b5d5bc0b0c958ae328cdddd19e6f4a7 /source | |
parent | 12a38abac6696992bc383a7745c4e15b316936d1 (diff) |
Gooseberry request: Dithering support for byte images when painting on
projection painting (2D will be separate commit).
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_math_color.h | 4 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_color_inline.c | 20 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_image_proj.c | 12 | ||||
-rw-r--r-- | source/blender/imbuf/intern/divers.c | 10 | ||||
-rw-r--r-- | source/blender/makesdna/DNA_scene_types.h | 2 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_sculpt_paint.c | 5 |
6 files changed, 38 insertions, 15 deletions
diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index 5c14ac55492..560ebe3d390 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -141,6 +141,10 @@ void xyz_to_lab(float x, float y, float z, float *l, float *a, float *b); MINLINE int compare_rgb_uchar(const unsigned char a[3], const unsigned char b[3], const int limit); +MINLINE float dither_random_value(float s, float t); +MINLINE void float_to_byte_dither_v3(unsigned char b[3], const float f[3], float dither, float s, float t); + + #define rgba_char_args_set_fl(col, r, g, b, a) \ rgba_char_args_set(col, (r) * 255, (g) * 255, (b) * 255, (a) * 255) diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index 9233749d5df..dc62d04ad55 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -31,6 +31,8 @@ #include "BLI_math_color.h" #include "BLI_utildefines.h" +#include "math.h" + #ifndef __MATH_COLOR_INLINE_C__ #define __MATH_COLOR_INLINE_C__ @@ -269,6 +271,24 @@ MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const unsigned char return 0; } +MINLINE float dither_random_value(float s, float t) +{ + static float vec[2] = {12.9898f, 78.233f}; + float value; + + value = sinf(s * vec[0] + t * vec[1]) * 43758.5453f; + return value - floorf(value); +} + +MINLINE void float_to_byte_dither_v3(unsigned char b[3], const float f[3], float dither, float s, float t) +{ + float dither_value = dither_random_value(s, t) * 0.005f * dither; + + b[0] = FTOCHAR(dither_value + f[0]); + b[1] = FTOCHAR(dither_value + f[1]); + b[2] = FTOCHAR(dither_value + f[2]); +} + /**************** Alpha Transformations *****************/ MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4]) diff --git a/source/blender/editors/sculpt_paint/paint_image_proj.c b/source/blender/editors/sculpt_paint/paint_image_proj.c index cbfa53d2660..6bc597fa175 100644 --- a/source/blender/editors/sculpt_paint/paint_image_proj.c +++ b/source/blender/editors/sculpt_paint/paint_image_proj.c @@ -203,6 +203,7 @@ typedef struct ProjPaintState { /* the paint color. It can change depending of inverted mode or not */ float paint_color[3]; float paint_color_linear[3]; + float dither; Brush *brush; short tool, blend, mode; @@ -4156,7 +4157,7 @@ static void do_projectpaint_soften(ProjPaintState *ps, ProjPixel *projPixel, flo } } -static void do_projectpaint_draw(ProjPaintState *ps, ProjPixel *projPixel, const float texrgb[3], float mask) +static void do_projectpaint_draw(ProjPaintState *ps, ProjPixel *projPixel, const float texrgb[3], float mask, float dither, float u, float v) { float rgb[3]; unsigned char rgba_ub[4]; @@ -4170,7 +4171,7 @@ static void do_projectpaint_draw(ProjPaintState *ps, ProjPixel *projPixel, const copy_v3_v3(rgb, ps->paint_color); } - rgb_float_to_uchar(rgba_ub, rgb); + float_to_byte_dither_v3(rgba_ub, rgb, dither, u, v); rgba_ub[3] = f_to_char(mask); if (ps->do_masking) { @@ -4351,7 +4352,8 @@ static void *do_projectpaint_thread(void *ph_v) } else { linearrgb_to_srgb_v3_v3(color_f, color_f); - rgba_float_to_uchar(projPixel->newColor.ch, color_f); + float_to_byte_dither_v3(projPixel->newColor.ch, color_f, ps->dither, projPixel->x_px, projPixel->y_px); + projPixel->newColor.ch[3] = FTOCHAR(color_f[3]); IMB_blend_color_byte(projPixel->pixel.ch_pt, projPixel->origColor.ch_pt, projPixel->newColor.ch, ps->blend); } @@ -4547,7 +4549,7 @@ static void *do_projectpaint_thread(void *ph_v) break; default: if (is_floatbuf) do_projectpaint_draw_f(ps, projPixel, texrgb, mask); - else do_projectpaint_draw(ps, projPixel, texrgb, mask); + else do_projectpaint_draw(ps, projPixel, texrgb, mask, ps->dither, projPixel->x_px, projPixel->y_px); break; } } @@ -4851,6 +4853,8 @@ static void project_state_init(bContext *C, Object *ob, ProjPaintState *ps, int if (ps->normal_angle_range <= 0.0f) ps->do_mask_normal = false; /* no need to do blending */ + ps->dither = settings->imapaint.dither; + return; } diff --git a/source/blender/imbuf/intern/divers.c b/source/blender/imbuf/intern/divers.c index 81aef4ac6e4..59d08128e5f 100644 --- a/source/blender/imbuf/intern/divers.c +++ b/source/blender/imbuf/intern/divers.c @@ -122,16 +122,6 @@ static void clear_dither_context(DitherContext *di) MEM_freeN(di); } -MINLINE float dither_random_value(float s, float t) -{ - static float vec[2] = {12.9898f, 78.233f}; - float st[2]; - float value; - copy_v2_fl2(st, s, t); - - value = sinf(dot_v2v2(st, vec)) * 43758.5453f; - return value - floorf(value); -} /************************* Generic Buffer Conversion *************************/ diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h index 8acfd233130..fabfa7653f2 100644 --- a/source/blender/makesdna/DNA_scene_types.h +++ b/source/blender/makesdna/DNA_scene_types.h @@ -849,7 +849,7 @@ typedef struct ImagePaintSettings { struct Image *clone; /* clone layer for image mode for projective texture painting */ struct Image *canvas; /* canvas when the explicit system is used for painting */ float stencil_col[3]; - float pad1; + float dither; /* dither amount used when painting on byte images */ } ImagePaintSettings; /* ------------------------------------------- */ diff --git a/source/blender/makesrna/intern/rna_sculpt_paint.c b/source/blender/makesrna/intern/rna_sculpt_paint.c index 3b19e450f3b..0a282eeb7ac 100644 --- a/source/blender/makesrna/intern/rna_sculpt_paint.c +++ b/source/blender/makesrna/intern/rna_sculpt_paint.c @@ -709,6 +709,11 @@ static void rna_def_image_paint(BlenderRNA *brna) RNA_def_property_float_sdna(prop, NULL, "stencil_col"); RNA_def_property_ui_text(prop, "Stencil Color", "Stencil color in the viewport"); RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, "rna_ImaPaint_viewport_update"); + + prop = RNA_def_property(srna, "dither", PROP_FLOAT, PROP_NONE); + RNA_def_property_range(prop, 0.0, 2.0); + RNA_def_property_ui_text(prop, "Dither", "Amount of dithering when painting on byte images"); + RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL); prop = RNA_def_property(srna, "use_clone_layer", PROP_BOOLEAN, PROP_NONE); RNA_def_property_boolean_sdna(prop, NULL, "flag", IMAGEPAINT_PROJECT_LAYER_CLONE); |