Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CachedImageRemapper.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
2 
27 #include "CachedImageRemapper.h"
28 
29 #include <vigra/basicimageview.hxx>
30 #include <vigra/copyimage.hxx>
32 
33 
34 namespace HuginBase {
35 
37 {
38  invalidate();
39 }
40 
43  const PanoramaOptions & popts,
44  unsigned int imgNr,
45  vigra::Rect2D outputROI,
46  AppBase::ProgressDisplay* progress)
47 {
48  // always map to HDR mode. curve and exposure is applied in preview window, for speed
49  PanoramaOptions opts = popts;
51  opts.outputExposureValue = 0.0;
52 
53  // return old image, if already in cache and if it has changed since the last rendering
54  if (set_contains(m_images, imgNr)) {
55  // return cached image if the parameters of the image have not changed
56  SrcPanoImage oldParam = m_imagesParam[imgNr];
57  if (oldParam == pano.getSrcImage(imgNr)
58  && m_panoOpts[imgNr].getHFOV() == opts.getHFOV()
59  && m_panoOpts[imgNr].getWidth() == opts.getWidth()
60  && m_panoOpts[imgNr].getHeight() == opts.getHeight()
61  && m_panoOpts[imgNr].getProjection() == opts.getProjection()
62  && m_panoOpts[imgNr].getProjectionParameters() == opts.getProjectionParameters()
63  )
64  {
65  DEBUG_DEBUG("using cached remapped image " << imgNr);
66  return m_images[imgNr];
67  }
68  }
69 
71 
72  // remap image
73  DEBUG_DEBUG("remapping image " << imgNr);
74 
75  // load image
76  const SrcPanoImage & img = pano.getImage(imgNr);
77 
78  ImageCache::EntryPtr e = ImageCache::getInstance().getSmallImage(img.getFilename().c_str());
79  if ( (e->image8->width() == 0) && (e->image16->width() == 0) && (e->imageFloat->width() == 0) ) {
80  throw std::runtime_error("could not retrieve small source image for preview generation");
81  }
82  vigra::Size2D srcImgSize;
83  if (e->image8->width() > 0)
84  srcImgSize = e->image8->size();
85  else if (e->image16->width() > 0)
86  srcImgSize = e->image16->size();
87  else
88  srcImgSize = e->imageFloat->size();
89 
90  MRemappedImage *remapped = new MRemappedImage;
91  remapped->m_ICCProfile = *(e->iccProfile);
92  SrcPanoImage srcPanoImg = pano.getSrcImage(imgNr);
93  // adjust distortion parameters for small preview image
94  srcPanoImg.resize(srcImgSize, nullptr);
95 
96  vigra::FImage srcFlat;
97  // use complete image, by supplying an empty mask image
98  vigra::BImage srcMask;
99 
100  if (img.getVigCorrMode() & SrcPanoImage::VIGCORR_FLATFIELD) {
101  ImageCache::EntryPtr e = ImageCache::getInstance().getSmallImage(img.getFlatfieldFilename().c_str());
102  if (!e) {
103  delete remapped;
104  throw std::runtime_error("could not retrieve flatfield image for preview generation");
105  }
106  if (e->image8->width()) {
107  srcFlat.resize(e->image8->size());
108  vigra::copyImage(srcImageRange(*(e->image8),
109  vigra::RGBToGrayAccessor<vigra::RGBValue<vigra::UInt8> >()),
110  vigra::destImage(srcFlat));
111  } else if (e->image16->width()) {
112  srcFlat.resize(e->image16->size());
113  vigra::copyImage(srcImageRange(*(e->image16),
114  vigra::RGBToGrayAccessor<vigra::RGBValue<vigra::UInt16> >()),
115  vigra::destImage(srcFlat));
116  } else {
117  srcFlat.resize(e->imageFloat->size());
118  vigra::copyImage(srcImageRange(*(e->imageFloat),
119  vigra::RGBToGrayAccessor<vigra::RGBValue<float> >()),
120  vigra::destImage(srcFlat));
121  }
122  }
123  progress->setMessage("remapping");
124 
125  // compute the bounding output rectangle here!
126  vigra::Rect2D outROI = estimateOutputROI(pano, opts, imgNr);
127  DEBUG_DEBUG("srcPanoImg size: " << srcPanoImg.getSize() << " pano roi:" << outROI);
128 
129  if (e->imageFloat->width()) {
130  // remap image
131  remapImage(*(e->imageFloat),
132  srcMask,
133  srcFlat,
134  srcPanoImg,
135  opts,
136  outROI,
137  *remapped,
138  progress);
139  } else if (e->image16->width()) {
140  // remap image
141  remapImage(*(e->image16),
142  srcMask,
143  srcFlat,
144  srcPanoImg,
145  opts,
146  outROI,
147  *remapped,
148  progress);
149  } else {
150  remapImage(*(e->image8),
151  srcMask,
152  srcFlat,
153  srcPanoImg,
154  opts,
155  outROI,
156  *remapped,
157  progress);
158  }
159 
160  m_images[imgNr] = remapped;
161  m_imagesParam[imgNr] = pano.getSrcImage(imgNr);
162  m_panoOpts[imgNr] = opts;
163  return remapped;
164 }
165 
166 
168 {
169  DEBUG_DEBUG("Clear remapped cache");
170  for(std::map<unsigned int, MRemappedImage*>::iterator it = m_images.begin();
171  it != m_images.end(); ++it)
172  {
173  delete (*it).second;
174  }
175  // remove all images
176  m_images.clear();
177  m_imagesParam.clear();
178 }
179 
180 void SmallRemappedImageCache::invalidate(unsigned int imgNr)
181 {
182  DEBUG_DEBUG("Remove " << imgNr << " from remapped cache");
183  if (set_contains(m_images, imgNr)) {
184  delete (m_images[imgNr]);
185  m_images.erase(imgNr);
186  m_imagesParam.erase(imgNr);
187  }
188 }
189 
190 
191 } //namespace
PanoramaOptions::ProjectionFormat getProjection() const
virtual MRemappedImage * getRemapped(const PanoramaData &pano, const PanoramaOptions &opts, unsigned int imgNr, vigra::Rect2D outputROI, AppBase::ProgressDisplay *progress)
create a remapped pano image.
std::map< unsigned, MRemappedImage * > m_images
void remapImage(SrcImgType &srcImg, const MaskImgType &srcAlpha, const FlatImgType &srcFlat, const SrcPanoImage &src, const PanoramaOptions &dest, vigra::Rect2D outputRect, RemappedPanoImage< DestImgType, MaskImgType > &remapped, AppBase::ProgressDisplay *progress)
remap a single image
vigra::Rect2D estimateOutputROI(const PanoramaData &pano, const PanoramaOptions &opts, unsigned i, const double maxLength)
unsigned int getHeight() const
get panorama height
bool set_contains(const _Container &c, const typename _Container::key_type &key)
Definition: stl_utils.h:74
static ImageCache & getInstance()
get the global ImageCache object
Definition: ImageCache.cpp:294
std::map< unsigned, SrcPanoImage > m_imagesParam
std::shared_ptr< Entry > EntryPtr
a shared pointer to the entry
Definition: ImageCache.h:112
void resize(const vigra::Size2D &size, VariableMap *potentialLinkedVars)
&quot;resize&quot; image, adjusts all distortion coefficients for usage with a source image of size size potent...
vigra::ImageImportInfo::ICCProfile m_ICCProfile
void setMessage(const std::string &message, const std::string &filename="")
sets the message to given string
Model for a panorama.
Definition: PanoramaData.h:81
virtual const SrcPanoImage & getImage(std::size_t nr) const =0
get a panorama image, counting starts with 0
EntryPtr getSmallImage(const std::string &filename)
get an small image.
Definition: ImageCache.cpp:639
vigra::pair< typename ROIImage< Image, Alpha >::image_traverser, typename ROIImage< Image, Alpha >::ImageAccessor > destImage(ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:324
vigra::triple< typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::ImageConstAccessor > srcImageRange(const ROIImage< Image, Mask > &img)
helper function for ROIImages
Definition: ROIImage.h:287
void softFlush()
a soft version of flush.
Definition: ImageCache.cpp:173
Nona::RemappedPanoImage< vigra::FRGBImage, vigra::BImage > MRemappedImage
unsigned int getWidth() const
const std::vector< double > & getProjectionParameters() const
Get the optional projection parameters.
std::map< unsigned, PanoramaOptions > m_panoOpts
#define DEBUG_DEBUG(msg)
Definition: utils.h:68
vigra::pair< typename ROIImage< Image, Alpha >::mask_const_traverser, typename ROIImage< Image, Alpha >::MaskConstAccessor > srcMask(const ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:347
void copyImage(SrcImageIterator src_upperleft, SrcImageIterator src_lowerright, SrcAccessor src_acc, DestImageIterator dest_upperleft, DestAccessor dest_acc)
Definition: openmp_vigra.h:305
void invalidate()
invalidates all images
virtual SrcPanoImage getSrcImage(unsigned imgNr) const =0
get a complete description of a source image
All variables of a source image.
Definition: SrcPanoImage.h:194
Panorama image options.
struct to hold a image state for stitching