wxLensDB.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 "wxLensDB.h"
00029 #include "lensdb/LensDB.h"
00030 #include "platform.h"
00031 #include "base_wx/wxPlatform.h"
00032 #include "panodata/ImageVariableTranslate.h"
00033 
00034 using namespace std;
00035 
00037 class LoadLensDBDialog : public wxDialog
00038 {
00039 public:
00041     LoadLensDBDialog(wxWindow *parent);
00042     void SetCameraMaker(std::string camMaker);
00043     std::string GetCameraMaker() const;
00044     void SetCameraModel(std::string camModel);
00045     std::string GetCameraModel() const;
00046     void SetLensName(std::string lensname);
00047     std::string GetLensName() const;
00049     bool ActivateSelectedLens();
00050     void SetFocalLength(double focal);
00051     double GetFocalLength() const;
00052     void SetAperture(double aperture);
00053     double GetAperture() const;
00054     void SetSubjectDistance(double distance);
00055     double GetSubjectDistance() const;
00056     bool GetLoadDistortion() const;
00057     bool GetLoadVignetting() const;
00058 
00059 protected:
00061     void OnOk(wxCommandEvent & e);
00063     void OnSearch(wxCommandEvent & e);
00064     void OnCheckChanged(wxCommandEvent & e);
00065 
00066 private:
00067     wxListBox *m_lenslist;
00068     wxCheckBox *m_fuzzySearch;
00069     wxCheckBox *m_loadDistortion;
00070     wxCheckBox *m_loadVignetting;
00071     double m_focal;
00072     double m_aperture;
00073     double m_distance;
00074     HuginBase::LensDB::LensDBList m_dblenslist;
00075     DECLARE_EVENT_TABLE()
00076 };
00077 
00078 BEGIN_EVENT_TABLE(LoadLensDBDialog,wxDialog)
00079     EVT_BUTTON(wxID_OK, LoadLensDBDialog::OnOk)
00080     EVT_BUTTON(XRCID("load_lens_search"), LoadLensDBDialog::OnSearch)
00081     EVT_LISTBOX(XRCID("load_lens_list"), LoadLensDBDialog::OnCheckChanged)
00082     EVT_CHECKBOX(XRCID("load_lens_distortion"), LoadLensDBDialog::OnCheckChanged)
00083     EVT_CHECKBOX(XRCID("load_lens_vignetting"), LoadLensDBDialog::OnCheckChanged)
00084 END_EVENT_TABLE()
00085 
00086 LoadLensDBDialog::LoadLensDBDialog(wxWindow *parent)
00087 {
00088     // load our children. some children might need special
00089     // initialization. this will be done later.
00090     wxXmlResource::Get()->LoadDialog(this, parent, wxT("load_lens_dlg"));
00091 
00092     //set parameters
00093     wxConfigBase * config = wxConfigBase::Get();
00094     // get display size
00095     int dx,dy;
00096     wxDisplaySize(&dx,&dy);
00097     int w = config->Read(wxT("/LoadLensDialog/width"),-1l);
00098     int h = config->Read(wxT("/LoadLensDialog/height"),-1l);
00099     if (w>0 && w<=dx && h>0 && h<=dy)
00100     {
00101         SetClientSize(w,h);
00102     }
00103     else
00104     {
00105         Fit();
00106     }
00107     //position
00108     int x = config->Read(wxT("/LoadLensDialog/positionX"),-1l);
00109     int y = config->Read(wxT("/LoadLensDialog/positionY"),-1l);
00110     if ( y >= 0 && x >= 0) 
00111     {
00112         this->Move(x, y);
00113     } 
00114     else 
00115     {
00116         this->Move(0, 44);
00117     };
00118     bool b;
00119     config->Read(wxT("/LoadLensDialog/fuzzySearch"),&b,false);
00120     XRCCTRL(*this,"load_lens_fuzzy",wxCheckBox)->SetValue(b);
00121     config->Read(wxT("/LoadLensDialog/loadDistortion"),&b,true);
00122     m_loadDistortion=XRCCTRL(*this,"load_lens_distortion",wxCheckBox);
00123     m_loadDistortion->SetValue(b);
00124     config->Read(wxT("/LoadLensDialog/loadVignetting"),&b,true);
00125     m_loadVignetting=XRCCTRL(*this,"load_lens_vignetting",wxCheckBox);
00126     m_loadVignetting->SetValue(b);
00127     m_lenslist=XRCCTRL(*this,"load_lens_list",wxListBox);
00128 };
00129 
00130 void LoadLensDBDialog::SetCameraMaker(std::string camMaker)
00131 {
00132     if(!camMaker.empty())
00133     {
00134         XRCCTRL(*this,"load_lens_camera_maker",wxTextCtrl)->SetValue(wxString(camMaker.c_str(), wxConvLocal));
00135     };
00136 };
00137 
00138 std::string LoadLensDBDialog::GetCameraMaker() const
00139 {
00140     return std::string(XRCCTRL(*this,"load_lens_camera_maker",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00141 };
00142 
00143 void LoadLensDBDialog::SetCameraModel(std::string camModel)
00144 {
00145     if(!camModel.empty())
00146     {
00147         XRCCTRL(*this,"load_lens_camera_model",wxTextCtrl)->SetValue(wxString(camModel.c_str(), wxConvLocal));
00148     };
00149 };
00150 
00151 std::string LoadLensDBDialog::GetCameraModel() const
00152 {
00153     return std::string(XRCCTRL(*this,"load_lens_camera_model",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00154 };
00155 
00156 void LoadLensDBDialog::SetLensName(std::string lensname)
00157 {
00158     if(!lensname.empty())
00159     {
00160         XRCCTRL(*this,"load_lens_name",wxTextCtrl)->SetValue(wxString(lensname.c_str(), wxConvLocal));
00161     };
00162     wxCommandEvent dummy;
00163     OnSearch(dummy);
00164 };
00165 
00166 std::string LoadLensDBDialog::GetLensName() const
00167 {
00168     return std::string(XRCCTRL(*this,"load_lens_name",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00169 };
00170 
00171 bool LoadLensDBDialog::ActivateSelectedLens()
00172 {
00173     int index=m_lenslist->GetSelection();
00174     if(index!=wxNOT_FOUND)
00175     {
00176         HuginBase::LensDB::LensDB::GetSingleton().SetActiveLens(m_dblenslist.GetLens(index));
00177         return true;
00178     }
00179     else
00180     {
00181         return false;
00182     };
00183 };
00184 
00185 void LoadLensDBDialog::SetFocalLength(double focal)
00186 {
00187     m_focal=focal;
00188     XRCCTRL(*this,"load_lens_focallength",wxTextCtrl)->SetValue(hugin_utils::doubleTowxString(m_focal,1));
00189 };
00190 
00191 double LoadLensDBDialog::GetFocalLength() const
00192 {
00193     return m_focal;
00194 };
00195 
00196 void LoadLensDBDialog::SetAperture(double aperture)
00197 {
00198     m_aperture=aperture;
00199     XRCCTRL(*this,"load_lens_aperture",wxTextCtrl)->SetValue(hugin_utils::doubleTowxString(m_aperture,1));
00200 };
00201 
00202 double LoadLensDBDialog::GetAperture() const
00203 {
00204     return m_aperture;
00205 };
00206 
00207 void LoadLensDBDialog::SetSubjectDistance(double distance)
00208 {
00209     m_distance=distance;
00210     XRCCTRL(*this,"load_lens_distance",wxTextCtrl)->SetValue(hugin_utils::doubleTowxString(m_distance,0));
00211 };
00212 
00213 double LoadLensDBDialog::GetSubjectDistance() const
00214 {
00215     return m_distance;
00216 };
00217 
00218 bool LoadLensDBDialog::GetLoadDistortion() const
00219 {
00220     return m_loadDistortion->GetValue();
00221 };
00222 
00223 bool LoadLensDBDialog::GetLoadVignetting() const
00224 {
00225     return m_loadVignetting->GetValue();
00226 };
00227 
00228 // utility functions
00229 bool str2double(wxWindow* parent, wxString s, double & d)
00230 {
00231     if (!hugin_utils::stringToDouble(std::string(s.mb_str(wxConvLocal)), d)) 
00232     {
00233         wxMessageBox(wxString::Format(_("The input \"%s\" is not a valid number."),s.c_str()),_("Warning"), wxOK | wxICON_ERROR, parent);
00234         return false;
00235     }
00236     return true;
00237 }
00238 
00239 void LoadLensDBDialog::OnOk(wxCommandEvent & e)
00240 {
00241     if(!m_loadDistortion->GetValue() && !m_loadVignetting->GetValue())
00242     {
00243         return;
00244     };
00245     if(m_lenslist->GetSelection()==wxNOT_FOUND)
00246     {
00247         wxBell();
00248         return;
00249     };
00250     if(!str2double(this,XRCCTRL(*this,"load_lens_focallength",wxTextCtrl)->GetValue(),m_focal))
00251     {
00252         return;
00253     };
00254     if(m_loadVignetting->GetValue())
00255     {
00256         if(!str2double(this,XRCCTRL(*this,"load_lens_aperture",wxTextCtrl)->GetValue(),m_aperture))
00257         {
00258             return;
00259         };
00260         if(!str2double(this,XRCCTRL(*this,"load_lens_distance",wxTextCtrl)->GetValue(),m_distance))
00261         {
00262             return;
00263         };
00264     };
00265     //store selected options
00266     wxConfigBase * config = wxConfigBase::Get();
00267     wxSize sz = this->GetClientSize();
00268     config->Write(wxT("/LoadLensDialog/width"), sz.GetWidth());
00269     config->Write(wxT("/LoadLensDialog/height"), sz.GetHeight());
00270     wxPoint ps = this->GetPosition();
00271     config->Write(wxT("/LoadLensDialog/positionX"), ps.x);
00272     config->Write(wxT("/LoadLensDialog/positionY"), ps.y);
00273     config->Write(wxT("/LoadLensDialog/fuzzySearch"),XRCCTRL(*this,"load_lens_fuzzy",wxCheckBox)->GetValue());
00274     config->Write(wxT("/LoadLensDialog/loadDistortion"),m_loadDistortion->GetValue());
00275     config->Write(wxT("/LoadLensDialog/loadVignetting"),m_loadVignetting->GetValue());
00276     config->Flush(); 
00277     e.Skip();
00278 };
00279 
00280 void LoadLensDBDialog::OnSearch(wxCommandEvent & e)
00281 {
00282     XRCCTRL(*this,"load_lens_status",wxStaticText)->SetLabel(wxT(""));
00283     if(!GetLensName().empty() || (!GetCameraModel().empty()))
00284     {
00285         m_lenslist->Clear();
00286         bool fuzzy=XRCCTRL(*this,"load_lens_fuzzy",wxCheckBox)->GetValue();
00287         if(HuginBase::LensDB::LensDB::GetSingleton().FindLenses(GetCameraMaker(),GetCameraModel(),GetLensName(),m_dblenslist,fuzzy))
00288         {
00289             wxArrayString items;
00290             for(size_t i=0; i<m_dblenslist.GetLensCount(); i++)
00291             {
00292                 wxString lensname=wxString(m_dblenslist.GetLensName(i).c_str(),wxConvLocal);
00293                 //now translate a string part
00294                 lensname.Replace(wxT("Focal length multiplier:"), _("Focal length multiplier:"), true);
00295                 items.push_back(lensname);
00296             };
00297             m_lenslist->InsertItems(items,0);
00298             m_lenslist->DeselectAll();
00299             XRCCTRL(*this,"load_lens_status",wxStaticText)->SetLabel(wxString::Format(_("%d lenses found."),items.size()));
00300         }
00301         else
00302         {
00303             XRCCTRL(*this,"load_lens_status",wxStaticText)->SetLabel(_("No lens found."));
00304         };
00305     }
00306     else
00307     {
00308         wxBell();
00309     };
00310     XRCCTRL(*this,"wxID_OK",wxButton)->Enable(false);
00311 };
00312 
00313 void LoadLensDBDialog::OnCheckChanged(wxCommandEvent & e)
00314 {
00315     int sel=m_lenslist->GetSelection();
00316     XRCCTRL(*this,"wxID_OK",wxButton)->Enable(sel!=wxNOT_FOUND && (m_loadDistortion->GetValue() || m_loadVignetting->GetValue()));
00317 };
00318 
00319 bool ApplyLensDBParameters(wxWindow * parent, PT::Panorama *pano, HuginBase::UIntSet images, PT::PanoCommand*& cmd)
00320 {
00321     LoadLensDBDialog dlg(parent);
00322     const HuginBase::SrcPanoImage & img=pano->getImage(*images.begin());
00323     dlg.SetCameraMaker(img.getExifMake());
00324     dlg.SetCameraModel(img.getExifModel());
00325     dlg.SetLensName(img.getExifLens());
00326     dlg.SetFocalLength(img.getExifFocalLength());
00327     dlg.SetAperture(img.getExifAperture());
00328     dlg.SetSubjectDistance(img.getExifDistance());
00329     if(dlg.ShowModal()==wxID_OK)
00330     {
00331         HuginBase::LensDB::LensDB & lensDB=HuginBase::LensDB::LensDB::GetSingleton();
00332         if(dlg.ActivateSelectedLens())
00333         {
00334             double focal=dlg.GetFocalLength();
00335             std::vector<PT::PanoCommand*> cmds;
00336             HuginBase::BaseSrcPanoImage::Projection proj;
00337             if(lensDB.GetProjection(proj))
00338             {
00339                 cmds.push_back(new PT::ChangeImageProjectionCmd(*pano,images,proj));
00340             };
00341             HuginBase::BaseSrcPanoImage::CropMode cropMode;
00342             hugin_utils::FDiff2D cropLeftTop;
00343             hugin_utils::FDiff2D cropRightBottom;
00344             if(lensDB.GetCrop(focal, cropMode, cropLeftTop, cropRightBottom))
00345             {
00346                 cmds.push_back(new PT::ChangeImageCropModeCmd(*pano, images, cropMode));
00347                 if(cropMode!=HuginBase::BaseSrcPanoImage::NO_CROP)
00348                 {
00349                     vigra::Rect2D cropRect;
00350                     const HuginBase::SrcPanoImage & img=pano->getImage(*images.begin());
00351                     int width=img.getSize().width();
00352                     int height=img.getSize().height();
00353                     if(width>height)
00354                     {
00355                         cropRect=vigra::Rect2D(cropLeftTop.x*width,cropLeftTop.y*height,cropRightBottom.x*width,cropRightBottom.y*height);
00356                     }
00357                     else
00358                     {
00359                         cropRect=vigra::Rect2D((1.0-cropRightBottom.y)*width,cropLeftTop.x*height,(1.0-cropLeftTop.y)*width,cropRightBottom.x*height);
00360                     };
00361                     cmds.push_back(new PT::ChangeImageCropRectCmd(*pano, images, cropRect));
00362                 };
00363             };
00364             //load lens distortion from database
00365             if(dlg.GetLoadDistortion())
00366             {
00367                 double hfov;
00368                 if(lensDB.GetFov(focal,hfov))
00369                 {
00370                     std::set<HuginBase::ImageVariableGroup::ImageVariableEnum> linkedVariables;
00371                     linkedVariables.insert(HuginBase::ImageVariableGroup::IVE_HFOV);
00372                     cmds.push_back(new PT::ChangePartImagesLinkingCmd(*pano, images, linkedVariables,
00373                         true,HuginBase::StandardImageVariableGroups::getLensVariables()));
00374                     cmds.push_back(new PT::ChangeImageHFOVCmd(*pano, images, hfov));
00375                 };
00376                 std::vector<double> dist;
00377                 if(lensDB.GetDistortion(focal,dist))
00378                 {
00379                     if(dist.size()==3)
00380                     {
00381                         dist.push_back(1.0-dist[0]-dist[1]-dist[2]);
00382                         std::set<HuginBase::ImageVariableGroup::ImageVariableEnum> linkedVariables;
00383                         linkedVariables.insert(HuginBase::ImageVariableGroup::IVE_RadialDistortion);
00384                         cmds.push_back(new PT::ChangePartImagesLinkingCmd(*pano, images, linkedVariables,
00385                             true,HuginBase::StandardImageVariableGroups::getLensVariables()));
00386                         cmds.push_back(new PT::ChangeImageRadialDistortionCmd(*pano,images,dist));
00387                     };
00388                 };
00389             };
00390             if(dlg.GetLoadVignetting())
00391             {
00392                 std::vector<double> vig;
00393                 if(lensDB.GetVignetting(focal,dlg.GetAperture(),dlg.GetSubjectDistance(),vig))
00394                 {
00395                     std::set<HuginBase::ImageVariableGroup::ImageVariableEnum> linkedVariables;
00396                     linkedVariables.insert(HuginBase::ImageVariableGroup::IVE_RadialVigCorrCoeff);
00397                     cmds.push_back(new PT::ChangePartImagesLinkingCmd(*pano, images, linkedVariables,
00398                         true,HuginBase::StandardImageVariableGroups::getLensVariables()));
00399                     cmds.push_back(new PT::ChangeImageRadialVigCorrCoeffCmd(*pano,images,vig));
00400                 };
00401             };
00402             cmd=new PT::CombinedPanoCommand(*pano, cmds);
00403             return true;
00404         };
00405     };
00406     return false;
00407 };
00408 
00410 class SaveLensDBDialog : public wxDialog
00411 {
00412 public:
00414     SaveLensDBDialog(wxWindow *parent);
00415     void SetCameraMaker(std::string maker);
00416     std::string GetCameraMaker() const;
00417     void SetCameraModel(std::string model);
00418     std::string GetCameraModel() const;
00419     void SetLensName(std::string lensname);
00420     std::string GetLensName() const;
00421     std::string GetLensMaker() const;
00422     void SetLensMount(std::string mount);
00423     std::string GetLensMount() const;
00424     void SetFocalLength(double focal);
00425     double GetFocalLength() const;
00426     void SetAperture(double aperture);
00427     double GetAperture() const;
00428     void SetSubjectDistance(double distance);
00429     double GetSubjectDistance() const;
00430     bool GetSaveDistortion() const;
00431     bool GetSaveVignetting() const;
00432     void DeactivateSaveVignetting();
00433 
00434 protected:
00436     void OnOk(wxCommandEvent & e);
00437     void OnCheckChanged(wxCommandEvent & e);
00438 
00439 private:
00440     wxCheckBox *m_saveDistortion;
00441     wxCheckBox *m_saveVignetting;
00442     double m_focal;
00443     double m_aperture;
00444     double m_distance;
00445     DECLARE_EVENT_TABLE()
00446 };
00447 
00448 BEGIN_EVENT_TABLE(SaveLensDBDialog,wxDialog)
00449     EVT_BUTTON(wxID_OK, SaveLensDBDialog::OnOk)
00450     EVT_CHECKBOX(XRCID("save_lens_distortion"), SaveLensDBDialog::OnCheckChanged)
00451     EVT_CHECKBOX(XRCID("save_lens_vignetting"), SaveLensDBDialog::OnCheckChanged)
00452 END_EVENT_TABLE()
00453 
00454 SaveLensDBDialog::SaveLensDBDialog(wxWindow *parent)
00455 {
00456     // load our children. some children might need special
00457     // initialization. this will be done later.
00458     wxXmlResource::Get()->LoadDialog(this, parent, wxT("save_lens_dlg"));
00459 
00460     //set parameters
00461     wxConfigBase * config = wxConfigBase::Get();
00462     // get display size
00463     int dx,dy;
00464     wxDisplaySize(&dx,&dy);
00465     int w = config->Read(wxT("/SaveLensDialog/width"),-1l);
00466     int h = config->Read(wxT("/SaveLensDialog/height"),-1l);
00467     if (w>0 && w<=dx && h>0 && h<=dy)
00468     {
00469         SetClientSize(w,h);
00470     }
00471     else
00472     {
00473         Fit();
00474     }
00475     //position
00476     int x = config->Read(wxT("/SaveLensDialog/positionX"),-1l);
00477     int y = config->Read(wxT("/SaveLensDialog/positionY"),-1l);
00478     if ( y >= 0 && x >= 0) 
00479     {
00480         this->Move(x, y);
00481     } 
00482     else 
00483     {
00484         this->Move(0, 44);
00485     };
00486     bool b;
00487     config->Read(wxT("/SaveLensDialog/saveDistortion"),&b,true);
00488     m_saveDistortion=XRCCTRL(*this,"save_lens_distortion",wxCheckBox);
00489     m_saveDistortion->SetValue(b);
00490     config->Read(wxT("/SaveLensDialog/saveVignetting"),&b,true);
00491     m_saveVignetting=XRCCTRL(*this,"save_lens_vignetting",wxCheckBox);
00492     m_saveVignetting->SetValue(b);
00493 };
00494 
00495 void SaveLensDBDialog::SetCameraMaker(std::string maker)
00496 {
00497     if(!maker.empty())
00498     {
00499         XRCCTRL(*this,"save_lens_camera_maker",wxTextCtrl)->SetValue(wxString(maker.c_str(), wxConvLocal));
00500     };
00501 };
00502 
00503 std::string SaveLensDBDialog::GetCameraMaker() const
00504 {
00505     return std::string(XRCCTRL(*this,"save_lens_camera_maker",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00506 };
00507 
00508 void SaveLensDBDialog::SetCameraModel(std::string model)
00509 {
00510     if(!model.empty())
00511     {
00512         XRCCTRL(*this,"save_lens_camera_model",wxTextCtrl)->SetValue(wxString(model.c_str(), wxConvLocal));
00513     };
00514 };
00515 
00516 std::string SaveLensDBDialog::GetCameraModel() const
00517 {
00518     return std::string(XRCCTRL(*this,"save_lens_camera_model",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00519 };
00520 
00521 void SaveLensDBDialog::SetLensName(std::string lensname)
00522 {
00523     if(!lensname.empty())
00524     {
00525         XRCCTRL(*this,"save_lens_name",wxTextCtrl)->SetValue(wxString(lensname.c_str(), wxConvLocal));
00526     };
00527 };
00528 
00529 std::string SaveLensDBDialog::GetLensName() const
00530 {
00531     return std::string(XRCCTRL(*this,"save_lens_name",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00532 };
00533 
00534 std::string SaveLensDBDialog::GetLensMaker() const
00535 {
00536     return std::string(XRCCTRL(*this,"save_lens_maker",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00537 };
00538 
00539 void SaveLensDBDialog::SetLensMount(std::string mount)
00540 {
00541     if(!mount.empty())
00542     {
00543         XRCCTRL(*this,"save_lens_mount",wxTextCtrl)->SetValue(wxString(mount.c_str(), wxConvLocal));
00544     };
00545 };
00546 
00547 std::string SaveLensDBDialog::GetLensMount() const
00548 {
00549     return std::string(XRCCTRL(*this,"save_lens_mount",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00550 };
00551 
00552 void SaveLensDBDialog::SetFocalLength(double focal)
00553 {
00554     m_focal=focal;
00555     XRCCTRL(*this,"save_lens_focallength",wxTextCtrl)->SetValue(hugin_utils::doubleTowxString(m_focal,1));
00556 };
00557 
00558 double SaveLensDBDialog::GetFocalLength() const
00559 {
00560     return m_focal;
00561 };
00562 
00563 void SaveLensDBDialog::SetAperture(double aperture)
00564 {
00565     m_aperture=aperture;
00566     XRCCTRL(*this,"save_lens_aperture",wxTextCtrl)->SetValue(hugin_utils::doubleTowxString(m_aperture,1));
00567 };
00568 
00569 double SaveLensDBDialog::GetAperture() const
00570 {
00571     return m_aperture;
00572 };
00573 
00574 void SaveLensDBDialog::SetSubjectDistance(double distance)
00575 {
00576     m_distance=distance;
00577     XRCCTRL(*this,"save_lens_distance",wxTextCtrl)->SetValue(hugin_utils::doubleTowxString(m_distance,0));
00578 };
00579 
00580 double SaveLensDBDialog::GetSubjectDistance() const
00581 {
00582     return m_distance;
00583 };
00584 
00585 bool SaveLensDBDialog::GetSaveDistortion() const
00586 {
00587     return m_saveDistortion->GetValue();
00588 };
00589 
00590 bool SaveLensDBDialog::GetSaveVignetting() const
00591 {
00592     return m_saveVignetting->GetValue();
00593 };
00594 
00595 void SaveLensDBDialog::DeactivateSaveVignetting()
00596 {
00597     m_saveVignetting->SetValue(false);
00598     m_saveVignetting->Disable();
00599 };
00600 
00601 void SaveLensDBDialog::OnOk(wxCommandEvent & e)
00602 {
00603     if(!m_saveDistortion->GetValue() && !m_saveVignetting->GetValue())
00604     {
00605         return;
00606     };
00607     if(GetLensName().empty() && GetCameraMaker().empty() && GetCameraModel().empty())
00608     {
00609         wxMessageBox(_("There is too little information for saving data into database. Please check your input!"),_("Warning"),wxOK|wxICON_ERROR,this);
00610         return;
00611     };
00612     if(!str2double(this,XRCCTRL(*this,"save_lens_focallength",wxTextCtrl)->GetValue(),m_focal))
00613     {
00614         return;
00615     };
00616     if(m_saveVignetting->GetValue())
00617     {
00618         if(!str2double(this,XRCCTRL(*this,"save_lens_aperture",wxTextCtrl)->GetValue(),m_aperture))
00619         {
00620             return;
00621         };
00622         if(!str2double(this,XRCCTRL(*this,"save_lens_distance",wxTextCtrl)->GetValue(),m_distance))
00623         {
00624             return;
00625         };
00626     };
00627     //store selected options
00628     wxConfigBase * config = wxConfigBase::Get();
00629     wxSize sz = this->GetClientSize();
00630     config->Write(wxT("/SaveLensDialog/width"), sz.GetWidth());
00631     config->Write(wxT("/SaveLensDialog/height"), sz.GetHeight());
00632     wxPoint ps = this->GetPosition();
00633     config->Write(wxT("/SaveLensDialog/positionX"), ps.x);
00634     config->Write(wxT("/SaveLensDialog/positionY"), ps.y);
00635     config->Write(wxT("/SaveLensDialog/saveDistortion"),m_saveDistortion->GetValue());
00636     if(m_saveVignetting->IsEnabled())
00637     {
00638         config->Write(wxT("/SaveLensDialog/saveVignetting"),m_saveVignetting->GetValue());
00639     };
00640     config->Flush(); 
00641     e.Skip();
00642 };
00643 
00644 void SaveLensDBDialog::OnCheckChanged(wxCommandEvent & e)
00645 {
00646     XRCCTRL(*this,"wxID_OK",wxButton)->Enable(m_saveDistortion->GetValue() || m_saveVignetting->GetValue());
00647 };
00648 
00649 bool ShowFileDialogWithWarning(wxFileDialog &dlg,wxString userDBPath)
00650 {
00651     if (dlg.ShowModal()==wxID_OK)
00652     {
00653         wxFileName filename(dlg.GetPath());
00654         if(!filename.HasExt())
00655         {
00656             filename.SetExt(wxT("xml"));
00657         };
00658         if(filename.GetPath()!=userDBPath)
00659         {
00660             if(wxMessageBox(wxString::Format(_("You selected the folder \"%s\" to save your database file.\nThis is not the default folder. You won't be able to automatically load this information back into Hugin.\nThe default folder for the database files is \"%s\".\nDo you want to proceed anyway?"),filename.GetPath().c_str(),userDBPath.c_str()),
00661                 _("Warning"),wxYES_NO,dlg.GetParent())==wxNO)
00662             {
00663                 return ShowFileDialogWithWarning(dlg,userDBPath);
00664             };
00665         };
00666         return true;
00667     }
00668     else
00669     {
00670         return false;
00671     };
00672 };
00673 
00674 //build mount name from camera maker and camera model
00675 std::string BuildMountName(std::string maker, std::string model)
00676 {
00677     std::string result;
00678     for(size_t i=0;i<maker.length();i++)
00679     {
00680         if(isalpha(maker[i]))
00681         {
00682             result+=tolower(maker[i]);
00683         }
00684         else
00685         {
00686             break;
00687         };
00688     };
00689     for(size_t i=0;i<model.length();i++)
00690     {
00691         if(isalnum(model[i]))
00692         {
00693             result+=model[i];
00694         };
00695     };
00696     return result;
00697 };
00698 
00699 bool SaveLensParameters(wxWindow * parent, const HuginBase::SrcPanoImage& img, bool includeVignetting)
00700 {
00701     HuginBase::LensDB::LensDB& lensDB=HuginBase::LensDB::LensDB::GetSingleton();
00702     wxString userDBPath=wxString(lensDB.GetUserDBPath().c_str(), wxConvLocal);
00703     if(!wxFileName::DirExists(userDBPath))
00704     {
00705         wxFileName::Mkdir(userDBPath, 511, wxPATH_MKDIR_FULL);
00706     };
00707     wxFileDialog dlg(parent,
00708                         _("Save lens into database file"),
00709                         userDBPath, wxT(""), 
00710                         _("Lensfun database files (*.xml)|*.xml"),
00711                         wxFD_SAVE, wxDefaultPosition);
00712     if(ShowFileDialogWithWarning(dlg,userDBPath))
00713     {
00714         SaveLensDBDialog lensDlg(parent);
00715         lensDlg.SetCameraMaker(img.getExifMake());
00716         lensDlg.SetCameraModel(img.getExifModel());
00717         lensDlg.SetLensName(img.getExifLens());
00718         lensDlg.SetFocalLength(img.getExifFocalLength());
00719         lensDlg.SetAperture(img.getExifAperture());
00720         lensDlg.SetSubjectDistance(img.getExifDistance());
00721         std::string mount;
00722         if(lensDB.GetCameraMount(img.getExifMake(), img.getExifModel(), mount))
00723         {
00724             lensDlg.SetLensMount(mount);
00725         };
00726         if(!includeVignetting)
00727         {
00728             lensDlg.DeactivateSaveVignetting();
00729         };
00730         if(lensDlg.ShowModal()==wxID_OK)
00731         {
00732             std::string filename=std::string(dlg.GetPath().mb_str(HUGIN_CONV_FILENAME));
00733             std::string lensname=lensDlg.GetLensName();
00734             if(lensname.empty())
00735             {
00736                 //empty lensname, assuming it is a compact camera
00737                 lensname="Standard";
00738             };
00739             mount=lensDlg.GetLensMount();
00740             if(!lensDlg.GetCameraMaker().empty() && !lensDlg.GetCameraModel().empty())
00741             {
00742                 if(mount.empty())
00743                 {
00744                     //checking, if camera is already in database
00745                     if(!lensDB.GetCameraMount(lensDlg.GetCameraMaker(), lensDlg.GetCameraModel(), mount))
00746                     {
00747                         //unknown camera, build mount name and save into database
00748                         mount=BuildMountName(lensDlg.GetCameraMaker(), lensDlg.GetCameraModel());
00749                         if(!lensDB.SaveCameraCrop(filename, lensDlg.GetCameraMaker(), lensDlg.GetCameraModel(), mount, img.getExifCropFactor()))
00750                         {
00751                             wxMessageBox(_("Could not save information into database file."),_("Error"),wxOK|wxICON_ERROR,parent);
00752                             return false;
00753                         };
00754                     };
00755                 }
00756                 else
00757                 {
00758                     //mount given, check if camera is already in database
00759                     std::string s;
00760                     if(!lensDB.GetCameraMount(lensDlg.GetCameraMaker(), lensDlg.GetCameraModel(), s))
00761                     {
00762                         if(!lensDB.SaveCameraCrop(filename, lensDlg.GetCameraMaker(), lensDlg.GetCameraModel(), mount, img.getExifCropFactor()))
00763                         {
00764                             wxMessageBox(_("Could not save information into database file."),_("Error"),wxOK|wxICON_ERROR,parent);
00765                             return false;
00766                         };
00767                     };
00768                 };
00769             };
00770 
00771             int e=lensDB.BeginSaveLens(filename, lensDlg.GetLensMaker(), lensname, mount, img.getProjection(), img.getExifCropFactor());
00772             if(e==0)
00773             {
00774                 double focal=lensDlg.GetFocalLength();
00775                 if(img.getCropMode()!=HuginBase::SrcPanoImage::NO_CROP)
00776                 {
00777                     vigra::Rect2D cropRect=img.getCropRect();
00778                     hugin_utils::FDiff2D cropLeftTop;
00779                     hugin_utils::FDiff2D cropRightBottom;
00780                     int width=img.getSize().width();
00781                     int height=img.getSize().height();
00782                     if(width>height)
00783                     {
00784                         cropLeftTop=hugin_utils::FDiff2D((double)cropRect.left()/width,(double)cropRect.top()/height);
00785                         cropRightBottom=hugin_utils::FDiff2D((double)cropRect.right()/width,(double)cropRect.bottom()/height);
00786                     }
00787                     else
00788                     {
00789                         cropLeftTop=hugin_utils::FDiff2D((double)cropRect.top()/height,1.0-(double)cropRect.right()/width);
00790                         cropRightBottom=hugin_utils::FDiff2D((double)cropRect.bottom()/height,1.0-(double)cropRect.left()/width);
00791                     };
00792                     lensDB.SaveCrop(focal,img.getCropMode(),cropLeftTop,cropRightBottom);
00793                 };
00794                 if(lensDlg.GetSaveDistortion())
00795                 {
00796                     lensDB.SaveHFOV(focal,img.getHFOV());
00797                     lensDB.SaveDistortion(focal,img.getRadialDistortion());
00798                 };
00799                 if(includeVignetting && lensDlg.GetSaveVignetting())
00800                 {
00801                     lensDB.SaveVignetting(focal,lensDlg.GetAperture(),lensDlg.GetSubjectDistance(),img.getRadialVigCorrCoeff());
00802                 };
00803                 if(lensDB.EndSaveLens())
00804                 {
00805                     lensDB.ReloadUserPart();
00806                     return true;
00807                 }
00808                 else
00809                 {
00810                     wxMessageBox(_("Could not save information into database file."),_("Error"),wxOK|wxICON_ERROR,parent);
00811                 };
00812             }
00813             else
00814             {
00815                 if(e==1)
00816                 {
00817                     wxMessageBox(wxString::Format(_("Could not initialize database."),_("Error"),wxOK|wxICON_ERROR,parent));
00818                 }
00819                 else
00820                 {
00821                     wxMessageBox(wxString::Format(_("The current selected lens does not match with the information about this lens in the selected database file.\nCould not proceed.\n(Error code: %d)"),e),
00822                         _("Error"),wxOK|wxICON_ERROR,parent);
00823                 };
00824                 return false;
00825             };
00826         };
00827     }
00828     return false;
00829 };
00830 
00832 class SaveCamDBDialog : public wxDialog
00833 {
00834 public:
00836     SaveCamDBDialog(wxWindow *parent);
00837     void SetCameraMaker(std::string maker);
00838     std::string GetCameraMaker() const;
00839     void SetCameraModel(std::string model);
00840     std::string GetCameraModel() const;
00841     void SetCameraMount(std::string mount);
00842     std::string GetCameraMount() const;
00843 
00844 protected:
00846     void OnOk(wxCommandEvent & e);
00847 
00848 private:
00849     DECLARE_EVENT_TABLE()
00850 };
00851 
00852 BEGIN_EVENT_TABLE(SaveCamDBDialog,wxDialog)
00853     EVT_BUTTON(wxID_OK, SaveCamDBDialog::OnOk)
00854 END_EVENT_TABLE()
00855 
00856 SaveCamDBDialog::SaveCamDBDialog(wxWindow *parent)
00857 {
00858     // load our children. some children might need special
00859     // initialization. this will be done later.
00860     wxXmlResource::Get()->LoadDialog(this, parent, wxT("save_cam_dlg"));
00861 
00862     //set parameters
00863     wxConfigBase * config = wxConfigBase::Get();
00864     //position
00865     int x = config->Read(wxT("/SaveCamDialog/positionX"),-1l);
00866     int y = config->Read(wxT("/SaveCamDialog/positionY"),-1l);
00867     if ( y >= 0 && x >= 0) 
00868     {
00869         this->Move(x, y);
00870     } 
00871     else 
00872     {
00873         this->Move(0, 44);
00874     };
00875 };
00876 
00877 void SaveCamDBDialog::SetCameraMaker(std::string maker)
00878 {
00879     if(!maker.empty())
00880     {
00881         XRCCTRL(*this,"save_cam_maker",wxTextCtrl)->SetValue(wxString(maker.c_str(), wxConvLocal));
00882     };
00883 };
00884 
00885 std::string SaveCamDBDialog::GetCameraMaker() const
00886 {
00887     return std::string(XRCCTRL(*this,"save_cam_maker",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00888 };
00889 
00890 void SaveCamDBDialog::SetCameraModel(std::string model)
00891 {
00892     if(!model.empty())
00893     {
00894         XRCCTRL(*this,"save_cam_model",wxTextCtrl)->SetValue(wxString(model.c_str(), wxConvLocal));
00895     };
00896 };
00897 
00898 std::string SaveCamDBDialog::GetCameraModel() const
00899 {
00900     return std::string(XRCCTRL(*this,"save_cam_model",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00901 };
00902 
00903 void SaveCamDBDialog::SetCameraMount(std::string mount)
00904 {
00905     if(!mount.empty())
00906     {
00907         XRCCTRL(*this,"save_cam_mount",wxTextCtrl)->SetValue(wxString(mount.c_str(), wxConvLocal));
00908     };
00909 };
00910 
00911 std::string SaveCamDBDialog::GetCameraMount() const
00912 {
00913     return std::string(XRCCTRL(*this,"save_cam_mount",wxTextCtrl)->GetValue().Trim().mb_str(wxConvLocal));
00914 };
00915 
00916 void SaveCamDBDialog::OnOk(wxCommandEvent & e)
00917 {
00918     if(GetCameraMaker().empty())
00919     {
00920         wxMessageBox(_("The maker field contains only an empty string."),_("Warning"),wxOK|wxICON_ERROR,this);
00921         return;
00922     };
00923     if(GetCameraModel().empty())
00924     {
00925         wxMessageBox(_("The model field contains only an empty string."),_("Warning"),wxOK|wxICON_ERROR,this);
00926         return;
00927     };
00928     //store selected options
00929     wxConfigBase * config = wxConfigBase::Get();
00930     wxPoint ps = this->GetPosition();
00931     config->Write(wxT("/SaveCamDialog/positionX"), ps.x);
00932     config->Write(wxT("/SaveCamDialog/positionY"), ps.y);
00933     config->Flush(); 
00934     e.Skip();
00935 };
00936 
00937 bool SaveCameraCropFactor(wxWindow * parent, const HuginBase::SrcPanoImage& img)
00938 {
00939     HuginBase::LensDB::LensDB& lensDB=HuginBase::LensDB::LensDB::GetSingleton();
00940     wxString userDBPath=wxString(lensDB.GetUserDBPath().c_str(), wxConvLocal);
00941     if(!wxFileName::DirExists(userDBPath))
00942     {
00943         wxFileName::Mkdir(userDBPath, 511, wxPATH_MKDIR_FULL);
00944     };
00945     wxFileDialog dlg(parent,
00946                         _("Save camera into database file"),
00947                         userDBPath, wxT(""), 
00948                         _("Lensfun database files (*.xml)|*.xml"),
00949                         wxFD_SAVE, wxDefaultPosition);
00950     if(ShowFileDialogWithWarning(dlg,userDBPath))
00951     {
00952         SaveCamDBDialog camDlg(parent);
00953         camDlg.SetCameraMaker(img.getExifMake());
00954         camDlg.SetCameraModel(img.getExifModel());
00955         std::string mount;
00956         if(lensDB.GetCameraMount(img.getExifMake(), img.getExifModel(), mount))
00957         {
00958             camDlg.SetCameraMount(mount);
00959         };
00960         if(camDlg.ShowModal()==wxID_OK)
00961         {
00962             if(lensDB.SaveCameraCrop(std::string(dlg.GetPath().mb_str(HUGIN_CONV_FILENAME)),
00963                 camDlg.GetCameraMaker(),camDlg.GetCameraModel(),camDlg.GetCameraMount(),img.getExifCropFactor()))
00964             {
00965                 return true;
00966             }
00967             else
00968             {
00969                 wxMessageBox(_("Could not save information into database file."),_("Error"),wxOK|wxICON_ERROR,parent);
00970             };
00971         };
00972     };
00973     return false;
00974 };
00975 

Generated on Tue Sep 16 01:25:29 2014 for Hugintrunk by  doxygen 1.3.9.1