CachedImageRemapper.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00027 #include "CachedImageRemapper.h"
00028 
00029 #include <vigra/basicimageview.hxx>
00030 #include <algorithms/nona/ComputeImageROI.h>
00031 
00032 
00033 namespace HuginBase {
00034 
00035 using namespace vigra;
00036 
00037 
00038 
00039 SmallRemappedImageCache::~SmallRemappedImageCache()
00040 {
00041     invalidate();
00042 }
00043 
00044 SmallRemappedImageCache::MRemappedImage *
00045 SmallRemappedImageCache::getRemapped(const PanoramaData& pano,
00046                                      const PanoramaOptions & popts,
00047                                      unsigned int imgNr,
00048                                      vigra::Rect2D outputROI,
00049                                      AppBase::MultiProgressDisplay& progress)
00050 {
00051     // always map to HDR mode. curve and exposure is applied in preview window, for speed
00052     PanoramaOptions opts = popts;
00053     opts.outputMode = PanoramaOptions::OUTPUT_HDR;
00054     opts.outputExposureValue = 0.0;
00055 
00056     // return old image, if already in cache and if it has changed since the last rendering
00057     if (set_contains(m_images, imgNr)) {
00058         // return cached image if the parameters of the image have not changed
00059         SrcPanoImage oldParam = m_imagesParam[imgNr];
00060         if (oldParam == pano.getSrcImage(imgNr)
00061                 && m_panoOpts[imgNr].getHFOV() == opts.getHFOV()
00062                 && m_panoOpts[imgNr].getWidth() == opts.getWidth()
00063                 && m_panoOpts[imgNr].getHeight() == opts.getHeight()
00064                 && m_panoOpts[imgNr].getProjection() == opts.getProjection()
00065                 && m_panoOpts[imgNr].getProjectionParameters() == opts.getProjectionParameters()
00066            )
00067         {
00068             DEBUG_DEBUG("using cached remapped image " << imgNr);
00069             return m_images[imgNr];
00070         }
00071     }
00072 
00073     ImageCache::getInstance().softFlush();
00074 
00075     typedef  BasicImageView<RGBValue<unsigned char> > BRGBImageView;
00076 
00077 //    typedef NumericTraits<PixelType>::RealPromote RPixelType;
00078 
00079     // remap image
00080     DEBUG_DEBUG("remapping image " << imgNr);
00081 
00082     // load image
00083     const SrcPanoImage & img = pano.getImage(imgNr);
00084 
00085     ImageCache::EntryPtr e = ImageCache::getInstance().getSmallImage(img.getFilename().c_str());
00086     if ( (e->image8->width() == 0) && (e->image16->width() == 0) && (e->imageFloat->width() == 0) ) {
00087         throw std::runtime_error("could not retrieve small source image for preview generation");
00088     }
00089     Size2D srcImgSize;
00090     if (e->image8->width() > 0)
00091         srcImgSize = e->image8->size();
00092     else if (e->image16->width() > 0)
00093         srcImgSize = e->image16->size();
00094     else
00095         srcImgSize = e->imageFloat->size();
00096 
00097     MRemappedImage *remapped = new MRemappedImage;
00098     SrcPanoImage srcPanoImg = pano.getSrcImage(imgNr);
00099     // adjust distortion parameters for small preview image
00100     srcPanoImg.resize(srcImgSize);
00101 
00102     FImage srcFlat;
00103     // use complete image, by supplying an empty mask image
00104     BImage srcMask;
00105 
00106     if (img.getVigCorrMode() & SrcPanoImage::VIGCORR_FLATFIELD) {
00107         ImageCache::EntryPtr e = ImageCache::getInstance().getSmallImage(img.getFlatfieldFilename().c_str());
00108         if (!e) {
00109             throw std::runtime_error("could not retrieve flatfield image for preview generation");
00110         }
00111         if (e->image8->width()) {
00112             srcFlat.resize(e->image8->size());
00113             copyImage(srcImageRange(*(e->image8),
00114                                     RGBToGrayAccessor<RGBValue<UInt8> >()),
00115                       destImage(srcFlat));
00116         } else if (e->image16->width()) {
00117             srcFlat.resize(e->image16->size());
00118             copyImage(srcImageRange(*(e->image16),
00119                                            RGBToGrayAccessor<RGBValue<vigra::UInt16> >()),
00120                              destImage(srcFlat));
00121         } else {
00122             srcFlat.resize(e->imageFloat->size());
00123             copyImage(srcImageRange(*(e->imageFloat),
00124                                     RGBToGrayAccessor<RGBValue<float> >()),
00125                       destImage(srcFlat));
00126         }
00127     }
00128     progress.pushTask(AppBase::ProgressTask("remapping", "", 0));
00129 
00130     // compute the bounding output rectangle here!
00131     vigra::Rect2D outROI = estimateOutputROI(pano, opts, imgNr);
00132     DEBUG_DEBUG("srcPanoImg size: " << srcPanoImg.getSize() << " pano roi:" << outROI);
00133 
00134     if (e->imageFloat->width()) {
00135         // remap image
00136         remapImage(*(e->imageFloat),
00137                    srcMask,
00138                    srcFlat,
00139                    srcPanoImg,
00140                    opts,
00141                    outROI,
00142                    *remapped,
00143                    progress);
00144     } else if (e->image16->width()) {
00145         // remap image
00146         remapImage(*(e->image16),
00147                    srcMask,
00148                    srcFlat,
00149                    srcPanoImg,
00150                    opts,
00151                    outROI,
00152                    *remapped,
00153                    progress);
00154     } else {
00155         remapImage(*(e->image8),
00156                      srcMask,
00157                      srcFlat,
00158                      srcPanoImg,
00159                      opts,
00160                      outROI,
00161                      *remapped,
00162                      progress);
00163     }
00164 
00165     progress.popTask();
00166 
00167     m_images[imgNr] = remapped;
00168     m_imagesParam[imgNr] = pano.getSrcImage(imgNr);
00169     m_panoOpts[imgNr] = opts;
00170     return remapped;
00171 }
00172 
00173 
00174 void SmallRemappedImageCache::invalidate()
00175 {
00176     DEBUG_DEBUG("Clear remapped cache");
00177     for(std::map<unsigned int, MRemappedImage*>::iterator it = m_images.begin();
00178         it != m_images.end(); ++it)
00179     {
00180         delete (*it).second;
00181     }
00182     // remove all images
00183     m_images.clear();
00184     m_imagesParam.clear();
00185 }
00186 
00187 void SmallRemappedImageCache::invalidate(unsigned int imgNr)
00188 {
00189     DEBUG_DEBUG("Remove " << imgNr << " from remapped cache");
00190     if (set_contains(m_images, imgNr)) {
00191         delete (m_images[imgNr]);
00192         m_images.erase(imgNr);
00193         m_imagesParam.erase(imgNr);
00194     }
00195 }
00196 
00197 
00198 } //namespace

Generated on 28 Nov 2014 for Hugintrunk by  doxygen 1.4.7