Main Page | File List

Canvas.cpp

00001 #include "Canvas.h"
00002 #include "color.h"
00003 #include "xcplusplus.h"
00004 
00005 #include <vector>
00006 #include <algorithm>
00007 
00008 
00009 
00010 void Canvas :: init() 
00011 { 
00012   int N = hReso*vReso;
00013     
00014   delete [] rgbs; 
00015   rgbs = new RGB [N]; 
00016   fill( rgbs, rgbs + N, RGB(0.0, 0.0, 0.0) );
00017 }
00018   
00019 
00020 
00021 void Canvas :: WritePPM(const char *_filename) const
00022 {
00023   Canvas* This =  (Canvas*)this;
00024     
00025   if(!_filename) 
00026     _filename = filename.c_str();
00027 
00028   ofstream file;
00029   file.open (_filename, ios::out | ios::binary);
00030     
00031   if(file) 
00032   {
00033     file << "P6\n";   // color rawbits format
00034     file << hReso << " " << vReso << endl << 100 << endl;
00035   
00036     unsigned char r, g, b;
00037     for (int row = vReso-1; row >= 0; row--) 
00038       for (int col = 0; col < hReso; col++) 
00039       {
00040         RGB& c = This->pixel(row, col);
00041         
00042         if(c[0] < 0 || c[1] < 0 || c[2] < 0)
00043           throw "RGB color underflow\n"; 
00044         if(c[0] > 1.0 || c[1] > 1.0 || c[2] > 1.0)
00045           throw "RGB color overflow\n"; 
00046 
00047         r = (unsigned char) (100.0 * c[0]);  
00048         g = (unsigned char) (100.0 * c[1]);  
00049         b = (unsigned char) (100.0 * c[2]);  
00050 
00051         file << r << g << b;
00052       }
00053 
00054     file << endl;
00055     file.close();
00056   }
00057   else 
00058     cerr << "Can't open output file " << _filename << endl;
00059 }
00060   
00061 
00062 void Canvas :: ReadPPM(const string& filename)
00063 {
00064   ifstream file;
00065   file.open (filename.c_str(), ios::in | ios::binary);
00066     
00067   if(file) 
00068   {
00069     string id;
00070       
00071     skip_comment_lines(file, '#');
00072 
00073     file >> id;
00074 
00075     if(id != "P6") throw "not P6 ppm format\n";   // color rawbits format
00076 
00077     float scale;
00078     skip_comment_lines(file, '#');
00079     file >> hReso >> vReso;
00080     skip_comment_lines(file, '#');
00081     file >> scale;
00082 
00083     init();
00084     unsigned char r, g, b;
00085     file >> noskipws >> r;
00086       
00087     for (int row = vReso-1; row >= 0; row--) 
00088     {
00089       for (int col = 0; col < hReso; col++) 
00090       {
00091         file >> noskipws >> r >> noskipws >> g >> noskipws >> b;
00092         pixel(row, col)[0] = r / scale;  
00093         pixel(row, col)[1] = g / scale;  
00094         pixel(row, col)[2] = b / scale;  
00095       }
00096     }
00097       
00098     file.close();
00099   }
00100   else 
00101     cerr << "Can't open input file " << filename << endl;
00102 }
00103 
00104 
00105 

Generated on Mon Aug 2 16:51:15 2004 by doxygen 1.3.6