Follow this link to skip to the main content

claraty::Image_IO_Factory Class Reference

#include <image_io_factory.h>

List of all members.

Public Member Functions

 Image_IO_Factory ()
virtual ~Image_IO_Factory ()
virtual bool save_image_bytes (const std::string &filename, unsigned char *raster, int nr, int nc, int bytes_per_pixel, int sig_bits, const std::string &comments, bool color_image=false)=0
virtual bool load_image_bytes (const std::string &filename, unsigned char **&rows, unsigned int &nr, unsigned int &nc, std::string &comments, int &maxval, int &bytes_per_pixel, bool &color)=0

Static Public Member Functions

template<typename T>
static unsigned char * rasterize_image (const Image< T > &img, bool &new_storage_allocated)
template<typename T>
static unsigned char * rasterize_image (const RGB_Image< T > &img)
template<typename T>
static void move_raster_to_image (unsigned char **rows, int bytes_per_pixel, Image< T > &img, bool color)
template<typename T>
static void move_raster_to_image (unsigned char **rows, int bytes_per_pixel, RGB_Image< T > &img, bool color)
template<typename T>
static int compute_sigbits (const Image< T > &img)
template<typename T>
static bool save_image (const std::string &filename, const Image< T > &img, bool scale_to_255=false)
template<typename T>
static bool save_image (const std::string &filename, const RGB_Image< T > &img, bool scale_to_255=false)
template<typename T>
static bool load_image (const std::string &filename, Image< T > &img)
template<typename T>
static bool load_image (const std::string &filename, RGB_Image< T > &img)

Static Public Attributes

static Factory< Image_IO_Factory,
std::string > * 
factory

Static Private Member Functions

template<typename T>
static unsigned char * host_to_net (const T *hostraster, int numPixels)


Detailed Description

Definition at line 59 of file image_io_factory.h.


Constructor & Destructor Documentation

claraty::Image_IO_Factory::Image_IO_Factory (  )  [inline]

Definition at line 66 of file image_io_factory.h.

00066 { }

virtual claraty::Image_IO_Factory::~Image_IO_Factory (  )  [inline, virtual]

Definition at line 67 of file image_io_factory.h.

00067 { }


Member Function Documentation

template<typename T>
unsigned char * claraty::Image_IO_Factory::host_to_net ( const T *  hostraster,
int  numPixels 
) [static, private]

Definition at line 165 of file image_io_factory.h.

Referenced by rasterize_image().

00167 {
00168   unsigned char *netraster = new unsigned char[numPixels * 2];
00169   for (int i = 0; i < numPixels; i++) {
00170     unsigned short pix = static_cast<unsigned short>(hostraster[i]);
00171     netraster[i * 2] = (pix & 0xff00) >> 8;
00172     netraster[i * 2 + 1] = pix & 0x00ff;
00173   }
00174   return netraster;
00175 }

virtual bool claraty::Image_IO_Factory::save_image_bytes ( const std::string &  filename,
unsigned char *  raster,
int  nr,
int  nc,
int  bytes_per_pixel,
int  sig_bits,
const std::string &  comments,
bool  color_image = false 
) [pure virtual]

Referenced by save_image().

virtual bool claraty::Image_IO_Factory::load_image_bytes ( const std::string &  filename,
unsigned char **&  rows,
unsigned int &  nr,
unsigned int &  nc,
std::string &  comments,
int &  maxval,
int &  bytes_per_pixel,
bool &  color 
) [pure virtual]

Referenced by load_image().

template<typename T>
unsigned char * claraty::Image_IO_Factory::rasterize_image ( const Image< T > &  img,
bool &  new_storage_allocated 
) [static]

Definition at line 179 of file image_io_factory.h.

References claraty::Image< T >::get_raster(), host_to_net(), claraty::N_2D_Array< T >::is_subarray(), claraty::N_2D_Array< T >::ncols(), and claraty::N_2D_Array< T >::nrows().

Referenced by save_image().

