imageppm.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  *                                                                         *
00022  *   $Log: imageppm.cpp,v $
00023  *   Revision 1.4  2005/11/09 13:31:51  martius
00024  *   GPL'ised
00025  *
00026  ***************************************************************************/
00027 #include "imageppm.h"
00028 #include "stdio.h"
00029 
00030 static int readNumber (char *filename, FILE *f)
00031 {
00032   int c,n=0;
00033   for(;;) {
00034     c = fgetc(f);
00035     if (c==EOF) fprintf (stderr,"unexpected end of file in \"%s\"\n",filename);
00036     if (c >= '0' && c <= '9') n = n*10 + (c - '0');
00037     else {
00038       ungetc (c,f);
00039       return n;
00040     }
00041   }
00042 }
00043 
00044 
00045 static void skipWhiteSpace (char *filename, FILE *f)
00046 {
00047   int c,d;
00048   for(;;) {
00049     c = fgetc(f);
00050     if (c==EOF) fprintf (stderr, "unexpected end of file in \"%s\"\n",filename);
00051 
00052     // skip comments
00053     if (c == '#') {
00054       do {
00055         d = fgetc(f);
00056         if (d==EOF) fprintf (stderr, "unexpected end of file in \"%s\"\n",filename);
00057       } while (d != '\n');
00058       continue;
00059     }
00060 
00061     if (c > ' ') {
00062       ungetc (c,f);
00063       return;
00064     }
00065   }
00066 }
00067 
00068 
00069 
00070 ImagePPM::ImagePPM () 
00071 {   image_data = 0;
00072 }
00073 
00074 ImagePPM::ImagePPM (int width, int height, unsigned char* data){
00075   image_width = width;
00076   image_height = height;
00077   image_data = data;
00078 }
00079 
00080 
00081 int ImagePPM::loadImage(char*filename)
00082 {
00083   FILE *f = fopen (filename,"rb");
00084   if (!f) 
00085   {  fprintf (stderr, "Can't open image file `%s'\n", filename);
00086      return 0;
00087   }
00088 
00089   // read in header
00090   if (fgetc(f) != 'P' || fgetc(f) != '6')
00091     fprintf (stderr, "image file \"%s\" is not a binary PPM (no P6 header)\n",filename);
00092   skipWhiteSpace (filename,f);
00093 
00094   // read in image parameters
00095   image_width = readNumber (filename,f);
00096   skipWhiteSpace (filename,f);
00097   image_height = readNumber (filename,f);
00098   skipWhiteSpace (filename,f);
00099   int max_value = readNumber (filename,f);
00100 
00101   // check values
00102   if (image_width < 1 || image_height < 1)
00103     fprintf (stderr, "bad image file \"%s\"\n",filename);
00104   if (max_value != 255)
00105     fprintf (stderr, "image file \"%s\" must have color range of 255\n",filename);
00106 
00107   // read either nothing, LF (10), or CR,LF (13,10)
00108   int c = fgetc(f);
00109   if (c == 10) {
00110     // LF
00111   }
00112   else if (c == 13) {
00113     // CR
00114     c = fgetc(f);
00115     if (c != 10) ungetc (c,f);
00116   }
00117   else ungetc (c,f);
00118 
00119   // read in rest of data
00120   image_data = new unsigned char [image_width*image_height*3];
00121   if (fread( image_data, image_width*image_height*3, 1, f) != 1){
00122     fprintf (stderr, "Can not read data from image file `%s'\n",filename);
00123     return 0;
00124   } 
00125   fclose (f);
00126   return 1;
00127 }
00128 
00129 
00130 int ImagePPM::storeImage(char*filename)
00131 {
00132   FILE *f = fopen (filename,"wb");
00133   if (!f) 
00134   {  fprintf (stderr, "Can't open image file `%s'\n", filename);
00135      return 0;
00136   }
00137 
00138   // write header
00139   fprintf(f,"P6\n");
00140   fprintf(f,"# CREATOR ImagePPM class of lpzrobots project\n");
00141   fprintf(f,"%i %i\n", image_width, image_height);
00142   fprintf(f,"255\n");
00143 
00144   // write data
00145   if (fwrite( image_data, image_width*image_height*3, 1, f) != 1){
00146     fprintf (stderr, "Can not write data toimage file `%s'\n",filename);
00147     return 0;
00148   } 
00149   fclose (f);
00150   return 1;
00151 }
00152 
00153 
00154 ImagePPM::~ImagePPM()
00155 {
00156   if(image_data) delete[] image_data;
00157 }

Generated on Tue Apr 4 19:05:03 2006 for Robotsystem from Robot Group Leipzig by  doxygen 1.4.5