libcvd-members
[Top][All Lists]
Advanced

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

[libcvd-members] libcvd cvd/colourspace.h cvd/colourspace_conver...


From: Olaf Kähler
Subject: [libcvd-members] libcvd cvd/colourspace.h cvd/colourspace_conver...
Date: Wed, 17 Mar 2010 12:49:26 +0000

CVSROOT:        /sources/libcvd
Module name:    libcvd
Changes by:     Olaf Kähler <ok245>     10/03/17 12:49:26

Modified files:
        cvd            : colourspace.h colourspace_convert.h 
                         colourspaces.h videosource.h 
        cvd/Linux      : dvbuffer3.h 
        cvd_src        : bayer.cxx colourspace_convert.cxx 
                         videosource_dvbuffer.cc 
                         videosource_nodvbuffer.cc 
        cvd_src/Linux  : dvbuffer3_dc1394v2.cc 

Log message:
        added 16bit bayer support (both little and big endian), added 
workaround for incorrectly reported bayer patterns of some unibrain cameras

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/colourspace.h?cvsroot=libcvd&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/colourspace_convert.h?cvsroot=libcvd&r1=1.11&r2=1.12
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/colourspaces.h?cvsroot=libcvd&r1=1.16&r2=1.17
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/videosource.h?cvsroot=libcvd&r1=1.27&r2=1.28
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd/Linux/dvbuffer3.h?cvsroot=libcvd&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd_src/bayer.cxx?cvsroot=libcvd&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd_src/colourspace_convert.cxx?cvsroot=libcvd&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd_src/videosource_dvbuffer.cc?cvsroot=libcvd&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd_src/videosource_nodvbuffer.cc?cvsroot=libcvd&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/libcvd/cvd_src/Linux/dvbuffer3_dc1394v2.cc?cvsroot=libcvd&r1=1.9&r2=1.10

Patches:
Index: cvd/colourspace.h
===================================================================
RCS file: /sources/libcvd/libcvd/cvd/colourspace.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- cvd/colourspace.h   11 Mar 2009 21:35:12 -0000      1.10
+++ cvd/colourspace.h   17 Mar 2010 12:49:24 -0000      1.11
@@ -100,6 +100,118 @@
                /// @param height The height of the image
                void bayer_to_rgb_rggb(const unsigned char* rggb, unsigned 
char* rgb, unsigned int width, unsigned int height);
 
+               /// Convert 16bit Bayer pattern of the form bggr to greyscale 
data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_bggr(const unsigned short* bggr, unsigned 
short* grey, unsigned int width, unsigned int height);
+
+               /// Convert 16bit Bayer pattern of the form gbrg to greyscale 
data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_gbrg(const unsigned short* gbrg, unsigned 
short* grey, unsigned int width, unsigned int height);
+
+               /// Convert 16bit Bayer pattern of the form grbg to greyscale 
data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_grbg(const unsigned short* grbg, unsigned 
short* grey, unsigned int width, unsigned int height);
+               
+               /// Convert 16bit Bayer pattern of the form rggb to greyscale 
data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_rggb(const unsigned short* rggb, unsigned 
short* grey, unsigned int width, unsigned int height);
+               
+               /// Convert 16bit Bayer pattern of the form bggr to rgb444 data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_bggr(const unsigned short* bggr, unsigned 
short* rgb, unsigned int width, unsigned int height);
+
+               /// Convert 16bit Bayer pattern of the form gbrg to rgb444 data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_gbrg(const unsigned short* gbrg, unsigned 
short* rgb, unsigned int width, unsigned int height);
+
+               /// Convert 16bit Bayer pattern of the form grbg to rgb444 data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_grbg(const unsigned short* grbg, unsigned 
short* rgb, unsigned int width, unsigned int height);
+               
+               /// Convert 16bit Bayer pattern of the form rggb to rgb444 data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_rggb(const unsigned short* rggb, unsigned 
short* rgb, unsigned int width, unsigned int height);
+
+               /// Convert 16bit big endian Bayer pattern of the form bggr to 
host endian greyscale data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_bggr_be(const unsigned short* bggr, unsigned 
short* grey, unsigned int width, unsigned int height);
+
+               /// Convert 16bit big endian Bayer pattern of the form gbrg to 
host endian greyscale data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_gbrg_be(const unsigned short* gbrg, unsigned 
short* grey, unsigned int width, unsigned int height);
+
+               /// Convert 16bit big endian Bayer pattern of the form grbg to 
host endian greyscale data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_grbg_be(const unsigned short* grbg, unsigned 
short* grey, unsigned int width, unsigned int height);
+               
+               /// Convert 16bit big endian Bayer pattern of the form rggb to 
host endian greyscale data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_grey_rggb_be(const unsigned short* rggb, unsigned 
short* grey, unsigned int width, unsigned int height);
+               
+               /// Convert 16bit big endian Bayer pattern of the form bggr to 
host endian rgb data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_bggr_be(const unsigned short* bggr, unsigned 
short* rgb, unsigned int width, unsigned int height);
+
+               /// Convert 16bit big endian Bayer pattern of the form gbrg to 
host endian rgb data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_gbrg_be(const unsigned short* gbrg, unsigned 
short* rgb, unsigned int width, unsigned int height);
+
+               /// Convert 16bit big endian Bayer pattern of the form grbg to 
host endian rgb data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_grbg_be(const unsigned short* grbg, unsigned 
short* rgb, unsigned int width, unsigned int height);
+               
+               /// Convert 16bit big endian Bayer pattern of the form rggb to 
host endian rgb data
+               /// @param bggr The input data
+               /// @param grey The output data
+               /// @param width The width of the image
+               /// @param height The height of the image
+               void bayer_to_rgb_rggb_be(const unsigned short* rggb, unsigned 
short* rgb, unsigned int width, unsigned int height);
+
                /// Convert yuv422 to rgb444.
                /// @param yuv The yuv422 data: yuyvyuyv....
                /// @param rgb The output data

Index: cvd/colourspace_convert.h
===================================================================
RCS file: /sources/libcvd/libcvd/cvd/colourspace_convert.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- cvd/colourspace_convert.h   26 Oct 2009 15:44:58 -0000      1.11
+++ cvd/colourspace_convert.h   17 Mar 2010 12:49:24 -0000      1.12
@@ -48,6 +48,41 @@
        template<> void convert_image(const BasicImage<bayer_gbrg>& from, 
BasicImage<Rgb<byte> >& to);
        template<> void convert_image(const BasicImage<bayer_rggb>& from, 
BasicImage<Rgb<byte> >& to);
         
+       /// Convert 16bit Bayer pattern of various forms to greyscale data
+       /// @param from The input data
+       /// @param to The output data
+       /// @ingroup gImageIO
+       template<> void convert_image(const BasicImage<bayer_bggr16>& from, 
BasicImage<unsigned short>& to);
+       template<> void convert_image(const BasicImage<bayer_grbg16>& from, 
BasicImage<unsigned short>& to);
+       template<> void convert_image(const BasicImage<bayer_gbrg16>& from, 
BasicImage<unsigned short>& to);
+       template<> void convert_image(const BasicImage<bayer_rggb16>& from, 
BasicImage<unsigned short>& to);
+
+       /// Convert 16bit Bayer pattern of various forms to rgb data
+       /// @param from The input data
+       /// @param to The output data
+       /// @ingroup gImageIO
+       template<> void convert_image(const BasicImage<bayer_bggr16>& from, 
BasicImage<Rgb<unsigned short> >& to);
+       template<> void convert_image(const BasicImage<bayer_grbg16>& from, 
BasicImage<Rgb<unsigned short> >& to);
+       template<> void convert_image(const BasicImage<bayer_gbrg16>& from, 
BasicImage<Rgb<unsigned short> >& to);
+       template<> void convert_image(const BasicImage<bayer_rggb16>& from, 
BasicImage<Rgb<unsigned short> >& to);
+
+       /// Convert 16bit big endian Bayer pattern of various forms to 
greyscale data
+       /// @param from The input data
+       /// @param to The output data
+       /// @ingroup gImageIO
+       template<> void convert_image(const BasicImage<bayer_bggr16be>& from, 
BasicImage<unsigned short>& to);
+       template<> void convert_image(const BasicImage<bayer_grbg16be>& from, 
BasicImage<unsigned short>& to);
+       template<> void convert_image(const BasicImage<bayer_gbrg16be>& from, 
BasicImage<unsigned short>& to);
+       template<> void convert_image(const BasicImage<bayer_rggb16be>& from, 
BasicImage<unsigned short>& to);
+
+       /// Convert 16bit big endian Bayer pattern of various forms to rgb data
+       /// @param from The input data
+       /// @param to The output data
+       /// @ingroup gImageIO
+       template<> void convert_image(const BasicImage<bayer_bggr16be>& from, 
BasicImage<Rgb<unsigned short> >& to);
+       template<> void convert_image(const BasicImage<bayer_grbg16be>& from, 
BasicImage<Rgb<unsigned short> >& to);
+       template<> void convert_image(const BasicImage<bayer_gbrg16be>& from, 
BasicImage<Rgb<unsigned short> >& to);
+       template<> void convert_image(const BasicImage<bayer_rggb16be>& from, 
BasicImage<Rgb<unsigned short> >& to);
        
        /// Convert YUV 411 pixel data to RGB
        /// @param from The input data
