Main Page   Class Hierarchy   Compound List   File List   Compound Members  

gfxImage.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2001-2002 David Pritchard <drpritch@alumni.uwaterloo.ca>
00003 // 
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later
00008 // version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 #include <freecloth/gfx/gfxImage.h>
00020 #include <freecloth/resmgt/rcShdPtr.h>
00021 
00022 ////////////////////////////////////////////////////////////////////////////////
00023 // CLASS GfxImage
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     //DGFX_TRACE_ENTER( "GfxImage ctor" );
00038     //DGFX_TRACE( "width,height = " << _width << ", " << _height );
00039     //DGFX_TRACE( "format = " << _format );
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     //DGFX_TRACE_ENTER( "GfxImage ctor 2" );
00057     //DGFX_TRACE( "width,height = " << _width << ", " << _height );
00058     //DGFX_TRACE( "format = " << _format );
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     //DGFX_TRACE_ENTER( "GfxImage dtor" );
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 

Generated on Fri May 2 16:51:13 2003 for Freecloth by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002