libcvd-members
[Top][All Lists]
Advanced

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

[libcvd-members] libcvd pnm_src/fits.cc test/test_images.cxx cvd...


From: Edward Rosten
Subject: [libcvd-members] libcvd pnm_src/fits.cc test/test_images.cxx cvd...
Date: Thu, 04 Jun 2009 17:07:51 +0000

CVSROOT:        /cvsroot/libcvd
Module name:    libcvd
Changes by:     Edward Rosten <edrosten>        09/06/04 17:07:51

Modified files:
        pnm_src        : fits.cc 
        test           : test_images.cxx 
        cvd/internal/io: fits.h 
        cvd            : image_io.h documentation.h 
Added files:
        pnm_src        : fitswrite.cc 

Log message:
        Fixed fits reading (they're not upside-down any more).
        
        Added FITS writing.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/libcvd/pnm_src/fits.cc?cvsroot=libcvd&r1=1.5&r2=1.6
http://cvs.savannah.gnu.org/viewcvs/libcvd/pnm_src/fitswrite.cc?cvsroot=libcvd&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/libcvd/test/test_images.cxx?cvsroot=libcvd&r1=1.15&r2=1.16
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/internal/io/fits.h?cvsroot=libcvd&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/image_io.h?cvsroot=libcvd&r1=1.40&r2=1.41
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/documentation.h?cvsroot=libcvd&r1=1.26&r2=1.27

Patches:
Index: pnm_src/fits.cc
===================================================================
RCS file: /cvsroot/libcvd/libcvd/pnm_src/fits.cc,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- pnm_src/fits.cc     27 May 2009 14:28:42 -0000      1.5
+++ pnm_src/fits.cc     4 Jun 2009 17:07:50 -0000       1.6
@@ -71,17 +71,6 @@
                return "";
 }
 
-
-static bool get_bool(const string& s)
-{
-       if(s == "T")
-               return 1;
-       else if (s == "F")
-               return 0;
-       else
-               throw(Exceptions::Image_IO::MalformedImage("Invalid boolean: `" 
+ s + "'"));
-}
-
 static int get_int(const string& s)
 {
        int i;
@@ -384,7 +373,7 @@
                for(int r=0; r < a2; r++)
                        for(int p=0; p < a3; p++)
                                for(int b=0; b < bpp; b++)
-                                               data[((r * a1 + c)*a3 + p)*bpp 
+ b] = raw_data[p*a1*a2*bpp+ (r*a1+c)*bpp+b];
+                                               data[((r * a1 + c)*a3 + p)*bpp 
+ b] = raw_data[p*a1*a2*bpp+ ((a2-r-1)*a1+c)*bpp+b];
        
        bytes_per_pixel = a3 * bpp;
 }

Index: test/test_images.cxx
===================================================================
RCS file: /cvsroot/libcvd/libcvd/test/test_images.cxx,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- test/test_images.cxx        24 Jan 2009 19:30:27 -0000      1.15
+++ test/test_images.cxx        4 Jun 2009 17:07:50 -0000       1.16
@@ -39,10 +39,26 @@
 {
        static C r()
        {
-               return rand() % (1+numeric_limits<C>::max());
+               union
+               {
+                       unsigned int  i;
+                       C c;
+               } u;
+
+               u.i = rand();
+               return u.c;
+       }
+};
+
+template<> struct randpix<bool>
+{
+       static double r()
+       {
+               return rand() % 2;
        }
 };
 
+
 template<> struct randpix<double>
 {
        static double r()
@@ -292,6 +308,29 @@
                          TypeList<Rgb<unsigned short>,
                                       Head> > > > >::exec(ImageType::PNM);
 
+       cerr << "Testing FITS (type " << ImageType::FITS << ")\n";
+       randtest<
+                         TypeList<byte,
+                         TypeList<short,
+                         TypeList<unsigned short,
+                         TypeList<int,
+                         TypeList<float,
+                         TypeList<double,
+                         TypeList<Rgb<byte>,
+                         TypeList<Rgb<short>,
+                         TypeList<Rgb<unsigned short>,
+                         TypeList<Rgb<int>,
+                         TypeList<Rgb<float>,
+                         TypeList<Rgb<double>,
+                         TypeList<Rgba<byte>,
+                         TypeList<Rgba<short>,
+                         TypeList<Rgba<unsigned short>,
+                         TypeList<Rgba<int>,
+                         TypeList<Rgba<float>,
+                         TypeList<Rgba<double>,
+                                      Head> > > > > > > > > > > > > > > > > > 
>::exec(ImageType::FITS);
+
+
        #ifdef CVD_HAVE_PNG
        cerr << "Testing PNG (type " << ImageType::PNG << ")\n";
        randtest<

Index: cvd/internal/io/fits.h
===================================================================
RCS file: /cvsroot/libcvd/libcvd/cvd/internal/io/fits.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- cvd/internal/io/fits.h      24 Jan 2009 19:30:22 -0000      1.3
+++ cvd/internal/io/fits.h      4 Jun 2009 17:07:50 -0000       1.4
@@ -103,20 +103,23 @@
        };
 
        
