TuttleOFX  1
main.cpp
Go to the documentation of this file.
00001 #define WITHOUT_OFX
00002 
00003 #include <tuttle/plugin/utils/global.hpp>
00004 #include <terry/globals.hpp>
00005 #include <tuttle/plugin/ofx/ofxsGil.hpp>
00006 #include <tuttle/plugin/ofx/Progress.hpp>
00007 
00008 #include <lensDistortResample.hpp>
00009 
00010 #include <string>
00011 #include <sstream>
00012 
00013 #include <boost/gil/gil_all.hpp>
00014 #include <boost/gil/extension/io/png_dynamic_io.hpp>
00015 #include <boost/gil/extension/io/jpeg_dynamic_io.hpp>
00016 #include <boost/gil/extension/io/tiff_dynamic_io.hpp>
00017 
00018 using namespace boost::gil;
00019 using namespace tuttle::plugin::lens;
00020 typedef double F;
00021 
00022 int main( int argc, char** argv )
00023 {
00024         if( argc <= 2 )
00025         {
00026                 TUTTLE_LOG_FATAL( "need one argument (image filename) : " << argv[0] << " image.jpg 0.5" );
00027                 return 1;
00028         }
00029         std::string path( argv[1] );
00030         std::string ext( path, path.rfind( '.' ) + 1, 3 );
00031 
00032         TUTTLE_LOG_VAR( TUTTLE_INFO, path );
00033         TUTTLE_LOG_VAR( TUTTLE_INFO, ext );
00034 
00035         boost::gil::rgba8_image_t img_read;
00036         /*
00037            if( ext == "jpg" )
00038             jpeg_read_image( path.c_str(), img_read );
00039            else*/if( ext == "png" )
00040                 png_read_image( path.c_str(), img_read );
00041         /*else if( ext == "tif" )
00042             tiff_read_image( path.c_str(), img_read );*/
00043         else
00044         {
00045                 TUTTLE_LOG_FATAL( "Image file extension not recognize : " << ext );
00046                 return 1;
00047         }
00048 
00049         std::stringstream ss( argv[2] ); // Could of course also have done ss("1234") directly.
00050         double coef1;
00051 
00052         if( ( ss >> coef1 ).fail() )
00053         {
00054                 TUTTLE_LOG_FATAL( "Value not recognize : " << argv[2] );
00055                 return 1;
00056         }
00057         TUTTLE_LOG_VAR( TUTTLE_INFO, coef1 );
00058 
00059         boost::gil::rgb32f_image_t img_src( img_read.dimensions() );
00060         boost::gil::rgb32f_image_t img_dst( img_read.dimensions() );
00061 
00062         copy_and_convert_pixels( const_view( img_read ), view( img_src ) );
00063         //      copy_and_convert_pixels( const_view(img_read), view(img_dst) );
00064 
00065         //    typedef mpl::vector<rgb32_image_t> my_img_types;
00066         //      any_image<my_img_types> img;
00067         //      jpeg_read_image("input.jpg", img);
00068 
00069         //png_write_view( "output_crop.png", color_converted_view<rgb8_pixel_t>( subimage_view(const_view(img_src), -250, -558, img_src.dimensions().x+800, img_src.dimensions().y+1500 ) ) );
00070 
00071         fill_pixels( view( img_dst ), rgb32f_pixel_t( 0.8, 0.0, 0.0 ) );
00072 
00073         tuttle::plugin::lens::AdvancedLensDistortParams<double> _p;
00074         /*
00075            point2<int> imgDataSize = point2<int>( img_src.dimensions().x, img_src.dimensions().y );
00076            _p._dstDataSize = point2<int>( imgDataSize );
00077            _p._imgSize = point2<double>( imgDataSize.x, imgDataSize.y );
00078            _p._imgCenter = point2<double>( _p._imgSize.x / 2.0, _p._imgSize.y / 2.0 );
00079            _p._imgDiagonal = std::sqrt( _p._imgSize.x * _p._imgSize.x + _p._imgSize.y * _p._imgSize.y );
00080            _p._pixelRatio = 1.0;
00081            _p._pixelRatioInv = 1.0 / _p._pixelRatio;
00082 
00083 
00084            int _lensType = 0;
00085            int _interpolation = 1;
00086            _p._coef1 = coef1;
00087            _p._coef2 = 0.0;
00088            _p._squeeze = 1;
00089            _p._asymmetric = point2<double>( 0.0, 0.0);
00090            _p._lensCenter = _p.centerNormalizedToPixel( point2<double>( 0.0, 0.0) );
00091            _p._scale = 1.0;
00092          */
00093         //--------------------------------------------------------------------//
00094         ELensType _lensType           = eNormalLens;
00095         EInterpolation _interpolation = eBilinear;
00096         _p._coef1 = coef1;
00097         if( _p._coef1 >= 0 )
00098                 _p._distort = true;     // distort
00099         else
00100                 _p._distort = false;     // undistort
00101         _p._coef1      = std::abs( _p._coef1 );
00102         _p._coef2      = 0;
00103         _p._squeeze    = 0;
00104         _p._asymmetric = point2<double>( 0.0, 0.0 );
00105         _p._scale      = 1.0;
00106         if( _p._scale != 0 )
00107                 _p._scale = 1.0 / _p._scale;
00108         point2<F> imgShift = point2<F>( 0.0, 0.0 );
00109         point2<F> imgSize  = point2<F>( img_src.dimensions().x, img_src.dimensions().y );
00110         _p._imgCenterSrc  = point2<F>( imgSize.x * 0.5, imgSize.y * 0.5 );
00111         _p._imgCenterDst  = point2<F>( imgSize.x * 0.5, imgSize.y * 0.5 ) + imgShift;
00112         _p._imgDiagonal   = std::sqrt( _p._imgCenterSrc.x * _p._imgCenterSrc.x + _p._imgCenterSrc.y * _p._imgCenterSrc.y );
00113         _p._pixelRatio    = 1;
00114         _p._lensCenterDst = point2<double>( 0.5, 0.5 ) * imgSize;
00115         _p._lensCenterSrc = _p._lensCenterDst + imgShift;
00116 
00117         TUTTLE_LOG_INFO( "---" );
00118         TUTTLE_LOG_VAR2( TUTTLE_INFO, _p._lensCenterSrc.x, _p._lensCenterSrc.y );
00119         TUTTLE_LOG_VAR2( TUTTLE_INFO, _p._lensCenterDst.x, _p._lensCenterDst.y );
00120         TTUTTLE_LOG_VAR2( TUTTLE_INFO, _p._imgCenterSrc.x, _p._imgCenterSrc.y );
00121         TUTTLE_LOG_VAR2( TUTTLE_INFO, _p._imgCenterDst.x, _p._imgCenterDst.y );
00122         TUTTLE_LOG_VAR( TUTTLE_INFO, _p._imgDiagonal );
00123         TUTTLE_LOG_VAR( TUTTLE_INFO, _p._pixelRatio );
00124 
00125         ofx::Progress progress;
00126         OfxRectI procWindow;
00127         procWindow.x1 = 0;
00128         procWindow.y1 = 0;
00129         procWindow.x2 = imgSize.x;
00130         procWindow.y2 = imgSize.y;
00131         tuttle::plugin::lens::resample_pixels<bilinear_sampler>( const_view( img_src ), view( img_dst ), static_cast<tuttle::plugin::lens::NormalLensDistortParams<double>*>( &_p ), procWindow, &progress );
00132 
00133         png_write_view( "output_src.png", color_converted_view<rgb8_pixel_t>( const_view( img_src ) ) );
00134         png_write_view( "output_dst.png", color_converted_view<rgb8_pixel_t>( const_view( img_dst ) ) );
00135         tiff_write_view( "output_dst.tiff", color_converted_view<rgb8_pixel_t>( const_view( img_dst ) ) );
00136 }
00137