@@ -109,6 +144,22 @@
        template<> struct IsConvertible<bayer_grbg,  Rgb<byte> > { static const 
bool is=1; };
        template<> struct IsConvertible<bayer_gbrg,  Rgb<byte> > { static const 
bool is=1; };
        template<> struct IsConvertible<bayer_rggb,  Rgb<byte> > { static const 
bool is=1; };
+       template<> struct IsConvertible<bayer_bggr16  ,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_grbg16  ,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_gbrg16  ,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_rggb16  ,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_bggr16  ,Rgb<unsigned short> > { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_grbg16  ,Rgb<unsigned short> > { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_gbrg16  ,Rgb<unsigned short> > { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_rggb16  ,Rgb<unsigned short> > { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_bggr16be,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_grbg16be,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_gbrg16be,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_rggb16be,unsigned short>       { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_bggr16be,Rgb<unsigned short> > { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_grbg16be,Rgb<unsigned short> > { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_gbrg16be,Rgb<unsigned short> > { 
static const bool is=1; };
+       template<> struct IsConvertible<bayer_rggb16be,Rgb<unsigned short> > { 
static const bool is=1; };
 }
 
 #endif

Index: cvd/colourspaces.h
===================================================================
RCS file: /sources/libcvd/libcvd/cvd/colourspaces.h,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- cvd/colourspaces.h  26 Oct 2009 15:44:58 -0000      1.16
+++ cvd/colourspaces.h  17 Mar 2010 12:49:24 -0000      1.17
@@ -69,6 +69,86 @@
                bayer_rggb(unsigned char v) : val(v) {}
        };
        
+       /// 16 bit Bayer datatype representing the colour filter pattern BGGR
+       /// @ingroup gVideoBuffer
+       struct bayer_bggr16
+       {
+               unsigned short  val;
+               operator unsigned short() const { return val; }
+               bayer_bggr16() {}
+               bayer_bggr16(unsigned short v) : val(v) {}
+       };
+
+       /// 16bit Bayer datatype representing the colour filter pattern GBRG
+       /// @ingroup gVideoBuffer
+       struct bayer_gbrg16
+       {
+               unsigned short val;
+               operator unsigned short() const { return val; }
+               bayer_gbrg16() {}
+               bayer_gbrg16(unsigned short v) : val(v) {}
+       };
+       
+       /// 16bit Bayer datatype representing the colour filter pattern GRBG
+       /// @ingroup gVideoBuffer
+       struct bayer_grbg16
+       {
+               unsigned short val;
+               operator unsigned short() const { return val; }
+               bayer_grbg16() {}
+               bayer_grbg16(unsigned short v) : val(v) {}
+       };
+
+       /// 16bit Bayer datatype representing the colour filter pattern RGGB
+       /// @ingroup gVideoBuffer
+       struct bayer_rggb16
+       {
+               unsigned short val;
+               operator unsigned short() const { return val; }
+               bayer_rggb16() {}
+               bayer_rggb16(unsigned char v) : val(v) {}
+       };
+
+       /// 16 bit big endian Bayer datatype representing the colour filter 
pattern BGGR
+       /// @ingroup gVideoBuffer
+       struct bayer_bggr16be
+       {
+               unsigned short  val;
+               operator unsigned short() const { return val; }
+               bayer_bggr16be() {}
+               bayer_bggr16be(unsigned short v) : val(v) {}
+       };
+
+       /// 16bit big endian Bayer datatype representing the colour filter 
pattern GBRG
+       /// @ingroup gVideoBuffer
+       struct bayer_gbrg16be
+       {
+               unsigned short val;
+               operator unsigned short() const { return val; }
+               bayer_gbrg16be() {}
+               bayer_gbrg16be(unsigned short v) : val(v) {}
+       };
+       
+       /// 16bit big endian Bayer datatype representing the colour filter 
pattern GRBG
+       /// @ingroup gVideoBuffer
+       struct bayer_grbg16be
+       {
+               unsigned short val;
+               operator unsigned short() const { return val; }
+               bayer_grbg16be() {}
+               bayer_grbg16be(unsigned short v) : val(v) {}
+       };
+
+       /// 16bit big endian Bayer datatype representing the colour filter 
pattern RGGB
+       /// @ingroup gVideoBuffer
+       struct bayer_rggb16be
+       {
+               unsigned short val;
+               operator unsigned short() const { return val; }
+               bayer_rggb16be() {}
+               bayer_rggb16be(unsigned char v) : val(v) {}
+       };
+
        /// typedef to support old bayer datatype
        /// @ingroup gVideoBuffer
        /// @deprecated
@@ -111,6 +191,16 @@
                template<> struct type_name<bayer_grbg> { static std::string 
name(){return "bayer_grbg" ;}};
                template<> struct type_name<bayer_rggb> { static std::string 
name(){return "bayer_rggb" ;}};
 
+               template<> struct type_name<bayer_bggr16> { static std::string 
name(){return "bayer_bggr16" ;}};
+               template<> struct type_name<bayer_gbrg16> { static std::string 
name(){return "bayer_gbrg16" ;}};
+               template<> struct type_name<bayer_grbg16> { static std::string 
name(){return "bayer_grbg16" ;}};
+               template<> struct type_name<bayer_rggb16> { static std::string 
name(){return "bayer_rggb16" ;}};
+
+               template<> struct type_name<bayer_bggr16be> { static 
std::string name(){return "bayer_bggr16be" ;}};
+               template<> struct type_name<bayer_gbrg16be> { static 
std::string name(){return "bayer_gbrg16be" ;}};
+               template<> struct type_name<bayer_grbg16be> { static 
std::string name(){return "bayer_grbg16be" ;}};
+               template<> struct type_name<bayer_rggb16be> { static 
std::string name(){return "bayer_rggb16be" ;}};
+
                template<> struct type_name<yuv411> { static std::string 
name(){return "yuv411" ;}};
                template<> struct type_name<yuv422> { static std::string 
name(){return "yuv422" ;}};
                template<> struct type_name<yuv420p>{ static std::string 
name(){return "yuv420p";}};
@@ -157,6 +247,86 @@
             static const int bits_used = 8;
             static const unsigned char max_intensity=(1 << bits_used) - 1;
         };
+
+        template<int LIFT> struct traits<bayer_bggr16, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
+
+       template<int LIFT> struct traits<bayer_rggb16, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
+
+       template<int LIFT> struct traits<bayer_gbrg16, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
+       
+       template<int LIFT> struct traits<bayer_grbg16, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
+
+        template<int LIFT> struct traits<bayer_bggr16be, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
+
+       template<int LIFT> struct traits<bayer_rggb16be, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
+
+       template<int LIFT> struct traits<bayer_gbrg16be, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
+       
+       template<int LIFT> struct traits<bayer_grbg16be, LIFT>
+        {
+            typedef int wider_type;
+            typedef float float_type;
+            static const bool integral = true;
+            static const bool is_signed = false;
+            static const int bits_used = 16;
+            static const unsigned short max_intensity=(1 << bits_used) - 1;
+        };
     }
 
    
@@ -179,6 +349,38 @@
       {
        enum { is_pod = 1 };
       };
+      template<> struct is_POD<bayer_bggr16>
+      {
+       enum { is_pod = 1 };
+      };
+      template<> struct is_POD<bayer_gbrg16>
+      {
+       enum { is_pod = 1 };
+      };
+      template<> struct is_POD<bayer_grbg16>
+      {
+       enum { is_pod = 1 };
+      };
+      template<> struct is_POD<bayer_rggb16>
+      {
+       enum { is_pod = 1 };
+      };
+      template<> struct is_POD<bayer_bggr16be>
+      {
+       enum { is_pod = 1 };
+      };
+      template<> struct is_POD<bayer_gbrg16be>
+      {
+       enum { is_pod = 1 };
+      };
+      template<> struct is_POD<bayer_grbg16be>
+      {
+       enum { is_pod = 1 };
+      };
+      template<> struct is_POD<bayer_rggb16be>
+      {
+       enum { is_pod = 1 };
+      };
       template<> struct is_POD<yuv411>
       {
        enum { is_pod = 1 };

Index: cvd/videosource.h
===================================================================
RCS file: /sources/libcvd/libcvd/cvd/videosource.h,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -b -r1.27 -r1.28
--- cvd/videosource.h   22 Jan 2010 14:42:37 -0000      1.27
+++ cvd/videosource.h   17 Mar 2010 12:49:24 -0000      1.28
@@ -131,6 +131,14 @@
                        return makeConvertBufferBit<T, bayer_grbg>(r);
                else if(c == "bayer_rggb")
                        return makeConvertBufferBit<T, bayer_rggb>(r);
+               else if(c == "bayer_bggr16be")
+                       return makeConvertBufferBit<T, bayer_bggr16be>(r);
+               else if(c == "bayer_gbrg16be")
+                       return makeConvertBufferBit<T, bayer_gbrg16be>(r);
+               else if(c == "bayer_grbg16be")
+                       return makeConvertBufferBit<T, bayer_grbg16be>(r);
+               else if(c == "bayer_rggb16be")
+                       return makeConvertBufferBit<T, bayer_rggb16be>(r);
                else
                        throw  VideoSourceException("ColorspaceBuffer cannot 
handle type " + c);
        }
@@ -231,6 +239,10 @@
        template <> VideoBuffer<bayer_gbrg>* makeDVBuffer2(int cam, ImageRef 
size, float fps, ImageRef offset, bool verbose, int format7_mode);
        template <> VideoBuffer<bayer_grbg>* makeDVBuffer2(int cam, ImageRef 
size, float fps, ImageRef offset, bool verbose, int format7_mode);
        template <> VideoBuffer<bayer_rggb>* makeDVBuffer2(int cam, ImageRef 
size, float fps, ImageRef offset, bool verbose, int format7_mode);
+       template <> VideoBuffer<bayer_bggr16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset, bool verbose, int format7_mode);
+       template <> VideoBuffer<bayer_gbrg16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset, bool verbose, int format7_mode);
+       template <> VideoBuffer<bayer_grbg16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset, bool verbose, int format7_mode);
+       template <> VideoBuffer<bayer_rggb16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset, bool verbose, int format7_mode);
 
        void get_dc1394_options(const VideoSource& vs, ImageRef& size, float& 
fps, ImageRef& offset, bool& verbose, int& format7_mode);
 

