libcvd-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libcvd-members] libcvd cvd/image_io.h cvd/internal/io/pnm_grok....


From: Edward Rosten
Subject: [libcvd-members] libcvd cvd/image_io.h cvd/internal/io/pnm_grok....
Date: Tue, 05 Aug 2008 23:54:46 +0000

CVSROOT:        /cvsroot/libcvd
Module name:    libcvd
Changes by:     Edward Rosten <edrosten>        08/08/05 23:54:46

Modified files:
        cvd            : image_io.h 
        cvd/internal/io: pnm_grok.h 
        pnm_src        : pnm_grok.cxx 

Log message:
        Converted PNM writing to the new system.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/image_io.h?cvsroot=libcvd&r1=1.36&r2=1.37
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/internal/io/pnm_grok.h?cvsroot=libcvd&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/libcvd/pnm_src/pnm_grok.cxx?cvsroot=libcvd&r1=1.10&r2=1.11

Patches:
Index: cvd/image_io.h
===================================================================
RCS file: /cvsroot/libcvd/libcvd/cvd/image_io.h,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -b -r1.36 -r1.37
--- cvd/image_io.h      5 Aug 2008 23:03:47 -0000       1.36
+++ cvd/image_io.h      5 Aug 2008 23:54:45 -0000       1.37
@@ -275,7 +275,7 @@
          case ImageType::PNM:  
          case ImageType::Automatic:
          case ImageType::Unknown:
-               PNM::writePNM(im, o); break;
+               Internal::writeImage<PixelType, PNM::pnm_writer>(im, o); break;
          #ifdef CVD_HAVE_JPEG
                  case ImageType::JPEG: JPEG::writeJPEG(im,o); break;
          #endif

Index: cvd/internal/io/pnm_grok.h
===================================================================
RCS file: /cvsroot/libcvd/libcvd/cvd/internal/io/pnm_grok.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- cvd/internal/io/pnm_grok.h  24 Apr 2008 23:13:34 -0000      1.8
+++ cvd/internal/io/pnm_grok.h  5 Aug 2008 23:54:46 -0000       1.9
@@ -154,93 +154,47 @@
        im.resize(ImageRef(pnm.x_size(), pnm.y_size()));
                readPNM(im, pnm);
        }
