CImage.cpp

Go to the documentation of this file.
00001 
00012 #include <fstream>
00013 #include <iostream>
00014 #include <string>
00015 
00016 #include "popassert.h"
00017 #include "CImage.h"
00018 
00019 
00020 // The following structure must be packed that way.
00021 #pragma pack( 1 )
00022 
00023 
00027 struct bmpinfoheader
00028 {
00029   unsigned long biSize;
00030   long biWidth;
00031   long biHeight;
00032   unsigned short biPlanes;
00033   unsigned short biBitCount;
00034   unsigned long biCompression;
00035   unsigned long biSizeImage;
00036   long biXPelsPerMeter;
00037   long biYPelsPerMeter;
00038   unsigned long biClrUsed;
00039   unsigned long biClrImportant;
00040 };
00041 
00045 struct bmpfileheader
00046 {
00047   unsigned short bfType;
00048   unsigned long bfSize;
00049   unsigned short bfReserved1;
00050   unsigned short bfReserved2;
00051   unsigned long bfOffBits;
00052 };
00053 
00054 #pragma pack( )
00055 
00056 
00061 int CImage::WriteBmpFile(const std::string& FileName) const
00062 {
00063   bmpinfoheader bmih;  // Structure containing a header
00064   bmpfileheader bmfh;  // Structure containing a header
00065 
00066   unsigned int extrabytes, bytesize; 
00067 
00068   // Used to add bytes so that width is a multiple of 4
00069   extrabytes = (4 - (m_Width * 3) % 4) % 4;
00070 
00071   // Size of the image
00072   bytesize = (m_Width * 3 + extrabytes) * m_Height;
00073 
00074   // Info needed by BMP header
00075   bmfh.bfType = ('M' << 8) + 'B';
00076   bmfh.bfSize = 0;
00077   bmfh.bfReserved1 = 0;
00078   bmfh.bfReserved2 = 0;
00079   bmfh.bfOffBits = sizeof(bmpfileheader) + sizeof(bmpinfoheader);
00080 
00081   bmih.biSize = sizeof(bmpinfoheader);
00082   bmih.biWidth = m_Width;
00083   bmih.biHeight = m_Height;
00084   bmih.biPlanes = 1; // Must be that
00085   bmih.biBitCount = 24; // 3 * sizeof(char), in bits
00086   bmih.biCompression = 0L; // Correspond to BI_RGB
00087   bmih.biSizeImage = bytesize;
00088   bmih.biXPelsPerMeter = 0;
00089   bmih.biYPelsPerMeter = 0;
00090   bmih.biClrUsed = 0;
00091   bmih.biClrImportant = 0;
00092 
00093 
00094   // File writing
00095   std::ofstream File;
00096 
00097   File.open(FileName.c_str(), std::ios::binary);
00098   if (File.fail())
00099     {
00100       return -1;
00101     }
00102 
00103   // Writing of the headers
00104   File.write( (char *) &bmfh, sizeof(bmpfileheader));
00105   File.write( (char *) &bmih, sizeof(bmpinfoheader));
00106 
00107 
00108   // We allocate memory for a phony pointer to complete line so they
00109   // hold a multiple of 4 bytes
00110   char *pTemp = new char[extrabytes];
00111 
00112   char ColorTemp[3];
00113 
00114   // (b, g, r) colors are written line by line, from the lower left corner
00115   // to the upper right corner
00116 
00117   for (int j = m_Height; j > 0; --j)
00118     {
00119       for (int i = 0; i < m_Width; ++i)
00120         {
00121           ColorTemp[0] = char (255 * (*this)(i, j-1, 2));
00122           ColorTemp[1] = char (255 * (*this)(i, j-1, 1));
00123           ColorTemp[2] = char (255 * (*this)(i, j-1, 0));
00124           File.write( (char *) &ColorTemp, 3*sizeof(char));
00125         }
00126       File.write( (char *) &pTemp, extrabytes*sizeof(char));
00127     }
00128 
00129   delete []pTemp;
00130 
00131   bool ReturnValue = !File.fail();
00132 
00133   File.close();
00134 
00135   ReturnValue = ReturnValue && !File.fail();
00136 
00137   return ( (ReturnValue) ? 0 : -2 );
00138 }
00139 

Generated on Fri Dec 5 03:20:33 2008 for Mathematical Ray-tracer by  doxygen 1.5.4