LensTools.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
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, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  */
00025 
00026 #include "panoinc_WX.h"
00027 #include "panoinc.h"
00028 #include "LensTools.h"
00029 #include <algorithm>
00030 #include "panodata/ImageVariableTranslate.h"
00031 
00032 using namespace std;
00033 
00034 void FillLensProjectionList(wxControlWithItems* list)
00035 {
00036     list->Clear();
00037     list->Append(_("Normal (rectilinear)"),(void*)HuginBase::SrcPanoImage::RECTILINEAR);
00038     list->Append(_("Panoramic (cylindrical)"),(void*)HuginBase::SrcPanoImage::PANORAMIC);
00039     list->Append(_("Circular fisheye"),(void*)HuginBase::SrcPanoImage::CIRCULAR_FISHEYE);
00040     list->Append(_("Full frame fisheye"),(void*)HuginBase::SrcPanoImage::FULL_FRAME_FISHEYE);
00041     list->Append(_("Equirectangular"),(void*)HuginBase::SrcPanoImage::EQUIRECTANGULAR);
00042     list->Append(_("Orthographic"),(void*)HuginBase::SrcPanoImage::FISHEYE_ORTHOGRAPHIC);
00043     list->Append(_("Stereographic"),(void*)HuginBase::SrcPanoImage::FISHEYE_STEREOGRAPHIC);
00044     list->Append(_("Equisolid"),(void*)HuginBase::SrcPanoImage::FISHEYE_EQUISOLID);
00045     list->Append(_("Fisheye Thoby"),(void*)HuginBase::SrcPanoImage::FISHEYE_THOBY);
00046     list->SetSelection(0);
00047 };
00048 
00049 void SelectProjection(wxControlWithItems* list,size_t new_projection)
00050 {
00051     for(unsigned int i=0;i<list->GetCount();i++)
00052     {
00053         if((size_t)list->GetClientData(i)==new_projection)
00054         {
00055             list->SetSelection(i);
00056             return;
00057         };
00058     };
00059     list->SetSelection(0);
00060 };
00061 
00062 size_t GetSelectedProjection(wxControlWithItems* list)
00063 {
00064     return (size_t)(list->GetClientData(list->GetSelection()));
00065 };
00066 
00067 void SaveLensParameters(const wxString filename, HuginBase::Panorama* pano, unsigned int imgNr)
00068 {
00069     HuginBase::StandardImageVariableGroups variable_groups(*pano);
00070     const HuginBase::Lens & lens = variable_groups.getLensForImage(imgNr);
00071     const HuginBase::VariableMap & vars = pano->getImageVariables(imgNr);
00072     // get the variable map
00073     char * p = setlocale(LC_NUMERIC,NULL);
00074     char * old_locale = strdup(p);
00075     setlocale(LC_NUMERIC,"C");
00076     wxFileConfig cfg(wxT("hugin lens file"),wxT(""),filename);
00077     cfg.Write(wxT("Lens/image_width"), (long) lens.getImageSize().x);
00078     cfg.Write(wxT("Lens/image_height"), (long) lens.getImageSize().y);
00079     cfg.Write(wxT("Lens/type"), (long) lens.getProjection());
00080     cfg.Write(wxT("Lens/hfov"), const_map_get(vars,"v").getValue());
00081     cfg.Write(wxT("Lens/hfov_link"), const_map_get(lens.variables,"v").isLinked() ? 1:0);
00082     cfg.Write(wxT("Lens/crop"), lens.getCropFactor());
00083 
00084     // loop to save lens variables
00085     const char ** varname = HuginBase::Lens::variableNames;
00086     while (*varname)
00087     {
00088         //ignore exposure value and hfov, hfov is separately handled by the code above
00089         if (string(*varname) == "Eev" || string(*varname) == "v")
00090         {
00091             varname++;
00092             continue;
00093         }
00094         wxString key(wxT("Lens/"));
00095         key.append(wxString(*varname, wxConvLocal));
00096         cfg.Write(key, const_map_get(vars,*varname).getValue());
00097         key.append(wxT("_link"));
00098         cfg.Write(key, const_map_get(lens.variables,*varname).isLinked() ? 1:0);
00099         varname++;
00100     }
00101 
00102     const HuginBase::SrcPanoImage & image = pano->getImage(imgNr);
00103     cfg.Write(wxT("Lens/crop/enabled"), image.getCropMode()==HuginBase::SrcPanoImage::NO_CROP ? 0l : 1l);
00104     cfg.Write(wxT("Lens/crop/autoCenter"), image.getAutoCenterCrop() ? 1l : 0l);
00105     const vigra::Rect2D cropRect=image.getCropRect();
00106     cfg.Write(wxT("Lens/crop/left"), cropRect.left());
00107     cfg.Write(wxT("Lens/crop/top"), cropRect.top());
00108     cfg.Write(wxT("Lens/crop/right"), cropRect.right());
00109     cfg.Write(wxT("Lens/crop/bottom"), cropRect.bottom());
00110 
00111     if (image.hasEXIFread())
00112     {
00113         // write exif data to ini file
00114         cfg.Write(wxT("EXIF/CameraMake"),  wxString(image.getExifMake().c_str(), wxConvLocal));
00115         cfg.Write(wxT("EXIF/CameraModel"), wxString(image.getExifModel().c_str(), wxConvLocal));
00116         cfg.Write(wxT("EXIF/FocalLength"), image.getExifFocalLength());
00117         cfg.Write(wxT("EXIF/Aperture"), image.getExifAperture());
00118         cfg.Write(wxT("EXIF/ISO"), image.getExifISO());
00119         cfg.Write(wxT("EXIF/CropFactor"), image.getExifCropFactor()); 
00120         cfg.Write(wxT("EXIF/Distance"), image.getExifDistance()); 
00121     }
00122     cfg.Flush();
00123 
00124     // reset locale
00125     setlocale(LC_NUMERIC,old_locale);
00126     free(old_locale);
00127 };
00128 
00129 bool ApplyLensParameters(wxWindow * parent, PT::Panorama *pano, HuginBase::UIntSet images, PT::PanoCommand*& cmd)
00130 {
00131     HuginBase::StandardImageVariableGroups variable_groups(*pano);
00132     HuginBase::Lens lens=variable_groups.getLensForImage(*images.begin());
00133     bool cropped=false;
00134     bool autoCenterCrop=false;
00135     vigra::Rect2D cropRect;
00136     if (LoadLensParametersChoose(parent, lens, cropped, autoCenterCrop, cropRect))
00137     {
00138         // Merge the lens parameters with the image variable map.
00139         HuginBase::VariableMapVector vars(images.size());
00140         size_t i=0;
00141         for(HuginBase::UIntSet::const_iterator it=images.begin();it!=images.end();it++,i++)
00142         {
00143             vars[i]=pano->getImageVariables(*it);
00144             for (HuginBase::LensVarMap::iterator it2 = lens.variables.begin(); it2 != lens.variables.end(); it2++)
00145             {
00146                 if(it2->second.getName()=="EeV")
00147                 {
00148                     continue;
00149                 };
00150                 vars[i].find(it2->first)->second.setValue(it2->second.getValue());
00151             }
00152         };
00153 
00157         std::vector<PT::PanoCommand*> cmds;
00158         // update links
00159         std::set<HuginBase::ImageVariableGroup::ImageVariableEnum> linkedVariables;
00160         std::set<HuginBase::ImageVariableGroup::ImageVariableEnum> unlinkedVariables;
00161         for (HuginBase::LensVarMap::iterator it = lens.variables.begin(); it != lens.variables.end(); it++)
00162         {
00163             if(it->second.getName()=="EeV")
00164             {
00165                 continue;
00166             };
00167 #define image_variable( name, type, default_value ) \
00168             if (HuginBase::PTOVariableConverterFor##name::checkApplicability(it->second.getName()))\
00169             {\
00170                 if(it->second.isLinked())\
00171                     linkedVariables.insert(HuginBase::ImageVariableGroup::IVE_##name);\
00172                 else\
00173                     unlinkedVariables.insert(HuginBase::ImageVariableGroup::IVE_##name);\
00174             }
00175 #include "panodata/image_variables.h"
00176 #undef image_variable
00177         }
00178         if (!linkedVariables.empty())
00179         {
00180             cmds.push_back(new PT::ChangePartImagesLinkingCmd(*pano, images, linkedVariables,
00181                 true,HuginBase::StandardImageVariableGroups::getLensVariables()));
00182         }
00183         if (!unlinkedVariables.empty())
00184         {
00185             cmds.push_back(new PT::ChangePartImagesLinkingCmd(*pano, images, unlinkedVariables,
00186                 false,HuginBase::StandardImageVariableGroups::getLensVariables()));
00187         }
00188         //update lens parameters
00189         cmds.push_back(new PT::UpdateImagesVariablesCmd(*pano, images, vars));
00190 
00191         // Set the lens projection type.
00192         cmds.push_back(new PT::ChangeImageProjectionCmd(*pano, images, (HuginBase::SrcPanoImage::Projection) lens.getProjection()));
00193         // update crop factor
00194         cmds.push_back(new PT::ChangeImageExifCropFactorCmd(*pano,images,lens.getCropFactor()));
00195         // update the crop rect
00196         cmds.push_back(new PT::ChangeImageAutoCenterCropCmd(*pano,images,autoCenterCrop));
00197         if(cropped)
00198         {
00199             cmds.push_back(new PT::ChangeImageCropRectCmd(*pano,images,cropRect));
00200         }
00201         else
00202         {
00203             cmds.push_back(new PT::ChangeImageCropModeCmd(*pano,images,HuginBase::BaseSrcPanoImage::NO_CROP));
00204         };
00205         cmd=new PT::CombinedPanoCommand(*pano, cmds);
00206         return true;
00207     }
00208     else
00209     {
00210         return false;
00211     };
00212 };
00213 
00214 bool LoadLensParametersChoose(wxWindow * parent, HuginBase::Lens & lens, 
00215      bool & cropped, bool & autoCenterCrop, vigra::Rect2D & cropRect)
00216 {
00217     wxString fname;
00218     wxFileDialog dlg(parent,
00219                         _("Load lens parameters"),
00220                         wxConfigBase::Get()->Read(wxT("/lensPath"),wxT("")), wxT(""),
00221                         _("Lens Project Files (*.ini)|*.ini|All files (*.*)|*.*"),
00222                         wxFD_OPEN, wxDefaultPosition);
00223     dlg.SetDirectory(wxConfigBase::Get()->Read(wxT("/lensPath"),wxT("")));
00224     if (dlg.ShowModal() == wxID_OK)
00225     {
00226         fname = dlg.GetPath();
00227         wxConfig::Get()->Write(wxT("/lensPath"), dlg.GetDirectory());  // remember for later
00228         // read with with standart C numeric format
00229         char * p = setlocale(LC_NUMERIC,NULL);
00230         char * old_locale = strdup(p);
00231         setlocale(LC_NUMERIC,"C");
00232         {
00233             wxFileConfig cfg(wxT("hugin lens file"),wxT(""),fname);
00234             long w=0;
00235             cfg.Read(wxT("Lens/image_width"), &w);
00236             long h=0;
00237             cfg.Read(wxT("Lens/image_height"), &h);
00238             if (w>0 && h>0) {
00239                 vigra::Size2D sz = lens.getImageSize();
00240                 if (w != sz.x || h != sz.y) {
00241                     cerr << "Image size: " << sz << " size in lens parameter file: " << w << "x" << h << std::endl;
00242                     int ret = wxMessageBox(_("Incompatible lens parameter file, image sizes do not match\nApply settings anyway?"), _("Error loading lens parameters"), wxICON_QUESTION |wxYES_NO);
00243                     if (ret == wxNO) {
00244                         setlocale(LC_NUMERIC,old_locale);
00245                         free(old_locale);
00246                         return false;
00247                     }
00248                 }
00249             } else {
00250                 // lens ini file didn't store the image size,
00251                 // assume everything is all right.
00252             }
00253             long integer=0;
00254             if(cfg.Read(wxT("Lens/type"), &integer))
00255             {
00256                 lens.setProjection((HuginBase::Lens::LensProjectionFormat) integer);
00257             };
00258             double d=1;
00259             if(cfg.Read(wxT("Lens/crop"), &d))
00260             {
00261                 lens.setCropFactor(d);
00262             };
00263             //special treatment for hfov, we are reading hfov and hfov_linked instead of v and v_linked
00264             d=50;
00265             if(cfg.Read(wxT("Lens/hfov"), &d))
00266             {
00267                 map_get(lens.variables,"v").setValue(d);
00268             };
00269             integer=1;
00270             if(cfg.Read(wxT("Lens/hfov_linked"), &integer))
00271             {
00272                 map_get(lens.variables,"v").setLinked(integer != 0);
00273             };
00274             DEBUG_DEBUG("read lens hfov: " << d);
00275 
00276             // loop to load lens variables
00277             const char ** varname = HuginBase::Lens::variableNames;
00278             while (*varname) {
00279                 if (string(*varname) == "Eev")
00280                 {
00281                     varname++;
00282                     continue;
00283                 }
00284                 wxString key(wxT("Lens/"));
00285                 key.append(wxString(*varname, wxConvLocal));
00286                 d = 0;
00287                 if (cfg.Read(key,&d))
00288                 {
00289                     // only set value if variabe was found in the script
00290                     map_get(lens.variables, *varname).setValue(d);
00291                     integer = 1;
00292                     key.append(wxT("_link"));
00293                     if(cfg.Read(key, &integer))
00294                     {
00295                         map_get(lens.variables, *varname).setLinked(integer != 0);
00296                     };
00297                 }
00298                 varname++;
00299             }
00300 
00301             // crop parameters
00302             long v=0;
00303             cfg.Read(wxT("Lens/crop/enabled"), &v);
00304             cropped=(v!=0);
00305             if(cropped)
00306             {
00307                 long left=0;
00308                 long top=0;
00309                 long right=0;
00310                 long bottom=0;
00311                 if(cfg.Read(wxT("Lens/crop/left"), &left) && cfg.Read(wxT("Lens/crop/top"), &top) &&
00312                     cfg.Read(wxT("Lens/crop/right"), &right) && cfg.Read(wxT("Lens/crop/bottom"), &bottom))
00313                 {
00314                     cropped=true;
00315                     cropRect=vigra::Rect2D(left,top,right,bottom);
00316                 }
00317                 else
00318                 {
00319                     cropped=false;
00320                 };
00321             };
00322             v=1;
00323             if(cfg.Read(wxT("Lens/crop/autoCenter"), &v))
00324             {
00325                 autoCenterCrop=(v!=0);
00326             };
00327         }
00328         // reset locale
00329         setlocale(LC_NUMERIC,old_locale);
00330         free(old_locale);
00331         return true;
00332     }
00333     else
00334     {
00335         return false;
00336     };
00337 };
00338 
00339 void SaveLensParametersToIni(wxWindow * parent, PT::Panorama *pano, const HuginBase::UIntSet images)
00340 {
00341     if (images.size() == 1)
00342     {
00343         unsigned int imgNr = *(images.begin());
00344         wxFileDialog dlg(parent,
00345                          _("Save lens parameters file"),
00346                          wxConfigBase::Get()->Read(wxT("/lensPath"),wxT("")), wxT(""),
00347                          _("Lens Project Files (*.ini)|*.ini|All files (*)|*"),
00348                          wxFD_SAVE, wxDefaultPosition);
00349         dlg.SetDirectory(wxConfigBase::Get()->Read(wxT("/lensPath"),wxT("")));
00350         if (dlg.ShowModal() == wxID_OK)
00351         {
00352             wxFileName filename(dlg.GetPath());
00353             if(!filename.HasExt())
00354             {
00355                 filename.SetExt(wxT("ini"));
00356             }
00357             wxConfig::Get()->Write(wxT("/lensPath"), dlg.GetDirectory());  // remember for later
00358             SaveLensParameters(filename.GetFullPath(),pano,imgNr);
00359         }
00360     }
00361     else 
00362     {
00363         wxLogError(_("Please select an image and try again"));
00364     }
00365 }
00366 
00367 

Generated on Thu Jul 31 01:25:31 2014 for Hugintrunk by  doxygen 1.3.9.1