Index: cvd/Linux/dvbuffer3.h
===================================================================
RCS file: /sources/libcvd/libcvd/cvd/Linux/dvbuffer3.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- cvd/Linux/dvbuffer3.h       22 Jan 2010 14:42:37 -0000      1.7
+++ cvd/Linux/dvbuffer3.h       17 Mar 2010 12:49:24 -0000      1.8
@@ -91,12 +91,24 @@
       { static const DV3ColourSpace space = RAW8; }; 
     template<> struct CSConvert<bayer_rggb>
       { static const DV3ColourSpace space = RAW8; }; 
+    template<> struct CSConvert<bayer_bggr16be>
+      { static const DV3ColourSpace space = RAW16; }; 
+    template<> struct CSConvert<bayer_gbrg16be>
+      { static const DV3ColourSpace space = RAW16; }; 
+    template<> struct CSConvert<bayer_grbg16be>
+      { static const DV3ColourSpace space = RAW16; }; 
+    template<> struct CSConvert<bayer_rggb16be>
+      { static const DV3ColourSpace space = RAW16; }; 
 
     template<class C> struct CSFilter { static const DV3ColourFilter filter = 
UNDEFINED; };
     template<> struct CSFilter<bayer_bggr> { static const DV3ColourFilter 
filter = BGGR; };
     template<> struct CSFilter<bayer_gbrg> { static const DV3ColourFilter 
filter = GBRG; };
     template<> struct CSFilter<bayer_grbg> { static const DV3ColourFilter 
filter = GRBG; };
     template<> struct CSFilter<bayer_rggb> { static const DV3ColourFilter 
filter = RGGB; };
+    template<> struct CSFilter<bayer_bggr16be> { static const DV3ColourFilter 
filter = BGGR; };
+    template<> struct CSFilter<bayer_gbrg16be> { static const DV3ColourFilter 
filter = GBRG; };
+    template<> struct CSFilter<bayer_grbg16be> { static const DV3ColourFilter 
filter = GRBG; };
+    template<> struct CSFilter<bayer_rggb16be> { static const DV3ColourFilter 
filter = RGGB; };
 
     struct LibDCParams;
 #endif

Index: cvd_src/bayer.cxx
===================================================================
RCS file: /sources/libcvd/libcvd/cvd_src/bayer.cxx,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- cvd_src/bayer.cxx   11 Mar 2009 22:39:13 -0000      1.7
+++ cvd_src/bayer.cxx   17 Mar 2010 12:49:24 -0000      1.8
@@ -20,125 +20,414 @@
 */
 #include <string.h>
 #include <cvd/colourspace.h>
+#include <netinet/in.h>
 
 //Written by Ethan