-#if 0
        
////////////////////////////////////////////////////////////////////////////////
        //
-       // FITS writing, copied and midified from tiff.h
+       // FITS writing, copied and modified from tiff.h
        //
 
-       template<int g8> struct IntMapper    { typedef unsigned short type;};
-       template<>       struct IntMapper<1> { typedef unsigned char type; };
-
+       template<typename C>     struct IntMapper                   { typedef 
int type;};
+       template<>               struct IntMapper<bool>             { typedef 
unsigned char type; };
+       template<>               struct IntMapper<char>             { typedef 
short type; };
+       template<>               struct IntMapper<unsigned char>    { typedef 
unsigned char type; };
+       template<>               struct IntMapper<short>            { typedef 
short type; };
+       template<>               struct IntMapper<unsigned short>   { typedef 
unsigned short type; };
+       template<>               struct IntMapper<int>              { typedef 
int type; };
 
        //Mapping for integral types
        template<class ComponentIn, int is_integral> struct ComponentMapper_
        {
-               typedef typename IntMapper< 
(Pixel::traits<ComponentIn>::bits_used > 8) >::type type;
+               typedef typename IntMapper<ComponentIn>::type type;
        };
 
        //Mapping for non integral types
@@ -146,6 +149,7 @@
 
 
        
+       
        class WritePimpl;
 
        class writer
@@ -155,17 +159,23 @@
                        ~writer();
 
                        void write_raw_pixel_line(const unsigned char*);
+                       void write_raw_pixel_line(const short*);
                        void write_raw_pixel_line(const unsigned short*);
+                       void write_raw_pixel_line(const int*);
                        void write_raw_pixel_line(const float*);
                        void write_raw_pixel_line(const double*);
 
                        void write_raw_pixel_line(const Rgb<unsigned char>*);
+                       void write_raw_pixel_line(const Rgb<short>*);
                        void write_raw_pixel_line(const Rgb<unsigned short>*);
+                       void write_raw_pixel_line(const Rgb<int>*);
                        void write_raw_pixel_line(const Rgb<float>*);
                        void write_raw_pixel_line(const Rgb<double>*);
 
                        void write_raw_pixel_line(const Rgba<unsigned char>*);
+                       void write_raw_pixel_line(const Rgba<short>*);
                        void write_raw_pixel_line(const Rgba<unsigned short>*);
+                       void write_raw_pixel_line(const Rgba<int>*);
                        void write_raw_pixel_line(const Rgba<float>*);
                        void write_raw_pixel_line(const Rgba<double>*);
 
@@ -178,8 +188,6 @@
                        WritePimpl* t; 
        };
        
-#endif 
-
 }
 }
 #endif

Index: cvd/image_io.h
===================================================================
RCS file: /cvsroot/libcvd/libcvd/cvd/image_io.h,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -b -r1.40 -r1.41
--- cvd/image_io.h      24 Jan 2009 19:30:19 -0000      1.40
+++ cvd/image_io.h      4 Jun 2009 17:07:50 -0000       1.41
@@ -86,6 +86,7 @@
                        #endif
                        TXT=7,
                        TEXT=7,
+                       FITS=8,
                };
        }
 
@@ -161,6 +162,9 @@
                        /// with the load() function. There is no metadata, so 
it is not possible to support 
                        /// multiple types. 
                        TXT,
+                       /// FITS images. Supports (native) byte, short, 
unsigned short, int, float and double
+                       /// of greyscale, RGB and RGBA. Signed char is 
supported lossley but inefficiently. 
+                       FITS,
                };
        }
        #endif
@@ -293,6 +297,7 @@
          #ifdef CVD_HAVE_TIFF
                  case ImageType::TIFF: Internal::writeImage<PixelType, 
TIFF::tiff_writer>(im,o); break;
          #endif
+         case ImageType::FITS: Internal::writeImage<PixelType, 
FITS::writer>(im,o); break;
          case ImageType::BMP: BMP::writeBMP(im, o); break;
          case ImageType::TXT: Internal::writeImage<PixelType, 
TEXT::writer>(im, o); break;
          case ImageType::PS:   Internal::writeImage<PixelType, PS::writer>(im, 
o); break;

Index: cvd/documentation.h
===================================================================
RCS file: /cvsroot/libcvd/libcvd/cvd/documentation.h,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -b -r1.26 -r1.27
--- cvd/documentation.h 5 Mar 2009 14:48:37 -0000       1.26
+++ cvd/documentation.h 4 Jun 2009 17:07:50 -0000       1.27
@@ -48,14 +48,14 @@
                        - PNM   
                        - BMP
                        - ASCII text
