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
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <cmtkconfig.h>
00034
00035 #include <Pipeline/cmtkImageRGB.h>
00036
00037 namespace
00038 cmtk
00039 {
00040
00043
00044 ImageRGB *ImageRGB::New()
00045 {
00046 return new ImageRGB;
00047 }
00048
00049 ImageRGB::ImageRGB()
00050 {
00051 Data = NULL;
00052 DataSize = 0;
00053 AlphaChannel = IMAGE_RGB;
00054 BytesPerPixel = 3;
00055 }
00056
00057 ImageRGB::~ImageRGB()
00058 {
00059 Memory::DeleteArray( this->Data );
00060 }
00061
00062 byte*
00063 ImageRGB::GetDataPtr( const bool forceAlloc )
00064 {
00065 if ( ! forceAlloc ) return Data;
00066
00067 if ( Data == NULL )
00068 {
00069 DataSize = BytesPerPixel * this->Plane::GetNumPixels();
00070 Data = Memory::AllocateArray<byte>( DataSize );
00071 }
00072 else
00073 {
00074 if ( DataSize != (BytesPerPixel * this->Plane::GetNumPixels()) )
00075 {
00076 Memory::DeleteArray( Data );
00077 Data = NULL;
00078 return this->GetDataPtr( true );
00079 }
00080 }
00081 return Data;
00082 }
00083
00084 void ImageRGB::SetAlphaChannel( const ImageAlphaToggle alphaChannel, const bool convertData )
00085 {
00086 if ( alphaChannel != AlphaChannel )
00087 {
00088 AlphaChannel = alphaChannel;
00089 BytesPerPixel = (AlphaChannel == IMAGE_RGB) ? 3 : 4;
00090
00091 byte *oldData = Data;
00092
00093 Data = NULL;
00094 this->GetDataPtr( true );
00095
00096
00097 if ( convertData )
00098 {
00099 byte *fromPtr = oldData;
00100 byte *toPtr = Data;
00101 unsigned int numberOfPixels = this->GetNumPixels();
00102
00103 if ( AlphaChannel == IMAGE_RGB )
00104 {
00105
00106 for ( unsigned int i = 0; i < numberOfPixels; ++i, fromPtr += 4, toPtr += 3 )
00107 {
00108 toPtr[0] = fromPtr[0];
00109 toPtr[1] = fromPtr[1];
00110 toPtr[2] = fromPtr[2];
00111 }
00112 }
00113 else
00114 {
00115
00116 for ( unsigned int i = 0; i < numberOfPixels; ++i, fromPtr += 3, toPtr += 4 )
00117 {
00118 toPtr[0] = fromPtr[0];
00119 toPtr[1] = fromPtr[1];
00120 toPtr[2] = fromPtr[2];
00121 toPtr[3] = 255;
00122 }
00123 }
00124 }
00125
00126 Memory::DeleteArray( oldData );
00127 }
00128 }
00129
00130 void ImageRGB::GetPixel( RGBA& rgb, const int index )
00131 {
00132 byte* pixelPtr = Data + (BytesPerPixel * index);
00133 rgb.R = pixelPtr[0];
00134 rgb.G = pixelPtr[1];
00135 rgb.B = pixelPtr[2];
00136 if ( AlphaChannel == IMAGE_RGBA )
00137 rgb.Alpha = pixelPtr[3];
00138 else
00139 rgb.Alpha = 255;
00140 }
00141
00142 void ImageRGB::SetPixel( const int index, const RGBA& rgb )
00143 {
00144 byte* pixelPtr = Data + (BytesPerPixel * index);
00145 pixelPtr[0] = rgb.R;
00146 pixelPtr[1] = rgb.G;
00147 pixelPtr[2] = rgb.B;
00148 if ( AlphaChannel == IMAGE_RGBA )
00149 pixelPtr[3] = rgb.Alpha;
00150 }
00151
00152 void ImageRGB::GetPixel( RGB& rgb, const int index )
00153 {
00154 byte* pixelPtr = Data + (BytesPerPixel * index);
00155 rgb.R = pixelPtr[0];
00156 rgb.G = pixelPtr[1];
00157 rgb.B = pixelPtr[2];
00158 }
00159
00160 void ImageRGB::SetPixel( const int index, const RGB& rgb )
00161 {
00162 byte* pixelPtr = Data + (BytesPerPixel * index);
00163 pixelPtr[0] = rgb.R;
00164 pixelPtr[1] = rgb.G;
00165 pixelPtr[2] = rgb.B;
00166 }
00167
00168 bool
00169 ImageRGB::IsGreyscale() const
00170 {
00171 unsigned int numPixels = this->GetNumPixels();
00172 const byte *data = this->GetDataPtr();
00173 byte increment = (this->GetAlphaChannel() == IMAGE_RGB) ? 3 : 4;
00174
00175 for ( unsigned int idx = 0; idx < numPixels; ++idx, data += increment )
00176 {
00177 if ( (data[0] != data[1]) || (data[1] != data[2]) )
00178 return 0;
00179 }
00180
00181 return 1;
00182 }
00183
00184 }