ImageBase.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "PreCompiled.h"
00019 #ifndef _PreComp_
00020 # include <cmath>
00021 # include <cstring>
00022 #endif
00023
00024 #include "ImageBase.h"
00025 #include <Base/Exception.h>
00026
00027 using namespace Image;
00028
00029
00030 ImageBase::ImageBase()
00031 {
00032 _pPixelData = NULL;
00033 _owner = true;
00034 _width = 0;
00035 _height = 0;
00036 _setColorFormat(IB_CF_GREY8, 8);
00037 }
00038
00039
00040 ImageBase::~ImageBase()
00041 {
00042 try
00043 {
00044 clear();
00045 }
00046 catch(...) {}
00047 }
00048
00049
00050 ImageBase::ImageBase(const ImageBase &rhs)
00051 {
00052
00053 if (rhs._owner == true)
00054 {
00055
00056 _pPixelData = NULL;
00057 _owner = false;
00058 if (createCopy((void *)(rhs._pPixelData), rhs._width, rhs._height, rhs._format, rhs._numSigBitsPerSample) != 0)
00059 throw Base::Exception("ImageBase::ImageBase. Error creating copy of image");
00060 }
00061 else
00062 {
00063
00064 _pPixelData = rhs._pPixelData;
00065 _owner = rhs._owner;
00066 _width = rhs._width;
00067 _height = rhs._height;
00068 _setColorFormat(rhs._format, rhs._numSigBitsPerSample);
00069 }
00070 }
00071
00072
00073 ImageBase & ImageBase::operator=(const ImageBase &rhs)
00074 {
00075 if (this == &rhs)
00076 return *this;
00077
00078
00079 clear();
00080
00081
00082 if (rhs._owner == true)
00083 {
00084
00085 _owner = false;
00086 if (createCopy((void *)(rhs._pPixelData), rhs._width, rhs._height, rhs._format, rhs._numSigBitsPerSample) != 0)
00087 throw Base::Exception("ImageBase::operator=. Error creating copy of image");
00088 }
00089 else
00090 {
00091
00092 _pPixelData = rhs._pPixelData;
00093 _owner = rhs._owner;
00094 _width = rhs._width;
00095 _height = rhs._height;
00096 _setColorFormat(rhs._format, rhs._numSigBitsPerSample);
00097 }
00098
00099 return *this;
00100 }
00101
00102
00103
00104
00105 void ImageBase::clear()
00106 {
00107
00108 if (_owner == true)
00109 {
00110 delete [] _pPixelData;
00111 _pPixelData = NULL;
00112 }
00113
00114 else
00115 {
00116 _pPixelData = NULL;
00117 }
00118
00119
00120 _owner = true;
00121 _width = 0;
00122 _height = 0;
00123 _setColorFormat(IB_CF_GREY8, 8);
00124 }
00125
00126
00127
00128 int ImageBase::_setColorFormat(int format, unsigned short numSigBitsPerSample)
00129 {
00130 switch (format)
00131 {
00132 case IB_CF_GREY8:
00133 _numSamples = 1;
00134 _numBitsPerSample = 8;
00135 _numBytesPerPixel = 1;
00136 break;
00137 case IB_CF_GREY16:
00138 _numSamples = 1;
00139 _numBitsPerSample = 16;
00140 _numBytesPerPixel = 2;
00141 break;
00142 case IB_CF_GREY32:
00143 _numSamples = 1;
00144 _numBitsPerSample = 32;
00145 _numBytesPerPixel = 4;
00146 break;
00147 case IB_CF_RGB24:
00148 _numSamples = 3;
00149 _numBitsPerSample = 8;
00150 _numBytesPerPixel = 3;
00151 break;
00152 case IB_CF_RGB48:
00153 _numSamples = 3;
00154 _numBitsPerSample = 16;
00155 _numBytesPerPixel = 6;
00156 break;
00157 case IB_CF_BGR24:
00158 _numSamples = 3;
00159 _numBitsPerSample = 8;
00160 _numBytesPerPixel = 3;
00161 break;
00162 case IB_CF_BGR48:
00163 _numSamples = 3;
00164 _numBitsPerSample = 16;
00165 _numBytesPerPixel = 6;
00166 break;
00167 case IB_CF_RGBA32:
00168 _numSamples = 4;
00169 _numBitsPerSample = 8;
00170 _numBytesPerPixel = 4;
00171 break;
00172 case IB_CF_RGBA64:
00173 _numSamples = 4;
00174 _numBitsPerSample = 16;
00175 _numBytesPerPixel = 8;
00176 break;
00177 case IB_CF_BGRA32:
00178 _numSamples = 4;
00179 _numBitsPerSample = 8;
00180 _numBytesPerPixel = 4;
00181 break;
00182 case IB_CF_BGRA64:
00183 _numSamples = 4;
00184 _numBitsPerSample = 16;
00185 _numBytesPerPixel = 8;
00186 break;
00187 default:
00188 return -1;
00189 }
00190
00191 if ((numSigBitsPerSample == 0) || (numSigBitsPerSample > _numBitsPerSample))
00192 _numSigBitsPerSample = _numBitsPerSample;
00193 else
00194 _numSigBitsPerSample = numSigBitsPerSample;
00195
00196 _format = format;
00197 return 0;
00198 }
00199
00200
00201
00202
00203
00204 int ImageBase::_allocate()
00205 {
00206
00207 if (_pPixelData != NULL)
00208 return -1;
00209
00210
00211 _owner = true;
00212 try
00213 {
00214 _pPixelData = new unsigned char [_width * _height * _numBytesPerPixel];
00215 }
00216 catch(...)
00217 {
00218
00219 return -1;
00220 }
00221
00222 return 0;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 int ImageBase::createCopy(void* pSrcPixelData, unsigned long width, unsigned long height, int format, unsigned short numSigBitsPerSample)
00234 {
00235
00236 clear();
00237
00238
00239 if (_setColorFormat(format, numSigBitsPerSample) != 0)
00240 return -1;
00241
00242
00243 _width = width;
00244 _height = height;
00245
00246
00247 if (_allocate() != 0)
00248 {
00249 clear();
00250 return -2;
00251 }
00252
00253
00254 memcpy((void *)_pPixelData, pSrcPixelData, _width * _height * _numBytesPerPixel);
00255
00256 return 0;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 int ImageBase::pointTo(void* pSrcPixelData, unsigned long width, unsigned long height, int format, unsigned short numSigBitsPerSample, bool takeOwnership)
00272 {
00273
00274 clear();
00275
00276
00277 if (_setColorFormat(format, numSigBitsPerSample) != 0)
00278 return -1;
00279
00280
00281 _width = width;
00282 _height = height;
00283
00284
00285 _owner = false;
00286 _pPixelData = (unsigned char *)pSrcPixelData;
00287
00288
00289 if (takeOwnership == true)
00290 _owner = true;
00291 else
00292 _owner = false;
00293
00294 return 0;
00295 }
00296
00297
00298
00299
00300 int ImageBase::getSample(int x, int y, unsigned short sampleIndex, double &value)
00301 {
00302 if ((_pPixelData == NULL) ||
00303 (sampleIndex >= _numSamples) ||
00304 (x < 0) || (x >= (int)_width) ||
00305 (y < 0) || (y >= (int)_height))
00306 return -1;
00307
00308
00309 switch (_format)
00310 {
00311 case IB_CF_GREY8:
00312 case IB_CF_RGB24:
00313 case IB_CF_BGR24:
00314 case IB_CF_RGBA32:
00315 case IB_CF_BGRA32:
00316 {
00317 unsigned char* pSample = _pPixelData + _numSamples * (y * _width + x) + sampleIndex;
00318 value = (double)(*pSample);
00319 }
00320 break;
00321 case IB_CF_GREY16:
00322 case IB_CF_RGB48:
00323 case IB_CF_BGR48:
00324 case IB_CF_RGBA64:
00325 case IB_CF_BGRA64:
00326 {
00327 unsigned short* pPix16 = (unsigned short *)_pPixelData;
00328 unsigned short* pSample = pPix16 + _numSamples * (y * _width + x) + sampleIndex;
00329 value = (double)(*pSample);
00330 }
00331 break;
00332 case IB_CF_GREY32:
00333 {
00334 unsigned long* pPix32 = (unsigned long *)_pPixelData;
00335 unsigned long* pSample = pPix32 + y * _width + x;
00336 value = (double)(*pSample);
00337 }
00338 break;
00339 default:
00340 return -1;
00341 }
00342 return 0;
00343 }
00344
00345
00346