Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

image_container_traits.h

Go to the documentation of this file.
00001 
00050 #ifndef __IMAGE_CONTAINER_TRAITS__
00051 #define __IMAGE_CONTAINER_TRAITS__
00052 
00053 #include <algorithm>
00054 
00055 #include<qimage.h>
00056 #include<qpixmap.h>
00057 
00058 #include "array.h"
00059 #include "channel_image.h"
00060 #include "color.h"
00061 
00062 #include "image_file.h"
00063 
00064 namespace Image_file{
00065 
00066   /*
00067 
00068     ##############
00069     # QT version #
00070     ##############
00071   
00072   */
00073 
00074 
00075   template<>
00076   void load(const char*    file_name,
00077             QPixmap* const target){
00078     
00079     target->load(QString(file_name));
00080   }
00081 
00082 
00083   
00084   template<>
00085   void save(const char*    file_name,
00086             const QPixmap& source){
00087 
00088     const std::string format = guess_format(file_name);
00089 
00090     if (format == std::string()) {
00091       char* escape_format = strdup(QImage::outputFormats().first());
00092 
00093       Message::warning<<"output format not recognized. "
00094                       <<"image written ("<<file_name<<") in format : "
00095                       <<escape_format<<Message::done;
00096 
00097       source.save(QString(file_name),escape_format);
00098 
00099       free(escape_format);
00100 
00101     }
00102     else{ 
00103       source.save(QString(file_name),format.c_str());
00104     }    
00105   }
00106   
00107   template<>
00108   void load(const char*   file_name,
00109             QImage* const target){
00110     
00111     target->load(QString(file_name));
00112   }
00113 
00114 
00115   
00116   template<>
00117   void save(const char*   file_name,
00118             const QImage& source){
00119 
00120     const std::string format = guess_format(file_name);
00121 
00122     if (format == std::string()) {
00123       char* escape_format = strdup(QImage::outputFormats().first());
00124 
00125       Message::warning<<"output format not recognized. "
00126                       <<"image written ("<<file_name<<") in format : "
00127                       <<escape_format<<Message::done;
00128 
00129       source.save(QString(file_name),escape_format);
00130 
00131       free(escape_format);
00132 
00133     }
00134     else{
00135       source.save(QString(file_name),format.c_str());
00136     }    
00137   }
00138 
00139   
00140   
00141   namespace Image_container_traits{
00142 
00143     /*
00144 
00145       #############################
00146       # RGB_channel_image version #
00147       #############################
00148     
00149     */
00150 
00151     template<typename Real>
00152     inline unsigned int
00153     get_width(const RGB_channel_image<Real>& container){
00154       return container.x_size();
00155     }
00156 
00157     
00158     template<typename Real>
00159     inline unsigned int
00160     get_height(const RGB_channel_image<Real>& container){
00161       return container.y_size();
00162     }
00163 
00164     
00165     template<typename Real>
00166     inline void
00167     set_size(RGB_channel_image<Real>* const container,
00168              const unsigned int        width,
00169              const unsigned int        height){
00170       container->resize(width,height);
00171     }
00172 
00173     
00174     template<typename Real>
00175     inline void
00176     set_value(RGB_channel_image<Real>* const container,
00177               const unsigned int       x,
00178               const unsigned int       y,
00179               const double             r,
00180               const double             g,
00181               const double             b){
00182     
00183       (*container)[RGB_channel_image<Real>::red](x,y)   = r;
00184       (*container)[RGB_channel_image<Real>::green](x,y) = g;
00185       (*container)[RGB_channel_image<Real>::blue](x,y)  = b;
00186     }
00187 
00188   
00189     template<typename Real>
00190     inline void
00191     get_value(const RGB_channel_image<Real>& container,
00192               const unsigned int             x,
00193               const unsigned int             y,
00194               double* const                  r,
00195               double* const                  g,
00196               double* const                  b){
00197 
00198       *r = container[RGB_channel_image<Real>::red](x,y);
00199       *g = container[RGB_channel_image<Real>::green](x,y);
00200       *b = container[RGB_channel_image<Real>::blue](x,y);
00201     }
00202 
00203 
00204 
00205     
00206    /*
00207 
00208       #############################
00209       # Array_2D<Color,A> version #
00210       #############################
00211     
00212     */
00213 
00214     template<typename Color,typename A>
00215     inline unsigned int
00216     get_width(const Array_2D<Color,A>& container){
00217       return container.x_size();
00218     }
00219 
00220     
00221     template<typename Color,typename A>
00222     inline unsigned int
00223     get_height(const Array_2D<Color,A>& container){
00224       return container.y_size();
00225     }
00226 
00227     
00228     template<typename Color,typename A>
00229     inline void
00230     set_size(Array_2D<Color,A>* const container,
00231             const unsigned int        width,
00232             const unsigned int        height){
00233       container->resize(width,height);
00234     }
00235 
00236     
00237     template<typename Color,typename A>
00238     inline void
00239     set_value(Array_2D<Color,A>* const container,
00240               const unsigned int       x,
00241               const unsigned int       y,
00242               const double             r,
00243               const double             g,
00244               const double             b){
00245     
00246       (*container)(x,y) = Color(r,g,b);
00247     }
00248 
00249   
00250     template<typename Color,typename A>
00251     inline void
00252     get_value(const Array_2D<Color,A>&    container,
00253               const unsigned int          x,
00254               const unsigned int          y,
00255               double* const               r,
00256               double* const               g,
00257               double* const               b){
00258 
00259       container(x,y).get_RGB(r,g,b);
00260     }
00261 
00262 
00263   
00264     /*
00265 
00266       ##################
00267       # double version #
00268       ##################
00269     
00270     */
00271 
00272 
00273     template<typename A>
00274     inline unsigned int
00275     get_width(const Array_2D<double,A>& container){
00276       return container.x_size();
00277     }
00278 
00279     
00280     template<typename A>
00281     inline unsigned int
00282     get_height(const Array_2D<double,A>& container){
00283       return container.y_size();
00284     }
00285 
00286     
00287     template<typename A>
00288     inline void
00289     set_size(Array_2D<double,A>* const container,
00290             const unsigned int        width,
00291             const unsigned int        height){
00292       container->resize(width,height);
00293     }
00294 
00295     
00296     template<typename A>
00297     inline void
00298     set_value(Array_2D<double,A>* const container,
00299               const unsigned int        x,
00300               const unsigned int        y,
00301               const double              r,
00302               const double              g,
00303               const double              b){
00304     
00305       (*container)(x,y) = std::max(std::max(r,g),b);
00306     }
00307 
00308   
00309     template<typename A>
00310     inline void
00311     get_value(const Array_2D<double,A>& container,
00312               const unsigned int        x,
00313               const unsigned int        y,
00314               double* const             r,
00315               double* const             g,
00316               double* const             b){
00317 
00318       *r = container(x,y);
00319       *g = *r;
00320       *b = *r;
00321     }
00322 
00323 
00324     /*
00325 
00326       #################
00327       # float version #
00328       #################
00329     
00330     */
00331 
00332 
00333     template<typename A>
00334     inline unsigned int
00335     get_width(const Array_2D<float,A>& container){
00336       return container.x_size();
00337     }
00338 
00339     
00340     template<typename A>
00341     inline unsigned int
00342     get_height(const Array_2D<float,A>& container){
00343       return container.y_size();
00344     }
00345 
00346     
00347     template<typename A>
00348     inline void
00349     set_size(Array_2D<float,A>* const container,
00350             const unsigned int        width,
00351             const unsigned int        height){
00352       container->resize(width,height);
00353     }
00354 
00355   
00356     template<typename A>
00357     inline void
00358     set_value(Array_2D<float,A>* const container,
00359               const unsigned int       x,
00360               const unsigned int       y,
00361               const double             r,
00362               const double             g,
00363               const double             b){
00364     
00365       (*container)(x,y) = static_cast<float>(std::max(std::max(r,g),b));
00366     }
00367 
00368   
00369     template<typename A>
00370     inline void
00371     get_value(const Array_2D<float,A>&    container,
00372               const unsigned int          x,
00373               const unsigned int          y,
00374               double* const               r,
00375               double* const               g,
00376               double* const               b){
00377 
00378       *r = static_cast<double>(container(x,y));
00379       *g = *r;
00380       *b = *r;
00381     }
00382 
00383 
00384     /*
00385 
00386       ################
00387       # char version #
00388       ################
00389     
00390     */
00391 
00392 
00393     template<typename A>
00394     inline unsigned int
00395     get_width(const Array_2D<char,A>& container){
00396       return container.x_size();
00397     }
00398 
00399     
00400     template<typename A>
00401     inline unsigned int
00402     get_height(const Array_2D<char,A>& container){
00403       return container.y_size();
00404     }
00405 
00406     
00407     template<typename A>
00408     inline void
00409     set_size(Array_2D<char,A>* const container,
00410             const unsigned int        width,
00411             const unsigned int        height){
00412       container->resize(width,height);
00413     }
00414     
00415     template<typename A>
00416     inline void
00417     set_value(Array_2D<char,A>* const container,
00418               const unsigned int      x,
00419               const unsigned int      y,
00420               const double            r,
00421               const double            g,
00422               const double            b){
00423     
00424       (*container)(x,y) = static_cast<char>(255*std::max(std::max(r,g),b));
00425     }
00426 
00427   
00428     template<typename A>
00429     inline void
00430     get_value(const Array_2D<char,A>&    container,
00431               const unsigned int         x,
00432               const unsigned int         y,
00433               double* const              r,
00434               double* const              g,
00435               double* const              b){
00436 
00437       *r = static_cast<double>(container(x,y)) / 255.0;
00438       *g = *r;
00439       *b = *r;
00440     }
00441 
00442 
00443     /*
00444 
00445       ################
00446       # bool version #
00447       ################
00448     
00449     */
00450 
00451 
00452     template<typename A>
00453     inline unsigned int
00454     get_width(const Array_2D<bool,A>& container){
00455       return container.x_size();
00456     }
00457 
00458     
00459     template<typename A>
00460     inline unsigned int
00461     get_height(const Array_2D<bool,A>& container){
00462       return container.y_size();
00463     }
00464 
00465     
00466     template<typename A>
00467     inline void
00468     set_size(Array_2D<bool,A>* const container,
00469             const unsigned int        width,
00470             const unsigned int        height){
00471       container->resize(width,height);
00472     }
00473 
00474     
00475     template<typename A>
00476     inline void
00477     set_value(Array_2D<bool,A>* const container,
00478               const unsigned int      x,
00479               const unsigned int      y,
00480               const double            r,
00481               const double            g,
00482               const double            b){
00483     
00484       (*container)(x,y) = ((r!=0.0)||(g!=0.0)||(b!=0.0));
00485     }
00486 
00487   
00488     template<typename A>
00489     inline void
00490     get_value(const Array_2D<bool,A>& container,
00491               const unsigned int      x,
00492               const unsigned int      y,
00493               double* const           r,
00494               double* const           g,
00495               double* const           b){
00496 
00497       *r = container(x,y) ? 1.0 : 0.0;
00498       *g = *r;
00499       *b = *r;
00500     }
00501 
00502   }// END OF namespace  
00503 }// END OF namespace
00504 
00505 #endif

Generated on Fri Aug 20 15:03:52 2004 by doxygen1.2.18