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

Generated on 27 Jun 2016 for Hugintrunk by  doxygen 1.4.7