+//Modified by Olaf :)
 
 namespace CVD{namespace ColourSpace{
 
+template <class T>
+struct read_host_byteorder
+{
+  static const T & get(const T & from) { return from; }
+};
+
+template <class T>
+struct read_net_byteorder;
+
+template <>
+struct read_net_byteorder<unsigned short>
+{
+  static unsigned short get(const unsigned short & from) { return ntohs(from); 
}
+};
+
+template <>
+struct read_net_byteorder<uint32_t>
+{
+  static unsigned short get(const unsigned short & from) { return ntohl(from); 
}
+};
+
+
 // RGRGRG
 // GBGBGB
 // RGRGRG
 // GBGBGB
+template <class T, class R>
 struct bayer_sample_rggb {
-       static inline void upper_left(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][0] = row[0];            out[0][1] = (row[1] + 
next[0])/2; out[0][2] = next[1];
-               out[1][0] = (row[0]+row[2])/2; out[1][1] = row[1];              
 out[1][2] = next[1];
-       }
-       static inline void upper_row(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][0] = row[0];            out[0][1] = (row[-1]+ row[1] + 
next[0])/3; out[0][2] = (next[-1]+next[1])/2;
-               out[1][0] = (row[0]+row[2])/2; out[1][1] = row[1];              
          out[1][2] = next[1];
-       }
-       static inline void upper_right(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][0] = row[0]; out[0][1] = (row[-1]+ row[1] + next[0])/3; 
out[0][2] = (next[-1]+next[1])/2;
-               out[1][0] = row[0]; out[1][1] = row[1];                        
out[1][2] = next[1];
-       }
-
-       static inline void odd_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][0] = (previous[0] + next[0])/2; out[0][1] = row[0]; 
out[0][2] = row[1];
-               out[1][0] = (previous[0] + next[0] + previous[2] + next[2])/4; 
out[1][1] = (row[0]+row[2]+previous[0]+next[0])/4;  out[1][2] = row[1];
-       }
-       static inline void odd_row(unsigned char (*out)[3], const unsigned char 
* previous, const unsigned char * row, const unsigned char * next){
-               out[0][0] = (previous[0] + next[0])/2; out[0][1] = row[0]; 
out[0][2] = (row[-1]+row[1])/2;
-               out[1][0] = (previous[0] + next[0] + previous[2] + next[2])/4; 
out[1][1] = (row[0]+row[2]+previous[1]+next[1])/4;  out[1][2] = row[1];
-       }
-       static inline void odd_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][0] = (previous[0] + next[0])/2; out[0][1] = row[0]; 
out[0][2] = (row[-1]+row[1])/2;
-               out[1][0] = (previous[0] + next[0])/2; out[1][1] = 
(row[0]+previous[0]+next[0])/3;  out[1][2] = row[1];
-       }
-
-       static inline void even_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][0] = row[0]; out[0][1] = (row[1]+previous[0]+next[0])/3; 
out[0][2] = (previous[1]+next[1])/2;
-               out[1][0] = (row[0]+row[2])/2; out[1][1] = row[1];  out[1][2] = 
out[0][2];
-       }
-       static inline void even_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][0] = row[0]; out[0][1] = 
(row[-1]+row[1]+previous[0]+next[0])/4; out[0][2] = 
(previous[-1]+next[-1]+previous[1]+next[1])/4;
-               out[1][0] = (row[0]+row[2])/2; out[1][1] = row[1];  out[1][2] = 
(previous[0] + next[0])/2;
-       }
-       static inline void even_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][0] = row[0]; out[0][1] = 
(row[-1]+row[1]+previous[0]+next[0])/4; out[0][2] = 
(previous[-1]+next[-1]+previous[1]+next[1])/4;
-               out[1][0] = row[0]; out[1][1] = row[1];  out[1][2] = 
(previous[0] + next[0])/2;
-       }
-
-       static inline void lower_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][0] = previous[0];  out[0][1] = row[0]; out[0][2] = 
row[1];
-               out[1][0] = (previous[0]+previous[2])/2; out[1][1] = 
(row[0]+row[2]+previous[1])/3; out[1][2] = row[1];
-       }
-       static inline void lower_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][0] = previous[0]; out[0][1] = row[0]; out[0][2] = 
(row[-1]+row[1])/2;
-               out[1][0] = (previous[0] + previous[2])/2; out[1][1] = 
(row[0]+row[2]+previous[1])/3;  out[1][2] = row[1];
-       }
-       static inline void lower_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][0] = previous[0]; out[0][1] = row[0]; out[0][2] = 
(row[-1]+row[1])/2;
-               out[1][0] = previous[0]; out[1][1] = (row[0]+previous[0])/2;  
out[1][2] = row[1];
+       static inline void upper_left(T (*out)[3], const T * row, const T * 
next){
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][0] = row0;
+               out[0][1] = (row1 + next0)/2;
+               out[0][2] = next1;
+               out[1][0] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][2] = next1;
+       }
+       static inline void upper_row(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][0] = row0;
+               out[0][1] = (row_1+ row1 + next0)/3;
+               out[0][2] = (next_1+next1)/2;
+               out[1][0] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][2] = next1;
+       }
+       static inline void upper_right(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][0] = row0;
+               out[0][1] = (row_1+ row1 + next0)/3;
+               out[0][2] = (next_1+next1)/2;
+               out[1][0] = row0;
+               out[1][1] = row1;
+               out[1][2] = next1;
+       }
+
+       static inline void odd_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T prev2 = R::get(previous[2]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next2 = R::get(next[2]);
+               out[0][0] = (prev0 + next0)/2;
+               out[0][1] = row0;
+               out[0][2] = row1;
+               out[1][0] = (prev0 + next0 + prev2 + next2)/4;
+               out[1][1] = (row0+row2+prev0+next0)/4;
+               out[1][2] = row1;
+       }
+       static inline void odd_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T prev2 = R::get(previous[2]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               T next2 = R::get(next[2]);
+               out[0][0] = (prev0 + next0)/2;
+               out[0][1] = row0;
+               out[0][2] = (row_1+row1)/2;
+               out[1][0] = (prev0 + next0 + prev2 + next2)/4;
+               out[1][1] = (row0+row2+prev1+next1)/4;
+               out[1][2] = row1;
+       }
+       static inline void odd_right(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next0 = R::get(next[0]);
+               out[0][0] = (prev0 + next0)/2;
+               out[0][1] = row0;
+               out[0][2] = (row_1+row1)/2;
+               out[1][0] = (prev0 + next0)/2;
+               out[1][1] = (row0+prev0+next0)/3;
+               out[1][2] = row1;
+       }
+
+       static inline void even_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][0] = row0;
+               out[0][1] = (row1+prev0+next0)/3;
+               out[0][2] = (prev1+next1)/2;
+               out[1][0] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][2] = out[0][2];
+       }
+       static inline void even_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][0] = row0;
+               out[0][1] = (row_1+row1+prev0+next0)/4;
+               out[0][2] = (prev_1+next_1+prev1+next1)/4;
+               out[1][0] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][2] = (prev0 + next0)/2;
+       }
+       static inline void even_right(T (*out)[3], const T * previous, const T 
* row, const T * next){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][0] = row0;
+               out[0][1] = (row_1+row1+prev0+next0)/4;
+               out[0][2] = (prev_1+next_1+prev1+next1)/4;
+               out[1][0] = row0;
+               out[1][1] = row1;
+               out[1][2] = (prev0 + next0)/2;
+       }
+
+       static inline void lower_left(T (*out)[3], const T * previous, const T 
* row){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T prev2 = R::get(previous[2]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][0] = prev0;
+               out[0][1] = row0;
+               out[0][2] = row1;
+               out[1][0] = (prev0+prev2)/2;
+               out[1][1] = (row0+row2+prev1)/3;
+               out[1][2] = row1;
+       }
+       static inline void lower_row(T (*out)[3], const T * previous, const T * 
row){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T prev2 = R::get(previous[2]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][0] = prev0;
+               out[0][1] = row0;
+               out[0][2] = (row_1+row1)/2;
+               out[1][0] = (prev0 + prev2)/2;
+               out[1][1] = (row0+row2+prev1)/3;
+               out[1][2] = row1;
+       }
+       static inline void lower_right(T (*out)[3], const T * previous, const T 
* row){
+               T prev0 = R::get(previous[0]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               out[0][0] = prev0;
+               out[0][1] = row0;
+               out[0][2] = (row_1+row1)/2;
+               out[1][0] = prev0;
+               out[1][1] = (row0+prev0)/2;
+               out[1][2] = row1;
        }
 };
 
+
 // BGBGBG
 // GRGRGR
 // BGBGBG
 // GRGRGR
 // swap red and blue
+template <class T, class R>
 struct bayer_sample_bggr {
-       static inline void upper_left(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][2] = row[0];            out[0][1] = (row[1] + 
next[0])/2; out[0][0] = next[1];
-               out[1][2] = (row[0]+row[2])/2; out[1][1] = row[1];              
 out[1][0] = next[1];
-       }
-       static inline void upper_row(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][2] = row[0];            out[0][1] = (row[-1]+ row[1] + 
next[0])/3; out[0][0] = (next[-1]+next[1])/2;
-               out[1][2] = (row[0]+row[2])/2; out[1][1] = row[1];              
          out[1][0] = next[1];
