use color conversions functions in more places.

also add rgba_float_to_uchar, rgba_uchar_to_float
This commit is contained in:
2012-01-19 10:04:51 +00:00
parent 47514a0d71
commit a87c5eb52c
13 changed files with 72 additions and 85 deletions

View File

@@ -186,7 +186,7 @@ void BKE_image_merge(struct Image *dest, struct Image *source);
int BKE_image_has_alpha(struct Image *image);
/* image_gen.c */
void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, float color[4]);
void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, const float color[4]);
void BKE_image_buf_fill_checker(unsigned char *rect, float *rect_float, int height, int width);
void BKE_image_buf_fill_checker_color(unsigned char *rect, float *rect_float, int height, int width);

View File

@@ -527,7 +527,7 @@ void brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall,
float xy[2], rgba[4], *dstf;
int x, y, rowbytes, xoff, yoff, imbflag;
const int radius= brush_size(scene, brush);
char *dst, crgb[3];
unsigned char *dst, crgb[3];
const float alpha= brush_alpha(scene, brush);
float brush_rgb[3];
@@ -571,10 +571,10 @@ void brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall,
}
else {
float alpha_f; /* final float alpha to convert to char */
F3TOCHAR3(brush->rgb, crgb);
rgb_float_to_uchar(crgb, brush->rgb);
for (y=0; y < ibuf->y; y++) {
dst = (char*)ibuf->rect + y*rowbytes;
dst = (unsigned char *)ibuf->rect + y*rowbytes;
for (x=0; x < ibuf->x; x++, dst+=4) {
xy[0] = x + xoff;
@@ -590,19 +590,15 @@ void brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall,
}
else if (texfall == 1) {
brush_sample_tex(scene, brush, xy, rgba, 0);
dst[0] = FTOCHAR(rgba[0]);
dst[1] = FTOCHAR(rgba[1]);
dst[2] = FTOCHAR(rgba[2]);
dst[3] = FTOCHAR(rgba[3]);
rgba_float_to_uchar(dst, rgba);
}
else if (texfall == 2) {
brush_sample_tex(scene, brush, xy, rgba, 0);
mul_v3_v3(rgba, brush->rgb);
alpha_f = rgba[3] * alpha * brush_curve_strength_clamp(brush, len_v2(xy), radius);
dst[0] = FTOCHAR(rgba[0]);
dst[1] = FTOCHAR(rgba[1]);
dst[2] = FTOCHAR(rgba[2]);
rgb_float_to_uchar(dst, rgba);
dst[3] = FTOCHAR(alpha_f);
}
else {
@@ -843,7 +839,7 @@ static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, i
Brush *brush= painter->brush;
ImBuf *ibuf, *maskibuf, *texibuf;
float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
char *b, *m, *t, *ot= NULL;
unsigned char *b, *m, *t, *ot= NULL;
int dotexold, origx= x, origy= y;
const int radius= brush_size(painter->scene, brush);
@@ -895,12 +891,12 @@ static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, i
}
else {
for (; y < h; y++) {
b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
b = (unsigned char *)ibuf->rect + (y*ibuf->x + origx)*4;
t = (unsigned char *)texibuf->rect + (y*texibuf->x + origx)*4;
m = (unsigned char *)maskibuf->rect + (y*maskibuf->x + origx)*4;
if (dotexold)
ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
ot = (unsigned char *)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
if (dotexold) {
@@ -915,10 +911,7 @@ static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, i
xy[1] = y + yoff;
brush_sample_tex(scene, brush, xy, rgba, 0);
t[0]= FTOCHAR(rgba[0]);
t[1]= FTOCHAR(rgba[1]);
t[2]= FTOCHAR(rgba[2]);
t[3]= FTOCHAR(rgba[3]);
rgba_float_to_uchar(t, rgba);
}
b[0] = t[0]*m[0]/255;

View File

@@ -33,7 +33,7 @@
#include "BLI_math_base.h"
#include "BLF_api.h"
void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, float color[4])
void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, const float color[4])
{
int x, y;
@@ -41,22 +41,17 @@ void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width,
if(rect_float) {
for(y= 0; y<height; y++) {
for(x= 0; x<width; x++) {
rect_float[0]= color[0];
rect_float[1]= color[1];
rect_float[2]= color[2];
rect_float[3]= color[3];
copy_v4_v4(rect_float, color);
rect_float+= 4;
}
}
}
if(rect) {
char ccol[4];
unsigned char ccol[4];
rgba_float_to_uchar(ccol, color);
ccol[0]= (char)(color[0]*255.0f);
ccol[1]= (char)(color[1]*255.0f);
ccol[2]= (char)(color[2]*255.0f);
ccol[3]= (char)(color[3]*255.0f);
for(y= 0; y<height; y++) {
for(x= 0; x<width; x++) {

View File

@@ -793,10 +793,7 @@ void nodeAddToPreview(bNode *node, float *col, int x, int y, int do_manage)
linearrgb_to_srgb_uchar4(tar, col);
}
else {
tar[0]= FTOCHAR(col[0]);
tar[1]= FTOCHAR(col[1]);
tar[2]= FTOCHAR(col[2]);
tar[3]= FTOCHAR(col[3]);
rgba_float_to_uchar(tar, col);
}
}
//else printf("prv out bound x y %d %d\n", x, y);

View File

@@ -102,7 +102,9 @@ void rgb_float_set_hue_float_offset(float * rgb, float hue_offset);
void rgb_byte_set_hue_float_offset(unsigned char * rgb, float hue_offset);
void rgb_uchar_to_float(float col_r[3], const unsigned char col_ub[3]);
void rgba_uchar_to_float(float col_r[4], const unsigned char col_ub[4]);
void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3]);
void rgba_float_to_uchar(unsigned char col_r[4], const float col_f[4]);
/***************** lift/gamma/gain / ASC-CDL conversion *****************/

View File

@@ -344,6 +344,14 @@ void rgb_uchar_to_float(float col_r[3], const unsigned char col_ub[3])
col_r[2]= ((float)col_ub[2]) / 255.0f;
}
void rgba_uchar_to_float(float col_r[4], const unsigned char col_ub[4])
{
col_r[0]= ((float)col_ub[0]) / 255.0f;
col_r[1]= ((float)col_ub[1]) / 255.0f;
col_r[2]= ((float)col_ub[2]) / 255.0f;
col_r[3]= ((float)col_ub[3]) / 255.0f;
}
void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3])
{
int r, g, b;
@@ -357,6 +365,21 @@ void rgb_float_to_uchar(unsigned char col_r[3], const float col_f[3])
col_r[2]= (char)((b <= 0)? 0 : (b >= 255)? 255 : b);
}
void rgba_float_to_uchar(unsigned char col_r[4], const float col_f[4])
{
int r, g, b, a;
r= (int)(col_f[0] * 255.0f);
g= (int)(col_f[1] * 255.0f);
b= (int)(col_f[2] * 255.0f);
a= (int)(col_f[3] * 255.0f);
col_r[0]= (char)((r <= 0)? 0 : (r >= 255)? 255 : r);
col_r[1]= (char)((g <= 0)? 0 : (g >= 255)? 255 : g);
col_r[2]= (char)((b <= 0)? 0 : (b >= 255)? 255 : b);
col_r[3]= (char)((a <= 0)? 0 : (a >= 255)? 255 : a);
}
/* ********************************* color transforms ********************************* */

