00001 /*!@file Image/IO.C I/O operations for Image's 00002 */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: Rob Peters <rjpeters@klab.caltech.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/IO.C $ 00036 // $Id: IO.C 7811 2007-01-26 23:38:43Z rjpeters $ 00037 // 00038 00039 #ifndef IMAGE_IO_C_DEFINED 00040 #define IMAGE_IO_C_DEFINED 00041 00042 #include "Image/IO.H" 00043 00044 #include "Image/Image.H" 00045 #include "Image/Pixels.H" 00046 00047 #include <iomanip> 00048 #include <vector> 00049 00050 namespace 00051 { 00052 // The point of these little functions is to convert numbers to a type 00053 // that will print as a number; basically the only problem is with byte, 00054 // which is likely a typedef for char, so if it is printed as-is, then we 00055 // get the characters themselves rather than their numeric 00056 // representation... so by overloading we can make printable(byte) return 00057 // an int. 00058 00059 00060 // The default printable() is just a template that returns the val unchanged. 00061 template <class T> T printable(T val) { return val; } 00062 00063 // Overloads of printable() can specialize the behavior for specific 00064 // types, such as byte here: 00065 int printable(byte val) { return int(val); } 00066 00067 } 00068 00069 // ###################################################################### 00070 template <class T> 00071 std::ostream& operator<<(std::ostream& os, const Image<T>& img) 00072 { 00073 os << "[\n"; 00074 for (int y = 0; y < img.getHeight(); ++y) 00075 { 00076 for (int x = 0; x < img.getWidth(); ++x) 00077 { 00078 os << std::setw(5) 00079 << std::showpoint << std::fixed << std::setprecision(3) 00080 << printable(img.getVal(x,y)) << " "; 00081 } 00082 os << "\n"; 00083 } 00084 os << "]\n"; 00085 00086 return os; 00087 } 00088 00089 // ###################################################################### 00090 template <class T> 00091 void writeImageToStream(std::ostream& os, const Image<T>& img) 00092 { 00093 Dims dims = img.getDims(); 00094 os << dims.w() << ' ' << dims.h() << '\n'; 00095 00096 typename Image<T>::const_iterator ptr = img.begin(); 00097 for (int y = 0; y < dims.h(); ++y) 00098 { 00099 for (int x = 0; x < dims.w(); ++x) 00100 os << *ptr++ << ' '; 00101 00102 os << '\n'; 00103 } 00104 os << '\n'; 00105 } 00106 00107 // ###################################################################### 00108 template <class T> 00109 void readImageFromStream(std::istream& is, Image<T>& img) 00110 { 00111 int w,h; 00112 is >> w; is >> h; 00113 img.resize(w,h,NO_INIT); 00114 typename Image<T>::iterator ptr; 00115 for (ptr = img.beginw(); ptr != img.endw(); ++ptr) 00116 is >> *ptr; 00117 } 00118 00119 // ###################################################################### 00120 // Get the explicit instantiations: 00121 #include "inst/Image/IO.I" 00122 00123 template std::ostream& operator<<(std::ostream&, const Image<int>&); 00124 template std::ostream& operator<<(std::ostream&, const Image<double>&); 00125 00126 // ###################################################################### 00127 /* So things look consistent in everyone's emacs... */ 00128 /* Local Variables: */ 00129 /* indent-tabs-mode: nil */ 00130 /* End: */ 00131 00132 #endif // !IMAGE_IO_C_DEFINED