-       }
-       static inline void upper_right(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][2] = row[0]; out[0][1] = (row[-1]+ row[1] + next[0])/3; 
out[0][0] = (next[-1]+next[1])/2;
-               out[1][2] = row[0]; out[1][1] = row[1];                        
out[1][0] = next[1];
-       }
-
-       static inline void odd_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][2] = (previous[0] + next[0])/2; out[0][1] = row[0]; 
out[0][0] = row[1];
-               out[1][2] = (previous[0] + next[0] + previous[2] + next[2])/4; 
out[1][1] = (row[0]+row[2]+previous[0]+next[0])/4;  out[1][0] = row[1];
-       }
-       static inline void odd_row(unsigned char (*out)[3], const unsigned char 
* previous, const unsigned char * row, const unsigned char * next){
-               out[0][2] = (previous[0] + next[0])/2; out[0][1] = row[0]; 
out[0][0] = (row[-1]+row[1])/2;
-               out[1][2] = (previous[0] + next[0] + previous[2] + next[2])/4; 
out[1][1] = (row[0]+row[2]+previous[1]+next[1])/4;  out[1][0] = row[1];
-       }
-       static inline void odd_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][2] = (previous[0] + next[0])/2; out[0][1] = row[0]; 
out[0][0] = (row[-1]+row[1])/2;
-               out[1][2] = (previous[0] + next[0])/2; out[1][1] = 
(row[0]+previous[0]+next[0])/3;  out[1][0] = row[1];
-       }
-
-       static inline void even_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][2] = row[0]; out[0][1] = (row[1]+previous[0]+next[0])/3; 
out[0][0] = (previous[1]+next[1])/2;
-               out[1][2] = (row[0]+row[2])/2; out[1][1] = row[1];  out[1][0] = 
out[0][0];
-       }
-       static inline void even_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][2] = row[0]; out[0][1] = 
(row[-1]+row[1]+previous[0]+next[0])/4; out[0][0] = 
(previous[-1]+next[-1]+previous[1]+next[1])/4;
-               out[1][2] = (row[0]+row[2])/2; out[1][1] = row[1];  out[1][0] = 
(previous[1] + next[1])/2;
-       }
-       static inline void even_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               out[0][2] = row[0]; out[0][1] = 
(row[-1]+row[1]+previous[0]+next[0])/4; out[0][0] = 
(previous[-1]+next[-1]+previous[1]+next[1])/4;
-               out[1][2] = row[0]; out[1][1] = row[1];  out[1][0] = 
(previous[1] + next[1])/2;
-       }
-
-       static inline void lower_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][2] = previous[0];  out[0][1] = row[0]; out[0][0] = 
row[1];
-               out[1][2] = (previous[0]+previous[2])/2; out[1][1] = 
(row[0]+row[2]+previous[1])/3; out[1][0] = row[1];
-       }
-       static inline void lower_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][2] = previous[0]; out[0][1] = row[0]; out[0][0] = 
(row[-1]+row[1])/2;
-               out[1][2] = (previous[0] + previous[2])/2; out[1][1] = 
(row[0]+row[2]+previous[1])/3;  out[1][0] = row[1];
-       }
-       static inline void lower_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][2] = previous[0]; out[0][1] = row[0]; out[0][0] = 
(row[-1]+row[1])/2;
-               out[1][2] = previous[0]; out[1][1] = (row[0]+previous[0])/2;  
out[1][0] = row[1];
+       static inline void upper_left(T (*out)[3], const T * row, const T * 
next){
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][2] = row0;
+               out[0][1] = (row1 + next0)/2;
+               out[0][0] = next1;
+               out[1][2] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][0] = next1;
+       }
+       static inline void upper_row(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][2] = row0;
+               out[0][1] = (row_1+ row1 + next0)/3;
+               out[0][0] = (next_1+next1)/2;
+               out[1][2] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][0] = next1;
+       }
+       static inline void upper_right(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][2] = row0;
+               out[0][1] = (row_1+ row1 + next0)/3;
+               out[0][0] = (next_1+next1)/2;
+               out[1][2] = row0;
+               out[1][1] = row1;
+               out[1][0] = next1;
+       }
+
+       static inline void odd_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T prev2 = R::get(previous[2]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next2 = R::get(next[2]);
+               out[0][2] = (prev0 + next0)/2;
+               out[0][1] = row0;
+               out[0][0] = row1;
+               out[1][2] = (prev0 + next0 + prev2 + next2)/4;
+               out[1][1] = (row0+row2+prev0+next0)/4;
+               out[1][0] = row1;
+       }
+       static inline void odd_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T prev2 = R::get(previous[2]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               T next2 = R::get(next[2]);
+               out[0][2] = (prev0 + next0)/2;
+               out[0][1] = row0;
+               out[0][0] = (row_1+row1)/2;
+               out[1][2] = (prev0 + next0 + prev2 + next2)/4;
+               out[1][1] = (row0+row2+prev1+next1)/4;
+               out[1][0] = row1;
+       }
+       static inline void odd_right(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next0 = R::get(next[0]);
+               out[0][2] = (prev0 + next0)/2;
+               out[0][1] = row0;
+               out[0][0] = (row_1+row1)/2;
+               out[1][2] = (prev0 + next0)/2;
+               out[1][1] = (row0+prev0+next0)/3;
+               out[1][0] = row1;
+       }
+
+       static inline void even_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][2] = row0;
+               out[0][1] = (row1+prev0+next0)/3;
+               out[0][0] = (prev1+next1)/2;
+               out[1][2] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][0] = out[0][0];
+       }
+       static inline void even_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][2] = row0;
+               out[0][1] = (row_1+row1+prev0+next0)/4;
+               out[0][0] = (prev_1+next_1+prev1+next1)/4;
+               out[1][2] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][0] = (prev1 + next1)/2;
+       }
+       static inline void even_right(T (*out)[3], const T * previous, const T 
* row, const T * next){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][2] = row0;
+               out[0][1] = (row_1+row1+prev0+next0)/4;
+               out[0][0] = (prev_1+next_1+prev1+next1)/4;
+               out[1][2] = row0;
+               out[1][1] = row1;
+               out[1][0] = (prev1 + next1)/2;
+       }
+
+       static inline void lower_left(T (*out)[3], const T * previous, const T 
* row){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T prev2 = R::get(previous[2]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][2] = prev0;
+               out[0][1] = row0;
+               out[0][0] = row1;
+               out[1][2] = (prev0+prev2)/2;
+               out[1][1] = (row0+row2+prev1)/3;
+               out[1][0] = row1;
+       }
+       static inline void lower_row(T (*out)[3], const T * previous, const T * 
row){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T prev2 = R::get(previous[2]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][2] = prev0;
+               out[0][1] = row0;
+               out[0][0] = (row_1+row1)/2;
+               out[1][2] = (prev0 + prev2)/2;
+               out[1][1] = (row0+row2+prev1)/3;
+               out[1][0] = row1;
+       }
+       static inline void lower_right(T (*out)[3], const T * previous, const T 
* row){
+               T prev0 = R::get(previous[0]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               out[0][2] = prev0;
+               out[0][1] = row0;
+               out[0][0] = (row_1+row1)/2;
+               out[1][2] = prev0;
+               out[1][1] = (row0+prev0)/2;
+               out[1][0] = row1;
        }
 };
 
@@ -146,116 +435,254 @@
 // RGRGRG
 // GBGBGB
 // RGRGRG
+template <class T, class R>
 struct bayer_sample_gbrg {
-       static inline void upper_left(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][0] = next[0];            out[0][1] = row[0];             
        out[0][2] = row[1];
-               out[1][0] = (next[0]+next[2])/2;out[1][1] = 
(row[0]+row[2]+next[1])/3;  out[1][2] = row[1];
-       }
-       static inline void upper_row(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][0] = next[0];            out[0][1] = row[0];             
        out[0][2] = (row[-1]+row[1])/2;
-               out[1][0] = (next[0]+next[2])/2;out[1][1] = 
(row[0]+row[2]+next[1])/3;  out[1][2] = row[1];
-       }
-       static inline void upper_right(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][0] = next[0];    out[0][1] = row[0];                     
out[0][2] = (row[-1]+row[1])/2;
-               out[1][0] = next[0];    out[1][1] = (row[0]+next[1])/2;         
out[1][2] = row[1];
-       }
-
-       static inline void odd_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_rggb::even_left(out, previous, row, next);
-       }
-       static inline void odd_row(unsigned char (*out)[3], const unsigned char 
* previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_rggb::even_row(out, previous, row, next);
-       }
-       static inline void odd_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_rggb::even_right(out, previous, row, next);
-       }
-
-       static inline void even_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_rggb::odd_left(out, previous, row, next);
-       }
-       static inline void even_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_rggb::odd_row(out, previous, row, next);
-       }
-       static inline void even_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_rggb::odd_right(out, previous, row, next);
-       }
-
-       static inline void lower_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][0] = row[0];  out[0][1] = (previous[0] + row[1])/2;      
out[0][2] = previous[1];
-               out[1][0] = (row[0]+row[2])/2; out[1][1] = row[1];              
out[1][2] = previous[1];
-       }
-       static inline void lower_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][0] = row[0];  out[0][1] = (row[-1]+previous[0] + 
row[1])/3;      out[0][2] = (previous[-1]+previous[1])/2;
-               out[1][0] = (row[0]+row[2])/2; out[1][1] = row[1];              
        out[1][2] = previous[1];
-       }
-       static inline void lower_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][0] = row[0];  out[0][1] = (row[-1]+previous[0] + 
row[1])/3;      out[0][2] = (previous[-1]+previous[1])/2;
-               out[1][0] = row[0];  out[1][1] = row[1];                        
        out[1][2] = previous[1];
+       static inline void upper_left(T (*out)[3], const T * row, const T * 
next){
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               T next2 = R::get(next[2]);
+               out[0][0] = next0;
+               out[0][1] = row0;
+               out[0][2] = row1;
+               out[1][0] = (next0+next2)/2;
+               out[1][1] = (row0+row2+next1)/3;
+               out[1][2] = row1;
+       }
+       static inline void upper_row(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               T next2 = R::get(next[2]);
+               out[0][0] = next0;
+               out[0][1] = row0;
+               out[0][2] = (row_1+row1)/2;
+               out[1][0] = (next0+next2)/2;
+               out[1][1] = (row0+row2+next1)/3;
+               out[1][2] = row1;
+       }
+       static inline void upper_right(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][0] = next0;
+               out[0][1] = row0;
+               out[0][2] = (row_1+row1)/2;
+               out[1][0] = next0;
+               out[1][1] = (row0+next1)/2;
+               out[1][2] = row1;
+       }
+
+       static inline void odd_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_rggb<T,R>::even_left(out, previous, row, next);
+       }
+       static inline void odd_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_rggb<T,R>::even_row(out, previous, row, next);
+       }
+       static inline void odd_right(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_rggb<T,R>::even_right(out, previous, row, next);
+       }
+
+       static inline void even_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_rggb<T,R>::odd_left(out, previous, row, next);
+       }
+       static inline void even_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_rggb<T,R>::odd_row(out, previous, row, next);
+       }
+       static inline void even_right(T (*out)[3], const T * previous, const T 
* row, const T * next){
+               bayer_sample_rggb<T,R>::odd_right(out, previous, row, next);
+       }
+
+       static inline void lower_left(T (*out)[3], const T * previous, const T 
* row){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][0] = row0;
+               out[0][1] = (prev0 + row1)/2;
+               out[0][2] = prev1;
+               out[1][0] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][2] = prev1;
+       }
+       static inline void lower_row(T (*out)[3], const T * previous, const T * 
row){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][0] = row0;
+               out[0][1] = (row_1+prev0 + row1)/3;
+               out[0][2] = (prev_1+prev1)/2;
+               out[1][0] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][2] = prev1;
+       }
+       static inline void lower_right(T (*out)[3], const T * previous, const T 
* row){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               out[0][0] = row0;
+               out[0][1] = (row_1+prev0 + row1)/3;
+               out[0][2] = (prev_1+prev1)/2;
+               out[1][0] = row0;
+               out[1][1] = row1;
+               out[1][2] = prev1;
        }
 };
 
