00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <freecloth/gfx/gfxImageWriterPNM.h>
00020 #include <freecloth/gfx/gfxImage.h>
00021 #include <freecloth/base/fstream>
00022
00023
00024
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
00041
00042
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
00113 os.write(
00114 reinterpret_cast<const char*>( image.getRawData() ),
00115 image.getRawSize()
00116 );
00117 }
00118 }