00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <freecloth/gfx/gfxImage.h>
00020 #include <freecloth/resmgt/rcShdPtr.h>
00021
00022
00023
00024
00025
00026
00027 GfxImage::GfxImage(
00028 const RawDataPtr& dataPtr,
00029 UInt32 width,
00030 UInt32 height,
00031 Format format
00032 ) : _dataPtr( dataPtr ),
00033 _width( width ),
00034 _height( height ),
00035 _format( format )
00036 {
00037
00038
00039
00040 DGFX_ASSERT( dataPtr->size() == getRawSize() );
00041 DGFX_ASSERT( getBitsPerComponent( format ) == 8 );
00042 }
00043
00044
00045
00046 GfxImage::GfxImage(
00047 UInt32 width,
00048 UInt32 height,
00049 Format format
00050 ) : _dataPtr( new RawData ),
00051 _width( width ),
00052 _height( height ),
00053 _format( format )
00054 {
00055 _dataPtr->resize( getRawSize(), 0 );
00056
00057
00058
00059 }
00060
00061
00062
00063 GfxImage::GfxImage(
00064 const GfxImage& rhs
00065 ) : _dataPtr( new RawData ),
00066 _width( rhs._width ),
00067 _height( rhs._height ),
00068 _format( rhs._format )
00069 {
00070 _dataPtr->resize( getRawSize(), 0 );
00071 ::memcpy( getRawData(), rhs.getRawData(), getRawSize() );
00072 }
00073
00074
00075 GfxImage::~GfxImage()
00076 {
00077
00078 }
00079
00080
00081
00082 GfxImage::Format GfxImage::getFormat() const
00083 {
00084 return _format;
00085 }
00086
00087
00088
00089 UInt32 GfxImage::getBitsPerComponent( Format format )
00090 {
00091 switch( format ) {
00092 case GREY8: {
00093 return 8;
00094 }
00095
00096 case RGB24: {
00097 return 8;
00098 }
00099 }
00100 return 0;
00101 }
00102
00103
00104
00105 UInt32 GfxImage::getNbComponents( Format format )
00106 {
00107 switch( format ) {
00108 case GREY8: {
00109 return 1;
00110 }
00111
00112 case RGB24: {
00113 return 3;
00114 }
00115 }
00116 return 0;
00117 }
00118
00119
00120
00121 UInt32 GfxImage::getBitsPerPixel() const
00122 {
00123 return getBitsPerComponent( _format ) * getNbComponents( _format );
00124 }
00125
00126
00127
00128 UInt32 GfxImage::getBytesPerPixel() const
00129 {
00130 return getBitsPerPixel() / 8;
00131 }
00132
00133
00134
00135 UInt32 GfxImage::getBytesPerRow() const
00136 {
00137 return getBytesPerPixel() * getWidth();
00138 }
00139
00140
00141
00142 UInt32 GfxImage::getNbComponents() const
00143 {
00144 return getNbComponents( _format );
00145 }
00146
00147
00148
00149 UInt32 GfxImage::getBitsPerComponent() const
00150 {
00151 return getBitsPerComponent( _format );
00152 }
00153
00154
00155
00156 RCShdPtr<GfxImage> GfxImage::pad(
00157 UInt32 padX, UInt32 padY, UInt8 r, UInt8 g, UInt8 b
00158 ) const {
00159 DGFX_ASSERT( getFormat() == GfxImage::RGB24 );
00160 RCShdPtr<GfxImage> result( new GfxImage(
00161 getWidth() + padX*2,
00162 getHeight() + padY*2,
00163 getFormat()
00164 ) );
00165 UInt32 x,y;
00166 for ( y = 0; y < padY; ++y ) {
00167 UInt32 y2 = y + getHeight() + padY;
00168 for ( x = 0; x < result->getWidth(); ++x ) {
00169 result->getData( x, y, 0 ) = result->getData( x, y2, 0 ) = r;
00170 result->getData( x, y, 1 ) = result->getData( x, y2, 1 ) = g;
00171 result->getData( x, y, 2 ) = result->getData( x, y2, 2 ) = b;
00172 }
00173 }
00174 for ( y = 0; y < getHeight(); ++y ) {
00175 UInt32 y2 = y + padY;
00176 for ( x = 0; x < padX; ++x ) {
00177 UInt32 x2 = x + getWidth() + padX;
00178 result->getData( x, y2, 0 ) = result->getData( x2, y2, 0 ) = r;
00179 result->getData( x, y2, 1 ) = result->getData( x2, y2, 1 ) = g;
00180 result->getData( x, y2, 2 ) = result->getData( x2, y2, 2 ) = b;
00181 }
00182 for ( x = 0; x < getWidth(); ++x ) {
00183 UInt32 x2 = x + padX;
00184 result->getData( x2, y2, 0 ) = getData( x, y, 0 );
00185 result->getData( x2, y2, 1 ) = getData( x, y, 1 );
00186 result->getData( x2, y2, 2 ) = getData( x, y, 2 );
00187 }
00188 }
00189 return result;
00190 }
00191
00192
00193
00194 RCShdPtr<GfxImage> GfxImage::pad(
00195 UInt32 padX, UInt32 padY, UInt8 v
00196 ) const {
00197 DGFX_ASSERT( getFormat() == GfxImage::GREY8 );
00198 RCShdPtr<GfxImage> result( new GfxImage(
00199 getWidth() + padX*2,
00200 getHeight() + padY*2,
00201 getFormat()
00202 ) );
00203 UInt32 x,y;
00204 for ( y = 0; y < padY; ++y ) {
00205 UInt32 y2 = y + getHeight() + padY;
00206 for ( x = 0; x < result->getWidth(); ++x ) {
00207 result->getData( x, y ) = result->getData( x, y2 ) = v;
00208 }
00209 }
00210 for ( y = 0; y < getHeight(); ++y ) {
00211 UInt32 y2 = y + padY;
00212 for ( x = 0; x < padX; ++x ) {
00213 UInt32 x2 = x + getWidth() + padX;
00214 result->getData( x, y2 ) = result->getData( x2, y2 ) = v;
00215 }
00216 for ( x = 0; x < getWidth(); ++x ) {
00217 UInt32 x2 = x + padX;
00218 result->getData( x2, y2 ) = getData( x, y );
00219 }
00220 }
00221 return result;
00222 }
00223