Commit 0fb08ad6 authored by Sebastien Barre's avatar Sebastien Barre
Browse files

ENH: remove duplicated code, use vtkMath implementation for HSV<->RGB conversion

parent da8be228
......@@ -18,10 +18,9 @@
#include "vtkLookupTable.h"
#include "vtkBitArray.h"
#include "vtkObjectFactory.h"
#include "vtkMath.h"
#include <math.h>
vtkCxxRevisionMacro(vtkLookupTable, "1.90");
vtkCxxRevisionMacro(vtkLookupTable, "1.91");
vtkStandardNewMacro(vtkLookupTable);
// Construct with range=(0,1); and hsv ranges set up for rainbow color table
......@@ -131,8 +130,8 @@ int vtkLookupTable::Allocate(int sz, int ext)
// Force the lookup table to rebuild
void vtkLookupTable::ForceBuild()
{
int i, hueCase;
float hue, sat, val, lx, ly, lz, frac, hinc, sinc, vinc, ainc;
int i;
float hue, sat, val, hinc, sinc, vinc, ainc;
float rgba[4], alpha;
unsigned char *c_rgba;
......@@ -150,52 +149,7 @@ void vtkLookupTable::ForceBuild()
val = this->ValueRange[0] + i*vinc;
alpha = this->AlphaRange[0] + i*ainc;
hueCase = static_cast<int>(hue * 6);
frac = 6*hue - hueCase;
lx = val*(1.0 - sat);
ly = val*(1.0 - sat*frac);
lz = val*(1.0 - sat*(1.0 - frac));
switch (hueCase)
{
/* 0<hue<1/6 */
case 0:
case 6:
rgba[0] = val;
rgba[1] = lz;
rgba[2] = lx;
break;
/* 1/6<hue<2/6 */
case 1:
rgba[0] = ly;
rgba[1] = val;
rgba[2] = lx;
break;
/* 2/6<hue<3/6 */
case 2:
rgba[0] = lx;
rgba[1] = val;
rgba[2] = lz;
break;
/* 3/6<hue/4/6 */
case 3:
rgba[0] = lx;
rgba[1] = ly;
rgba[2] = val;
break;
/* 4/6<hue<5/6 */
case 4:
rgba[0] = lz;
rgba[1] = lx;
rgba[2] = val;
break;
/* 5/6<hue<1 */
case 5:
rgba[0] = val;
rgba[1] = lx;
rgba[2] = ly;
break;
}
vtkMath::HSVToRGB(hue, sat, val, &rgba[0], &rgba[1], &rgba[2]);
rgba[3] = alpha;
c_rgba = this->Table->WritePointer(4*i,4);
......@@ -239,7 +193,6 @@ void vtkLookupTable::ForceBuild()
}
break;
}
}
this->BuildTime.Modified();
}
......
......@@ -17,10 +17,11 @@
=========================================================================*/
#include "vtkColorTransferFunction.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPiecewiseFunction.h"
vtkCxxRevisionMacro(vtkColorTransferFunction, "1.46");
vtkCxxRevisionMacro(vtkColorTransferFunction, "1.47");
vtkStandardNewMacro(vtkColorTransferFunction);
// Construct a new vtkColorTransferFunction with default values
......@@ -64,116 +65,6 @@ vtkColorTransferFunction::~vtkColorTransferFunction()
delete [] this->Table;
}
void vtkColorTransferFunction::RGBToHSV(float R, float G, float B,
float &H, float &S, float &V)
{
float cmax, cmin;
cmax = R;
cmin = R;
if (G > cmax)
{
cmax = G;
}
else if (G < cmin)
{
cmin = G;
}
if (B > cmax)
{
cmax = B;
}
else if (B < cmin)
{
cmin = B;
}
V = cmax;
if (V > 0.0)
{
S = (cmax - cmin)/cmax;
}
else
{
S = 0.0;
}
if (S > 0)
{
if (R == cmax)
{
H = 0.17*(G - B)/(cmax - cmin);
}
else if (G == cmax)
{
H = 0.33 + 0.17*(B - R)/(cmax - cmin);
}
else
{
H = 0.67 + 0.17*(R - G)/(cmax - cmin);
}
if (H < 0.0)
{
H = H + 1.0;
}
}
else
{
H = 0.0;
}
}
void vtkColorTransferFunction::HSVToRGB(float hue, float sat, float V,
float &R, float &G, float &B)
{
// compute RGB from HSV
if (hue > 0.17 && hue <= 0.33) // green/red
{
G = 1.0;
R = (0.33-hue)/0.16;
B = 0.0;
}
else if (hue > 0.33 && hue <= 0.5) // green/blue
{
G = 1.0;
B = (hue - 0.33)/0.17;
R = 0.0;
}
else if (hue > 0.5 && hue <= 0.67) // blue/green
{
B = 1.0;
G = (0.67 - hue)/0.17;
R = 0.0;
}
else if (hue > 0.67 && hue <= 0.83) // blue/red
{
B = 1.0;
R = (hue - 0.67)/0.16;
G = 0.0;
}
else if (hue > 0.83 && hue <= 1.0) // red/blue
{
R = 1.0;
B = (1.0-hue)/0.17;
G = 0.0;
}
else // red/green
{
R = 1.0;
G = hue/0.17;
B = 0.0;
}
// add Saturation to the equation.
R = (sat*R + (1.0 - sat));
G = (sat*G + (1.0 - sat));
B = (sat*B + (1.0 - sat));
R = R * V;
G = G * V;
B = B * V;
}
// Add a point defined in RGB
int vtkColorTransferFunction::AddRGBPoint( float x, float r,
float g, float b )
......@@ -267,7 +158,7 @@ int vtkColorTransferFunction::AddHSVPoint( float x, float h,
{
float r, b, g;
this->HSVToRGB( h, s, v, r, g, b );
vtkMath::HSVToRGB(h, s, v, &r, &g, &b);
return this->AddRGBPoint( x, r, g, b );
}
......@@ -402,8 +293,8 @@ void vtkColorTransferFunction::AddHSVSegment( float x1, float h1,
{
float r1, r2, b1, b2, g1, g2;
this->HSVToRGB( h1, s1, v1, r1, g1, b1 );
this->HSVToRGB( h2, s2, v2, r2, g2, b2 );
vtkMath::HSVToRGB(h1, s1, v1, &r1, &g1, &b1);
vtkMath::HSVToRGB(h2, s2, v2, &r2, &g2, &b2);
this->AddRGBSegment( x1, r1, g1, b1, x2, r2, g2, b2 );
}
......@@ -543,8 +434,8 @@ void vtkColorTransferFunction::GetTable( float x1, float x2,
else
{
float h1, h2, h3, s1, s2, s3, v1, v2, v3;
this->RGBToHSV(*(fptr-3), *(fptr-2), *(fptr-1), h1, s1, v1);
this->RGBToHSV(*(fptr+1), *(fptr+2), *(fptr+3), h2, s2, v2);
vtkMath::RGBToHSV(*(fptr-3), *(fptr-2), *(fptr-1), &h1, &s1, &v1);
vtkMath::RGBToHSV(*(fptr+1), *(fptr+2), *(fptr+3), &h2, &s2, &v2);
s3 = (1.0-weight)*s1 + weight*s2;
v3 = (1.0-weight)*v1 + weight*v2;
// Do we need to cross the 0/1 boundary?
......@@ -574,7 +465,7 @@ void vtkColorTransferFunction::GetTable( float x1, float x2,
h3 = (h3>1.0)?(1.0):((h3<0.0)?(0.0):(h3));
s3 = (s3>1.0)?(1.0):((s3<0.0)?(0.0):(s3));
v3 = (v3>1.0)?(1.0):((v3<0.0)?(0.0):(v3));
this->HSVToRGB(h3, s3, v3, *tptr, *(tptr+1), *(tptr+2) );
vtkMath::HSVToRGB(h3, s3, v3, tptr, tptr + 1, tptr + 2);
tptr += 3;
}
}
......@@ -686,8 +577,8 @@ const unsigned char *vtkColorTransferFunction::GetTable( float x1, float x2,
else
{
float h1, h2, h3, s1, s2, s3, v1, v2, v3;
this->RGBToHSV(*(fptr-3), *(fptr-2), *(fptr-1), h1, s1, v1);
this->RGBToHSV(*(fptr+1), *(fptr+2), *(fptr+3), h2, s2, v2);
vtkMath::RGBToHSV(*(fptr-3), *(fptr-2), *(fptr-1), &h1, &s1, &v1);
vtkMath::RGBToHSV(*(fptr+1), *(fptr+2), *(fptr+3), &h2, &s2, &v2);
s3 = (1.0-weight)*s1 + weight*s2;
v3 = (1.0-weight)*v1 + weight*v2;
// Do we need to cross the 0/1 boundary?
......@@ -717,7 +608,7 @@ const unsigned char *vtkColorTransferFunction::GetTable( float x1, float x2,
h3 = (h3>1.0)?(1.0):((h3<0.0)?(0.0):(h3));
s3 = (s3>1.0)?(1.0):((s3<0.0)?(0.0):(s3));
v3 = (v3>1.0)?(1.0):((v3<0.0)?(0.0):(v3));
this->HSVToRGB(h3, s3, v3, h1, s1, v1 );
vtkMath::HSVToRGB(h3, s3, v3, &h1, &s1, &v1);
*(tptr++) = (unsigned char)(255*h1);
*(tptr++) = (unsigned char)(255*s1);
*(tptr++) = (unsigned char)(255*v1);
......
......@@ -144,10 +144,6 @@ protected:
int FunctionSize;
int NumberOfPoints;
// conversion methods
void RGBToHSV( float r, float g, float b, float &h, float &s, float &v );
void HSVToRGB( float h, float s, float v, float &r, float &g, float &b );
// An evaluated color (0 to 255 RGBA A=255)
unsigned char UnsignedCharRGBAValue[4];
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment