Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ConvertHDR.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "ConvertHDR.hh"
26 #include <Base/Image/ImageConvert.hh>
27 #include <Base/Debug/LogFacility.hh>
28 
29 using namespace BIAS;
30 using namespace std;
31 
32 namespace BIAS {
33 
34 /**
35  This class handles special conversions for HDR images
36  @author Anne Sedlazeck
37  @date feb 2009
38  */
39 template<class StorageType>
41 
42 }
43 
44 template<class StorageType>
46 
47 }
48 
49 /** simple conversion to unsigned char, assumed bit depth is 16 -> all pixel values are divided by 65535
50  * supports unsigned short int images.
51  * deprecated, use other method
52  */
53 template<class StorageType> int ConvertHDR<StorageType>::ToUnsignedChar(
55  StorageType>& src,
57  unsigned char>& dst){
58 
60 
61  if (!dst.IsEmpty())
62  dst.Release();
63  dst.Init(src.GetWidth(), src.GetHeight(), src.GetChannelCount(), targetST);
64  dst.SetROI(*src.GetROI());
65  if (targetST == src.GetStorageType()) {
66  dst = src;
67  return 0;
68  }
69 
70  dst.SetColorModel(src.GetColorModel());
71 
72  switch (src.GetStorageType()) {
74  {
75  unsigned int values = src.GetPixelCount() * src.GetChannelCount();
76  unsigned char *d = (unsigned char*)dst.GetImageData();
77  float *s =(float*) src.GetImageData();
78  for (unsigned int i=0; i<values; i++){
79  d[i] = (unsigned char) *s;
80  s ++;
81  }
82  }
83  break;
84 #ifdef BUILD_IMAGE_USHORT
86  {
87  unsigned int values = src.GetPixelCount() * src.GetChannelCount();
88  unsigned char *d = (unsigned char*)dst.GetImageData();
89  unsigned short int *s =(unsigned short int*) src.GetImageData();
90 // StorageType minVal, maxVal;
91 // src.GetMinMaxPixelValue(minVal, maxVal,0);
92 // cout << "min " << minVal << " max " << maxVal << endl;
93 // src.GetMinMaxPixelValue(minVal, maxVal,1);
94 // cout << "min " << minVal << " max " << maxVal << endl;
95 // src.GetMinMaxPixelValue(minVal, maxVal,2);
96 // cout << "min " << minVal << " max " << maxVal << endl;
97 
98  BIASWARN("Conversion from unsigned short int to unsigned char by cast only!!!");
99  for (unsigned int i=0; i<values; i++)
100  d[i] = (unsigned char) (s[i]/65535.0*255.0);
101  }
102  break;
103 #endif // BUILD_IMAGE_SHORT
104  default:
105  BIASERR("ConvertHDR() not fully implemented "<<src.GetStorageType());
106  BIASABORT;
107  return -1;
108  }
109  return 0;
110 
111 }
112 
113 /** Conversion to unsigned char image, leaves min/max part intact.
114  * @param gamma = gamma value for gamma correction (gamma = 1 means no gamma correction)
115  * @param currentMaxVal = current maximun possible value - caused by bit depth for example bit depth 12 means a currentMaxVal of 4095
116  * the actual min and max values of the image are incorporated, meaning, the image is not stretched to fill the whole range of values
117  * between 0 and 255.
118  */
121  float gamma, unsigned int currentMaxVal){
122 
123 
125 
126  // prepare dst image
127  if (!dst.IsEmpty())
128  dst.Release();
129  dst.Init(src.GetWidth(), src.GetHeight(), src.GetChannelCount(), targetST);
130 
131  dst.SetROI(*src.GetROI());
132 
133  // if dst image is already unsigned char don't do anything
134  if (targetST == src.GetStorageType()) {
135  dst = src;
136  return 0;
137  }
138 
139 
140  // build float image to get started
141  Image<float> fImage;
143 
144  // find min and max val across all channels
145  float maxVal = 0, minVal=FLT_MAX, maxValTmp, minValTmp;
146  for(unsigned short int ch = 0; ch < fImage.GetChannelCount(); ch++){
147  fImage.GetMinMaxPixelValue(minValTmp, maxValTmp, ch);
148  if(minValTmp < minVal){
149  minVal = minValTmp;
150  }
151  if(maxValTmp > maxVal){
152  maxVal = maxValTmp;
153  }
154  }
155 
156  BLD("convertHDR - maxVal is " << maxVal << " highestVal is " << currentMaxVal << endl);
157 
158  // scale image between 0 and 1 consistent to current value range
159  fImage.ScaleShiftBetween(minVal/(float)currentMaxVal, maxVal/(float)currentMaxVal);
160 
161 
162  dst.SetColorModel(fImage.GetColorModel());
163 
164  unsigned int values = fImage.GetPixelCount() * fImage.GetChannelCount();
165  unsigned char *d = (unsigned char*)dst.GetImageData();
166  float *s =(float*) fImage.GetImageData();
167  float gammaVal;
168  for (unsigned int i=0; i<values; i++){
169  gammaVal = pow((*s), gamma);
170  d[i] = (unsigned char) (gammaVal*255);
171  s ++;
172  }
173 
174  return 0;
175 
176 }
177 
178 
179 
180 #define INST(type) template class ConvertHDR<type>;
181 INST(float)
182 INST(unsigned char)
183 
184 #ifdef BUILD_IMAGE_USHORT
185 INST(unsigned short)
186 #endif
187 
188 // yet untest inits
189 
190 // #ifdef BUILD_IMAGE_INT
191 // INST(int);
192 // #endif
193 // #ifdef BUILD_IMAGE_CHAR
194 // INST(char);
195 // #endif
196 // #ifdef BUILD_IMAGE_SHORT
197 // INST(short);
198 // #endif
199 // #ifdef BUILD_IMAGE_USHORT
200 // INST(unsigned short);
201 // #endif
202 // #ifdef BUILD_IMAGE_USHORT
203 // INST(unsigned int);
204 // #endif
205 // #ifdef BUILD_IMAGE_DOUBLE
206 // INST(double);
207 // #endif
208 
209 } // namespace BIAS
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
int ScaleShiftBetween(double Min, double Max)
scales and shifts image so afterwards every pixel has a value between Min and Max ...
Definition: Image.cpp:1118
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
float image storage type
Definition: ImageBase.hh:118
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 ...
Definition: Image.cpp:802
int ToUnsignedCharGamma(BIAS::Image< StorageType > &src, BIAS::Image< unsigned char > &dst, float gamma, unsigned int currentMaxVal)
Conversion to unsigned char image, leaves min/max part intact.
Definition: ConvertHDR.cpp:119
unsigned int GetWidth() const
Definition: ImageBase.hh:312
ROI * GetROI()
Returns a pointer to the roi object.
Definition: ImageBase.hh:615
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
INST(unsigned char)
The image template class for specific storage types.
Definition: Image.hh:78
int SetROI(unsigned int UpperLeftX, unsigned int UpperLeftY, unsigned int LowerRightX, unsigned int LowerRightY)
deprecated, use SetROICorners()
Definition: ImageBase.cpp:1033
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
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
int ToUnsignedChar(BIAS::Image< StorageType > &src, BIAS::Image< unsigned char > &dst)
simple conversion to unsigned char, assumed bit depth is 16 -&gt; all pixel values are divided by 65535 ...
Definition: ConvertHDR.cpp:53
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
ConvertHDR()
std Con-/Destructor
Definition: ConvertHDR.cpp:40
unsigned long int GetPixelCount() const
returns number of pixels in image
Definition: ImageBase.hh:422