00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
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
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
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
00108 int c = fgetc(f);
00109 if (c == 10) {
00110
00111 }
00112 else if (c == 13) {
00113
00114 c = fgetc(f);
00115 if (c != 10) ungetc (c,f);
00116 }
00117 else ungetc (c,f);
00118
00119
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
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
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 }