2012-01-29 17:39:56 +00:00
|
|
|
#include "color.h"
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
2012-01-24 13:16:20 +00:00
|
|
|
#include <assert.h>
|
2012-02-21 13:41:02 +00:00
|
|
|
#include <string.h>
|
2013-01-14 19:07:56 +00:00
|
|
|
#include <math.h>
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "tools.h"
|
2012-03-08 15:10:25 +00:00
|
|
|
#include "curve.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2013-01-14 19:07:56 +00:00
|
|
|
/******************************** Color ********************************/
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
Color COLOR_TRANSPARENT = {0.0, 0.0, 0.0, 0.0};
|
|
|
|
Color COLOR_BLACK = {0.0, 0.0, 0.0, 1.0};
|
|
|
|
Color COLOR_RED = {1.0, 0.0, 0.0, 1.0};
|
|
|
|
Color COLOR_GREEN = {0.0, 1.0, 0.0, 1.0};
|
|
|
|
Color COLOR_BLUE = {0.0, 0.0, 1.0, 1.0};
|
|
|
|
Color COLOR_WHITE = {1.0, 1.0, 1.0, 1.0};
|
|
|
|
Color COLOR_GREY = {0.5, 0.5, 0.5, 1.0};
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void colorSave(PackStream* stream, Color* col)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
packWriteDouble(stream, &col->r);
|
|
|
|
packWriteDouble(stream, &col->g);
|
|
|
|
packWriteDouble(stream, &col->b);
|
|
|
|
packWriteDouble(stream, &col->a);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void colorLoad(PackStream* stream, Color* col)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
packReadDouble(stream, &col->r);
|
|
|
|
packReadDouble(stream, &col->g);
|
|
|
|
packReadDouble(stream, &col->b);
|
|
|
|
packReadDouble(stream, &col->a);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int colorTo32BitRGBA(Color* col)
|
|
|
|
{
|
|
|
|
return (((unsigned int)(col->a * 255.0)) << 24) | (((unsigned int)(col->b * 255.0)) << 16) | (((unsigned int)(col->g * 255.0)) << 8) | ((unsigned int)(col->r * 255.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int colorTo32BitBGRA(Color* col)
|
|
|
|
{
|
|
|
|
return (((unsigned int)(col->a * 255.0)) << 24) | (((unsigned int)(col->r * 255.0)) << 16) | (((unsigned int)(col->g * 255.0)) << 8) | ((unsigned int)(col->b * 255.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int colorTo32BitARGB(Color* col)
|
|
|
|
{
|
|
|
|
return (((unsigned int)(col->b * 255.0)) << 24) | (((unsigned int)(col->g * 255.0)) << 16) | (((unsigned int)(col->r * 255.0)) << 8) | ((unsigned int)(col->a * 255.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int colorTo32BitABGR(Color* col)
|
|
|
|
{
|
|
|
|
return (((unsigned int)(col->r * 255.0)) << 24) | (((unsigned int)(col->g * 255.0)) << 16) | (((unsigned int)(col->b * 255.0)) << 8) | ((unsigned int)(col->a * 255.0));
|
|
|
|
}
|
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
Color colorFrom32BitRGBA(unsigned int col)
|
|
|
|
{
|
|
|
|
Color result;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
result.r = ((double)(col & 0x000000FF)) / 255.0;
|
|
|
|
result.g = ((double)((col & 0x0000FF00) >> 8)) / 255.0;
|
|
|
|
result.b = ((double)((col & 0x00FF0000) >> 16)) / 255.0;
|
|
|
|
result.a = ((double)((col & 0xFF000000) >> 24)) / 255.0;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color colorFrom32BitBGRA(unsigned int col)
|
|
|
|
{
|
|
|
|
Color result;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
result.b = ((double)(col & 0x000000FF)) / 255.0;
|
|
|
|
result.g = ((double)((col & 0x0000FF00) >> 8)) / 255.0;
|
|
|
|
result.r = ((double)((col & 0x00FF0000) >> 16)) / 255.0;
|
|
|
|
result.a = ((double)((col & 0xFF000000) >> 24)) / 255.0;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color colorFrom32BitARGB(unsigned int col)
|
|
|
|
{
|
|
|
|
Color result;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
result.a = ((double)(col & 0x000000FF)) / 255.0;
|
|
|
|
result.r = ((double)((col & 0x0000FF00) >> 8)) / 255.0;
|
|
|
|
result.g = ((double)((col & 0x00FF0000) >> 16)) / 255.0;
|
|
|
|
result.b = ((double)((col & 0xFF000000) >> 24)) / 255.0;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color colorFrom32BitABGR(unsigned int col)
|
|
|
|
{
|
|
|
|
Color result;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
result.a = ((double)(col & 0x000000FF)) / 255.0;
|
|
|
|
result.b = ((double)((col & 0x0000FF00) >> 8)) / 255.0;
|
|
|
|
result.g = ((double)((col & 0x00FF0000) >> 16)) / 255.0;
|
|
|
|
result.r = ((double)((col & 0xFF000000) >> 24)) / 255.0;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-07-19 15:04:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
void colorMask(Color* base, Color* mask)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double new_a;
|
2011-12-10 13:25:22 +00:00
|
|
|
new_a = base->a + mask->a - (base->a * mask->a);
|
|
|
|
base->r = (mask->r * mask->a + base->r * base->a - base->r * base->a * mask->a) / new_a;
|
|
|
|
base->g = (mask->g * mask->a + base->g * base->a - base->g * base->a * mask->a) / new_a;
|
|
|
|
base->b = (mask->b * mask->a + base->b * base->a - base->b * base->a * mask->a) / new_a;
|
|
|
|
base->a = new_a;
|
2011-12-17 10:55:37 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
/*double mask_weight = mask->a;
|
|
|
|
double base_weight = 1.0 - mask_weight;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
base->r = mask->r * mask_weight + base->r * base_weight;
|
|
|
|
base->g = mask->g * mask_weight + base->g * base_weight;
|
|
|
|
base->b = mask->b * mask_weight + base->b * base_weight;
|
|
|
|
base->a = base->a + mask_weight * (1.0 - base->a);*/
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double colorNormalize(Color* col)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2013-01-14 19:07:56 +00:00
|
|
|
if (col->r > 1.0)
|
|
|
|
{
|
|
|
|
col->r = 1.0;
|
|
|
|
}
|
|
|
|
if (col->g > 1.0)
|
|
|
|
{
|
|
|
|
col->g = 1.0;
|
|
|
|
}
|
|
|
|
if (col->b > 1.0)
|
|
|
|
{
|
|
|
|
col->b = 1.0;
|
|
|
|
}
|
|
|
|
return 1.0;
|
|
|
|
/*double max = colorGetValue(col);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-11-24 14:19:43 +00:00
|
|
|
assert(max >= 0.0);
|
2012-01-24 13:16:20 +00:00
|
|
|
assert(col->r >= 0.0);
|
|
|
|
assert(col->g >= 0.0);
|
|
|
|
assert(col->b >= 0.0);
|
|
|
|
assert(col->a >= 0.0);
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
if (max > 1.0)
|
|
|
|
{
|
|
|
|
col->r /= max;
|
|
|
|
col->g /= max;
|
|
|
|
col->b /= max;
|
|
|
|
}
|
2013-01-14 19:07:56 +00:00
|
|
|
return max;*/
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double colorGetValue(Color* col)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double max;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
|
|
|
max = col->r;
|
|
|
|
if (col->g > max)
|
|
|
|
{
|
|
|
|
max = col->g;
|
|
|
|
}
|
|
|
|
if (col->b > max)
|
|
|
|
{
|
|
|
|
max = col->b;
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2013-01-14 19:07:56 +00:00
|
|
|
/******************************** ColorProfile ********************************/
|
|
|
|
struct ColorProfile
|
|
|
|
{
|
|
|
|
double minvalue;
|
|
|
|
double maxvalue;
|
|
|
|
};
|
|
|
|
|
|
|
|
ColorProfile* colorProfileCreate()
|
|
|
|
{
|
|
|
|
ColorProfile* profile;
|
|
|
|
|
|
|
|
profile = malloc(sizeof(ColorProfile));
|
|
|
|
|
|
|
|
colorProfileClear(profile);
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void colorProfileDelete(ColorProfile* profile)
|
|
|
|
{
|
|
|
|
free(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void colorProfileSave(PackStream* stream, ColorProfile* profile)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
|
|
|
|
void colorProfileLoad(PackStream* stream, ColorProfile* profile)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
|
|
|
|
void colorProfileClear(ColorProfile* profile)
|
|
|
|
{
|
|
|
|
profile->minvalue = 0.0;
|
|
|
|
profile->maxvalue = 3.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int colorProfileCollect(ColorProfile* profile, Color pixel)
|
|
|
|
{
|
|
|
|
int changed = 0;
|
|
|
|
double value = pixel.r + pixel.g + pixel.b;
|
|
|
|
|
|
|
|
if (value < profile->minvalue)
|
|
|
|
{
|
|
|
|
profile->minvalue = value;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (value > profile->maxvalue)
|
|
|
|
{
|
|
|
|
profile->maxvalue = value;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
float A = 0.15;
|
|
|
|
float B = 0.50;
|
|
|
|
float C = 0.10;
|
|
|
|
float D = 0.20;
|
|
|
|
float E = 0.02;
|
|
|
|
float F = 0.30;
|
|
|
|
float W = 11.2;
|
|
|
|
|
|
|
|
static double _uncharted2Tonemap(double x)
|
|
|
|
{
|
|
|
|
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color colorProfileApply(ColorProfile* profile, Color pixel)
|
|
|
|
{
|
|
|
|
double exposure_bias = 2.0;
|
|
|
|
double white_scale = 1.0 / _uncharted2Tonemap(W);
|
|
|
|
|
|
|
|
pixel.r = pow(_uncharted2Tonemap(pixel.r * exposure_bias) * white_scale, 1.0 / 2.2);
|
|
|
|
pixel.g = pow(_uncharted2Tonemap(pixel.g * exposure_bias) * white_scale, 1.0 / 2.2);
|
|
|
|
pixel.b = pow(_uncharted2Tonemap(pixel.b * exposure_bias) * white_scale, 1.0 / 2.2);
|
|
|
|
|
|
|
|
return pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************** ColorGradation ********************************/
|
|
|
|
struct ColorGradation
|
|
|
|
{
|
|
|
|
Curve* red;
|
|
|
|
Curve* green;
|
|
|
|
Curve* blue;
|
|
|
|
};
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
ColorGradation* colorGradationCreate()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
ColorGradation* result;
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
result = malloc(sizeof(ColorGradation));
|
|
|
|
result->red = curveCreate();
|
|
|
|
result->green = curveCreate();
|
|
|
|
result->blue = curveCreate();
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationDelete(ColorGradation* gradation)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveDelete(gradation->red);
|
|
|
|
curveDelete(gradation->green);
|
|
|
|
curveDelete(gradation->blue);
|
|
|
|
free(gradation);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationCopy(ColorGradation* source, ColorGradation* destination)
|
2012-01-29 17:39:56 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveCopy(source->red, destination->red);
|
|
|
|
curveCopy(source->green, destination->green);
|
|
|
|
curveCopy(source->blue, destination->blue);
|
|
|
|
}
|
2012-01-29 17:39:56 +00:00
|
|
|
|
2012-06-22 22:13:55 +00:00
|
|
|
void colorGradationClear(ColorGradation* gradation)
|
|
|
|
{
|
|
|
|
curveClear(gradation->red);
|
|
|
|
curveClear(gradation->green);
|
|
|
|
curveClear(gradation->blue);
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void colorGradationSave(PackStream* stream, ColorGradation* gradation)
|
2012-03-08 15:10:25 +00:00
|
|
|
{
|
2012-04-22 17:12:39 +00:00
|
|
|
curveSave(stream, gradation->red);
|
|
|
|
curveSave(stream, gradation->green);
|
|
|
|
curveSave(stream, gradation->blue);
|
2012-01-29 17:39:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void colorGradationLoad(PackStream* stream, ColorGradation* gradation)
|
2012-01-29 17:39:56 +00:00
|
|
|
{
|
2012-04-22 17:12:39 +00:00
|
|
|
curveLoad(stream, gradation->red);
|
|
|
|
curveLoad(stream, gradation->green);
|
|
|
|
curveLoad(stream, gradation->blue);
|
2012-03-08 15:10:25 +00:00
|
|
|
}
|
2012-01-29 17:39:56 +00:00
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationGetRedCurve(ColorGradation* gradation, Curve* curve)
|
|
|
|
{
|
|
|
|
curveCopy(gradation->red, curve);
|
2012-01-29 17:39:56 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationGetGreenCurve(ColorGradation* gradation, Curve* curve)
|
2012-02-21 13:41:02 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveCopy(gradation->green, curve);
|
2012-02-21 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationGetBlueCurve(ColorGradation* gradation, Curve* curve)
|
2012-02-21 13:41:02 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveCopy(gradation->blue, curve);
|
2012-02-21 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationSetRedCurve(ColorGradation* gradation, Curve* curve)
|
2012-02-21 13:41:02 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveCopy(curve, gradation->red);
|
|
|
|
curveValidate(gradation->red);
|
2012-02-21 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationSetGreenCurve(ColorGradation* gradation, Curve* curve)
|
2012-02-21 13:41:02 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveCopy(curve, gradation->green);
|
|
|
|
curveValidate(gradation->green);
|
2012-02-21 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
void colorGradationSetBlueCurve(ColorGradation* gradation, Curve* curve)
|
2012-02-21 13:41:02 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveCopy(curve, gradation->blue);
|
|
|
|
curveValidate(gradation->blue);
|
2012-02-21 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
void colorGradationQuickAdd(ColorGradation* gradation, double value, Color* col)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
colorGradationQuickAddRgb(gradation, value, col->r, col->g, col->b);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
void colorGradationQuickAddRgb(ColorGradation* gradation, double value, double r, double g, double b)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-03-08 15:10:25 +00:00
|
|
|
curveQuickAddPoint(gradation->red, value, r);
|
|
|
|
curveValidate(gradation->red);
|
|
|
|
|
|
|
|
curveQuickAddPoint(gradation->green, value, g);
|
|
|
|
curveValidate(gradation->green);
|
|
|
|
|
|
|
|
curveQuickAddPoint(gradation->blue, value, b);
|
|
|
|
curveValidate(gradation->blue);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
Color colorGradationGet(ColorGradation* gradation, double value)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
Color result;
|
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
result.r = curveGetValue(gradation->red, value);
|
|
|
|
result.g = curveGetValue(gradation->green, value);
|
|
|
|
result.b = curveGetValue(gradation->blue, value);
|
|
|
|
result.a = 1.0;
|
2013-01-14 19:07:56 +00:00
|
|
|
|
2012-03-08 15:10:25 +00:00
|
|
|
return result;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|