00181 {
00182   if (!img.is_subarray() && (sizeof(T) == 1 || htons(0xf00f) == 0xf00f)) {
00183     new_storage_allocated = false;
00184     return const_cast<unsigned char*>
00185       (reinterpret_cast<const unsigned char*>(img.get_raster()));
00186   }
00187 
00188   new_storage_allocated = true;
00189   size_t num_image_bytes = img.nrows() * img.ncols() * sizeof(T);
00190 
00191   Image<T> flattened_image(img);
00192 
00193   if (sizeof(T) == 1 || htons(0xf00f) == 0xf00f) {
00194     unsigned char *raster =
00195       reinterpret_cast<unsigned char*>(flattened_image.get_raster());
00196     unsigned char *raster_copy = new unsigned char[num_image_bytes];
00197     std::copy(raster, raster + num_image_bytes, raster_copy);
00198     return raster_copy;
00199   }
00200 
00201   // if we get this far, it means we've got > 1 byte per pixel, and we're
00202   // not in network byte order.
00203   return host_to_net(flattened_image.get_raster(),
00204                      img.nrows() * img.ncols());
00205 }

Here is the call graph for this function:

template<typename T>
unsigned char * claraty::Image_IO_Factory::rasterize_image ( const RGB_Image< T > &  img  )  [static]

Definition at line 208 of file image_io_factory.h.

References claraty::RGB_Image< T >::get_color(), claraty::RGB_Image< T >::get_num_of_cols(), and claraty::RGB_Image< T >::get_num_of_rows().

00209 {
00210   T *raster = new T[img.get_num_of_rows() * img.get_num_of_cols() * 3];
00211   T *ri = raster;
00212   for (int r = 0; r < img.get_num_of_rows(); r++)
00213     for (int c = 0; c < img.get_num_of_cols(); c++) {
00214       RGB_Color<T> color = img.get_color(c, r);
00215       if (sizeof(T) == 1) {
00216         *ri++ = color.red;
00217         *ri++ = color.green;
00218         *ri++ = color.blue;
00219       } else {
00220         *ri++ = htons(color.red);
00221         *ri++ = htons(color.green);
00222         *ri++ = htons(color.blue);
00223       }
00224     }
00225   return reinterpret_cast<unsigned char*>(raster);
00226 }

Here is the call graph for this function:

template<typename T>
void claraty::Image_IO_Factory::move_raster_to_image ( unsigned char **  rows,
int  bytes_per_pixel,
Image< T > &  img,
bool  color 
) [static]

Definition at line 229 of file image_io_factory.h.

References claraty::Image< T >::get_raster(), claraty::N_2D_Array< T >::ncols(), and claraty::N_2D_Array< T >::nrows().

Referenced by load_image().

00232 {
00233   T *pixel = img.get_raster();
00234   for (int r = 0; r < img.nrows(); r++) {
00235     unsigned char *row = rows[r];
00236     if (sizeof(T) == 1 && bytes_per_pixel == 1 && !color_img) {
00237       std::copy(row, row + img.ncols(), pixel);
00238       pixel += img.ncols();
00239     } else {
00240       unsigned char *ri = row;
00241       if (color_img) {
00242         for (int c = 0; c < img.ncols(); c++, ri += bytes_per_pixel * 3) {
00243           if (bytes_per_pixel == 1)
00244             *pixel++ = (ri[0] + ri[1] + ri[2]) / 3;
00245           else
00246             *pixel++ =
00247               (ntohs(reinterpret_cast<unsigned short*>(ri)[0]) +
00248                ntohs(reinterpret_cast<unsigned short*>(ri)[1]) +
00249                ntohs(reinterpret_cast<unsigned short*>(ri)[2])) / 3;
00250         }
00251       } else {
00252         for (int c = 0; c < img.ncols(); c++, ri += bytes_per_pixel) {
00253           if (bytes_per_pixel == 1)
00254             *pixel++ = *ri;
00255           else
00256             *pixel++ = ntohs(*reinterpret_cast<unsigned short*>(ri));
00257         }
00258       }
00259     }
00260     delete [] row;
00261   }
00262   delete [] rows;
00263 }

Here is the call graph for this function:

template<typename T>
void claraty::Image_IO_Factory::move_raster_to_image ( unsigned char **  rows,
int  bytes_per_pixel,
RGB_Image< T > &  img,
bool  color 
) [static]

Definition at line 266 of file image_io_factory.h.

References claraty::RGB_Image< T >::get_num_of_cols(), claraty::RGB_Image< T >::get_num_of_rows(), and claraty::RGB_Image< T >::set_color().