-                       - FITS (reading only)
+                       - FITS 
                        - PS   (saving only)
                        - EPS  (saving only)
                - External libraries required
                        - JPEG
                        - TIFF 
                        - PNG
-               - 1/8/16/32 bit integer and 32/64 bit floating point images in 
greyscale, RGB and RGBA.
+               - 1/8/16/32 bit signed and unsigned integer and 32/64 bit 
floating point images in greyscale, RGB and RGBA.
                - Optimum bit depth and colour depth selected automatically
     - Image grabbing from video sources:
                  - Linux

Index: pnm_src/fitswrite.cc
===================================================================
RCS file: pnm_src/fitswrite.cc
diff -N pnm_src/fitswrite.cc
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ pnm_src/fitswrite.cc        4 Jun 2009 17:07:50 -0000       1.1
@@ -0,0 +1,295 @@
+/*                       
+       This file is part of the CVD Library.
+
+       Copyright (C) 2005 The Authors
+
+       This library is free software; you can redistribute it and/or
+       modify it under the terms of the GNU Lesser General Public
+       License as published by the Free Software Foundation; either
+       version 2.1 of the License, or (at your option) any later version.
+
+       This library is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+       Lesser General Public License for more details.
+
+       You should have received a copy of the GNU Lesser General Public
+       License along with this library; if not, write to the Free Software
+       Foundation, Inc., 
+    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#include "cvd/internal/io/fits.h"
+#include "cvd/image_io.h"
+#include "cvd/config.h"
+#include <iostream>
+#include <sstream>
+#include <iomanip>
+#include <algorithm>
+
+using namespace CVD;
+using namespace CVD::FITS;
+using namespace CVD::Exceptions::Image_IO;
+using namespace std;
+
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// Private implementation of TIFF reading
+//
+
+class CVD::FITS::WritePimpl
+{
+       public:
+               WritePimpl(ostream&, ImageRef size, const string& t);
+               ~WritePimpl();
+
+               template<class C> void write_raw_pixel_line(const C* data);
+               template<class C> void write_raw_pixel_line_(const C* data);
+               void write_raw_pixel_line_(const unsigned short* dat);
+               void write_raw_pixel_line_(const Rgb<unsigned short>* dat);
+               void write_raw_pixel_line_(const Rgba<unsigned short>* dat);
+               void axes(const string& s, int x, int y);
+               void write(const string& s);
+               void write_short(short s);
+       private:
+               ostream& o;
+               ImageRef my_size;
+               string   type;
+               unsigned long row;
+               unsigned int cards;
+               int bpp, a1, a2, a3;
+               bool raw;
+               vector<unsigned char> data;
+};
+
+void WritePimpl::write(const string& s)
+{
+       string header;
+       header.resize(80);
+       fill(header.begin(), header.end(), ' ');
+       copy(s.begin(), s.end(), header.begin());
+       o << header;
+       cards++;
+}
+
+string int8(int i)
+{
+       ostringstream o;
+       o << setw(8) << setfill(' ') << i;
+       return o.str();
+}
+
+void WritePimpl::axes(const string& s, int x, int y)
+{
+       if(s.substr(0,9) == "CVD::Rgba")
+       {
+               write("NAXIS   =                    3"); 
+               write("NAXIS1  =             " + int8(x)); 
+               write("NAXIS2  =             " + int8(y)); 
+               write("NAXIS3  =                    4"); 
+               a3=4;
+
+       }
+       else if(s.substr(0,8) == "CVD::Rgb")
+       {
+               write("NAXIS   =                    3"); 
+               write("NAXIS1  =             " + int8(x)); 
+               write("NAXIS2  =             " + int8(y)); 
+               write("NAXIS3  =                    3"); 
+               a3=3;
+       }
+       else
+       {
+               write("NAXIS   =                    2"); 
+               write("NAXIS1  =             " + int8(x)); 
+               write("NAXIS2  =             " + int8(y)); 
+               a3=1;
+       }
+       a1=x;
+       a2=y;
+}
+
+template<class C> void WritePimpl::write_raw_pixel_line(const C* dat)
+{
+       //Do some type checking
+       if(type != PNM::type_name<C>::name())
+               throw WriteTypeMismatch(type, PNM::type_name<C>::name());
+       
+       //Do some sanity checking
+       if(row >= (unsigned long)my_size.y)
+               throw InternalLibraryError("CVD", "Write past end of image.");
+       
+       write_raw_pixel_line_(dat);
+       row++;
+}
+               
+
+template<class C> void WritePimpl::write_raw_pixel_line_(const C* dat)
+{
+       const unsigned char* d = reinterpret_cast<const unsigned char*>(dat);
+       for(unsigned int i=0; i < my_size.x * sizeof(C); i++)
+               data.push_back(d[i]);
+}
+
+void WritePimpl::write_short(short s)
+{
+       int num = (int)s - 32768;
+       short ns = num;
+       data.push_back((ns&0xff00)>>8);
+       data.push_back(ns&0xff);
+}
+
+void WritePimpl::write_raw_pixel_line_(const unsigned short* dat)
+{
+       for(int i=0; i < my_size.x; i++)
+               write_short(dat[i]);
+}
+
+void WritePimpl::write_raw_pixel_line_(const Rgb<unsigned short>* dat)
+{
+       for(int i=0; i < my_size.x; i++)
+       {
+               write_short(dat[i].red);
+               write_short(dat[i].green);
+               write_short(dat[i].blue);
+       }
+}
+void WritePimpl::write_raw_pixel_line_(const Rgba<unsigned short>* dat)
+{
+       for(int i=0; i < my_size.x; i++)
+       {
+               write_short(dat[i].red);
+               write_short(dat[i].green);
+               write_short(dat[i].blue);
+               write_short(dat[i].alpha);
+       }
+}
+
+
+WritePimpl::WritePimpl(ostream& os, ImageRef s, const string& t)
+:o(os),my_size(s),type(t),row(0),cards(0)
+{
+       raw=1;
+       write("SIMPLE  =                    T");
+
+       if(type == "unsigned char" || type == "CVD::Rgb<unsigned char>" || type 
== "CVD::Rgba<unsigned char>")
+       {
+               write("BITPIX  =                    8"); 
+               axes(type, s.x, s.y);
+               bpp=1;
+       }
+       else if(type == "short" || type == "CVD::Rgb<short>" || type == 
"CVD::Rgba<short>")
+       {
+               write("BITPIX  =                   16"); 
+               axes(type, s.x, s.y);
+               bpp=2;
+       }
+       else if(type == "unsigned short" || type == "CVD::Rgb<unsigned short>" 
|| type == "CVD::Rgba<unsigned short>")
+       {
+               write("BITPIX  =                   16"); 
+               axes(type, s.x, s.y);
+               write("BZERO   =                32768"); 
+               bpp=2;
+               raw=0;
+       }
+       else if(type == "int" || type == "CVD::Rgb<int>" || type == 
"CVD::Rgba<int>")
+       {
+               write("BITPIX  =                   32"); 
+               axes(type, s.x, s.y);
+               bpp=4;
+       }
+       else if(type == "float" || type == "CVD::Rgb<float>" || type == 
"CVD::Rgba<float>")
+       {
+               write("BITPIX  =                  -32"); 
+               axes(type, s.x, s.y);
+               bpp=4;
+       }
+       else if(type == "double" || type == "CVD::Rgb<double>" || type == 
"CVD::Rgba<double>")
+       {
+               write("BITPIX  =                  -64"); 
+               axes(type, s.x, s.y);
+               bpp=8;
+       }
+       else
+       {
+               throw UnsupportedImageSubType("FITS", t);
+       }
+
+       write("END");
+
+       while(cards % 36)
+               write("");
+}
+
+WritePimpl::~WritePimpl()
+{      
+
+       size_t nelems = data.size() / bpp;
+
+       //Make the data big endian
+       #ifdef CVD_ARCH_LITTLE_ENDIAN
+               if(raw)
+                       for(size_t i=0; i < nelems; i++)
+                               reverse(&data[i*bpp], &data[i*bpp] + bpp);
+       #endif
+
+       vector<unsigned char> raw_data(data.size());
+
+       //Convert inline to planar and flip
+       for(int c=0; c < a1; c++)
+               for(int r=0; r < a2; r++)
+                       for(int p=0; p < a3; p++)
+                               for(int b=0; b < bpp; b++)
+                                               raw_data[p*a1*a2*bpp+ 
(r*a1+c)*bpp+b] = data[(((a2-r-1) * a1 + c)*a3 + p)*bpp + b];
+       
+       o.write((char*)raw_data.data(), raw_data.size());
+}
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+//
+// Implementation of public parts of FITS reading
+//
+
+CVD::FITS::writer::writer(ostream& o, ImageRef size, const string& type)
+:t(new WritePimpl(o, size, type))
+{}
+
+CVD::FITS::writer::~writer()
+{
+       delete t;
+}
+
+//Mechanically generate the pixel reading calls.
+#define GEN1(X) void CVD::FITS::writer::write_raw_pixel_line(const 
X*d){t->write_raw_pixel_line(d);}
+#define GEN3(X) GEN1(X) GEN1(Rgb<X>) GEN1(Rgba<X>)
+
+GEN3(unsigned char)
+GEN3(unsigned short)
+GEN3(short)
+GEN3(int)
+GEN3(float)
+GEN3(double)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




reply via email to

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