+/*
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T prev2 = R::get(previous[2]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next_1 = R::get(next[-1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               T next2 = R::get(next[2]);
+*/
 // GRGRGR
 // BGBGBG
 // GRGRGR
 // BGBGBG
+template <class T, class R>
 struct bayer_sample_grbg {
-       static inline void upper_left(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][2] = next[0];            out[0][1] = row[0];             
        out[0][0] = row[1];
-               out[1][2] = (next[0]+next[2])/2;out[1][1] = 
(row[0]+row[2]+next[1])/3;  out[1][0] = row[1];
-       }
-       static inline void upper_row(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][2] = next[0];            out[0][1] = row[0];             
        out[0][0] = (row[-1]+row[1])/2;
-               out[1][2] = (next[0]+next[2])/2;out[1][1] = 
(row[0]+row[2]+next[1])/3;  out[1][0] = row[1];
-       }
-       static inline void upper_right(unsigned char (*out)[3], const unsigned 
char * row, const unsigned char * next){
-               out[0][2] = next[0];    out[0][1] = row[0];                     
out[0][0] = (row[-1]+row[1])/2;
-               out[1][2] = next[0];    out[1][1] = (row[0]+next[1])/2;         
out[1][0] = row[1];
-       }
-
-       static inline void odd_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_bggr::even_left(out, previous, row, next);
-       }
-       static inline void odd_row(unsigned char (*out)[3], const unsigned char 
* previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_bggr::even_row(out, previous, row, next);
-       }
-       static inline void odd_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_bggr::even_right(out, previous, row, next);
-       }
-
-       static inline void even_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_bggr::odd_left(out, previous, row, next);
-       }
-       static inline void even_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_bggr::odd_row(out, previous, row, next);
-       }
-       static inline void even_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row, const unsigned char * next){
-               bayer_sample_bggr::odd_right(out, previous, row, next);
-       }
-
-       static inline void lower_left(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][2] = row[0];  out[0][1] = (previous[0] + row[1])/2;      
out[0][0] = previous[1];
-               out[1][2] = (row[0]+row[2])/2; out[1][1] = row[1];              
out[1][0] = previous[1];
-       }
-       static inline void lower_row(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][2] = row[0];  out[0][1] = (row[-1]+previous[0] + 
row[1])/3;      out[0][0] = (previous[-1]+previous[1])/2;
-               out[1][2] = (row[0]+row[2])/2; out[1][1] = row[1];              
        out[1][0] = previous[1];
-       }
-       static inline void lower_right(unsigned char (*out)[3], const unsigned 
char * previous, const unsigned char * row){
-               out[0][2] = row[0];  out[0][1] = (row[-1]+previous[0] + 
row[1])/3;      out[0][0] = (previous[-1]+previous[1])/2;
-               out[1][2] = row[0];  out[1][1] = row[1];                        
        out[1][0] = previous[1];
+       static inline void upper_left(T (*out)[3], const T * row, const T * 
next){
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               T next2 = R::get(next[2]);
+               out[0][2] = next0;
+               out[0][1] = row0;
+               out[0][0] = row1;
+               out[1][2] = (next0+next2)/2;
+               out[1][1] = (row0+row2+next1)/3;
+               out[1][0] = row1;
+       }
+       static inline void upper_row(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               T next2 = R::get(next[2]);
+               out[0][2] = next0;
+               out[0][1] = row0;
+               out[0][0] = (row_1+row1)/2;
+               out[1][2] = (next0+next2)/2;
+               out[1][1] = (row0+row2+next1)/3;
+               out[1][0] = row1;
+       }
+       static inline void upper_right(T (*out)[3], const T * row, const T * 
next){
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T next0 = R::get(next[0]);
+               T next1 = R::get(next[1]);
+               out[0][2] = next0;
+               out[0][1] = row0;
+               out[0][0] = (row_1+row1)/2;
+               out[1][2] = next0;
+               out[1][1] = (row0+next1)/2;
+               out[1][0] = row1;
+       }
+
+       static inline void odd_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_bggr<T,R>::even_left(out, previous, row, next);
+       }
+       static inline void odd_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_bggr<T,R>::even_row(out, previous, row, next);
+       }
+       static inline void odd_right(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_bggr<T,R>::even_right(out, previous, row, next);
+       }
+
+       static inline void even_left(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_bggr<T,R>::odd_left(out, previous, row, next);
+       }
+       static inline void even_row(T (*out)[3], const T * previous, const T * 
row, const T * next){
+               bayer_sample_bggr<T,R>::odd_row(out, previous, row, next);
+       }
+       static inline void even_right(T (*out)[3], const T * previous, const T 
* row, const T * next){
+               bayer_sample_bggr<T,R>::odd_right(out, previous, row, next);
+       }
+
+       static inline void lower_left(T (*out)[3], const T * previous, const T 
* row){
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][2] = row0;
+               out[0][1] = (prev0 + row1)/2;
+               out[0][0] = prev1;
+               out[1][2] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][0] = prev1;
+       }
+       static inline void lower_row(T (*out)[3], const T * previous, const T * 
row){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               T row2 = R::get(row[2]);
+               out[0][2] = row0;
+               out[0][1] = (row_1+prev0 + row1)/3;
+               out[0][0] = (prev_1+prev1)/2;
+               out[1][2] = (row0+row2)/2;
+               out[1][1] = row1;
+               out[1][0] = prev1;
+       }
+       static inline void lower_right(T (*out)[3], const T * previous, const T 
* row){
+               T prev_1 = R::get(previous[-1]);
+               T prev0 = R::get(previous[0]);
+               T prev1 = R::get(previous[1]);
+               T row_1 = R::get(row[-1]);
+               T row0 = R::get(row[0]);
+               T row1 = R::get(row[1]);
+               out[0][2] = row0;
+               out[0][1] = (row_1+prev0 + row1)/3;
+               out[0][0] = (prev_1+prev1)/2;
+               out[1][2] = row0;
+               out[1][1] = row1;
+               out[1][0] = prev1;
        }
 };
 
 // TYPE is 0, 1, 2, 3 corresponding to the layouts RGGB = 0, GBRG = 1, GRBG = 
2, BGGR = 3
-template <class SAMPLER>
-void bayer_to_rgb(const unsigned char* bggr, unsigned char* rgb, unsigned int 
width, unsigned int height)
+//template <typename T, class SAMPLER>
+//void bayer_to_rgb(const T* bggr, T* rgb, unsigned int width, unsigned int 
height)
+template <typename T, class SAMPLER>
+void bayer_to_rgb(const T* bggr, T* rgb, unsigned int width, unsigned int 
height)
 {
-  const unsigned char* row = bggr;
-  const unsigned char* next = bggr+width;
-  const unsigned char* prev = bggr;
+  const T* row = bggr;
+  const T* next = bggr+width;
+  const T* prev = bggr;
   const unsigned int midcount = (width - 4)/2;
 
-  unsigned char (*out)[3] = (unsigned char (*)[3])rgb;
+  T (*out)[3] = (T (*)[3])rgb;
 
   SAMPLER::upper_left(out, row, next);
   out += 2; row += 2; next += 2;
@@ -298,33 +725,66 @@
 }
 
 void bayer_to_rgb_rggb(const unsigned char* rggb, unsigned char* rgb, unsigned 
int width, unsigned int height){
-       bayer_to_rgb<bayer_sample_rggb>(rggb, rgb, width, height);
+       bayer_to_rgb<unsigned char, bayer_sample_rggb<unsigned 
char,read_host_byteorder<unsigned char> > >(rggb, rgb, width, height);
 }
 
 void bayer_to_rgb_gbrg(const unsigned char* rggb, unsigned char* rgb, unsigned 
int width, unsigned int height){
-       bayer_to_rgb<bayer_sample_gbrg>(rggb, rgb, width, height);
+       bayer_to_rgb<unsigned char, bayer_sample_gbrg<unsigned 
char,read_host_byteorder<unsigned char> > >(rggb, rgb, width, height);
 }
 
 void bayer_to_rgb_grbg(const unsigned char* rggb, unsigned char* rgb, unsigned 
int width, unsigned int height){
-       bayer_to_rgb<bayer_sample_grbg>(rggb, rgb, width, height);
+       bayer_to_rgb<unsigned char, bayer_sample_grbg<unsigned 
char,read_host_byteorder<unsigned char> > >(rggb, rgb, width, height);
 }
 
 void bayer_to_rgb_bggr(const unsigned char* bggr, unsigned char* rgb, unsigned 
int width, unsigned int height){
-       bayer_to_rgb<bayer_sample_bggr>(bggr, rgb, width, height);
+       bayer_to_rgb<unsigned char, bayer_sample_bggr<unsigned 
char,read_host_byteorder<unsigned char> > >(bggr, rgb, width, height);
+}
+
+void bayer_to_rgb_rggb(const unsigned short* rggb, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_rggb<unsigned 
short,read_host_byteorder<unsigned short> > >(rggb, rgb, width, height);
+}
+
+void bayer_to_rgb_gbrg(const unsigned short* rggb, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_gbrg<unsigned 
short,read_host_byteorder<unsigned short> > >(rggb, rgb, width, height);
+}
+
+void bayer_to_rgb_grbg(const unsigned short* rggb, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_grbg<unsigned 
short,read_host_byteorder<unsigned short> > >(rggb, rgb, width, height);
+}
+
+void bayer_to_rgb_bggr(const unsigned short* bggr, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_bggr<unsigned 
short,read_host_byteorder<unsigned short> > >(bggr, rgb, width, height);
+}
+
+void bayer_to_rgb_rggb_be(const unsigned short* rggb, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_rggb<unsigned 
short,read_net_byteorder<unsigned short> > >(rggb, rgb, width, height);
 }
 