00269 {
00270   for (int r = 0; r < img.get_num_of_rows(); r++) {
00271     unsigned char *row = rows[r];
00272     unsigned char *ri = row;
00273     if (color_img) {
00274       for (int c = 0; c < img.get_num_of_cols(); c++) {
00275         RGB_Color<T> color;
00276         if (bytes_per_pixel == 1) {
00277           color.red = *ri++;
00278           color.green = *ri++;
00279           color.blue = *ri++;
00280         } else {
00281           color.red = ntohs(*reinterpret_cast<unsigned short*>(ri));
00282           ri += bytes_per_pixel;
00283           color.green = ntohs(*reinterpret_cast<unsigned short*>(ri));
00284           ri += bytes_per_pixel;
00285           color.blue = ntohs(*reinterpret_cast<unsigned short*>(ri));
00286           ri += bytes_per_pixel;
00287         }
00288         img.set_color(c, r, color);
00289       }
00290     } else {
00291       for (int c = 0; c < img.get_num_of_cols(); c++) {
00292         RGB_Color<T> color;
00293         if (bytes_per_pixel == 1)
00294           color.red = *ri;
00295         else
00296           color.red = ntohs(*reinterpret_cast<unsigned short*>(ri));
00297         color.green = color.red;
00298         color.blue = color.red;
00299         ri += bytes_per_pixel;
00300         img.set_color(c, r, color);
00301       }
00302     }
00303     delete [] row;
00304   }
00305   delete [] rows;
00306 }

Here is the call graph for this function:

template<typename T>
int claraty::Image_IO_Factory::compute_sigbits ( const Image< T > &  img  )  [static]

Definition at line 309 of file image_io_factory.h.

References claraty::Image< T >::get_maximum_value(), and claraty::Matrix< T >::max_value().

Referenced by save_image().

00310 {
00311   T maxval = img.get_maximum_value();
00312   if (maxval == 0)
00313     maxval = img.max_value(NULL, NULL);
00314   int bitsMax = 1;
00315   int sigbits = 1;
00316   while (bitsMax < maxval && bitsMax < 65535) {
00317     bitsMax = (bitsMax << 1) | 1;
00318     sigbits++;
00319   }
00320   /*
00321   if (sizeof(T) > 1) {
00322     bitsMax = cl_max(bitsMax, 256);
00323     sigbits = cl_max(sigbits, 9);
00324   }
00325   */
00326   return sigbits;
00327 }

Here is the call graph for this function:

template<typename T>
bool claraty::Image_IO_Factory::save_image ( const std::string &  filename,
const Image< T > &  img,
bool  scale_to_255 = false 
) [static]

Definition at line 330 of file image_io_factory.h.

References compute_sigbits(), claraty::Rescale_Op< T >::filter(), claraty::N_2D_Array< T >::ncols(), claraty::N_2D_Array< T >::nrows(), rasterize_image(), save_image_bytes(), and claraty::string_suffix().

Referenced by save_image().

00332 {
00333   if (scale_to_255) {
00334     Image<unsigned char> scaled_image;
00335     Rescale_Op<T> rescale(0, 255);
00336     rescale.filter(img, scaled_image);
00337     return save_image(filename, scaled_image, false);
00338   }
00339 
00340   Image_IO_Factory *io =
00341     (*Image_IO_Factory::factory)(string_suffix(filename));
00342   if (io == NULL) {
00343     std::cerr << "Unable to find helper class for saving images of type "
00344               << string_suffix(filename) << std::endl;
00345     return false;
00346   }
00347   bool new_storage;
00348   unsigned char *raster = rasterize_image(img, new_storage);
00349 
00350   bool ok = io->save_image_bytes(filename, raster,
00351                                  img.nrows(), img.ncols(), sizeof(T),
00352                                  compute_sigbits(img), "", false);
00353   if (new_storage)
00354     delete [] raster;
00355   delete io;
00356   return ok;
00357 }

Here is the call graph for this function:

template<typename T>
bool claraty::Image_IO_Factory::save_image ( const std::string &  filename,
const RGB_Image< T > &  img,
bool  scale_to_255 = false 
) [static]

Definition at line 360 of file image_io_factory.h.

References compute_sigbits(), claraty::Rescale_Op< T >::filter(), claraty::RGB_Image< T >::get_blue_channel(), claraty::RGB_Image< T >::get_green_channel(), claraty::RGB_Image< T >::get_num_of_cols(), claraty::RGB_Image< T >::get_num_of_rows(), claraty::RGB_Image< T >::get_red_channel(), rasterize_image(), save_image(), save_image_bytes(), and claraty::string_suffix().