-
-
-    void writePNMHeader(std::ostream& out, int channels, ImageRef size, int 
maxval, bool text, const std::string& comments);
-
-    void writePNMPixels(std::ostream& out, const byte* data, size_t count, 
bool text);
-    void writePNMPixels(std::ostream& out, const unsigned short* data, size_t 
count, bool text);
-    
-    template <class T, int N, bool 
SixteenBit=CVD::Internal::save_default<typename 
Pixel::Component<T>::type>::use_16bit> struct PNMWriter;
-    template <class T> struct PNMWriter<T,3,true> {
-      typedef Rgb<unsigned short> array;
-      static void write(const BasicImage<T>& im, std::ostream& out) {
-       writePNMHeader(out, 3, im.size(), 65535, false, "");    
-       std::vector<array> rowbuf(im.size().x);
-       for (int r=0; r<im.size().y; r++) {
-         Pixel::ConvertPixels<T, array>::convert(im[r], &(rowbuf[0]), 
im.size().x);
-         writePNMPixels(out, (const unsigned short*)&(rowbuf[0]), 
im.size().x*3, false);
-       }       
-      }
-    };
-
-    template <class T> struct PNMWriter<T,3,false> {
-      typedef Rgb<byte> array;
-      static void write(const BasicImage<T>& im, std::ostream& out) {
-       writePNMHeader(out, 3, im.size(), 255, false, "");      
-       std::vector<array> rowbuf(im.size().x);
-       for (int r=0; r<im.size().y; r++) {
-         Pixel::ConvertPixels<T, array>::convert(im[r], &(rowbuf[0]), 
im.size().x);
-         writePNMPixels(out, (const byte*)&(rowbuf[0]), im.size().x*3, false);
-       }       
-      }
-    };
-
-    template <class T> struct PNMWriter<T,1,true> {
-      typedef unsigned short S;
-      static void write(const BasicImage<T>& im, std::ostream& out) {
-       writePNMHeader(out, 1, im.size(), 65535, false, "");    
-       std::vector<S> rowbuf(im.size().x);
-       for (int r=0; r<im.size().y; r++) {
-         Pixel::ConvertPixels<T, S>::convert(im[r], &(rowbuf[0]), im.size().x);
-         writePNMPixels(out, &(rowbuf[0]), im.size().x, false);
-       }       
-      }
-    };
-
-    template <class T> struct PNMWriter<T,1,false> {
-      typedef byte S;
-      static void write(const BasicImage<T>& im, std::ostream& out) {
-       writePNMHeader(out, 1, im.size(), 255, false, "");      
-       std::vector<S> rowbuf(im.size().x);
-       for (int r=0; r<im.size().y; r++) {
-         Pixel::ConvertPixels<T, S>::convert(im[r], &(rowbuf[0]), im.size().x);
-         writePNMPixels(out, &(rowbuf[0]), im.size().x, false);
-       }       
-      }
-    };
-
-    template <> struct PNMWriter<byte,1> {
-      static void write(const BasicImage<byte>& im, std::ostream& out) {
-       writePNMHeader(out, 1, im.size(), 255, false, "");      
-       writePNMPixels(out, im.data(), im.totalsize(), false);
-      }
+       
////////////////////////////////////////////////////////////////////////////////
+       //
+       // PNM writing.
+       //
+
+       template<int isRgb, int isbyte> struct ComponentMapper      { typedef 
Rgb<byte> type; };
+       template<>                      struct ComponentMapper<1,0> { typedef 
Rgb<unsigned short> type; };
+       template<>                      struct ComponentMapper<0,1> { typedef 
byte type; };
+       template<>                      struct ComponentMapper<0,0> { typedef 
unsigned short type; };
+
+       class pnm_writer
+       {
+               public:
+                       pnm_writer(std::ostream&, ImageRef size, const 
std::string& type);
+                       ~pnm_writer();
+
+                       //void write_raw_pixel_line(const bool*);
+                       void write_raw_pixel_line(const unsigned char*);
+                       void write_raw_pixel_line(const unsigned short*);
+                       void write_raw_pixel_line(const Rgb<unsigned char>*);
+                       void write_raw_pixel_line(const Rgb<unsigned short>*);
+
+                       template<class Incoming> struct Outgoing
+                       {                
+                               typedef typename 
Pixel::Component<Incoming>::type Element;
+                               typedef typename 
ComponentMapper<Pixel::is_Rgb<Incoming>::value,
+                                                                               
                 std::numeric_limits<Element>::is_integer &&
+                                                                               
                 std::numeric_limits<Element>::digits <= 8>::type type;
+                       };              
+               private:
+
+                       template<class P> void sanity_check(const P*);
+                       void write_shorts(const unsigned short*, int n);
+
+               long row;
+               std::ostream& o;
+               ImageRef size;
+               std::string type;
     };
 
-    template <> struct PNMWriter<Rgb<byte>,3> {
-      static void write(const BasicImage<Rgb<byte> >& im, std::ostream& out) {
-       writePNMHeader(out, 3, im.size(), 255, false, "");      
-       writePNMPixels(out, (const byte*)im.data(), im.totalsize()*3, false);
-      }
-    };
 
-    template <> struct PNMWriter<unsigned short,1> {
-      static void write(const BasicImage<unsigned short>& im, std::ostream& 
out) {
-       writePNMHeader(out, 1, im.size(), 65535, false, "");    
-       writePNMPixels(out, im.data(), im.totalsize(), false);
-      }
-    };
-
-    template <> struct PNMWriter<Rgb<unsigned short>,3> {
-      static void write(const BasicImage<Rgb<unsigned short> >& im, 
std::ostream& out) {
-       writePNMHeader(out, 3, im.size(), 65535, false, "");    
-       writePNMPixels(out, (const unsigned short*)im.data(), im.totalsize()*3, 
false);
-      }
-    };
-
-    template <class T> void writePNM(const BasicImage<T>& im, std::ostream& 
out) {
-      PNMWriter<T, Pixel::is_Rgb<T>::value ? 3 : 1>::write(im, out);
-    }
   }
 }
 #endif