-inline unsigned char cie(unsigned char * c) { return (c[0]*77 + c[1]*150 + 
c[2]*29)>>8; }
+void bayer_to_rgb_gbrg_be(const unsigned short* rggb, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_gbrg<unsigned 
short,read_net_byteorder<unsigned short> > >(rggb, rgb, width, height);
+}
+
+void bayer_to_rgb_grbg_be(const unsigned short* rggb, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_grbg<unsigned 
short,read_net_byteorder<unsigned short> > >(rggb, rgb, width, height);
+}
+
+void bayer_to_rgb_bggr_be(const unsigned short* bggr, unsigned short* rgb, 
unsigned int width, unsigned int height){
+       bayer_to_rgb<unsigned short, bayer_sample_bggr<unsigned 
short,read_net_byteorder<unsigned short> > >(bggr, rgb, width, height);
+}
+
+template <class T>
+inline T cie(T * c) { return (c[0]*77 + c[1]*150 + c[2]*29)>>8; }
 
 // TYPE is 0, 1, 2, 3 corresponding to the layouts RGGB = 0, GBRG = 1, GRBG = 
2, BGGR = 3
-template<class SAMPLER>
-void bayer_to_grey(const unsigned char* bggr, unsigned char* grey, unsigned 
int width, unsigned int height)
+template<class T, class SAMPLER>
+void bayer_to_grey(const T* bggr, T* grey, unsigned int width, unsigned int 
height)
 {
-  const unsigned char* row = bggr;
-  const unsigned char* next = bggr+width;
-  const unsigned char* prev = bggr;
+  const T* row = bggr;
+  const T* next = bggr+width;
+  const T* prev = bggr;
   const unsigned int midcount = (width - 4)/2;
 
-  unsigned char out[2][3];
+  T out[2][3];
 
   SAMPLER::upper_left(out, row, next);
   grey[0] = cie(out[0]); grey[1] = cie(out[1]);
@@ -377,19 +837,51 @@
 }
 
 void bayer_to_grey_rggb(const unsigned char* rggb, unsigned char* grey, 
unsigned int width, unsigned int height){
-       bayer_to_grey<bayer_sample_rggb>(rggb, grey, width, height);
+       bayer_to_grey<unsigned char, bayer_sample_rggb<unsigned 
char,read_host_byteorder<unsigned char> > >(rggb, grey, width, height);
 }
 
 void bayer_to_grey_gbrg(const unsigned char* rggb, unsigned char* grey, 
unsigned int width, unsigned int height){
-       bayer_to_grey<bayer_sample_gbrg>(rggb, grey, width, height);
+       bayer_to_grey<unsigned char,bayer_sample_gbrg<unsigned 
char,read_host_byteorder<unsigned char> > >(rggb, grey, width, height);
 }
 
 void bayer_to_grey_grbg(const unsigned char* rggb, unsigned char* grey, 
unsigned int width, unsigned int height){
-       bayer_to_grey<bayer_sample_grbg>(rggb, grey, width, height);
+       bayer_to_grey<unsigned char,bayer_sample_grbg<unsigned 
char,read_host_byteorder<unsigned char> > >(rggb, grey, width, height);
 }
 
 void bayer_to_grey_bggr(const unsigned char* bggr, unsigned char* grey, 
unsigned int width, unsigned int height){
-       bayer_to_grey<bayer_sample_bggr>(bggr, grey, width, height);
+       bayer_to_grey<unsigned char,bayer_sample_bggr<unsigned 
char,read_host_byteorder<unsigned char> > >(bggr, grey, width, height);
+}
+
+void bayer_to_grey_rggb(const unsigned short* rggb, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short, bayer_sample_rggb<unsigned 
short,read_host_byteorder<unsigned short> > >(rggb, grey, width, height);
+}
+
+void bayer_to_grey_gbrg(const unsigned short* rggb, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short,bayer_sample_gbrg<unsigned 
short,read_host_byteorder<unsigned short> > >(rggb, grey, width, height);
+}
+
+void bayer_to_grey_grbg(const unsigned short* rggb, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short,bayer_sample_grbg<unsigned 
short,read_host_byteorder<unsigned short> > >(rggb, grey, width, height);
+}
+
+void bayer_to_grey_bggr(const unsigned short* bggr, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short,bayer_sample_bggr<unsigned 
short,read_host_byteorder<unsigned short> > >(bggr, grey, width, height);
+}
+
+void bayer_to_grey_rggb_be(const unsigned short* rggb, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short, bayer_sample_rggb<unsigned 
short,read_net_byteorder<unsigned short> > >(rggb, grey, width, height);
+}
+
+void bayer_to_grey_gbrg_be(const unsigned short* rggb, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short,bayer_sample_gbrg<unsigned 
short,read_net_byteorder<unsigned short> > >(rggb, grey, width, height);
+}
+
+void bayer_to_grey_grbg_be(const unsigned short* rggb, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short,bayer_sample_grbg<unsigned 
short,read_net_byteorder<unsigned short> > >(rggb, grey, width, height);
+}
+
+void bayer_to_grey_bggr_be(const unsigned short* bggr, unsigned short* grey, 
unsigned int width, unsigned int height){
+       bayer_to_grey<unsigned short,bayer_sample_bggr<unsigned 
short,read_net_byteorder<unsigned short> > >(bggr, grey, width, height);
 }
 
 }}

Index: cvd_src/colourspace_convert.cxx
===================================================================
RCS file: /sources/libcvd/libcvd/cvd_src/colourspace_convert.cxx,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- cvd_src/colourspace_convert.cxx     11 Mar 2009 21:35:13 -0000      1.8
+++ cvd_src/colourspace_convert.cxx     17 Mar 2010 12:49:24 -0000      1.9
@@ -76,6 +76,101 @@
     ColourSpace::bayer_to_grey_rggb(reinterpret_cast<const unsigned 
char*>(from.data()), to.data(), from.size().x, from.size().y);
   }
   