00362 {
00363   if (scale_to_255) {
00364     RGB_Image<unsigned char> scaled_image;
00365     Rescale_Op<T> rescale(0, 255);
00366     rescale.filter(img.get_red_channel(), scaled_image.get_red_channel());
00367     rescale.filter(img.get_green_channel(), scaled_image.get_green_channel());
00368     rescale.filter(img.get_blue_channel(), scaled_image.get_blue_channel());
00369     return save_image(filename, scaled_image, false);
00370   }
00371 
00372   Image_IO_Factory *io =
00373     (*Image_IO_Factory::factory)(string_suffix(filename));
00374   if (io == NULL) {
00375     std::cerr << "Unable to find helper class for saving images of type "
00376               << string_suffix(filename) << std::endl;
00377     return false;
00378   }
00379   unsigned char *raster = rasterize_image(img);
00380 
00381   bool ok = io->save_image_bytes(filename, raster,
00382                                  img.get_num_of_rows(),
00383                                  img.get_num_of_cols(), sizeof(T),
00384                                  compute_sigbits(img.get_red_channel()), "",
00385                                  true);
00386     
00387   delete [] raster;
00388   delete io;
00389   return ok;
00390 }

Here is the call graph for this function:

template<typename T>
bool claraty::Image_IO_Factory::load_image ( const std::string &  filename,
Image< T > &  img 
) [static]

Definition at line 393 of file image_io_factory.h.

References load_image_bytes(), move_raster_to_image(), claraty::N_2D_Array< T >::resize(), claraty::Image< T >::set_maximum_value(), and claraty::string_suffix().

00394 {
00395   Image_IO_Factory *io =
00396     (*Image_IO_Factory::factory)(string_suffix(filename));
00397   if (io == NULL) {
00398     std::cerr << "Unable to find helper class for loading images of type "
00399               << string_suffix(filename) << std::endl;
00400     return false;
00401   }
00402   unsigned char **rows;
00403   unsigned int nr, nc;
00404   std::string comments;
00405   int maxval;
00406   int bytes_per_pixel;
00407   bool color;
00408 
00409   bool ok = io->load_image_bytes(filename, rows, nr, nc, comments, maxval,
00410                                  bytes_per_pixel, color);
00411 
00412   if (!ok) {
00413     delete io;
00414     return false;
00415   }
00416   img.resize(nr, nc);
00417   img.set_maximum_value(maxval);
00418   move_raster_to_image(rows, bytes_per_pixel, img, color);
00419 
00420   delete io;
00421   return true;
00422 }

Here is the call graph for this function:

template<typename T>
bool claraty::Image_IO_Factory::load_image ( const std::string &  filename,
RGB_Image< T > &  img 
) [static]

Definition at line 425 of file image_io_factory.h.

References claraty::RGB_Image< T >::get_blue_channel(), claraty::RGB_Image< T >::get_green_channel(), claraty::RGB_Image< T >::get_red_channel(), load_image_bytes(), move_raster_to_image(), claraty::RGB_Image< T >::resize(), and claraty::string_suffix().

00427 {
00428   Image_IO_Factory *io =
00429     (*Image_IO_Factory::factory)(string_suffix(filename));
00430   if (io == NULL) {
00431     std::cerr << "Unable to find helper class for loading images of type "
00432               << string_suffix(filename) << std::endl;
00433     return false;
00434   }
00435   unsigned char **rows;
00436   unsigned int nr, nc;
00437   std::string comments;
00438   int maxval;
00439   int bytes_per_pixel;
00440   bool color;
00441 
00442   bool ok = io->load_image_bytes(filename, rows, nr, nc, comments, maxval,
00443                                  bytes_per_pixel, color);
00444 
00445   if (!ok) {
00446     delete io;
00447     return false;
00448   }
00449   img.resize(nr, nc);
00450   img.get_red_channel().set_maximum_value(maxval);
00451   img.get_green_channel().set_maximum_value(maxval);
00452   img.get_blue_channel().set_maximum_value(maxval);
00453   move_raster_to_image(rows, bytes_per_pixel, img, color);
00454 
00455   delete io;
00456   return true;
00457 }

Here is the call graph for this function:


Member Data Documentation

Definition at line 64 of file image_io_factory.h.


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