handle coarse rotation in lensfun distortion correction

This commit is contained in:
Alberto Griggio 2017-09-07 09:57:37 +02:00
parent 18f3bd6f45
commit 940577ce45
4 changed files with 66 additions and 43 deletions

View File

@ -322,26 +322,7 @@ void ImProcFunctions::transform (Imagefloat* original, Imagefloat* transformed,
LensCorrection *pLCPMap = nullptr;
if (needsLensfun()) {
const LFDatabase *db = LFDatabase::getInstance();
Glib::ustring make, model, lens;
if (params->lensProf.lfAutoMatch) {
make = metadata->getMake();
model = metadata->getModel();
lens = metadata->getLens();
} else {
make = params->lensProf.lfCameraMake;
model = params->lensProf.lfCameraModel;
lens = params->lensProf.lfLens;
}
LFCamera c = db->findCamera(make, model);
LFLens l = db->findLens(c, lens);
pLCPMap = db->getModifier(c, l, fW, fH, focalLen, fNumber, focusDist);
std::cout << "LENSFUN:\n"
<< " camera: " << c.getDisplayString() << "\n"
<< " lens: " << l.getDisplayString() << "\n"
<< " correction? " << (pLCPMap ? "yes" : "no") << std::endl;
pLCPMap = LFDatabase::findModifier(params->lensProf, metadata, fW, fH, params->coarse, rawRotationDeg);
} else if (needsLCP()) { // don't check focal length to allow distortion correction for lenses without chip
LCPProfile *pLCPProf = lcpStore->getProfile (params->lensProf.lcpFile);

View File

@ -1858,20 +1858,7 @@ void RawImageSource::preprocess (const RAWParams &raw, const LensProfParams &le
if (!hasFlatField && lensProf.useVign) {
std::unique_ptr<LensCorrection> pmap;
if (lensProf.useLensfun) {
const LFDatabase *db = LFDatabase::getInstance();
Glib::ustring make, model, lens;
if (lensProf.lfAutoMatch) {
make = idata->getMake();
model = idata->getModel();
lens = idata->getLens();
} else {
make = lensProf.lfCameraMake;
model = lensProf.lfCameraModel;
lens = lensProf.lfLens;
}
LFCamera c = db->findCamera(make, model);
LFLens l = db->findLens(c, lens);
pmap.reset(db->getModifier(c, l, W, H, idata->getFocalLen(), idata->getFNumber(), idata->getFocusDist()));
pmap.reset(LFDatabase::findModifier(lensProf, idata, W, H, coarse, -1));
} else {
LCPProfile *pLCPProf = lcpStore->getProfile(lensProf.lcpFile);

View File

@ -19,15 +19,20 @@
*/
#include "rtlensfun.h"
#include "settings.h"
#include <iostream>
namespace rtengine {
extern const Settings *settings;
//-----------------------------------------------------------------------------
// LFModifier
//-----------------------------------------------------------------------------
LFModifier::LFModifier(lfModifier *m):
data_(m)
LFModifier::LFModifier(lfModifier *m, bool swap_xy):
data_(m),
swap_xy_(swap_xy)
{
}
@ -52,9 +57,17 @@ void LFModifier::correctDistortion(double &x, double &y, int cx, int cy, double
}
float pos[2];
if (data_->ApplyGeometryDistortion(x+cx, y+cy, 1, 1, pos)) {
float xx = x + cx;
float yy = y + cy;
if (swap_xy_) {
std::swap(xx, yy);
}
if (data_->ApplyGeometryDistortion(xx, yy, 1, 1, pos)) {
x = pos[0] - cx;
y = pos[1] - cy;
if (swap_xy_) {
std::swap(x, y);
}
}
}
@ -257,19 +270,57 @@ LFLens LFDatabase::findLens(const LFCamera &camera, const Glib::ustring &name) c
LFModifier *LFDatabase::getModifier(const LFCamera &camera, const LFLens &lens,
int width, int height, float focalLen,
float aperture, float focusDist) const
float focalLen, float aperture, float focusDist,
int width, int height, bool swap_xy) const
{
LFModifier *ret = nullptr;
if (data_) {
if (camera.ok() && lens.ok()) {
lfModifier *mod = lfModifier::Create(lens.data_, camera.getCropFactor(), width, height);
mod->Initialize(lens.data_, LF_PF_F32, focalLen, aperture, focusDist > 0 ? focusDist : 1000, 0.0, LF_RECTILINEAR, LF_MODIFY_VIGNETTING | LF_MODIFY_DISTORTION, false);
ret = new LFModifier(mod);
ret = new LFModifier(mod, swap_xy);
}
}
return ret;
}
LFModifier *LFDatabase::findModifier(const LensProfParams &lensProf, const ImageMetaData *idata, int width, int height, const CoarseTransformParams &coarse, int rawRotationDeg)
{
const LFDatabase *db = getInstance();
Glib::ustring make, model, lens;
if (lensProf.lfAutoMatch) {
make = idata->getMake();
model = idata->getModel();
lens = idata->getLens();
} else {
make = lensProf.lfCameraMake;
model = lensProf.lfCameraModel;
lens = lensProf.lfLens;
}
LFCamera c = db->findCamera(make, model);
LFLens l = db->findLens(c, lens);
bool swap_xy = false;
if (rawRotationDeg >= 0) {
int rot = (coarse.rotate + rawRotationDeg) % 360;
swap_xy = (rot == 90 || rot == 270);
if (swap_xy) {
std::swap(width, height);
}
}
LFModifier *ret = db->getModifier(c, l, idata->getFocalLen(), idata->getFNumber(), idata->getFocusDist(), width, height, swap_xy);
if (settings->verbose) {
std::cout << "LENSFUN:\n"
<< " camera: " << c.getDisplayString() << "\n"
<< " lens: " << l.getDisplayString() << "\n"
<< " correction? " << (ret ? "yes" : "no") << std::endl;
}
return ret;
}
} // namespace rtengine

View File

@ -24,6 +24,7 @@
#include <glibmm.h>
#include <memory>
#include "lcp.h"
#include "procparams.h"
namespace rtengine {
@ -40,12 +41,13 @@ public:
void processVignetteLine3Channels(int width, int y, float *line) const;
private:
explicit LFModifier(lfModifier *m);
explicit LFModifier(lfModifier *m, bool rotateXY);
LFModifier(const LFModifier &);
LFModifier &operator=(const LFModifier &);
friend class LFDatabase;
lfModifier *data_;
bool swap_xy_;
};
class LFCamera {
@ -87,8 +89,10 @@ public:
LFCamera findCamera(const Glib::ustring &make, const Glib::ustring &model) const;
LFLens findLens(const LFCamera &camera, const Glib::ustring &name) const;
LFModifier *getModifier(const LFCamera &camera, const LFLens &lens,
int width, int height,
float focalLen, float aperture, float focusDist) const;
float focalLen, float aperture, float focusDist,
int width, int height, bool swap_xy) const;
static LFModifier *findModifier(const LensProfParams &lensProf, const ImageMetaData *idata, int width, int height, const CoarseTransformParams &coarse, int rawRotationDeg);
private:
LFDatabase();