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