Improve Lensfun auto lens matching

Handle lens names with spaces surrounding dashes.
This commit is contained in:
Lawrence Lee 2024-03-31 17:43:04 -07:00
parent 11240bc97d
commit 4e57202bf6
No known key found for this signature in database
GPG Key ID: 048FF2B76A63895F

View File

@ -19,6 +19,7 @@
*/ */
#include <iostream> #include <iostream>
#include <regex>
#include "imagedata.h" #include "imagedata.h"
#include "procparams.h" #include "procparams.h"
@ -475,23 +476,36 @@ LFLens LFDatabase::findLens(const LFCamera &camera, const Glib::ustring &name) c
} }
} }
} }
auto found = data_->FindLenses(camera.data_, nullptr, name.c_str()); const auto find_lens_from_name = [](const lfDatabase *database, const lfCamera *cam, const Glib::ustring &lens_name) {
for (size_t pos = 0; !found && pos < name.size(); ) { auto found = database->FindLenses(cam, nullptr, lens_name.c_str());
// try to split the maker from the model of the lens -- we have to for (size_t pos = 0; !found && pos < lens_name.size(); ) {
// guess a bit here, since there are makers with a multi-word name // try to split the maker from the model of the lens -- we have to
// (e.g. "Leica Camera AG") // guess a bit here, since there are makers with a multi-word name
if (name.find("f/", pos) == 0) { // (e.g. "Leica Camera AG")
break; // no need to search further if (lens_name.find("f/", pos) == 0) {
break; // no need to search further
}
Glib::ustring make, model;
auto i = lens_name.find(' ', pos);
if (i != Glib::ustring::npos) {
make = lens_name.substr(0, i);
model = lens_name.substr(i+1);
found = database->FindLenses(cam, make.c_str(), model.c_str());
pos = i+1;
} else {
break;
}
} }
Glib::ustring make, model; return found;
auto i = name.find(' ', pos); };
if (i != Glib::ustring::npos) { auto found = find_lens_from_name(data_, camera.data_, name);
make = name.substr(0, i); if (!found) {
model = name.substr(i+1); // Some names have white-space around the dash(s) while Lensfun does
found = data_->FindLenses(camera.data_, make.c_str(), model.c_str()); // not have any.
pos = i+1; const std::regex pattern("\\s*-\\s*");
} else { const auto formatted_name = std::regex_replace(name.raw(), pattern, "-");
break; if (name != formatted_name) {
found = find_lens_from_name(data_, camera.data_, formatted_name);
} }
} }
if (!found && camera && camera.isFixedLens()) { if (!found && camera && camera.isFixedLens()) {