img_convert.h
Go to the documentation of this file.
00001 #ifndef IMG_CONVERT_H_
00002 #define IMG_CONVERT_H_
00003 
00004 // implementation of conversions between basic image types
00005 // assumes linearity for both source and destination
00006 // uses CIE 1931 luminance definition for default grayscale conversion:
00007 // Y = 0.212671*R + 0.715160*G + 0.072169*B
00008 // assumes 255 for default alpha_value parameter
00009 
00010 #include "img/img_image.h"
00011 
00012 namespace img {
00013 
00014 // Y to RGB
00015 template<typename SrcScalarType, bool SrcSafe,typename DestScalarType, bool DestSafe> 
00016 inline void convert_Y_to_RGB(const Image<1,SrcScalarType,SrcSafe> &source, Image<3,DestScalarType,DestSafe> &destination)
00017 {
00018   assert(source.isValid());
00019   if(SrcSafe || DestSafe){
00020     if(!source.isValid())  throw ImageException("Invalid source image");
00021   }
00022   destination.setZero(source.width(),source.height());
00023   for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00024     for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00025       DestScalarType Y = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 0));
00026       destination.setValue(x_coord, y_coord, 0, Y);
00027       destination.setValue(x_coord, y_coord, 1, Y);
00028       destination.setValue(x_coord, y_coord, 2, Y);
00029     } 
00030   destination.attributes.setRange(source.attributes);
00031   destination.attributes.setGamma(source.attributes);
00032 }
00033 
00034 // Y to RGBA
00035 template<typename SrcScalarType, bool SrcSafe,typename DestScalarType, bool DestSafe> 
00036 inline void convert_Y_to_RGBA(const Image<1,SrcScalarType,SrcSafe> &source, Image<4,DestScalarType,DestSafe> &destination, DestScalarType alpha_value=255.0f )
00037 {
00038   assert(source.isValid());
00039   if(SrcSafe || DestSafe){
00040     if(!source.isValid())  throw ImageException("Invalid source image");
00041   }
00042   destination.setZero(source.width(),source.height());
00043   for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00044     for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00045       DestScalarType Y = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 0));
00046       destination.setValue(x_coord, y_coord, 0, Y);
00047       destination.setValue(x_coord, y_coord, 1, Y);
00048       destination.setValue(x_coord, y_coord, 2, Y);
00049       destination.setValue(x_coord, y_coord, 3, alpha_value);
00050     } 
00051   destination.attributes.setRange(source.attributes);
00052   destination.attributes.setGamma(source.attributes);
00053 }
00054 
00055 // RGB to Y
00056 template<typename SrcScalarType, bool SrcSafe,typename DestScalarType, bool DestSafe> 
00057 inline void convert_RGB_to_Y(const Image<3,SrcScalarType,SrcSafe> &source, Image<1,DestScalarType,DestSafe> &destination)
00058 {
00059   assert(source.isValid());
00060   if(SrcSafe || DestSafe){
00061     if(!source.isValid())  throw ImageException("Invalid source image");
00062   }
00063   destination.setZero(source.width(),source.height());
00064   for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00065     for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00066       DestScalarType R = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 0));
00067       DestScalarType G = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 1));
00068       DestScalarType B = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 2));
00069       destination.setValue(x_coord, y_coord, 0, 0.212671f*R + 0.715160f*G + 0.072169f*B);
00070     } 
00071   destination.attributes.setRange(source.attributes);
00072   destination.attributes.setGamma(source.attributes);
00073 }
00074 
00075 // RGB to RGBA
00076 template<typename SrcScalarType, bool SrcSafe,typename DestScalarType, bool DestSafe> 
00077 inline void convert_RGB_to_RGBA(const Image<3,SrcScalarType,SrcSafe> &source, Image<4,DestScalarType,DestSafe> &destination, DestScalarType alpha_value=255.0f )
00078 {
00079   assert(source.isValid());
00080   if(SrcSafe || DestSafe){
00081     if(!source.isValid())  throw ImageException("Invalid source image");
00082   }
00083   destination.setZero(source.width(),source.height());
00084   for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00085     for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00086       destination.setValue(x_coord, y_coord, 0, static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 0)));
00087       destination.setValue(x_coord, y_coord, 1, static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 1)));
00088       destination.setValue(x_coord, y_coord, 2, static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 2)));
00089       destination.setValue(x_coord, y_coord, 3, alpha_value);
00090     } 
00091   destination.attributes.setRange(source.attributes);
00092   destination.attributes.setGamma(source.attributes);
00093 }
00094 
00095 // RGBA to Y
00096 template<typename SrcScalarType, bool SrcSafe,typename DestScalarType, bool DestSafe> 
00097 inline void convert_RGBA_to_Y(const Image<4,SrcScalarType,SrcSafe> &source, Image<1,DestScalarType,DestSafe> &destination)
00098 {
00099   assert(source.isValid());
00100   if(SrcSafe || DestSafe){
00101     if(!source.isValid())  throw ImageException("Invalid source image");
00102   }
00103   destination.setZero(source.width(),source.height());
00104   for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00105     for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00106       DestScalarType R = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 0));
00107       DestScalarType G = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 1));
00108       DestScalarType B = static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 2));
00109       destination.setValue(x_coord, y_coord, 0, 0.212671f*R + 0.715160f*G + 0.072169f*B);
00110     } 
00111   destination.attributes.setRange(source.attributes);
00112   destination.attributes.setGamma(source.attributes);
00113 }
00114 
00115 // RGBA to RGB
00116 template<typename SrcScalarType, bool SrcSafe,typename DestScalarType, bool DestSafe> 
00117 inline void convert_RGBA_to_RGB(const Image<4,SrcScalarType,SrcSafe> &source, Image<3,DestScalarType,DestSafe> &destination)
00118 {
00119   assert(source.isValid());
00120   if(SrcSafe || DestSafe){
00121     if(!source.isValid())  throw ImageException("Invalid source image");
00122   }
00123   destination.setZero(source.width(),source.height());
00124   for (int y_coord = 0; y_coord < source.height(); ++y_coord)
00125     for (int x_coord = 0; x_coord < source.width(); ++x_coord){
00126       destination.setValue(x_coord, y_coord, 0, static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 0)));
00127       destination.setValue(x_coord, y_coord, 1, static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 1)));
00128       destination.setValue(x_coord, y_coord, 2, static_cast<DestScalarType>(source.getValue(x_coord, y_coord, 2)));
00129     } 
00130   destination.attributes.setRange(source.attributes);
00131   destination.attributes.setGamma(source.attributes);
00132 }
00133 
00134 // range conversion
00135 template<int Channels, typename SrcScalarType, bool SrcSafe, typename DestScalarType, bool DestSafe>
00136 inline void convert_range_0_255_to_0_1(const Image<Channels,SrcScalarType,SrcSafe> &source, Image<Channels,DestScalarType,DestSafe> &destination)
00137 {
00138   assert(source.isValid());
00139   assert(source.attributes.hasRange(0,255));
00140   if(SrcSafe || DestSafe){
00141     if(!source.isValid())  throw ImageException("Invalid source image");
00142     if(!source.attributes.hasRange(0,255)) throw ImageException("Invalid range attribute");
00143   }
00144   destination.setZero(source.width(),source.height());
00145   for(int offset=0;offset<source.dataValuesSize();offset++)
00146     destination.dataValues()[offset] = static_cast<DestScalarType>(source.dataValues()[offset]) / DestScalarType(255.0);
00147   destination.attributes=source.attributes;
00148   destination.attributes.setRange(0,1);
00149 }
00150 
00151 template<int Channels, typename SrcScalarType, bool SrcSafe, typename DestScalarType, bool DestSafe>
00152 inline void convert_range_0_1_to_0_255(const Image<Channels,SrcScalarType,SrcSafe> &source, Image<Channels,DestScalarType,DestSafe> &destination)
00153 {
00154   assert(source.isValid());
00155   assert(source.attributes.hasRange(0,1));
00156   if(SrcSafe || DestSafe){
00157     if(!source.isValid())  throw ImageException("Invalid source image");
00158     if(!source.attributes.hasRange(0,1)) throw ImageException("Invalid range attribute");
00159   }
00160   destination.setZero(source.width(),source.height());
00161   for(int offset=0;offset<source.dataValuesSize();offset++)
00162     destination.dataValues()[offset] = static_cast<DestScalarType>(source.dataValues()[offset]) * DestScalarType(255.0);
00163   destination.attributes=source.attributes;
00164   destination.attributes.setRange(0,255);
00165 }
00166 
00167 } //end namespace img
00168 
00169 #endif /*IMG_CONVERT_H_*/


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:53