ImageRemapper.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00027 #ifndef _NONA_IMAGEREMAPPER_H
00028 #define _NONA_IMAGEREMAPPER_H
00029 
00030 
00031 #include <panodata/PanoramaData.h>
00032 #include <nona/RemappedPanoImage.h>
00033 #include <vigra_ext/impexalpha.hxx>
00034 
00035 namespace HuginBase {
00036 namespace Nona {
00037 
00038         
00040     template <typename ImageType, typename AlphaType>
00041     class SingleImageRemapper
00042     {
00043         
00044         public:
00045             SingleImageRemapper() : m_clipExposureMask(false)
00046             {};
00047 
00052             virtual RemappedPanoImage<ImageType,AlphaType>* getRemapped(const PanoramaData & pano,
00053                                                                         const PanoramaOptions & opts,
00054                                                                         unsigned int imgNr,
00055                                                                         vigra::Rect2D outputROI,
00056                                                                         AppBase::ProgressDisplay* progress) = 0;
00057             
00058             virtual ~SingleImageRemapper() {};
00059             
00060             void SetClipExposureMask(bool doClipExposure)
00061             {
00062                 m_clipExposureMask = doClipExposure;
00063             }
00064 
00066             virtual     void release(RemappedPanoImage<ImageType,AlphaType>* d) = 0;
00067         protected:
00068             bool m_clipExposureMask;
00069         
00070     };
00071 
00072 
00074     template <typename ImageType, typename AlphaType>
00075     class FileRemapper : public SingleImageRemapper<ImageType, AlphaType>
00076     {
00077         
00078     public:
00079         FileRemapper() : SingleImageRemapper<ImageType, AlphaType>()
00080         {
00081             m_remapped = 0;
00082         }
00083 
00084         virtual ~FileRemapper() {};
00085 
00086     typedef std::vector<float> LUT;
00087 
00088 
00089     public:
00091         void loadImage(const PanoramaOptions & opts,
00092                      vigra::ImageImportInfo & info, ImageType & srcImg,
00093                      AlphaType & srcAlpha)
00094             {}
00095 
00097         virtual RemappedPanoImage<ImageType, AlphaType>*
00098         getRemapped(const PanoramaData & pano, const PanoramaOptions & opts,
00099                     unsigned int imgNr, vigra::Rect2D outputROI,
00100                     AppBase::ProgressDisplay* progress);
00101 
00103         virtual void release(RemappedPanoImage<ImageType,AlphaType>* d)
00104             { delete d; }
00105 
00106 
00107     protected:
00108         RemappedPanoImage<ImageType,AlphaType> * m_remapped;
00109 
00110     };
00111 
00112 
00113 
00115     template <class FFType, class SrcIter, class SrcAccessor, class DestIter, class DestAccessor>
00116     void applyFlatfield(vigra::triple<SrcIter, SrcIter, SrcAccessor> srcImg,
00117                         vigra::pair<DestIter, DestAccessor> destImg,
00118                         vigra::ImageImportInfo & ffInfo,
00119                         double gamma,
00120                         double gammaMaxVal,
00121                         bool division,
00122                         typename vigra::NumericTraits<typename SrcAccessor::value_type>::RealPromote a,
00123                         typename vigra::NumericTraits<typename SrcAccessor::value_type>::RealPromote b,
00124                         bool dither);
00125 
00126 
00127 
00128 }; // namespace
00129 }; // namespace
00130 
00131 
00132 
00133 
00134 //==============================================================================
00135 // templated implementations
00136 
00137 
00138 #include <vigra/functorexpression.hxx>
00139 #include <vigra_ext/VignettingCorrection.h>
00140 
00141 
00142 namespace HuginBase {
00143 namespace Nona {
00144     
00145     
00146 template <typename ImageType, typename AlphaType>
00147 RemappedPanoImage<ImageType, AlphaType>*
00148     FileRemapper<ImageType,AlphaType>::getRemapped(const PanoramaData & pano, const PanoramaOptions & opts,
00149                               unsigned int imgNr, vigra::Rect2D outputROI,
00150                               AppBase::ProgressDisplay* progress)
00151 {
00152     typedef typename ImageType::value_type PixelType;
00153     
00154     //typedef typename vigra::NumericTraits<PixelType>::RealPromote RPixelType;
00155     //        typedef typename vigra::BasicImage<RPixelType> RImportImageType;
00156     typedef typename vigra::BasicImage<float> FlatImgType;
00157     
00158     FlatImgType ffImg;
00159     AlphaType srcAlpha;
00160     
00161     // choose image type...
00162     const SrcPanoImage & img = pano.getImage(imgNr);
00163     
00164     vigra::Size2D origSrcSize = img.getSize();
00165     // DGSW FIXME - Unreferenced
00166     //          const PanoCommand::VariableMap & srcVars = pano.getImageVariables(imgNr);
00167     //          const Lens & lens = pano.getLens(img.getLensNr());
00168     
00169     vigra::Size2D destSize(opts.getWidth(), opts.getHeight());
00170     
00171     m_remapped = new RemappedPanoImage<ImageType, AlphaType>;
00172     
00173     // load image
00174     
00175     vigra::ImageImportInfo info(img.getFilename().c_str());
00176 
00177     int width = info.width();
00178     int height = info.height();
00179 
00180     if (opts.remapUsingGPU) {
00181         // Extend image width to multiple of 8 for fast GPU transfers.
00182         const int r = width % 8;
00183         if (r != 0) width += 8 - r;
00184     }
00185 
00186     ImageType srcImg(width, height);
00187     m_remapped->m_ICCProfile = info.getICCProfile();
00188     
00189     if (info.numExtraBands() > 0) {
00190         srcAlpha.resize(width, height);
00191     }
00192     //int nb = info.numBands() - info.numExtraBands();
00193     bool alpha = info.numExtraBands() > 0;
00194     std::string type = info.getPixelType();
00195     
00196     SrcPanoImage src = pano.getSrcImage(imgNr);
00197     
00198     // import the image
00199     progress->setMessage("loading", hugin_utils::stripPath(img.getFilename()));
00200     
00201     if (alpha) {
00202         vigra::importImageAlpha(info, vigra::destImage(srcImg),
00203                                 vigra::destImage(srcAlpha));
00204     } else {
00205         vigra::importImage(info, vigra::destImage(srcImg));
00206     }
00207     // check if the image needs to be scaled to 0 .. 1,
00208     // this only works for int -> float, since the image
00209     // has already been loaded into the output container
00210     double maxv = vigra_ext::getMaxValForPixelType(info.getPixelType());
00211     if (maxv != vigra_ext::LUTTraits<PixelType>::max()) {
00212         double scale = ((double)vigra_ext::LUTTraits<PixelType>::max()) /  maxv;
00213         //std::cout << "Scaling input image (pixel type: " << info.getPixelType() << " with: " << scale << std::endl;
00214         transformImage(vigra::srcImageRange(srcImg), destImage(srcImg),
00215                        vigra::functor::Arg1()*vigra::functor::Param(scale));
00216     }
00217     
00218     // load flatfield, if needed.
00219     if (img.getVigCorrMode() & SrcPanoImage::VIGCORR_FLATFIELD) {
00220         // load flatfield image.
00221         vigra::ImageImportInfo ffInfo(img.getFlatfieldFilename().c_str());
00222         progress->setMessage("flatfield vignetting correction", hugin_utils::stripPath(img.getFilename()));
00223         vigra_precondition(( ffInfo.numBands() == 1),
00224                            "flatfield vignetting correction: "
00225                            "Only single channel flatfield images are supported\n");
00226         ffImg.resize(ffInfo.width(), ffInfo.height());
00227         vigra::importImage(ffInfo, vigra::destImage(ffImg));
00228     }
00229     m_remapped->setMaskClipExposure(SingleImageRemapper<ImageType, AlphaType>::m_clipExposureMask);
00230     // remap the image
00231     
00232     remapImage(srcImg, srcAlpha, ffImg,
00233                pano.getSrcImage(imgNr), opts,
00234                outputROI,
00235                *m_remapped,
00236                progress);
00237     return m_remapped;
00238 }
00239 
00240 
00242 template <class FFType, class SrcIter, class SrcAccessor, class DestIter, class DestAccessor>
00243 void applyFlatfield(vigra::triple<SrcIter, SrcIter, SrcAccessor> srcImg,
00244                     vigra::pair<DestIter, DestAccessor> destImg,
00245                     vigra::ImageImportInfo & ffInfo,
00246                     double gamma,
00247                     double gammaMaxVal,
00248                     bool division,
00249                     typename vigra::NumericTraits<typename SrcAccessor::value_type>::RealPromote a,
00250                     typename vigra::NumericTraits<typename SrcAccessor::value_type>::RealPromote b,
00251                     bool dither)
00252 {
00253     FFType ffImg(ffInfo.width(), ffInfo.height());
00254     vigra::importImage(ffInfo, vigra::destImage(ffImg));
00255     vigra_ext::flatfieldVigCorrection(srcImg, vigra::srcImage(ffImg), 
00256                                       destImg, gamma, gammaMaxVal, division, a, b, dither);
00257 }
00258 
00259 
00260 }; // namespace
00261 }; // namespace
00262 
00263 #endif // _H

Generated on 1 Sep 2015 for Hugintrunk by  doxygen 1.4.7