PreviewColorPickerTool.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00009 /*
00010  *  This program is free software; you can redistribute it and/or
00011  *  modify it under the terms of the GNU General Public
00012  *  License as published by the Free Software Foundation; either
00013  *  version 2 of the License, or (at your option) any later version.
00014  *
00015  *  This software is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *  General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public
00021  *  License along with this software. If not, see
00022  *  <http://www.gnu.org/licenses/>.
00023  *
00024  */
00025 
00026 #include <hugin/PreviewColorPickerTool.h>
00027 #include <wx/platform.h>
00028 #include "GLPreviewFrame.h"
00029 #include <huginapp/ImageCache.h>
00030 #include <photometric/ResponseTransform.h>
00031 #include <vigra_ext/ImageTransforms.h>
00032 #include <vigra/copyimage.hxx>
00033 #include <vigra/inspectimage.hxx>
00034 
00036 #define ColorPickerSize 10
00037 
00038 // we want to be notified if the user click into the panorama
00039 void PreviewColorPickerTool::Activate()
00040 {
00041     helper->NotifyMe(PreviewToolHelper::MOUSE_PRESS, this);
00042     helper->SetStatusMessage(_("Click on a area which should be neutral gray / white in the final panorama."));
00043 };
00044 
00045 // The user click, get the current position and update white balance
00046 void PreviewColorPickerTool::MouseButtonEvent(wxMouseEvent &e)
00047 {
00048     if ( e.LeftDown() && helper->IsMouseOverPano())
00049     {
00050         CalcCorrection(helper->GetMousePanoPosition());
00051         if(m_count>0 && m_red!=0 && m_blue!=0)
00052         {
00053             helper->GetPreviewFrame()->UpdateGlobalWhiteBalance(m_red,m_blue);
00054         }
00055         else
00056         {
00057             wxBell();
00058         };
00059     };
00060 };
00061 
00062 void PreviewColorPickerTool::CalcCorrection(hugin_utils::FDiff2D pos)
00063 {
00064     m_red=0;
00065     m_blue=0;
00066     m_count=0;
00067     HuginBase::Panorama* pano=helper->GetPanoramaPtr();
00068     HuginBase::UIntSet activeImages=pano->getActiveImages();
00069     if(activeImages.size()>0)
00070     {
00071         for(HuginBase::UIntSet::iterator it=activeImages.begin();it!=activeImages.end();++it)
00072         {
00073             //check if point is inside the image, check also all 4 corners of rectangle
00074             HuginBase::PTools::Transform trans;
00075             trans.createTransform(pano->getImage(*it),pano->getOptions());
00076             double x;
00077             double y;
00078             if(trans.transformImgCoord(x,y,pos.x,pos.y))
00079             {
00080                 vigra::Point2D imagePos(x,y);
00081                 if(pano->getImage(*it).isInside(imagePos) && 
00082                     pano->getImage(*it).isInside(imagePos + vigra::Point2D(-ColorPickerSize,-ColorPickerSize)) &&
00083                     pano->getImage(*it).isInside(imagePos + vigra::Point2D(-ColorPickerSize, ColorPickerSize)) &&
00084                     pano->getImage(*it).isInside(imagePos + vigra::Point2D( ColorPickerSize,-ColorPickerSize)) &&
00085                     pano->getImage(*it).isInside(imagePos + vigra::Point2D( ColorPickerSize, ColorPickerSize)) 
00086                    )
00087                 {
00088                     CalcCorrectionForImage(*it,imagePos);
00089                 };
00090             };
00091         };
00092     };
00093     if(m_count>0)
00094     {
00095         m_red=m_red/m_count;
00096         m_blue=m_blue/m_count;
00097     };
00098 };
00099 
00100 void PreviewColorPickerTool::CalcCorrectionForImage(unsigned int i,vigra::Point2D pos)
00101 {
00102     const HuginBase::SrcPanoImage & img = helper->GetPanoramaPtr()->getImage(i);
00103     HuginBase::ImageCache::ImageCacheRGB8Ptr cacheImage8 = HuginBase::ImageCache::getInstance().getImage(img.getFilename())->get8BitImage();
00104 
00105     //copy only region to be inspected
00106     vigra::BRGBImage tempImage(2*ColorPickerSize,2*ColorPickerSize);
00107     vigra::copyImage(vigra::make_triple((*cacheImage8).upperLeft() + pos + vigra::Point2D(-ColorPickerSize,-ColorPickerSize),
00108                                         (*cacheImage8).upperLeft() + pos + vigra::Point2D( ColorPickerSize, ColorPickerSize),
00109                                         vigra::BRGBImage::Accessor()),
00110                      destImage(tempImage) );
00111 
00112     //now apply photometric corrections
00113     HuginBase::Photometric::InvResponseTransform<vigra::UInt8, double> invResponse(img);
00114     if (helper->GetPanoramaPtr()->getOptions().outputMode == HuginBase::PanoramaOptions::OUTPUT_LDR)
00115     {
00116         // select exposure and response curve for LDR output
00117         std::vector<double> outLut;
00118         vigra_ext::EMoR::createEMoRLUT(helper->GetPanoramaPtr()->getImage(0).getEMoRParams(), outLut);
00119         vigra_ext::enforceMonotonicity(outLut);
00120         invResponse.setOutput(1.0/pow(2.0,helper->GetPanoramaPtr()->getOptions().outputExposureValue), outLut,
00121                               255.0);
00122     }
00123     else
00124     {
00125         invResponse.setHDROutput(true,1.0/pow(2.0,helper->GetPanoramaPtr()->getOptions().outputExposureValue));
00126     }
00127     vigra::DRGBImage floatTemp(tempImage.size());
00128     vigra_ext::transformImageSpatial(srcImageRange(tempImage), destImage(floatTemp), invResponse, vigra::Diff2D(pos.x-ColorPickerSize,pos.y-ColorPickerSize));
00129 
00130     //calculate average
00131     vigra::FindAverage<vigra::DRGBImage::PixelType> average;
00132     vigra::inspectImage(srcImageRange(floatTemp), average);
00133     //range check
00134     vigra::RGBValue<double> RGBaverage=average.average();
00135     if(RGBaverage[0]>2 && RGBaverage[0]<253 &&
00136        RGBaverage[1]>2 && RGBaverage[1]<253 &&
00137        RGBaverage[2]>2 && RGBaverage[2]<253)
00138     {
00139         m_red+=RGBaverage[0]/RGBaverage[1];
00140         m_blue+=RGBaverage[2]/RGBaverage[1];
00141         m_count++;
00142     };
00143 };

Generated on 28 Sep 2016 for Hugintrunk by  doxygen 1.4.7