Back to index

extremetuxracer  0.5beta
image.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00003  * 
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  */
00018 
00019 #include "image.h"
00020 
00021 //Readers
00022 #include "png_reader.h"
00023 #include "rgb_reader.h"
00024 
00025 //Writers
00026 #include "ppm_writer.h"
00027 
00028 
00029 namespace pp {
00030        
00031 Image::Image(int width,int height, int depth)
00032 {
00033        if(width>0 && height>0 && depth>0 && depth <4){ 
00034               this->width=width;
00035               this->height=height;
00036               this->depth=depth;
00037               this->data=new unsigned char[width*height*depth];
00038               return;
00039        }else{
00040               this->width=0;
00041               this->height=0;
00042               this->depth=0;
00043               this->data=0;
00044               return;
00045        }
00046 }
00047 
00048 Image::~Image(void)
00049 {
00050        if(data!=0) delete data;    
00051 }
00052 
00053 Image*
00054 Image::readFile(const char* fileName)
00055 {
00056        int len = strlen(fileName);
00057        
00058        // PNG Image?
00059        if(!strcmp(fileName+len-3,"png")){
00060               Image* image=new ReaderPNG(fileName);
00061               
00062               // check if the image contains valid data
00063               if(image->data!=NULL){
00064                      return image;        
00065               } else {
00066                      delete image;
00067                      return NULL;
00068               }
00069        }else{
00070               // try it as rgb (should we check for this first?)
00071               Image* image=new ReaderRGB(fileName);
00072               return image;        
00073        }
00074 }
00075 
00076 bool 
00077 Image::writeToFile(const char* fileName) const
00078 {
00079        WriterPPM writer(fileName,*this);
00080        
00081        return true;
00082 }
00083 
00084 
00085 } //namespace pp