View File

@@ -163,7 +163,7 @@ struct uiBut {
float hardmin, hardmax, softmin, softmax;
float a1, a2;
float aspect;
char col[4];
unsigned char col[4];
uiButHandleFunc func;
void *func_arg1;

View File

@@ -2626,9 +2626,7 @@ void uiTemplateReportsBanner(uiLayout *layout, bContext *C)
uiBlockBeginAlign(block);
but= uiDefBut(block, ROUNDBOX, 0, "", 0, 0, UI_UNIT_X+10, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");
/* set the report's bg color in but->col - ROUNDBOX feature */
but->col[0]= FTOCHAR(rti->col[0]);
but->col[1]= FTOCHAR(rti->col[1]);
but->col[2]= FTOCHAR(rti->col[2]);
rgb_float_to_uchar(but->col, rti->col);
but->col[3]= 255;
but= uiDefBut(block, ROUNDBOX, 0, "", UI_UNIT_X+10, 0, UI_UNIT_X+width, UI_UNIT_Y, NULL, 0.0f, 0.0f, 0, 0, "");

View File

@@ -2501,7 +2501,7 @@ static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int stat
if (color_profile)
linearrgb_to_srgb_v3_v3(col, col);
F4TOCHAR4(col, wcol->inner);
rgba_float_to_uchar((unsigned char *)wcol->inner, col);
wcol->shaded = 0;
wcol->alpha_check = (wcol->inner[3] < 255);

View File

@@ -807,10 +807,8 @@ static void apply_tangmat_callback(DerivedMesh *lores_dm, DerivedMesh *hires_dm,
ibuf->userflags= IB_RECT_INVALID;
} else {
char *rrgb= (char*)ibuf->rect + pixel*4;
rrgb[0]= FTOCHAR(vec[0]);
rrgb[1]= FTOCHAR(vec[1]);
rrgb[2]= FTOCHAR(vec[2]);
unsigned char *rrgb= (unsigned char *)ibuf->rect + pixel*4;
rgb_float_to_uchar(rrgb, vec);
rrgb[3]= 255;
}
}

View File

@@ -349,11 +349,8 @@ static void draw_textured_begin(Scene *scene, View3D *v3d, RegionView3D *rv3d, O
Gtexdraw.islit= GPU_scene_object_lights(scene, ob, v3d->lay, rv3d->viewmat, !rv3d->is_persp);
}
obcol[0]= CLAMPIS(ob->col[0]*255, 0, 255);
obcol[1]= CLAMPIS(ob->col[1]*255, 0, 255);
obcol[2]= CLAMPIS(ob->col[2]*255, 0, 255);
obcol[3]= CLAMPIS(ob->col[3]*255, 0, 255);
rgba_float_to_uchar(obcol, ob->col);
glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
if(solidtex || v3d->drawtype==OB_TEXTURE) istex= 1;
else istex= 0;

View File

@@ -164,19 +164,6 @@ MINLINE unsigned char dither_value(unsigned short v_in, DitherContext *di, int i
/************************* Generic Buffer Conversion *************************/
MINLINE void byte_to_float_v4(float f[4], const uchar b[4])
{
f[0]= b[0] * (1.0f/255.0f);
f[1]= b[1] * (1.0f/255.0f);
f[2]= b[2] * (1.0f/255.0f);
f[3]= b[3] * (1.0f/255.0f);
}
MINLINE void float_to_byte_v4(uchar b[4], const float f[4])
{
F4TOCHAR4(f, b);
}
MINLINE void ushort_to_byte_v4(uchar b[4], const unsigned short us[4])
{
b[0]= USHORTTOUCHAR(us[0]);
@@ -233,7 +220,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
if(profile_to == profile_from) {
/* no color space conversion */
for(x = 0; x < width; x++, from+=3, to+=4) {
F3TOCHAR3(from, to);
rgb_float_to_uchar(to, from);
to[3] = 255;
}
}
@@ -241,7 +228,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
/* convert from linear to sRGB */
for(x = 0; x < width; x++, from+=3, to+=4) {
linearrgb_to_srgb_v3_v3(tmp, from);
F3TOCHAR3(tmp, to);
rgb_float_to_uchar(to, tmp);
to[3] = 255;
}
}
@@ -249,7 +236,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
/* convert from sRGB to linear */
for(x = 0; x < width; x++, from+=3, to+=4) {
srgb_to_linearrgb_v3_v3(tmp, from);
F3TOCHAR3(tmp, to);
rgb_float_to_uchar(to, tmp);
to[3] = 255;
}
}
@@ -267,7 +254,7 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
}
else {
for(x = 0; x < width; x++, from+=4, to+=4)
float_to_byte_v4(to, from);
rgba_float_to_uchar(to, from);
}
}
else if(profile_to == IB_PROFILE_SRGB) {
@@ -316,13 +303,13 @@ void IMB_buffer_byte_from_float(uchar *rect_to, const float *rect_from,
else if(predivide) {
for(x = 0; x < width; x++, from+=4, to+=4) {
srgb_to_linearrgb_predivide_v4(tmp, from);
float_to_byte_v4(to, tmp);
rgba_float_to_uchar(to, tmp);
}
}
else {
for(x = 0; x < width; x++, from+=4, to+=4) {
srgb_to_linearrgb_v4(tmp, from);
float_to_byte_v4(to, tmp);
rgba_float_to_uchar(to, tmp);
}
}
}
@@ -358,7 +345,7 @@ void IMB_buffer_float_from_byte(float *rect_to, const uchar *rect_from,
if(profile_to == profile_from) {
/* no color space conversion */
for(x = 0; x < width; x++, from+=4, to+=4)
byte_to_float_v4(to, from);
rgba_uchar_to_float(to, from);
}
else if(profile_to == IB_PROFILE_LINEAR_RGB) {
/* convert sRGB to linear */
@@ -377,13 +364,13 @@ void IMB_buffer_float_from_byte(float *rect_to, const uchar *rect_from,
/* convert linear to sRGB */
if(predivide) {
for(x = 0; x < width; x++, from+=4, to+=4) {
byte_to_float_v4(tmp, from);
rgba_uchar_to_float(tmp, from);
linearrgb_to_srgb_predivide_v4(to, tmp);
}
}
else {
for(x = 0; x < width; x++, from+=4, to+=4) {
byte_to_float_v4(tmp, from);
rgba_uchar_to_float(tmp, from);
linearrgb_to_srgb_v4(to, tmp);
}
}
@@ -502,16 +489,16 @@ void IMB_buffer_byte_from_byte(uchar *rect_to, const uchar *rect_from,
/* convert to sRGB to linear */
if(predivide) {
for(x = 0; x < width; x++, from+=4, to+=4) {
byte_to_float_v4(tmp, from);
rgba_uchar_to_float(tmp, from);
srgb_to_linearrgb_predivide_v4(tmp, tmp);
float_to_byte_v4(to, tmp);
rgba_float_to_uchar(to, tmp);
}
}
else {
for(x = 0; x < width; x++, from+=4, to+=4) {
byte_to_float_v4(tmp, from);
rgba_uchar_to_float(tmp, from);
srgb_to_linearrgb_v4(tmp, tmp);
float_to_byte_v4(to, tmp);
rgba_float_to_uchar(to, tmp);
}
}
}
@@ -519,16 +506,16 @@ void IMB_buffer_byte_from_byte(uchar *rect_to, const uchar *rect_from,
/* convert from linear to sRGB */
if(predivide) {
for(x = 0; x < width; x++, from+=4, to+=4) {
byte_to_float_v4(tmp, from);
rgba_uchar_to_float(tmp, from);
linearrgb_to_srgb_predivide_v4(tmp, tmp);
float_to_byte_v4(to, tmp);
rgba_float_to_uchar(to, tmp);
}
}
else {
for(x = 0; x < width; x++, from+=4, to+=4) {
byte_to_float_v4(tmp, from);
rgba_uchar_to_float(tmp, from);
linearrgb_to_srgb_v4(tmp, tmp);
float_to_byte_v4(to, tmp);
rgba_float_to_uchar(to, tmp);
}
}
}

View File

@@ -2170,10 +2170,9 @@ static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(qua
if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE) && (R.r.color_mgt_flag & R_COLOR_MANAGEMENT)) {
linearrgb_to_srgb_uchar3(col, shr.combined);
} else {
col[0]= FTOCHAR(shr.combined[0]);
col[1]= FTOCHAR(shr.combined[1]);
col[2]= FTOCHAR(shr.combined[2]);
}
else {
rgb_float_to_uchar(col, shr.combined);
}
if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE)) {
@@ -2205,9 +2204,7 @@ static void bake_displacement(void *handle, ShadeInput *UNUSED(shi), float dist,
col[3]= 1.0f;
} else {
char *col= (char *)(bs->rect + bs->rectx*y + x);
col[0]= FTOCHAR(disp);
col[1]= FTOCHAR(disp);
col[2]= FTOCHAR(disp);
col[0] = col[1] = col[2] = FTOCHAR(disp);
col[3]= 255;
}
if (bs->rect_mask) {