Follow this link to skip to the main content

claraty::Image_IO_Pgm_Simple Class Reference

#include <image_io_pgm_simple.h>

List of all members.

Public Member Functions

 Image_IO_Pgm_Simple ()
void save (const char *filename, const Image< unsigned char > &src_image)
void load (const char *filename, Image< unsigned char > &dest_image)

Private Member Functions

bool _get_next_num (FILE *file, int &value)


Detailed Description

Definition at line 44 of file image_io_pgm_simple.h.


Constructor & Destructor Documentation

claraty::Image_IO_Pgm_Simple::Image_IO_Pgm_Simple (  ) 

Definition at line 27 of file image_io_pgm_simple.cc.

00027                                          {
00028 }


Member Function Documentation

void claraty::Image_IO_Pgm_Simple::save ( const char *  filename,
const Image< unsigned char > &  src_image 
)

Definition at line 31 of file image_io_pgm_simple.cc.

References claraty::Image< T >::get_height(), claraty::Image< T >::get_raster(), claraty::Image< T >::get_width(), and claraty::N_2D_Array< T >::is_subarray().

00032                                                                       {
00033 
00034   FILE* image_file = fopen(filename, "w");
00035 
00036   if(!image_file) {
00037     printf("Image_IO_Pgm_Simple::save: could not open file: %s\n", filename);
00038     return;
00039   }
00040 
00041   fprintf(image_file, "P5\n%u %u\n255\n", src_image.get_width(), 
00042           src_image.get_height());
00043 
00044   if (src_image.is_subarray()) {
00045     Image<unsigned char> compact(src_image);
00046     fwrite(compact.get_raster(), 1, src_image.get_width() * src_image.get_height(),
00047            image_file);
00048   } else
00049     fwrite(src_image.get_raster(), 1, src_image.get_width()*src_image.get_height(),
00050            image_file);
00051 
00052   fclose(image_file);
00053 }

Here is the call graph for this function:

void claraty::Image_IO_Pgm_Simple::load ( const char *  filename,
Image< unsigned char > &  dest_image 
)

Definition at line 74 of file image_io_pgm_simple.cc.

References _get_next_num(), and claraty::N_2D_Array< T >::resize().

00075                                                                  {
00076 
00077   FILE* image_file = fopen(filename, "r");
00078 
00079   if(!image_file) {
00080     printf("Image_IO_Pgm_Simple::load: could not open file: %s\n", filename);
00081     return;
00082   }
00083 
00084   char str[100];
00085   int width, height;
00086   int depth;
00087 
00088   fscanf(image_file, "%s", str);
00089   //cout << "got " << str << endl;
00090 
00091   if(str[0] != 'P') {
00092     printf("Image_IO_Pgm_Simple::load: not a pnm file.\n");
00093     fclose(image_file);
00094     return;
00095   }
00096 
00097   if(str[1] == '5') {
00098     //printf("Image_IO_Pgm_Simple::load: got format 5\n");
00099 
00100     _get_next_num(image_file, width);
00101     _get_next_num(image_file, height);
00102     _get_next_num(image_file, depth);
00103 
00104     int whitespace = fgetc(image_file);
00105     assert(isspace(whitespace));
00106 
00107     //    printf("got: %d %d %d\n", width, height, depth);
00108 
00109     dest_image.resize(height, width);
00110 
00111     fread(&dest_image(0,0), 1, width*height, image_file);
00112     fclose(image_file);
00113     return;
00114   }
00115   else {
00116     printf("Image_IO_Pgm_Simple::load: format not supported\n");
00117     fclose(image_file);
00118     return;
00119   }
00120     
00121 }

Here is the call graph for this function:

bool claraty::Image_IO_Pgm_Simple::_get_next_num ( FILE *  file,
int &  value 
) [private]

Definition at line 55 of file image_io_pgm_simple.cc.

Referenced by load().

00055                                                               {
00056 
00057   char str[100];
00058 
00059   fscanf(file, "%s", str);
00060   //  cout << "first got: " << str << endl;
00061 
00062   if(str[0] == '#') {
00063     fscanf(file, "%[^\n]", str);
00064     //cout << "got: " << str << endl;
00065     return _get_next_num(file, value);
00066   }
00067   else {
00068     sscanf(str, "%d", &value);
00069     return true;
00070   }
00071 }


The documentation for this class was generated from the following files: