Skip to content

Commit

Permalink
separate out rgb2{linear,gamma}
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jan 11, 2023
1 parent f524e5c commit 73e4f76
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
55 changes: 34 additions & 21 deletions color_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,37 +189,50 @@ void hsv2rgb( double *hsv, double *rgb )

void rgb2xyz( double *rgb, double gamma, double *m0, double *m1, double *m2, double *xyz )
{
double copy[] = { rgb[0], rgb[1], rgb[2] };
/* weighted RGB */
rgb2linear(rgb, gamma, xyz);
struct pixel p = { xyz[0], xyz[1], xyz[2] };
_mult_v3_m33( &p, m0, m1, m2, xyz );
}


void rgb2linear( double *rgb, double gamma, double *out )
{
int i;
if (gamma < 0) {
/* special case for sRGB gamma curve */
for (i = 0; i < 3; i++)
if ( fabs(copy[i]) <= 0.04045 ) { copy[i] /= 12.92; }
else { copy[i] = _apow( (copy[i] + 0.055)/1.055, 2.4 ); }
}
else {
for (i = 0; i < 3; i++)
copy[i] = _apow(copy[i], gamma);
if (gamma < 0) { /* special case for sRGB gamma curve */
for (i = 0; i < 3; i++)
out[i] = fabs(rgb[i]) <= 0.04045 ? rgb[i] / 12.92 : _apow( (rgb[i] + 0.055)/1.055, 2.4 );
} else if (gamma == 1.0) { /* copy if different locations */
if (rgb != out)
for (i = 0; i < 3; i++)
out[i] = rgb[i];
} else {
for (i = 0; i < 3; i++)
out[i] = _apow(rgb[i], gamma);
}
struct pixel p = { copy[0], copy[1], copy[2] };
_mult_v3_m33( &p, m0, m1, m2, xyz );
}


void xyz2rgb( double *xyz, double gamma, double *m0, double *m1, double *m2, double *rgb )
{
struct pixel p = { xyz[0], xyz[1], xyz[2] };
_mult_v3_m33( &p, m0, m1, m2, rgb );
rgb2gamma(rgb, gamma, rgb);
}


void rgb2gamma( double *rgb, double gamma, double *out )
{
int i;
if (gamma < 0) {
/* special case for sRGB gamma curve */
for (i = 0; i < 3; i++)
rgb[i] = (fabs(rgb[i]) <= 0.0031308) ? 12.92 * rgb[i] : 1.055 * _apow(rgb[i], 1.0/2.4) - 0.055;
}
else {
for (i = 0; i < 3; i++)
rgb[i] = _apow(rgb[i], 1.0 / gamma);
if (gamma < 0) { /* special case for sRGB gamma curve */
for (i = 0; i < 3; i++)
out[i] = (fabs(rgb[i]) <= 0.0031308) ? 12.92 * rgb[i] : 1.055 * _apow(rgb[i], 1.0/2.4) - 0.055;
} else if (gamma == 1.0) { /* copy if different locations */
if (rgb != out)
for (i = 0; i < 3; i++)
out[i] = rgb[i];
} else {
for (i = 0; i < 3; i++)
out[i] = _apow(rgb[i], 1.0 / gamma);
}
}

Expand Down
2 changes: 2 additions & 0 deletions color_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ void rgb2hsv( double *rgb, double *hsv );
void hsv2rgb( double *hsv, double *rgb );
void rgb2xyz( double *rgb, double gamma, double *m0, double *m1, double *m2, double *xyz );
void xyz2rgb( double *xyz, double gamma, double *m0, double *m1, double *m2, double *rgb );
void rgb2linear( double *rgb, double gamma, double *out );
void rgb2gamma( double *rgb, double gamma, double *out );
void xyY2xyz( double *xyY, double *xyz );
void xyz2lab( double *xyz, double *w, double *lab );
void lab2lch( double *lab, double *lch );
Expand Down
2 changes: 1 addition & 1 deletion t/color_space.t
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ sub tapprox {
my $rgb_ = pdl(255, 10, 50) / 255;
my $a = rgb_to_xyz( $rgb_, 'Adobe' );
my $ans = pdl( 0.582073320819542, 0.299955362786115, 0.0546021884576833 );
ok( tapprox( $a, $ans), 'rgb_to_xyz Adobe' ) or diag($a, $ans);
ok( tapprox( $a, $ans), 'rgb_to_xyz Adobe' ) or diag($a, "\n", $ans);
}

# xyY_to_xyz
Expand Down

0 comments on commit 73e4f76

Please sign in to comment.