Gooseberry request: Dithering support for byte images when painting on

projection painting (2D will be separate commit).
This commit is contained in:
Antony Riakiotakis 2015-01-29 19:23:45 +01:00
parent 12a38abac6
commit ebc064f5c0
7 changed files with 39 additions and 15 deletions

View File

@ -1722,6 +1722,7 @@ class VIEW3D_PT_tools_projectpaint(View3DPaintPanel, Panel):
sub.prop(ipaint, "normal_angle", text="")
layout.prop(ipaint, "seam_bleed")
layout.prop(ipaint, "dither")
self.unified_paint_settings(layout, context)

View File

@ -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)

View File

@ -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])

View File

@ -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;
}

View File

@ -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 *************************/

View File

@ -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;
/* ------------------------------------------- */

View File

@ -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);