Main Page   Class Hierarchy   Compound List   File List   Compound Members  

gfxImageWriterPNM.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2001-2003 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/gfxImageWriterPNM.h>
00020 #include <freecloth/gfx/gfxImage.h>
00021 #include <freecloth/base/fstream>
00022 
00023 ////////////////////////////////////////////////////////////////////////////////
00024 // CLASS GfxImageWriterPNM
00025 
00026 //------------------------------------------------------------------------------
00027 
00028 GfxImageWriterPNM::GfxImageWriterPNM(
00029     FormatType formatType
00030 ) : _formatType( formatType )
00031 {
00032 }
00033 
00034 //------------------------------------------------------------------------------
00035 
00036 void GfxImageWriterPNM::writeImage(
00037     const String& filename,
00038     const GfxImage& image
00039 ) {
00040     // FIXME: ios::binary seems to do bad stuff...
00041     
00042     //if ( _formatType == ASCII ) {
00043     if ( 1 ) {
00044         std::ofstream os( filename.c_str() );
00045         writeImage( os, image );
00046     }
00047     else {
00048         std::ofstream os( filename.c_str(), std::ios::binary );
00049         writeImage( os, image );
00050     }
00051 }
00052 
00053 //------------------------------------------------------------------------------
00054 
00055 void GfxImageWriterPNM::writeImage(
00056     std::ostream& os,
00057     const GfxImage& image
00058 ) {
00059     DGFX_ASSERT( os.good() );
00060 
00061     switch( image.getFormat() ) {
00062         case GfxImage::GREY8: {
00063             if ( _formatType == ASCII ) {
00064                 os << "P2";
00065             }
00066             else {
00067                 os << "P5";
00068             }
00069         } break;
00070         case GfxImage::RGB24: {
00071             if ( _formatType == ASCII ) {
00072                 os << "P3";
00073             }
00074             else {
00075                 os << "P6";
00076             }
00077         } break;
00078     }
00079     os << std::endl;
00080     os << image.getWidth() << " " << image.getHeight() << std::endl;
00081     switch ( image.getBitsPerComponent() ) {
00082         case 8: {
00083             os << "255 ";
00084         } break;
00085         default: {
00086             DGFX_ASSERT( 0 );
00087         }
00088     }
00089 
00090     if ( _formatType == ASCII ) {
00091         UInt32 x, y, c;
00092         switch ( image.getBitsPerComponent() ) {
00093             case 8: {
00094                 for ( y = 0; y < image.getHeight(); ++y ) {
00095                     for ( x = 0; x < image.getWidth(); ++x ) {
00096                         for ( c = 0; c < image.getNbComponents(); ++c ) {
00097                             os << static_cast<UInt32>( image.getData( x, y, c ) );
00098                             os << " ";
00099                         }
00100                         os << " ";
00101                     }
00102                     os << std::endl;
00103                 }
00104             } break;
00105 
00106             default: {
00107                 DGFX_ASSERT( 0 );
00108             }
00109         }
00110     }
00111     else {
00112         // FIXME: dependent upon endianness!
00113         os.write(
00114             reinterpret_cast<const char*>( image.getRawData() ),
00115             image.getRawSize()
00116         );
00117     }
00118 }

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