Index: pnm_src/pnm_grok.cxx
===================================================================
RCS file: /cvsroot/libcvd/libcvd/pnm_src/pnm_grok.cxx,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- pnm_src/pnm_grok.cxx        9 Oct 2007 16:55:32 -0000       1.10
+++ pnm_src/pnm_grok.cxx        5 Aug 2008 23:54:46 -0000       1.11
@@ -79,6 +79,7 @@
 #define PPM 2
 
 using namespace std;
+using namespace CVD::Exceptions::Image_IO;
 
 namespace CVD
 {
@@ -504,6 +505,78 @@
            out << size.x << " " << size.y << endl << maxval << endl;
        }
 
+       pnm_writer::pnm_writer(std::ostream& out, ImageRef size_, const 
std::string& type_)
+       :row(0),o(out),size(size_),type(type_)
+       {
+               
+               if(type == "unsigned char")
+                       writePNMHeader(out, 1, size, 255, 0, "");
+               else if(type == "unsigned short")
+                       writePNMHeader(out, 1, size, 65535, 0, "");
+               else if(type == "CVD::Rgb<unsigned char>")
+                       writePNMHeader(out, 3, size, 255, 0, "");
+               else if(type == "CVD::Rgb<unsigned short>")
+                       writePNMHeader(out, 3, size, 65535, 0, "");
+               else
+                   throw UnsupportedImageSubType("PNM", type);
+       }
+
+       pnm_writer::~pnm_writer()
+       {}
+
+       void pnm_writer::write_shorts(const unsigned short* data, int n)
+       {
+         
+               #ifdef CVD_ARCH_LITTLE_ENDIAN
+                       const unsigned char* bdata = (const unsigned char*)data;
+                       for (int i=0; i<n; i++, bdata+=2) 
+                       {
+                               unsigned char lohi[2] = {bdata[1], bdata[0]};
+                               o.write((const char*)lohi,2);
+                       }
+               #else
+                       o.write((const char*)data, count*sizeof(unsigned 
short));
+               #endif
+       }
+
+       template<class P> void pnm_writer::sanity_check(const P*)
+       {
+           if(type != PNM::type_name<P>::name())
+                       throw WriteTypeMismatch(type, 
PNM::type_name<P>::name());
+       
+           //Do some sanity checking
+           if(row >= size.y)
+                       throw InternalLibraryError("CVD", "Write past end of 
image.");
+       
+           row++;
+       }
+
+       void pnm_writer::write_raw_pixel_line(const unsigned char* data)
+       {
+               sanity_check(data);
+               o.write(reinterpret_cast<const char*>(data), size.x);
+       }
+
+       void pnm_writer::write_raw_pixel_line(const unsigned short* data)
+       {
+               sanity_check(data);
+               write_shorts(data, size.x);
+       }
+
+       void pnm_writer::write_raw_pixel_line(const Rgb<unsigned char>* data)
+       {
+               sanity_check(data);
+               o.write(reinterpret_cast<const char*>(data), 3*size.x);
+       }
+
+       void pnm_writer::write_raw_pixel_line(const Rgb<unsigned short>* data)
+       {
+               sanity_check(data);
+               write_shorts(reinterpret_cast<const unsigned short*>(data), 
3*size.x);
+       }
+
+
+/*
        template <class T> void writePNMPixelsText(ostream& out, const T* data, 
size_t count) 
        {
            size_t lines = count / 25;
@@ -517,30 +590,7 @@
                out << (int)(data[k++]);
            out << endl;
        }
+*/
 
-       void writePNMPixels(ostream& out, const unsigned char* data, size_t 
count, bool text) 
-       {
-           if (text)
-               writePNMPixelsText(out, data, count);
-           else
-               out.write((const char*)data, count);
-       }
-
-       void writePNMPixels(ostream& out, const unsigned short* data, size_t 
count, bool text) 
-       {
-           if (text)
-               writePNMPixelsText(out, data, count);
-           else {
-#ifdef LONG_PNM_FAST_SAVE
-               out.write((const char*)data, count*sizeof(unsigned short));
-#else
-               const unsigned char* bdata = (const unsigned char*)data;
-               for (size_t i=0; i<count; i++, bdata+=2) {
-                   unsigned char lohi[2] = {bdata[1], bdata[0]};
-                   out.write((const char*)lohi,2);
-               }
-#endif
-           }
-       }       
     }
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]