+  template<> void convert_image(const BasicImage<bayer_bggr16>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_bggr(reinterpret_cast<const unsigned 
short*>(from.data()), 
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+  template<> void convert_image(const BasicImage<bayer_gbrg16>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_gbrg(reinterpret_cast<const unsigned 
short*>(from.data()),
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_grbg16>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_grbg(reinterpret_cast<const unsigned 
short*>(from.data()),
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+  
+  template<> void convert_image(const BasicImage<bayer_rggb16>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_rggb(reinterpret_cast<const unsigned 
short*>(from.data()),
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_bggr16>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_bggr(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+  
+  template<> void convert_image(const BasicImage<bayer_gbrg16>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_gbrg(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+  
+  template<> void convert_image(const BasicImage<bayer_grbg16>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_grbg(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_rggb16>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_rggb(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_bggr16be>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_bggr_be(reinterpret_cast<const unsigned 
short*>(from.data()), 
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_gbrg16be>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_gbrg_be(reinterpret_cast<const unsigned 
short*>(from.data()),
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_grbg16be>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_grbg_be(reinterpret_cast<const unsigned 
short*>(from.data()),
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+  
+  template<> void convert_image(const BasicImage<bayer_rggb16be>& from, 
BasicImage<Rgb<unsigned short> >& to)
+  {
+    ColourSpace::bayer_to_rgb_rggb_be(reinterpret_cast<const unsigned 
short*>(from.data()),
+                             reinterpret_cast<unsigned short*>(to.data()), 
+                             from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_bggr16be>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_bggr_be(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+  
+  template<> void convert_image(const BasicImage<bayer_gbrg16be>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_gbrg_be(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+  
+  template<> void convert_image(const BasicImage<bayer_grbg16be>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_grbg_be(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+
+  template<> void convert_image(const BasicImage<bayer_rggb16be>& from, 
BasicImage<unsigned short>& to)
+  {
+    ColourSpace::bayer_to_grey_rggb_be(reinterpret_cast<const unsigned 
short*>(from.data()), to.data(), from.size().x, from.size().y);
+  }
+
   template<> void convert_image(const BasicImage<yuv411>& from, 
BasicImage<Rgb<byte> >& to)
   {
     ColourSpace::yuv411_to_rgb(reinterpret_cast<const unsigned 
char*>(from.data()),

Index: cvd_src/videosource_dvbuffer.cc
===================================================================
RCS file: /sources/libcvd/libcvd/cvd_src/videosource_dvbuffer.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- cvd_src/videosource_dvbuffer.cc     22 Jan 2010 14:42:37 -0000      1.3
+++ cvd_src/videosource_dvbuffer.cc     17 Mar 2010 12:49:24 -0000      1.4
@@ -43,6 +43,26 @@
                return new DVBuffer3<bayer_bggr>(cam,size, fps, offset, v, m);
        }
 
+       template <> VideoBuffer<bayer_grbg16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset,  bool v, int m)
+       {
+               return new DVBuffer3<bayer_grbg16be>(cam,size, fps, offset, v, 
m);
+       }
+
+       template <> VideoBuffer<bayer_gbrg16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset,  bool v, int m)
+       {
+               return new DVBuffer3<bayer_gbrg16be>(cam,size, fps, offset, v, 
m);
+       }
+
+       template <> VideoBuffer<bayer_rggb16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset,  bool v, int m)
+       {
+               return new DVBuffer3<bayer_rggb16be>(cam,size, fps, offset, v, 
m);
+       }
+
+       template <> VideoBuffer<bayer_bggr16be>* makeDVBuffer2(int cam, 
ImageRef size, float fps, ImageRef offset,  bool v, int m)
+       {
+               return new DVBuffer3<bayer_bggr16be>(cam,size, fps, offset, v, 
m);
+       }
+
        template <> VideoBuffer<Rgb<byte> >* makeDVBuffer2(int cam, ImageRef 
size, float fps, ImageRef offset,  bool v, int m)
        {
                return new DVBuffer3<Rgb<byte> >(cam, size, fps, offset, v, m);

Index: cvd_src/videosource_nodvbuffer.cc
===================================================================
RCS file: /sources/libcvd/libcvd/cvd_src/videosource_nodvbuffer.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- cvd_src/videosource_nodvbuffer.cc   22 Jan 2010 14:42:37 -0000      1.3
+++ cvd_src/videosource_nodvbuffer.cc   17 Mar 2010 12:49:24 -0000      1.4
@@ -42,6 +42,26 @@
                throw VideoSourceException("DVBuffer3 is not compiled in to 
libcvd.");
        }
 
+       template <> VideoBuffer<bayer_grbg16be>* makeDVBuffer2(int, ImageRef, 
float, ImageRef, bool, int)
+       {
+               throw VideoSourceException("DVBuffer3 is not compiled in to 
libcvd.");
+       }
+
+       template <> VideoBuffer<bayer_gbrg16be>* makeDVBuffer2(int, ImageRef, 
float, ImageRef, bool, int)
+       {
+               throw VideoSourceException("DVBuffer3 is not compiled in to 
libcvd.");
+       }
+
+       template <> VideoBuffer<bayer_rggb16be>* makeDVBuffer2(int, ImageRef, 
float, ImageRef, bool, int)
+       {
+               throw VideoSourceException("DVBuffer3 is not compiled in to 
libcvd.");
+       }
+
+       template <> VideoBuffer<bayer_bggr16be>* makeDVBuffer2(int, ImageRef, 
float, ImageRef, bool, int)
+       {
+               throw VideoSourceException("DVBuffer3 is not compiled in to 
libcvd.");
+       }
+
        template <> VideoBuffer<Rgb<byte> >* makeDVBuffer2(int, ImageRef, 
float, ImageRef, bool, int)
        {
                throw VideoSourceException("DVBuffer3 is not compiled in to 
libcvd.");

Index: cvd_src/Linux/dvbuffer3_dc1394v2.cc
===================================================================
RCS file: /sources/libcvd/libcvd/cvd_src/Linux/dvbuffer3_dc1394v2.cc,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- cvd_src/Linux/dvbuffer3_dc1394v2.cc 22 Jan 2010 14:42:37 -0000      1.9
+++ cvd_src/Linux/dvbuffer3_dc1394v2.cc 17 Mar 2010 12:49:24 -0000      1.10
@@ -120,6 +120,13 @@
        return "error";
     }
 
+    static DV3ColourFilter DV3_from_DC_ColourFilter(dc1394color_filter_t f, 
uint32_t vendor, uint32_t model, uint64_t guid)
+    {
+      // some cameras report incorrect bayer patterns
+      if (guid==0x814436200006075) { return GBRG; }
+      return static_cast<DV3ColourFilter>(f - DC1394_COLOR_FILTER_MIN);
+    }
+
     struct LibDCParams
     {
       dc1394_t *pDC1394;
@@ -139,8 +146,21 @@
       friend class RawDVBuffer3;
     };
     
-    static dc1394color_coding_t DC_from_DV3_ColourSpace(DV3ColourSpace s)
+    static dc1394color_coding_t DC_from_DV3_ColourSpace(DV3ColourSpace s, 
uint32_t vendor, uint32_t model, uint64_t guid)
+    {
+      // some cameras report their raw bayer mode as being mono and do not
+      // have a mono mode at all...
+      if (guid==0x814436200006075) {
+        //vendor==0x81443 model==0x0 ?
+        switch(s)
     {
+         case MONO8:  return DC1394_COLOR_CODING_RAW8; 
+         case MONO16: return DC1394_COLOR_CODING_RAW16; 
+         case RAW8:   return DC1394_COLOR_CODING_MONO8; 
+         case RAW16:  return DC1394_COLOR_CODING_MONO16; 
+         default: break;
+         }
+      }
       switch(s)
        {
        case MONO8:  return DC1394_COLOR_CODING_MONO8; 
@@ -250,9 +270,10 @@
   
       if(!mpLDCP->pCamera) throw(All("Failed on dc1394_camera_new"));
   
+      log << "Selected camera: " << hex << mpLDCP->pCamera->vendor_id << ":" 
<< mpLDCP->pCamera->model_id << "(guid: " << mpLDCP->pCamera->guid << dec << 
")\n";
     
       // What mode to use?
-      dc1394color_coding_t nTargetColourCoding = 
DC_from_DV3_ColourSpace(colourspace);
+      dc1394color_coding_t nTargetColourCoding = 
DC_from_DV3_ColourSpace(colourspace, mpLDCP->pCamera->vendor_id, 
mpLDCP->pCamera->model_id, mpLDCP->pCamera->guid);
 
       dc1394_camera_reset(mpLDCP->pCamera);
 
@@ -486,7 +507,29 @@
                log << error << "\n";
                dc1394color_filter_t filterType;
                error = dc1394_format7_get_color_filter(mpLDCP->pCamera, nMode, 
&filterType);
-               mColourfilter = static_cast<DV3ColourFilter>(filterType - 
DC1394_COLOR_FILTER_MIN);
+               mColourfilter = DV3_from_DC_ColourFilter(filterType, 
mpLDCP->pCamera->vendor_id, mpLDCP->pCamera->model_id, mpLDCP->pCamera->guid);
+       }
+
+       // Hack Alert: If someone requested raw bayer output but we have not
+       // yet determined the bayer filter type, then try harder to find it.
+       // This happens, if a default mode announced as MONO8 is actually a
+       // raw bayer mode instead, which is a common quirk for many cameras.
+       // We will try to query the format 7 bayer patterns, which will
+       // probably be the same as for all other modes...
+       if ((mColourfilter == UNDEFINED) && ((colourspace == 
RAW8)||(colourspace == RAW16))) {
+               dc1394format7modeset_t modeset;
+               error = dc1394_format7_get_modeset(mpLDCP->pCamera, &modeset);
+               if(error) throw(All("Could not get Format 7 modes."));
+
+               for(int index = 0; index < DC1394_VIDEO_MODE_FORMAT7_NUM ; 
++index){
+                       dc1394color_filter_t filterType;
+                       dc1394video_mode_t nMode2 = 
static_cast<dc1394video_mode_t>( DC1394_VIDEO_MODE_FORMAT7_0 + index);
+                       error = 
dc1394_format7_get_color_filter(mpLDCP->pCamera, nMode2, &filterType);
+                       if (error) continue;
+                       mColourfilter = DV3_from_DC_ColourFilter(filterType, 
mpLDCP->pCamera->vendor_id, mpLDCP->pCamera->model_id, mpLDCP->pCamera->guid);
+
+                       if (mColourfilter != UNDEFINED) break;
+               }
        }
          
        // Hack Alert: The code below sets the iso channel without this




reply via email to

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