25 #include "ImageConvert.hh"
32 template <
class StorageType>
45 BIASERR(
"ImageConvert::ToHSV() sould be called with images of"\
52 res = RGBToHSV_(source,dest);
55 BIASERR(
"Image::ToHSV(): color model not implemented");
64 template <
class StorageType>
68 BIASERR(
"ImageConvert::RGBToHSV_ should be called from float image");
81 BIASWARN(
"Unneccessary image copy operation. This may hurt performance.");
86 register float *G = NULL , *B = NULL;
87 register float *ImageDataEnd =
90 register float *S = NULL , *V = NULL;
91 register float Min = 0, Max = 0;
92 float MinVal = 0, MaxVal = 0;
103 if ( (MinVal < 0.0) || (MaxVal > 1.0) )
112 while (R < ImageDataEnd){
114 if ( (*R >= *G) && (*R >= *B) ){
120 }
else if (*G >= *B){
137 *H = (*G - *B)/(Max - Min);
139 *H = 2.0f + (*B - *R)/(Max - Min);
141 *H = 4.0f + (*R - *G)/(Max - Min);
177 while (R < ImageDataEnd){
181 for (
register int i = 1; i < 3; i++){
193 *H = (*G - *B)/(Max - Min);
195 *H = 2 + (*B - *R)/(Max - Min);
197 *H = 4 + (*R - *G)/(Max - Min);
240 BIASERR(
"source not in RGB format");
251 register const unsigned char *R = source.
GetImageData();
252 register const unsigned char *ImageDataEnd =
255 register const unsigned char *G = NULL, *B = NULL;
256 register unsigned char *S = NULL, *V = NULL;
258 unsigned char Min = 0;
259 unsigned char Max = 0;
263 BIASASSERT( R != NULL);
264 BIASASSERT( H != NULL);
271 while (R < ImageDataEnd){
273 if ( (*R >= *G) && (*R >= *B) ){
279 }
else if (*G >= *B){
296 TmpH = 256 * (*G - *B)/(Max - Min);
298 TmpH = 512 + 256 * (*B - *R)/(Max - Min);
300 TmpH = 1024 + 256 * (*R - *G)/(Max - Min);
303 *H = (
unsigned char)(TmpH + 256);
305 *H = (
unsigned char)TmpH;
310 if (Max != (
unsigned char)0){
312 *S = 255 - (
unsigned char)(255.0 * (
float)Min/(float)Max);
316 *V = ((int)(*R) + (int)(*G) + (int)(*B)) / 3;
325 H++; S++; V++; R++; G++; B++;
337 while (R < ImageDataEnd){
347 if (Max < *G) Max = *G;
348 if (Max < *B) Max = *B;
349 if (Min > *G) Min = *G;
350 if (Min > *B) Min = *B;
355 TmpH = (256 * (*G - *B))/(Max - Min);
357 TmpH = 512 + (256 * (*B - *R))/(Max - Min);
359 TmpH = 1024 + (256 * (*R - *G))/(Max - Min);
362 *H = (
unsigned char)(TmpH + 256);
364 *H = (
unsigned char)TmpH;
370 if (Max != (
unsigned char)0){
372 *S = (
unsigned char)(255.0 * ((
float)Max-(float)Min)/(float)Max);
376 *V = ((int)(*R) + (int)(*G) + (int)(*B)) / 3;
396 template <
class StorageType>
405 BIASERR(
"Only for interleaved images!");
409 BIASERR(
"Only for 3 channels!");
412 unsigned int indexS = 0;
413 unsigned int indexD = 0;
414 StorageType h = 0, s = 0, v = 0, r = 0, g = 0, b = 0;
415 for (
unsigned int x = 0; x < nrOfPel; x++) {
432 template <
class StorageType>
435 StorageType &r, StorageType &g, StorageType &b)
437 float p1, p2, p3, i, f;
443 if (h == 360.0) h = 0.0;
445 i = (float)floor((
double)xh);
448 p2 = v * (1 - (s * f));
449 p3 = v * (1 - (s * (1 - f)));
453 r = StorageType(v); g = StorageType(p3); b = StorageType(p1);
break;
455 r = StorageType(p2); g = StorageType(v); b = StorageType(p1);
break;
457 r = StorageType(p1); g = StorageType(v); b = StorageType(p3);
break;
459 r = StorageType(p1); g = StorageType(p2); b = StorageType(v);
break;
461 r = StorageType(p3); g = StorageType(p1); b = StorageType(v);
break;
463 r = StorageType(v); g = StorageType(p1); b = StorageType(p2);
break;
466 r = (StorageType)(
float)(r * 255.);
467 g = (StorageType)(
float)(g * 255.);
468 b = (StorageType)(
float)(b * 255.);
static int HSVToRGB_(const Image< StorageType > &source, Image< StorageType > &dest)
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
int ScaleShiftBetween(double Min, double Max)
scales and shifts image so afterwards every pixel has a value between Min and Max ...
void SetColorModel(EColorModel Model)
unsigned int GetSizeByte() const
returns the nr.
void GetMinMaxPixelValue(StorageType &min, StorageType &max, unsigned short int channel=0, unsigned int *mincoo=NULL, unsigned int *maxcoo=NULL) const
returns the minimal and maximal pixel value in channel only Finds minimum and maximum pixel value in ...
unsigned int GetWidth() const
void SetInterleaved(bool interleaved)
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
color values, 3 channels, order: red,green,blue
static BIASImageBase_EXPORT int ToHSV(const Image< StorageType > &source, Image< StorageType > &dest)
Create a HSV converted copy of source image in this Release() and Init() are called if necessary...
static BIASImageBase_EXPORT int HSVToRGB(const StorageType h, const StorageType s, const StorageType v, StorageType &r, StorageType &g, StorageType &b)
conversion of one pixel in HSV to RGB colorspace
unsigned int GetHeight() const
The image template class for specific storage types.
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same "size" as Image of other type
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
enum EColorModel GetColorModel() const
enum EStorageType GetStorageType() const
HSV, 3 channels, order: hue, sat , value.
static int RGBToHSV_(const Image< StorageType > &source, Image< StorageType > &dest)
so far only implemented for StorageType=float
unsigned long int GetPixelCount() const
returns number of pixels in image