From 363f3cc2a65fa5c3d96ed519247eb073b9f9cd20 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Sat, 15 Jun 2024 09:04:22 +0200 Subject: [PATCH 01/12] iptransform: correct profile based distortion/CA before rotation Like perspective correction also rotation correction should be applied after distortion/CA correction. --- rtengine/iptransform.cc | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index 3d384ee62..d62a87199 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -515,14 +515,14 @@ bool ImProcFunctions::transCoord (int W, int H, const std::vector &src, break; } - if (pLCPMap && params->lensProf.useDist) { - pLCPMap->correctDistortion(x_d, y_d, w2, h2); - } - // rotate double Dx = x_d * cost - y_d * sint; double Dy = x_d * sint + y_d * cost; + if (pLCPMap && params->lensProf.useDist) { + pLCPMap->correctDistortion(Dx, Dy, w2, h2); + } + // distortion correction double s = 1; @@ -700,7 +700,7 @@ void ImProcFunctions::transform (Imagefloat* original, Imagefloat* transformed, } } transformGeneral(highQuality, original, dest, cx, cy, sx, sy, oW, oH, fW, fH, pLCPMap.get(), useOriginalBuffer); - + if (highQuality && dest != transformed) { transformLCPCAOnly(dest, transformed, cx, cy, pLCPMap.get(), useOriginalBuffer); } @@ -1240,25 +1240,25 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I break; } - if (enableLCPDist) { - pLCPMap->correctDistortion(x_d, y_d, w2, h2); - } - // rotate - const double Dxc = x_d * cost - y_d * sint; - const double Dyc = x_d * sint + y_d * cost; + double Dxr = x_d * cost - y_d * sint; + double Dyr = x_d * sint + y_d * cost; + + if (enableLCPDist) { + pLCPMap->correctDistortion(Dxr, Dyr, w2, h2); + } // distortion correction double s = 1.0; if (enableDistortion) { - const double r = sqrt(Dxc * Dxc + Dyc * Dyc) / maxRadius; + const double r = sqrt(Dxr * Dxr + Dyr * Dyr) / maxRadius; s = 1.0 - distAmount + distAmount * r; } for (int c = 0; c < (enableCA ? 3 : 1); ++c) { - double Dx = Dxc * (s + chDist[c]); - double Dy = Dyc * (s + chDist[c]); + double Dx = Dxr * (s + chDist[c]); + double Dy = Dyr * (s + chDist[c]); // de-center Dx += w2; @@ -1386,7 +1386,7 @@ void ImProcFunctions::transformLCPCAOnly(Imagefloat *original, Imagefloat *trans for (int c = 0; c < 3; c++) { double Dx = x; double Dy = y; - + pLCPMap->correctCA(Dx, Dy, cx, cy, c); // Extract integer and fractions of coordinates @@ -1522,4 +1522,3 @@ bool ImProcFunctions::needsTransform (int oW, int oH, int rawRotationDeg, const } - From 55fd4b975e48141e73e068c8332a46caba5755d1 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 09:34:14 +0200 Subject: [PATCH 02/12] iptransform: move profile based CA with and after distortion Doing profile based CA as the really first correction in a separate steps is probably not useful. Additionally many lens profile (lensfun) and future one provide functions to do them in a single step with better precision while other just provide a single step that does both (i.e. DNG WarpRectilinear). For the above reasons this patch removes the additional pass for CA correction. This will also improve the perfomance due to less work. --- rtengine/iptransform.cc | 136 +++++++++------------------------------- 1 file changed, 29 insertions(+), 107 deletions(-) diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index d62a87199..757d3d54c 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -420,7 +420,6 @@ homogeneous::Matrix perspectiveMatrix(double camera_focal_length, double bool ImProcFunctions::transCoord (int W, int H, const std::vector &src, std::vector &red, std::vector &green, std::vector &blue, double ascaleDef, const LensCorrection *pLCPMap) const { - enum PerspType { NONE, SIMPLE, CAMERA_BASED }; const PerspType perspectiveType = needsPerspective() ? ( (params->perspective.method == "camera_based") ? @@ -686,24 +685,8 @@ void ImProcFunctions::transform (Imagefloat* original, Imagefloat* transformed, highQuality = false; } else { highQuality = true; - // agriggio: CA correction via the lens profile has to be - // performed before separately from the the other transformations - // (except for the coarse rotation/flipping). In order to not - // change the code too much, I simply introduced a new mode - // TRANSFORM_HIGH_QUALITY_CA, which applies *only* profile-based - // CA correction. So, the correction in this case occurs in two - // steps, using an intermediate temporary image. There's room for - // optimization of course... - if (pLCPMap && params->lensProf.useCA && pLCPMap->isCACorrectionAvailable()) { - tmpimg.reset(new Imagefloat(transformed->getWidth(), transformed->getHeight())); - dest = tmpimg.get(); - } } transformGeneral(highQuality, original, dest, cx, cy, sx, sy, oW, oH, fW, fH, pLCPMap.get(), useOriginalBuffer); - - if (highQuality && dest != transformed) { - transformLCPCAOnly(dest, transformed, cx, cy, pLCPMap.get(), useOriginalBuffer); - } } } @@ -1095,7 +1078,9 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I // set up stuff, depending on the mode we are enum PerspType { NONE, SIMPLE, CAMERA_BASED }; const bool enableLCPDist = pLCPMap && params->lensProf.useDist; + const bool enableLCPCA = pLCPMap && params->lensProf.useCA && pLCPMap->isCACorrectionAvailable(); const bool enableCA = highQuality && needsCA(); + const bool doCACorrection = enableCA || enableLCPCA; const bool enableGradient = needsGradient(); const bool enablePCVignetting = needsPCVignetting(); const bool enableVignetting = needsVignetting(); @@ -1241,24 +1226,32 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I } // rotate - double Dxr = x_d * cost - y_d * sint; - double Dyr = x_d * sint + y_d * cost; + const double Dxr = x_d * cost - y_d * sint; + const double Dyr = x_d * sint + y_d * cost; - if (enableLCPDist) { - pLCPMap->correctDistortion(Dxr, Dyr, w2, h2); - } + for (int c = 0; c < (doCACorrection ? 3 : 1); ++c) { + double Dx = Dxr; + double Dy = Dyr; - // distortion correction - double s = 1.0; + if (enableLCPCA) { + pLCPMap->correctCA(Dx, Dy, w2, h2, c); + } - if (enableDistortion) { - const double r = sqrt(Dxr * Dxr + Dyr * Dyr) / maxRadius; - s = 1.0 - distAmount + distAmount * r; - } + if (enableLCPDist) { + pLCPMap->correctDistortion(Dx, Dy, w2, h2); + } - for (int c = 0; c < (enableCA ? 3 : 1); ++c) { - double Dx = Dxr * (s + chDist[c]); - double Dy = Dyr * (s + chDist[c]); + // distortion correction + double s = 1.0; + + if (enableDistortion) { + const double r = sqrt(Dx * Dx + Dy * Dy) / maxRadius; + s = 1.0 - distAmount + distAmount * r; + } + + // CA correction + Dx *= s + chDist[c]; + Dy *= s + chDist[c]; // de-center Dx += w2; @@ -1305,13 +1298,13 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I transformed->g(y, x) = vignmul * (original->g(yc, xc) * (1.0 - Dx) * (1.0 - Dy) + original->g(yc, xc + 1) * Dx * (1.0 - Dy) + original->g(yc + 1, xc) * (1.0 - Dx) * Dy + original->g(yc + 1, xc + 1) * Dx * Dy); transformed->b(y, x) = vignmul * (original->b(yc, xc) * (1.0 - Dx) * (1.0 - Dy) + original->b(yc, xc + 1) * Dx * (1.0 - Dy) + original->b(yc + 1, xc) * (1.0 - Dx) * Dy + original->b(yc + 1, xc + 1) * Dx * Dy); } else if (!useLog) { - if (enableCA) { + if (doCACorrection) { interpolateTransformChannelsCubic(chOrig[c], xc - 1, yc - 1, Dx, Dy, chTrans[c][y][x], vignmul); } else { interpolateTransformCubic(original, xc - 1, yc - 1, Dx, Dy, transformed->r(y, x), transformed->g(y, x), transformed->b(y, x), vignmul); } } else { - if (enableCA) { + if (doCACorrection) { interpolateTransformChannelsCubicLog(chOrig[c], xc - 1, yc - 1, Dx, Dy, chTrans[c][y][x], vignmul); } else { interpolateTransformCubicLog(original, xc - 1, yc - 1, Dx, Dy, transformed->r(y, x), transformed->g(y, x), transformed->b(y, x), vignmul); @@ -1325,7 +1318,7 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I const int x2 = LIM(xc + 1, 0, original->getWidth() - 1); if (useLog) { - if (enableCA) { + if (doCACorrection) { chTrans[c][y][x] = vignmul * xexpf(chOrig[c][y1][x1] * (1.0 - Dx) * (1.0 - Dy) + chOrig[c][y1][x2] * Dx * (1.0 - Dy) + chOrig[c][y2][x1] * (1.0 - Dx) * Dy + chOrig[c][y2][x2] * Dx * Dy); } else { transformed->r(y, x) = vignmul * xexpf(original->r(y1, x1) * (1.0 - Dx) * (1.0 - Dy) + original->r(y1, x2) * Dx * (1.0 - Dy) + original->r(y2, x1) * (1.0 - Dx) * Dy + original->r(y2, x2) * Dx * Dy); @@ -1333,7 +1326,7 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I transformed->b(y, x) = vignmul * xexpf(original->b(y1, x1) * (1.0 - Dx) * (1.0 - Dy) + original->b(y1, x2) * Dx * (1.0 - Dy) + original->b(y2, x1) * (1.0 - Dx) * Dy + original->b(y2, x2) * Dx * Dy); } } else { - if (enableCA) { + if (doCACorrection) { chTrans[c][y][x] = vignmul * (chOrig[c][y1][x1] * (1.0 - Dx) * (1.0 - Dy) + chOrig[c][y1][x2] * Dx * (1.0 - Dy) + chOrig[c][y2][x1] * (1.0 - Dx) * Dy + chOrig[c][y2][x2] * Dx * Dy); } else { transformed->r(y, x) = vignmul * (original->r(y1, x1) * (1.0 - Dx) * (1.0 - Dy) + original->r(y1, x2) * Dx * (1.0 - Dy) + original->r(y2, x1) * (1.0 - Dx) * Dy + original->r(y2, x2) * Dx * Dy); @@ -1343,7 +1336,7 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I } } } else { - if (enableCA) { + if (doCACorrection) { // not valid (source pixel x,y not inside source image, etc.) chTrans[c][y][x] = 0; } else { @@ -1357,77 +1350,6 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I } } - -void ImProcFunctions::transformLCPCAOnly(Imagefloat *original, Imagefloat *transformed, int cx, int cy, const LensCorrection *pLCPMap, bool useOriginalBuffer) -{ - assert(pLCPMap && params->lensProf.useCA && pLCPMap->isCACorrectionAvailable()); - const bool useLog = params->commonTrans.method == "log"; - - float** chTrans[3] = {transformed->r.ptrs, transformed->g.ptrs, transformed->b.ptrs}; - - std::unique_ptr tempLog; - if (useLog) { - if (!useOriginalBuffer) { - tempLog.reset(new Imagefloat(original->getWidth(), original->getHeight())); - logEncode(original, tempLog.get(), multiThread); - original = tempLog.get(); - } else { - logEncode(original, original, multiThread); - } - } - float** chOrig[3] = {original->r.ptrs, original->g.ptrs, original->b.ptrs}; - -#ifdef _OPENMP - #pragma omp parallel for if (multiThread) -#endif - - for (int y = 0; y < transformed->getHeight(); y++) { - for (int x = 0; x < transformed->getWidth(); x++) { - for (int c = 0; c < 3; c++) { - double Dx = x; - double Dy = y; - - pLCPMap->correctCA(Dx, Dy, cx, cy, c); - - // Extract integer and fractions of coordinates - int xc = (int)Dx; - Dx -= (double)xc; - int yc = (int)Dy; - Dy -= (double)yc; - - // Convert only valid pixels - if (yc >= 0 && yc < original->getHeight() && xc >= 0 && xc < original->getWidth()) { - - // multiplier for vignetting correction - if (yc > 0 && yc < original->getHeight() - 2 && xc > 0 && xc < original->getWidth() - 2) { - // all interpolation pixels inside image - if (!useLog) { - interpolateTransformChannelsCubic(chOrig[c], xc - 1, yc - 1, Dx, Dy, chTrans[c][y][x], 1.0); - } else { - interpolateTransformChannelsCubicLog(chOrig[c], xc - 1, yc - 1, Dx, Dy, chTrans[c][y][x], 1.0); - } - } else { - // edge pixels - int y1 = LIM (yc, 0, original->getHeight() - 1); - int y2 = LIM (yc + 1, 0, original->getHeight() - 1); - int x1 = LIM (xc, 0, original->getWidth() - 1); - int x2 = LIM (xc + 1, 0, original->getWidth() - 1); - if (!useLog) { - chTrans[c][y][x] = (chOrig[c][y1][x1] * (1.0 - Dx) * (1.0 - Dy) + chOrig[c][y1][x2] * Dx * (1.0 - Dy) + chOrig[c][y2][x1] * (1.0 - Dx) * Dy + chOrig[c][y2][x2] * Dx * Dy); - } else { - chTrans[c][y][x] = xexpf(chOrig[c][y1][x1] * (1.0 - Dx) * (1.0 - Dy) + chOrig[c][y1][x2] * Dx * (1.0 - Dy) + chOrig[c][y2][x1] * (1.0 - Dx) * Dy + chOrig[c][y2][x2] * Dx * Dy); - } - } - } else { - // not valid (source pixel x,y not inside source image, etc.) - chTrans[c][y][x] = 0; - } - } - } - } -} - - double ImProcFunctions::getTransformAutoFill (int oW, int oH, const LensCorrection *pLCPMap) const { if (!needsCA() && !needsDistortion() && !needsRotation() && !needsPerspective() && (!params->lensProf.useDist || pLCPMap == nullptr)) { From dc0e23c82c4ebedd901fb66593702ae3fb6df724 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:02:15 +0200 Subject: [PATCH 03/12] iptransform: apply distortion and CA correction in a single pass Some lens profile methods provides a way to correct distortion and CA in a single step. When available, applying distortion and CA correction when both enabled in a single pass is more precise (see lensfun ApplySubpixelGeometryDistortion doc) since it's usually how the profile is done. Instead applying the CA correction in a step after distortion correction could lead to a bit different (also if not always visible) correction. This is also required for future lens correction methods (like DNG WarpRectilinear or corrections based on vendor metadata) that provides only merged distortion and CA correction in a single pass. --- rtengine/iptransform.cc | 10 +++++----- rtengine/lcp.cc | 6 ++++++ rtengine/lcp.h | 3 +++ rtengine/rtlensfun.cc | 30 +++++++++++++++++++++++++++--- rtengine/rtlensfun.h | 1 + 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index 757d3d54c..08ee9a4a0 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -1233,12 +1233,12 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I double Dx = Dxr; double Dy = Dyr; - if (enableLCPCA) { - pLCPMap->correctCA(Dx, Dy, w2, h2, c); - } - - if (enableLCPDist) { + if (enableLCPDist && enableLCPCA) { + pLCPMap->correctDistortionAndCA(Dx, Dy, w2, h2, c); + } else if (enableLCPDist) { pLCPMap->correctDistortion(Dx, Dy, w2, h2); + } else if (enableLCPCA) { + pLCPMap->correctCA(Dx, Dy, w2, h2, c); } // distortion correction diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index 7507bbb11..ba96147dc 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -990,6 +990,12 @@ bool rtengine::LCPMapper::isCACorrectionAvailable() const return enableCA; } +void rtengine::LCPMapper::correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const +{ + correctDistortion(x, y, cx, cy); + correctCA(x, y, cx, cy, channel); +} + void rtengine::LCPMapper::correctDistortion(double &x, double &y, int cx, int cy) const { x += cx; diff --git a/rtengine/lcp.h b/rtengine/lcp.h index b59cc84c6..97cdd0890 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -166,6 +166,8 @@ private: class LensCorrection { public: virtual ~LensCorrection() {} + + virtual void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const = 0; virtual void correctDistortion(double &x, double &y, int cx, int cy) const = 0; virtual bool isCACorrectionAvailable() const = 0; virtual void correctCA(double &x, double &y, int cx, int cy, int channel) const = 0; @@ -194,6 +196,7 @@ public: ); + void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const override; void correctDistortion(double &x, double &y, int cx, int cy) const override; bool isCACorrectionAvailable() const override; void correctCA(double& x, double& y, int cx, int cy, int channel) const override; diff --git a/rtengine/rtlensfun.cc b/rtengine/rtlensfun.cc index 08010f50f..505346ad0 100644 --- a/rtengine/rtlensfun.cc +++ b/rtengine/rtlensfun.cc @@ -125,7 +125,6 @@ void LFModifier::correctDistortion(double &x, double &y, int cx, int cy) const } } - bool LFModifier::isCACorrectionAvailable() const { return (flags_ & LF_MODIFY_TCA); @@ -141,7 +140,7 @@ void LFModifier::correctCA(double &x, double &y, int cx, int cy, int channel) co // channels. We could consider caching the info to speed this up x += cx; y += cy; - + float pos[6]; if (swap_xy_) { std::swap(x, y); @@ -156,6 +155,31 @@ void LFModifier::correctCA(double &x, double &y, int cx, int cy, int channel) co y -= cy; } +void LFModifier::correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const +{ + assert(channel >= 0 && channel <= 2); + + // RT currently applies the CA correction per channel, whereas + // lensfun applies it to all the three channels simultaneously. This means + // we do the work 3 times, because each time we discard 2 of the 3 + // channels. We could consider caching the info to speed this up + x += cx; + y += cy; + + float pos[6]; + if (swap_xy_) { + std::swap(x, y); + } + data_->ApplySubpixelGeometryDistortion(x, y, 1, 1, pos); // This is thread-safe + x = pos[2*channel]; + y = pos[2*channel+1]; + if (swap_xy_) { + std::swap(x, y); + } + x -= cx; + y -= cy; +} + #ifdef _OPENMP void LFModifier::processVignette(int width, int height, float** rawData) const { @@ -396,7 +420,7 @@ bool LFDatabase::init(const Glib::ustring &dbdir) if (settings->verbose) { std::cout << (ok ? "OK" : "FAIL") << std::endl; } - + return ok; } diff --git a/rtengine/rtlensfun.h b/rtengine/rtlensfun.h index 1d941246f..39ca67ec2 100644 --- a/rtengine/rtlensfun.h +++ b/rtengine/rtlensfun.h @@ -53,6 +53,7 @@ public: explicit operator bool() const; + void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const override; void correctDistortion(double &x, double &y, int cx, int cy) const override; bool isCACorrectionAvailable() const override; void correctCA(double &x, double &y, int cx, int cy, int channel) const override; From b98fa4285720ccc1caeb475904af03c9649d4a79 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:03:07 +0200 Subject: [PATCH 04/12] lens profile: report availability of distortion and vignetting Beside CA, report also distortion and vignetting availability. Rename is${CORRECTION}Available methods to has${CORRECTION}. --- rtengine/iptransform.cc | 6 +++--- rtengine/lcp.cc | 14 +++++++++++++- rtengine/lcp.h | 10 ++++++++-- rtengine/rtlensfun.cc | 20 +++++++++++++++----- rtengine/rtlensfun.h | 5 ++++- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index 08ee9a4a0..468507385 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -518,7 +518,7 @@ bool ImProcFunctions::transCoord (int W, int H, const std::vector &src, double Dx = x_d * cost - y_d * sint; double Dy = x_d * sint + y_d * cost; - if (pLCPMap && params->lensProf.useDist) { + if (pLCPMap && params->lensProf.useDist && pLCPMap->hasDistortionCorrection()) { pLCPMap->correctDistortion(Dx, Dy, w2, h2); } @@ -1077,8 +1077,8 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I // set up stuff, depending on the mode we are enum PerspType { NONE, SIMPLE, CAMERA_BASED }; - const bool enableLCPDist = pLCPMap && params->lensProf.useDist; - const bool enableLCPCA = pLCPMap && params->lensProf.useCA && pLCPMap->isCACorrectionAvailable(); + const bool enableLCPDist = pLCPMap && params->lensProf.useDist && pLCPMap->hasDistortionCorrection(); + const bool enableLCPCA = pLCPMap && params->lensProf.useCA && pLCPMap->hasCACorrection(); const bool enableCA = highQuality && needsCA(); const bool doCACorrection = enableCA || enableLCPCA; const bool enableGradient = needsGradient(); diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index ba96147dc..0947cf709 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -985,11 +985,23 @@ rtengine::LCPMapper::LCPMapper( isFisheye = pProf->isFisheye; } -bool rtengine::LCPMapper::isCACorrectionAvailable() const +bool rtengine::LCPMapper::hasDistortionCorrection() const +{ + // assume lcp always provides distortion correction + return true; +} + +bool rtengine::LCPMapper::hasCACorrection() const { return enableCA; } +bool rtengine::LCPMapper::hasVignettingCorrection() const +{ + // assume lcp always provides vignetting correction + return true; +} + void rtengine::LCPMapper::correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const { correctDistortion(x, y, cx, cy); diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 97cdd0890..114131b4a 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -167,9 +167,12 @@ class LensCorrection { public: virtual ~LensCorrection() {} + virtual bool hasDistortionCorrection() const = 0; + virtual bool hasCACorrection() const = 0; + virtual bool hasVignettingCorrection() const = 0; + virtual void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const = 0; virtual void correctDistortion(double &x, double &y, int cx, int cy) const = 0; - virtual bool isCACorrectionAvailable() const = 0; virtual void correctCA(double &x, double &y, int cx, int cy, int channel) const = 0; virtual void processVignette(int width, int height, float** rawData) const = 0; virtual void processVignette3Channels(int width, int height, float** rawData) const = 0; @@ -196,9 +199,12 @@ public: ); + bool hasDistortionCorrection() const override; + bool hasCACorrection() const override; + bool hasVignettingCorrection() const override; + void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const override; void correctDistortion(double &x, double &y, int cx, int cy) const override; - bool isCACorrectionAvailable() const override; void correctCA(double& x, double& y, int cx, int cy, int channel) const override; void processVignette(int width, int height, float** rawData) const override; void processVignette3Channels(int width, int height, float** rawData) const override; diff --git a/rtengine/rtlensfun.cc b/rtengine/rtlensfun.cc index 505346ad0..f4714b6f7 100644 --- a/rtengine/rtlensfun.cc +++ b/rtengine/rtlensfun.cc @@ -102,6 +102,21 @@ LFModifier::operator bool() const } +bool LFModifier::hasDistortionCorrection() const +{ + return (flags_ & LF_MODIFY_DISTORTION); +} + +bool LFModifier::hasCACorrection() const +{ + return (flags_ & LF_MODIFY_TCA); +} + +bool LFModifier::hasVignettingCorrection() const +{ + return (flags_ & LF_MODIFY_VIGNETTING); +} + void LFModifier::correctDistortion(double &x, double &y, int cx, int cy) const { if (!data_) { @@ -125,11 +140,6 @@ void LFModifier::correctDistortion(double &x, double &y, int cx, int cy) const } } -bool LFModifier::isCACorrectionAvailable() const -{ - return (flags_ & LF_MODIFY_TCA); -} - void LFModifier::correctCA(double &x, double &y, int cx, int cy, int channel) const { assert(channel >= 0 && channel <= 2); diff --git a/rtengine/rtlensfun.h b/rtengine/rtlensfun.h index 39ca67ec2..78c9c9ff6 100644 --- a/rtengine/rtlensfun.h +++ b/rtengine/rtlensfun.h @@ -53,9 +53,12 @@ public: explicit operator bool() const; + bool hasDistortionCorrection() const override; + bool hasCACorrection() const override; + bool hasVignettingCorrection() const override; + void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const override; void correctDistortion(double &x, double &y, int cx, int cy) const override; - bool isCACorrectionAvailable() const override; void correctCA(double &x, double &y, int cx, int cy, int channel) const override; void processVignette(int width, int height, float** rawData) const override; void processVignette3Channels(int width, int height, float** rawData) const override; From 5159de89cbb113e51dbeda546294f7cb5b65a6ab Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 09:42:15 +0200 Subject: [PATCH 05/12] imagedata: report if file is DNG --- rtengine/imagedata.cc | 7 +++++++ rtengine/imagedata.h | 2 ++ rtengine/rtengine.h | 2 ++ rtgui/cacheimagedata.cc | 6 ++++++ rtgui/cacheimagedata.h | 2 ++ 5 files changed, 19 insertions(+) diff --git a/rtengine/imagedata.cc b/rtengine/imagedata.cc index 6507a970d..45db99f1e 100644 --- a/rtengine/imagedata.cc +++ b/rtengine/imagedata.cc @@ -565,6 +565,8 @@ FramesData::FramesData(const Glib::ustring &fname, time_t ts) : meta.getDimensions(w_, h_); + isDNG = find_exif_tag("Exif.Image.DNGVersion"); + // ----------------------- // Special file type detection (HDR, PixelShift) // ------------------------ @@ -778,6 +780,11 @@ bool FramesData::getHDR() const return isHDR; } +bool FramesData::getDNG() const +{ + return isDNG; +} + std::string FramesData::getImageType() const { return isPixelShift ? "PS" : isHDR ? "HDR" : "STD"; diff --git a/rtengine/imagedata.h b/rtengine/imagedata.h index 08f55bd62..f999d59e6 100644 --- a/rtengine/imagedata.h +++ b/rtengine/imagedata.h @@ -59,6 +59,7 @@ private: time_t modTimeStamp; bool isPixelShift; bool isHDR; + bool isDNG; int w_; int h_; @@ -69,6 +70,7 @@ public: unsigned int getFrameCount() const override; bool getPixelShift() const override; bool getHDR() const override; + bool getDNG() const override; std::string getImageType() const override; IIOSampleFormat getSampleFormat() const override; bool hasExif() const override; diff --git a/rtengine/rtengine.h b/rtengine/rtengine.h index f1ee4108c..0010f42b8 100644 --- a/rtengine/rtengine.h +++ b/rtengine/rtengine.h @@ -130,6 +130,8 @@ public: virtual bool getPixelShift () const = 0; /** @return false: not an HDR file ; true: single or multi-frame HDR file (e.g. Pentax HDR raw file or 32 bit float DNG file or Log compressed) */ virtual bool getHDR() const = 0; + /** @return true if the file is a DNG file */ + virtual bool getDNG() const = 0; /** @return false: not an HDR file ; true: single or multi-frame HDR file (e.g. Pentax HDR raw file or 32 bit float DNG file or Log compressed) */ virtual std::string getImageType() const = 0; diff --git a/rtgui/cacheimagedata.cc b/rtgui/cacheimagedata.cc index 0e9cc7f0f..872c60504 100644 --- a/rtgui/cacheimagedata.cc +++ b/rtgui/cacheimagedata.cc @@ -59,6 +59,7 @@ CacheImageData::CacheImageData() : iso(0), rating(0), isHDR (false), + isDNG (false), isPixelShift (false), sensortype(rtengine::ST_NONE), sampleFormat(rtengine::IIOSF_UNKNOWN), @@ -194,6 +195,10 @@ int CacheImageData::load (const Glib::ustring& fname) isHDR = keyFile.get_boolean ("ExifInfo", "IsHDR"); } + if (keyFile.has_key ("ExifInfo", "IsDNG")) { + isDNG = keyFile.get_boolean ("ExifInfo", "IsDNG"); + } + if (keyFile.has_key ("ExifInfo", "IsPixelShift")) { isPixelShift = keyFile.get_boolean ("ExifInfo", "IsPixelShift"); } @@ -316,6 +321,7 @@ int CacheImageData::save (const Glib::ustring& fname) keyFile.set_double ("ExifInfo", "FocusDist", focusDist); keyFile.set_integer ("ExifInfo", "ISO", iso); keyFile.set_boolean ("ExifInfo", "IsHDR", isHDR); + keyFile.set_boolean ("ExifInfo", "IsDNG", isDNG); keyFile.set_boolean ("ExifInfo", "IsPixelShift", isPixelShift); keyFile.set_string ("ExifInfo", "ExpComp", expcomp); } diff --git a/rtgui/cacheimagedata.h b/rtgui/cacheimagedata.h index 8c0fa6513..e3d5cc3da 100644 --- a/rtgui/cacheimagedata.h +++ b/rtgui/cacheimagedata.h @@ -60,6 +60,7 @@ public: unsigned iso; int rating; bool isHDR; + bool isDNG; bool isPixelShift; int sensortype; rtengine::IIO_Sample_Format sampleFormat; @@ -114,6 +115,7 @@ public: int getRating () const override { return rating; } // FIXME-piotr : missing rating bool getPixelShift () const override { return isPixelShift; } bool getHDR() const override { return isHDR; } + bool getDNG() const override { return isDNG; } std::string getImageType() const override { return isPixelShift ? "PS" : isHDR ? "HDR" : "STD"; } rtengine::IIOSampleFormat getSampleFormat() const override { return sampleFormat; } void getDimensions(int &w, int &h) const override From 2b97de233f330becff648c296daf009e3a6d1e07 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 09:51:08 +0200 Subject: [PATCH 06/12] Add initial metadata lens correction handling --- rtdata/languages/default | 1 + rtengine/CMakeLists.txt | 1 + rtengine/dcrop.cc | 2 +- rtengine/improcfun.h | 3 +- rtengine/iptransform.cc | 18 +++++- rtengine/lensmetadata.cc | 42 +++++++++++++ rtengine/lensmetadata.h | 47 +++++++++++++++ rtengine/procparams.cc | 8 ++- rtengine/procparams.h | 4 +- rtengine/rawimagesource.cc | 9 ++- rtgui/lensprofile.cc | 120 +++++++++++++++++++++++++++---------- rtgui/lensprofile.h | 6 +- 12 files changed, 222 insertions(+), 39 deletions(-) create mode 100644 rtengine/lensmetadata.cc create mode 100644 rtengine/lensmetadata.h diff --git a/rtdata/languages/default b/rtdata/languages/default index 46496826a..e6eb09b06 100644 --- a/rtdata/languages/default +++ b/rtdata/languages/default @@ -2837,6 +2837,7 @@ TP_LENSGEOM_FILL;Auto-fill TP_LENSGEOM_LABEL;Lens / Geometry TP_LENSGEOM_LIN;Linear TP_LENSGEOM_LOG;Logarithmic +TP_LENSPROFILE_CORRECTION_METADATA;From file metadata TP_LENSPROFILE_CORRECTION_AUTOMATCH;Automatically selected TP_LENSPROFILE_CORRECTION_LCPFILE;LCP file TP_LENSPROFILE_CORRECTION_MANUAL;Manually selected diff --git a/rtengine/CMakeLists.txt b/rtengine/CMakeLists.txt index 5d87310b9..c937198c8 100644 --- a/rtengine/CMakeLists.txt +++ b/rtengine/CMakeLists.txt @@ -148,6 +148,7 @@ set(RTENGINESOURCEFILES jpeg_ijg/jpeg_memsrc.cc labimage.cc lcp.cc + lensmetadata.cc lmmse_demosaic.cc loadinitial.cc metadata.cc diff --git a/rtengine/dcrop.cc b/rtengine/dcrop.cc index 580475c37..e630d1936 100644 --- a/rtengine/dcrop.cc +++ b/rtengine/dcrop.cc @@ -1850,7 +1850,7 @@ bool check_need_larger_crop_for_lcp_distortion(int fw, int fh, int x, int y, int return false; } - return (params.lensProf.useDist && (params.lensProf.useLensfun() || params.lensProf.useLcp())); + return (params.lensProf.useDist && (params.lensProf.useLensfun() || params.lensProf.useLcp() || params.lensProf.useMetadata())); } } // namespace diff --git a/rtengine/improcfun.h b/rtengine/improcfun.h index 9c6c794f0..c8c6e166e 100644 --- a/rtengine/improcfun.h +++ b/rtengine/improcfun.h @@ -130,6 +130,7 @@ class ImProcFunctions bool needsGradient() const; bool needsVignetting() const; bool needsLCP() const; + bool needsMetadata() const; bool needsLensfun() const; // static cmsUInt8Number* Mempro = NULL; @@ -155,7 +156,7 @@ enum class BlurType { ~ImProcFunctions(); bool needsLuminanceOnly() const { - return !(needsCA() || needsDistortion() || needsRotation() || needsPerspective() || needsLCP() || needsLensfun()) && (needsVignetting() || needsPCVignetting() || needsGradient()); + return !(needsCA() || needsDistortion() || needsRotation() || needsPerspective() || needsLCP() || needsLensfun() || needsMetadata()) && (needsVignetting() || needsPCVignetting() || needsGradient()); } void setScale(double iscale); diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index 468507385..10f37e781 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -26,6 +26,7 @@ #include "rt_math.h" #include "rtengine.h" #include "rtlensfun.h" +#include "lensmetadata.h" #include "sleef.h" using namespace std; @@ -659,7 +660,13 @@ void ImProcFunctions::transform (Imagefloat* original, Imagefloat* transformed, std::unique_ptr pLCPMap; - if (needsLensfun()) { + if (needsMetadata()) { + auto corr = MetadataLensCorrectionFinder::findCorrection(metadata); + if (corr) { + corr->initCorrections(oW, oH, params->coarse, rawRotationDeg); + pLCPMap = std::move(corr); + } + } else if (needsLensfun()) { pLCPMap = LFDatabase::getInstance()->findModifier(params->lensProf, metadata, oW, oH, params->coarse, rawRotationDeg); } else if (needsLCP()) { // don't check focal length to allow distortion correction for lenses without chip const std::shared_ptr pLCPProf = LCPStore::getInstance()->getProfile (params->lensProf.lcpFile); @@ -675,7 +682,7 @@ void ImProcFunctions::transform (Imagefloat* original, Imagefloat* transformed, } } - if (! (needsCA() || needsDistortion() || needsRotation() || needsPerspective() || needsLCP() || needsLensfun()) && (needsVignetting() || needsPCVignetting() || needsGradient())) { + if (! (needsCA() || needsDistortion() || needsRotation() || needsPerspective() || needsLCP() || needsMetadata() || needsLensfun()) && (needsVignetting() || needsPCVignetting() || needsGradient())) { transformLuminanceOnly (original, transformed, cx, cy, oW, oH, fW, fH); } else { bool highQuality; @@ -1422,6 +1429,11 @@ bool ImProcFunctions::needsVignetting () const return params->vignetting.amount; } +bool ImProcFunctions::needsMetadata () const +{ + return params->lensProf.useMetadata(); +} + bool ImProcFunctions::needsLCP () const { return params->lensProf.useLcp(); @@ -1439,7 +1451,7 @@ bool ImProcFunctions::needsTransform (int oW, int oH, int rawRotationDeg, const std::unique_ptr pLCPMap = LFDatabase::getInstance()->findModifier(params->lensProf, metadata, oW, oH, params->coarse, rawRotationDeg); needsLf = pLCPMap.get(); } - return needsCA () || needsDistortion () || needsRotation () || needsPerspective () || needsGradient () || needsPCVignetting () || needsVignetting () || needsLCP() || needsLf; + return needsCA () || needsDistortion () || needsRotation () || needsPerspective () || needsGradient () || needsPCVignetting () || needsVignetting () || needsLCP() || needsMetadata() || needsLf; } diff --git a/rtengine/lensmetadata.cc b/rtengine/lensmetadata.cc new file mode 100644 index 000000000..e92fc6cc9 --- /dev/null +++ b/rtengine/lensmetadata.cc @@ -0,0 +1,42 @@ +/* + * This file is part of RawTherapee. + * + * Copyright (c) 2024 Rawtherapee developers + * + * RawTherapee is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * RawTherapee is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with RawTherapee. If not, see . + */ +#include +#include + +#include "lensmetadata.h" + +namespace rtengine +{ + +std::unique_ptr MetadataLensCorrectionFinder::findCorrection(const FramesMetaData *meta) +{ + static const std::unordered_set makers = {}; + + std::string make = Glib::ustring(meta->getMake()).uppercase(); + + if (makers.find(make) == makers.end()) { + return nullptr; + } + + std::unique_ptr correction; + + return correction; +} + +} // namespace rtengine diff --git a/rtengine/lensmetadata.h b/rtengine/lensmetadata.h new file mode 100644 index 000000000..7991af8cd --- /dev/null +++ b/rtengine/lensmetadata.h @@ -0,0 +1,47 @@ +/* + * This file is part of RawTherapee. + * + * Copyright (c) 2024 Rawtherapee developers + * + * RawTherapee is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * RawTherapee is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with RawTherapee. If not, see . + */ +#pragma once + +#include + +#include "lcp.h" +#include "metadata.h" +#include "procparams.h" +#include "rtengine.h" + +namespace rtengine +{ + +/* MetadataLensCorrection is an abstract class for various lens correction based on raw file metadata + this metadata is vendor dependent */ +class MetadataLensCorrection : public LensCorrection, + public NonCopyable +{ +public: + virtual void initCorrections(int width, int height, const procparams::CoarseTransformParams &coarse, int rawRotationDeg) = 0; +}; + +/* MetadataLensCorrectionFinder tries to find and return MetadataLensCorrection for the provided metadata */ +class MetadataLensCorrectionFinder +{ +public: + static std::unique_ptr findCorrection(const FramesMetaData *meta); +}; + +} // namespace rtengine diff --git a/rtengine/procparams.cc b/rtengine/procparams.cc index b868a7627..e1a56a533 100644 --- a/rtengine/procparams.cc +++ b/rtengine/procparams.cc @@ -2064,13 +2064,19 @@ bool LensProfParams::lfManual() const return lcMode == LcMode::LENSFUNMANUAL; } +bool LensProfParams::useMetadata() const +{ + return lcMode == LcMode::METADATA; +} + const std::vector& LensProfParams::getMethodStrings() const { static const std::vector method_strings = { "none", "lfauto", "lfmanual", - "lcp" + "lcp", + "metadata" }; return method_strings; } diff --git a/rtengine/procparams.h b/rtengine/procparams.h index d71c3d172..e6945cfbd 100644 --- a/rtengine/procparams.h +++ b/rtengine/procparams.h @@ -966,7 +966,8 @@ struct LensProfParams { NONE, // No lens correction LENSFUNAUTOMATCH, // Lens correction using auto matched lensfun database entry LENSFUNMANUAL, // Lens correction using manually selected lensfun database entry - LCP // Lens correction using lcp file + LCP, // Lens correction using lcp file + METADATA // Lens correction using embedded metadata }; LcMode lcMode; @@ -985,6 +986,7 @@ struct LensProfParams { bool lfAutoMatch() const; bool useLcp() const; bool lfManual() const; + bool useMetadata() const; const std::vector& getMethodStrings() const; Glib::ustring getMethodString(LcMode mode) const; diff --git a/rtengine/rawimagesource.cc b/rtengine/rawimagesource.cc index 09b64ad41..8cd6ffedd 100644 --- a/rtengine/rawimagesource.cc +++ b/rtengine/rawimagesource.cc @@ -44,6 +44,7 @@ #include "rt_math.h" #include "rtengine.h" #include "rtlensfun.h" +#include "lensmetadata.h" #include "../rtgui/options.h" #define BENCHMARK @@ -1583,7 +1584,13 @@ void RawImageSource::preprocess(const RAWParams &raw, const LensProfParams &lens if (!hasFlatField && lensProf.useVign && lensProf.lcMode != LensProfParams::LcMode::NONE) { std::unique_ptr pmap; - if (lensProf.useLensfun()) { + if (lensProf.useMetadata()) { + auto corr = MetadataLensCorrectionFinder::findCorrection(idata); + if (corr) { + corr->initCorrections(W, H, coarse, -1); + pmap = std::move(corr); + } + } else if (lensProf.useLensfun()) { pmap = LFDatabase::getInstance()->findModifier(lensProf, idata, W, H, coarse, -1); } else { const std::shared_ptr pLCPProf = LCPStore::getInstance()->getProfile(lensProf.lcpFile); diff --git a/rtgui/lensprofile.cc b/rtgui/lensprofile.cc index 5f42a1cde..4410b2fe0 100644 --- a/rtgui/lensprofile.cc +++ b/rtgui/lensprofile.cc @@ -32,6 +32,7 @@ #include "../rtengine/lcp.h" #include "../rtengine/procparams.h" #include "../rtengine/rtlensfun.h" +#include "../rtengine/lensmetadata.h" using namespace rtengine; using namespace rtengine::procparams; @@ -56,6 +57,7 @@ LensProfilePanel::LensProfilePanel() : distGrid(Gtk::manage((new Gtk::Grid()))), corrUnchangedRB(Gtk::manage((new Gtk::RadioButton(M("GENERAL_UNCHANGED"))))), corrOffRB(Gtk::manage((new Gtk::RadioButton(corrGroup, M("GENERAL_NONE"))))), + corrMetadata(Gtk::manage((new Gtk::RadioButton(corrGroup, M("TP_LENSPROFILE_CORRECTION_METADATA"))))), corrLensfunAutoRB(Gtk::manage((new Gtk::RadioButton(corrGroup, M("TP_LENSPROFILE_CORRECTION_AUTOMATCH"))))), corrLensfunManualRB(Gtk::manage((new Gtk::RadioButton(corrGroup, M("TP_LENSPROFILE_CORRECTION_MANUAL"))))), corrLcpFileRB(Gtk::manage((new Gtk::RadioButton(corrGroup, M("TP_LENSPROFILE_CORRECTION_LCPFILE"))))), @@ -144,17 +146,18 @@ LensProfilePanel::LensProfilePanel() : // Populate modes grid: modesGrid->attach(*corrOffRB, 0, 0, 3, 1); - modesGrid->attach(*corrLensfunAutoRB, 0, 1, 3, 1); - modesGrid->attach(*corrLensfunManualRB, 0, 2, 3, 1); + modesGrid->attach(*corrMetadata, 0, 1, 3, 1); + modesGrid->attach(*corrLensfunAutoRB, 0, 2, 3, 1); + modesGrid->attach(*corrLensfunManualRB, 0, 3, 3, 1); - modesGrid->attach(*lensfunCamerasLbl, 0, 3, 1, 1); - modesGrid->attach(*lensfunCameras, 1, 3, 1, 1); - modesGrid->attach(*lensfunLensesLbl, 0, 4, 1, 1); - modesGrid->attach(*lensfunLenses, 1, 4, 1, 1); - modesGrid->attach(*warning, 2, 3, 1, 2); + modesGrid->attach(*lensfunCamerasLbl, 0, 4, 1, 1); + modesGrid->attach(*lensfunCameras, 1, 4, 1, 1); + modesGrid->attach(*lensfunLensesLbl, 0, 5, 1, 1); + modesGrid->attach(*lensfunLenses, 1, 5, 1, 1); + modesGrid->attach(*warning, 2, 4, 1, 2); - modesGrid->attach(*corrLcpFileRB, 0, 5, 1, 1); - modesGrid->attach(*corrLcpFileChooser, 1, 5, 1, 1); + modesGrid->attach(*corrLcpFileRB, 0, 6, 1, 1); + modesGrid->attach(*corrLcpFileChooser, 1, 6, 1, 1); // Populate distortions grid: @@ -179,6 +182,7 @@ LensProfilePanel::LensProfilePanel() : lensfunCameras->signal_changed().connect(sigc::mem_fun(*this, &LensProfilePanel::onLensfunCameraChanged)); lensfunLenses->signal_changed().connect(sigc::mem_fun(*this, &LensProfilePanel::onLensfunLensChanged)); corrOffRB->signal_toggled().connect(sigc::bind(sigc::mem_fun(*this, &LensProfilePanel::onCorrModeChanged), corrOffRB)); + corrMetadata->signal_toggled().connect(sigc::bind(sigc::mem_fun(*this, &LensProfilePanel::onCorrModeChanged), corrMetadata)); corrLensfunAutoRB->signal_toggled().connect(sigc::bind(sigc::mem_fun(*this, &LensProfilePanel::onCorrModeChanged), corrLensfunAutoRB)); corrLensfunManualRB->signal_toggled().connect(sigc::bind(sigc::mem_fun(*this, &LensProfilePanel::onCorrModeChanged), corrLensfunManualRB)); corrLcpFileRB->signal_toggled().connect(sigc::bind(sigc::mem_fun(*this, &LensProfilePanel::onCorrModeChanged), corrLcpFileRB)); @@ -211,30 +215,52 @@ void LensProfilePanel::read(const rtengine::procparams::ProcParams* pp, const Pa break; } + case procparams::LensProfParams::LcMode::METADATA: { + if (metadata) { + auto metadataCorrection= rtengine::MetadataLensCorrectionFinder::findCorrection(metadata); + if (metadataCorrection) { + corrMetadata->set_active(true); + corrMetadata->set_sensitive(true); + } else { + corrMetadata->set_sensitive(false); + corrOffRB->set_active(true); + } + } else { + corrMetadata->set_sensitive(false); + } + break; + } + case procparams::LensProfParams::LcMode::NONE: { corrOffRB->set_active(true); setManualParamsVisibility(false); + + ckbUseDist->set_sensitive(false); + ckbUseVign->set_sensitive(false); + ckbUseCA->set_sensitive(false); break; } } - if (pp->lensProf.lcpFile.empty()) { - const Glib::ustring lastFolder = corrLcpFileChooser->get_current_folder(); - corrLcpFileChooser->set_current_folder(lastFolder); - corrLcpFileChooser->unselect_all(); - bindCurrentFolder(*corrLcpFileChooser, options.lastLensProfileDir); - updateDisabled(false); - } - else if (LCPStore::getInstance()->isValidLCPFileName(pp->lensProf.lcpFile)) { - corrLcpFileChooser->set_filename(pp->lensProf.lcpFile); - - if (corrLcpFileRB->get_active()) { - updateDisabled(true); + if (pp->lensProf.lcMode == procparams::LensProfParams::LcMode::LCP) { + if (pp->lensProf.lcpFile.empty()) { + const Glib::ustring lastFolder = corrLcpFileChooser->get_current_folder(); + corrLcpFileChooser->set_current_folder(lastFolder); + corrLcpFileChooser->unselect_all(); + bindCurrentFolder(*corrLcpFileChooser, options.lastLensProfileDir); + updateLCPDisabled(false); + } + else if (LCPStore::getInstance()->isValidLCPFileName(pp->lensProf.lcpFile)) { + corrLcpFileChooser->set_filename(pp->lensProf.lcpFile); + + if (corrLcpFileRB->get_active()) { + updateLCPDisabled(true); + } + } + else { + corrLcpFileChooser->unselect_filename(corrLcpFileChooser->get_filename()); + updateLCPDisabled(false); } - } - else { - corrLcpFileChooser->unselect_filename(corrLcpFileChooser->get_filename()); - updateDisabled(false); } const LFDatabase* const db = LFDatabase::getInstance(); @@ -308,6 +334,9 @@ void LensProfilePanel::write(rtengine::procparams::ProcParams* pp, ParamsEdited* if (corrLcpFileRB->get_active()) { pp->lensProf.lcMode = procparams::LensProfParams::LcMode::LCP; } + else if (corrMetadata->get_active()) { + pp->lensProf.lcMode = procparams::LensProfParams::LcMode::METADATA; + } else if (corrLensfunManualRB->get_active()) { pp->lensProf.lcMode = procparams::LensProfParams::LcMode::LENSFUNMANUAL; } @@ -367,12 +396,18 @@ void LensProfilePanel::setRawMeta(bool raw, const rtengine::FramesMetaData* pMet // CA is very focus layer dependent, otherwise it might even worsen things allowFocusDep = false; - ckbUseCA->set_active(false); - ckbUseCA->set_sensitive(false); enableListener(); } + corrMetadata->set_sensitive(false); + if (pMeta) { + metadataCorrection = MetadataLensCorrectionFinder::findCorrection(pMeta); + if (metadataCorrection) { + corrMetadata->set_sensitive(true); + } + } + isRaw = raw; metadata = pMeta; } @@ -381,7 +416,7 @@ void LensProfilePanel::onLCPFileChanged() { lcpFileChanged = true; const bool valid = LCPStore::getInstance()->isValidLCPFileName(corrLcpFileChooser->get_filename()); - updateDisabled(valid); + updateLCPDisabled(valid); if (listener) { if (valid) { @@ -552,10 +587,20 @@ void LensProfilePanel::onCorrModeChanged(const Gtk::RadioButton* rbChanged) lensfunAutoChanged = true; lcpFileChanged = true; - updateDisabled(true); + updateLCPDisabled(true); mode = M("TP_LENSPROFILE_CORRECTION_LCPFILE"); + } else if (rbChanged == corrMetadata) { + lcModeChanged = true; + useLensfunChanged = true; + lensfunAutoChanged = true; + lcpFileChanged = true; + + updateMetadataDisabled(); + + mode = M("TP_LENSPROFILE_CORRECTION_METADATA"); + } else if (rbChanged == corrUnchangedRB) { lcModeChanged = false; useLensfunChanged = false; @@ -680,7 +725,7 @@ void LensProfilePanel::LFDbHelper::fillLensfunLenses() } } -void LensProfilePanel::updateDisabled(bool enable) +void LensProfilePanel::updateLCPDisabled(bool enable) { if (!batchMode) { ckbUseDist->set_sensitive(enable); @@ -689,6 +734,21 @@ void LensProfilePanel::updateDisabled(bool enable) } } +void LensProfilePanel::updateMetadataDisabled() +{ + if (!batchMode) { + if (metadataCorrection) { + ckbUseDist->set_sensitive(metadataCorrection->hasDistortionCorrection()); + ckbUseVign->set_sensitive(metadataCorrection->hasVignettingCorrection()); + ckbUseCA->set_sensitive(metadataCorrection->hasCACorrection()); + } else { + ckbUseDist->set_sensitive(false); + ckbUseVign->set_sensitive(false); + ckbUseCA->set_sensitive(false); + } + } +} + bool LensProfilePanel::setLensfunCamera(const Glib::ustring& make, const Glib::ustring& model) { if (!make.empty() && !model.empty()) { diff --git a/rtgui/lensprofile.h b/rtgui/lensprofile.h index 42746f41e..cdf7bd6e4 100644 --- a/rtgui/lensprofile.h +++ b/rtgui/lensprofile.h @@ -22,6 +22,7 @@ #include "guiutils.h" #include "toolpanel.h" +#include "../rtengine/lensmetadata.h" class LensProfilePanel final : public ToolParamBlock, @@ -89,7 +90,8 @@ private: void fillLensfunLenses(); }; - void updateDisabled(bool enable); + void updateLCPDisabled(bool enable); + void updateMetadataDisabled(); bool setLensfunCamera(const Glib::ustring& make, const Glib::ustring& model); bool setLensfunLens(const Glib::ustring& lens); @@ -113,12 +115,14 @@ private: bool allowFocusDep; bool isRaw; const rtengine::FramesMetaData* metadata; + std::unique_ptr metadataCorrection; Gtk::Grid* const modesGrid; Gtk::Grid* const distGrid; Gtk::RadioButton* const corrUnchangedRB; Gtk::RadioButton::Group corrGroup; Gtk::RadioButton* const corrOffRB; + Gtk::RadioButton* const corrMetadata; Gtk::RadioButton* const corrLensfunAutoRB; Gtk::RadioButton* const corrLensfunManualRB; Gtk::RadioButton* const corrLcpFileRB; From f64ad13363c340815b67fe74119189126536f40c Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:23:25 +0200 Subject: [PATCH 07/12] lensmetadata: add abstract center radius helper class Add a CenterRadiusMetadataLensCorrection helper class that will be implemented by vendor specific corrections based on center radius. --- rtengine/lensmetadata.cc | 108 +++++++++++++++++++++++++++++++++++++++ rtengine/lensmetadata.h | 42 +++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/rtengine/lensmetadata.cc b/rtengine/lensmetadata.cc index e92fc6cc9..c531de65b 100644 --- a/rtengine/lensmetadata.cc +++ b/rtengine/lensmetadata.cc @@ -24,6 +24,114 @@ namespace rtengine { +CenterRadiusMetadataLensCorrection::CenterRadiusMetadataLensCorrection(const FramesMetaData *meta) : + swap_xy(false) +{ + metadata = Exiv2Metadata(meta->getFileName()); + metadata.load(); +} + +void CenterRadiusMetadataLensCorrection::initCorrections(int width, int height, const procparams::CoarseTransformParams &coarse, int rawRotationDeg) +{ + if (rawRotationDeg >= 0) { + int rot = (coarse.rotate + rawRotationDeg) % 360; + swap_xy = (rot == 90 || rot == 270); + if (swap_xy) { + std::swap(width, height); + } + } + + w2 = width * 0.5f; + h2 = height * 0.5f; + rf = 1 / std::sqrt(SQR(w2) + SQR(h2)); +} + +void CenterRadiusMetadataLensCorrection::process(double &x, double &y, int cx, int cy, int channel, bool dist, bool ca) const +{ + double xx = x + cx; + double yy = y + cy; + if (swap_xy) { + std::swap(xx, yy); + } + + double xc = xx - w2; + double yc = yy - h2; + + double rout = rf * std::sqrt(SQR(xc) + SQR(yc)); + double cf = 1; + if (dist && ca) { + cf = distortionAndCACorrectionFactor(rout, channel); + } else if (dist) { + cf = distortionCorrectionFactor(rout); + } else if (ca) { + cf = caCorrectionFactor(rout, channel); + } + + x = cf * xc + w2; + y = cf * yc + h2; + + if (swap_xy) { + std::swap(x, y); + } + x -= cx; + y -= cy; +} + +void CenterRadiusMetadataLensCorrection::correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const +{ + if (!hasDistortionCorrection() || !hasCACorrection()) { + return; + } + + process(x, y, cx, cy, channel, true, true); +} + +void CenterRadiusMetadataLensCorrection::correctDistortion(double &x, double &y, int cx, int cy) const +{ + if (!hasDistortionCorrection()) { + return; + } + + process(x, y, cx, cy, 1, true, false); +} + +void CenterRadiusMetadataLensCorrection::correctCA(double &x, double &y, int cx, int cy, int channel) const +{ + if (!hasCACorrection()) { + return; + } + + process(x, y, cx, cy, channel, false, true); +} + +void CenterRadiusMetadataLensCorrection::processVignetteNChannels(int width, int height, float **rawData, int channels) const +{ + if (!hasVignettingCorrection()) { + return; + } + + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + double xc = x - w2; + double yc = y - h2; + double sf = vignettingCorrectionFactor(rf * std::sqrt(SQR(xc) + SQR(yc))); + for (int c = 0; c < channels; c++) { + rawData[y][x + c] *= sf; + } + } + } +} + +void CenterRadiusMetadataLensCorrection::processVignette(int width, int height, float **rawData) const +{ + return processVignetteNChannels(width, height, rawData, 1); +} + +void CenterRadiusMetadataLensCorrection::processVignette3Channels(int width, int height, float **rawData) const +{ + return processVignetteNChannels(width, height, rawData, 3); +} + std::unique_ptr MetadataLensCorrectionFinder::findCorrection(const FramesMetaData *meta) { static const std::unordered_set makers = {}; diff --git a/rtengine/lensmetadata.h b/rtengine/lensmetadata.h index 7991af8cd..ac042b7ce 100644 --- a/rtengine/lensmetadata.h +++ b/rtengine/lensmetadata.h @@ -37,6 +37,48 @@ public: virtual void initCorrections(int width, int height, const procparams::CoarseTransformParams &coarse, int rawRotationDeg) = 0; }; +/* CenterRadiusMetadataLensCorrection is an abstract class the extends MetadataLensCorrection to easily handle center radius based corrections */ +class CenterRadiusMetadataLensCorrection : public MetadataLensCorrection +{ +public: + CenterRadiusMetadataLensCorrection(const FramesMetaData *meta); + + void process(double &x, double &y, int cx, int cy, int channel, bool dist, bool ca) const; + + void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const override; + void correctDistortion(double &x, double &y, int cx, int cy) const override; + void correctCA(double &x, double &y, int cx, int cy, int channel) const override; + void processVignette(int width, int height, float **rawData) const override; + void processVignette3Channels(int width, int height, float **rawData) const override; + + void processVignetteNChannels(int width, int height, float **rawData, int channels) const; + void initCorrections(int width, int height, const procparams::CoarseTransformParams &coarse, int rawRotationDeg) override; + + /* Implementers should implement the below methods */ + virtual bool hasDistortionCorrection() const override = 0; + virtual bool hasCACorrection() const override = 0; + virtual bool hasVignettingCorrection() const override = 0; + + /* These methods should return the distortion correction factor (cf) for the + * provided radius rout (radius of the output image (corrected)). + * So rin = rout * cf + * */ + virtual double distortionCorrectionFactor(double rout) const = 0; + virtual double caCorrectionFactor(double rout, int channel) const = 0; + virtual double distortionAndCACorrectionFactor(double rout, int channel) const = 0; + + /* This methods should return the vignetting correction factor (cf) for the + * provided radius */ + virtual double vignettingCorrectionFactor(double r) const = 0; + +protected: + bool swap_xy; + double w2; + double h2; + double rf; + Exiv2Metadata metadata; +}; + /* MetadataLensCorrectionFinder tries to find and return MetadataLensCorrection for the provided metadata */ class MetadataLensCorrectionFinder { From 27dc084e81b3145eae103919a2395d3d25b0c036 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:26:22 +0200 Subject: [PATCH 08/12] Add Sony metadata lens correction --- rtengine/lensmetadata.cc | 163 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 162 insertions(+), 1 deletion(-) diff --git a/rtengine/lensmetadata.cc b/rtengine/lensmetadata.cc index c531de65b..0e8156f39 100644 --- a/rtengine/lensmetadata.cc +++ b/rtengine/lensmetadata.cc @@ -24,6 +24,31 @@ namespace rtengine { +namespace +{ + +/* interpolateLinearSpline does a simple linear spline interpolation. Values + * outside the external knots will return the value of the nearest knot without + * any additional interpolation. */ +double interpolateLinearSpline(const std::vector &xi, const std::vector &yi, double x) +{ + if (x < xi[0]) { + return yi[0]; + } + + for (size_t i = 1; i < xi.size(); i++) { + if (x >= xi[i - 1] && x <= xi[i]) { + double dydx = (yi[i] - yi[i - 1]) / (xi[i] - xi[i - 1]); + + return yi[i - 1] + (x - xi[i - 1]) * dydx; + } + } + + return yi[yi.size() - 1]; +} + +} // namespace + CenterRadiusMetadataLensCorrection::CenterRadiusMetadataLensCorrection(const FramesMetaData *meta) : swap_xy(false) { @@ -132,9 +157,133 @@ void CenterRadiusMetadataLensCorrection::processVignette3Channels(int width, int return processVignetteNChannels(width, height, rawData, 3); } +/* Fuji, Sony, Olympus metadata handling and algorithms adapted from + * - src/iop/lens.cc + * - src/common/exif.cc + * in darktable 4.6 */ +/* + This file is part of darktable, + Copyright (C) 2019-2024 darktable developers. + + darktable is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + darktable is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with darktable. If not, see . +*/ + +class SonyMetadataLensCorrection : public CenterRadiusMetadataLensCorrection +{ +public: + SonyMetadataLensCorrection(const FramesMetaData *meta) : + CenterRadiusMetadataLensCorrection(meta) + { + parse(); + setup(); + } + +private: + int nc; + std::array distortion; + std::array ca_r; + std::array ca_b; + std::array vignetting; + + std::vector knots; + std::vector dist; + std::array, 3> ca; + std::vector vig; + + void parse() + { + if (Exiv2::versionNumber() < EXIV2_MAKE_VERSION(0, 27, 4)) { + throw std::runtime_error("cannot get Sony correction data, too old exiv2 version " + Exiv2::versionString()); + } + + auto &exif = metadata.exifData(); + + auto posd = exif.findKey(Exiv2::ExifKey("Exif.SubImage1.DistortionCorrParams")); + auto posc = exif.findKey(Exiv2::ExifKey("Exif.SubImage1.ChromaticAberrationCorrParams")); + auto posv = exif.findKey(Exiv2::ExifKey("Exif.SubImage1.VignettingCorrParams")); + + /* Sony metadata corrections parameters define some splines with N knots */ + if (posd == exif.end() || posc == exif.end() || posv == exif.end()) { + throw std::runtime_error("cannot get Sony correction data"); + } + + const int nc = to_long(posd); + if (nc <= 16 && 2 * nc == to_long(posc) && nc == to_long(posv)) { + this->nc = nc; + for (int i = 0; i < nc; i++) { + distortion[i] = to_long(posd, i + 1); + ca_r[i] = to_long(posc, i + 1); + ca_b[i] = to_long(posc, nc + i + 1); + vignetting[i] = to_long(posv, i + 1); + } + } else { + throw std::runtime_error("cannot get Sony correction data"); + } + } + + void setup() + { + knots.resize(nc); + dist.resize(nc); + vig.resize(nc); + for (int i = 0; i < 3; ++i) { + ca[i].resize(nc); + } + + for (int i = 0; i < this->nc; i++) { + knots[i] = (i + 0.5) / (nc - 1); + + dist[i] = distortion[i] * powf(2, -14) + 1; + + ca[0][i] = ca[1][i] = ca[2][i] = 1.f; + ca[0][i] *= ca_r[i] * powf(2, -21) + 1; + ca[2][i] *= ca_b[i] * powf(2, -21) + 1; + + vig[i] = 1 / powf(2, 0.5f - powf(2, vignetting[i] * powf(2, -13) - 1)); + } + } + + double distortionCorrectionFactor(double rout) const override + { + return interpolateLinearSpline(knots, dist, rout); + } + + double caCorrectionFactor(double rout, int channel) const override + { + return interpolateLinearSpline(knots, ca[channel], rout); + } + + double distortionAndCACorrectionFactor(double rout, int channel) const override + { + return distortionCorrectionFactor(rout) * caCorrectionFactor(rout, channel); + } + + double vignettingCorrectionFactor(double r) const override + { + return interpolateLinearSpline(knots, vig, r); + } + + bool hasDistortionCorrection() const override { return true; } + bool hasVignettingCorrection() const override { return true; } + bool hasCACorrection() const override { return true; } +}; + std::unique_ptr MetadataLensCorrectionFinder::findCorrection(const FramesMetaData *meta) { - static const std::unordered_set makers = {}; + static const std::unordered_set makers = { + "SONY", + }; std::string make = Glib::ustring(meta->getMake()).uppercase(); @@ -144,6 +293,18 @@ std::unique_ptr MetadataLensCorrectionFinder::findCorrec std::unique_ptr correction; + try { + if (make == "SONY") { + correction.reset(new SonyMetadataLensCorrection(meta)); + } + } catch (std::exception &exc) { + if (settings->verbose) { + std::cerr << "error parsing lens metadata: " << exc.what() << std::endl; + } + + correction.reset(nullptr); + } + return correction; } From b570c70f5b47233e3a3b8d99ac0502041a5692a4 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:27:16 +0200 Subject: [PATCH 09/12] Add Fujifilm metadata lens correction --- rtengine/lensmetadata.cc | 207 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) diff --git a/rtengine/lensmetadata.cc b/rtengine/lensmetadata.cc index 0e8156f39..006976371 100644 --- a/rtengine/lensmetadata.cc +++ b/rtengine/lensmetadata.cc @@ -279,10 +279,215 @@ private: bool hasCACorrection() const override { return true; } }; +class FujiMetadataLensCorrection : public CenterRadiusMetadataLensCorrection +{ +public: + FujiMetadataLensCorrection(const FramesMetaData *meta) : + CenterRadiusMetadataLensCorrection(meta) + { + parse(); + setup(); + } + +private: + const static int MAXKNOTS = 16; + + int nc; + double cropf; + + std::array fuji_knots; + std::array fuji_distortion; + std::array fuji_ca_r; + std::array fuji_ca_b; + std::array fuji_vignetting; + + std::vector knots_dist; + std::vector dist; + std::array, 3> ca; + std::vector knots_vig; + std::vector vig; + + void parse() + { + if (Exiv2::versionNumber() < EXIV2_MAKE_VERSION(0, 27, 4)) { + throw std::runtime_error("cannot get Fuji correction data, too old exiv2 version " + Exiv2::versionString()); + } + + auto &exif = metadata.exifData(); + + /* FujiFilm metadata corrections parameters define some splines with N knots */ + auto posd = exif.findKey(Exiv2::ExifKey("Exif.Fujifilm.GeometricDistortionParams")); + auto posc = exif.findKey(Exiv2::ExifKey("Exif.Fujifilm.ChromaticAberrationParams")); + auto posv = exif.findKey(Exiv2::ExifKey("Exif.Fujifilm.VignettingParams")); + + // X-Trans IV/V + if (posd != exif.end() && posc != exif.end() && posv != exif.end() && + posd->count() == 19 && posc->count() == 29 && posv->count() == 19) { + const int nc = 9; + this->nc = nc; + + for (int i = 0; i < nc; i++) { + const float kd = posd->toFloat(i + 1); + const float kc = posc->toFloat(i + 1); + const float kv = posv->toFloat(i + 1); + + // Check that the knots position is the same for distortion, ca and vignetting, + if (kd != kc || kd != kv) { + throw std::runtime_error("cannot get Fuji correction data: unexpected data"); + } + + fuji_knots[i] = kd; + fuji_distortion[i] = posd->toFloat(i + 10); + fuji_ca_r[i] = posc->toFloat(i + 10); + fuji_ca_b[i] = posc->toFloat(i + 19); + fuji_vignetting[i] = posv->toFloat(i + 10); + } + + // Account for the 1.25x crop modes in some Fuji cameras + auto it = exif.findKey(Exiv2::ExifKey("Exif.Fujifilm.CropMode")); + + if (it != exif.end() && (to_long(it) == 2 || to_long(it) == 4)) { + cropf = 1.25f; + } else { + cropf = 1; + } + } + // X-Trans I/II/III + else if (posd != exif.end() && posc != exif.end() && posv != exif.end() && + posd->count() == 23 && posc->count() == 31 && posv->count() == 23) { + const int nc = 11; + this->nc = nc; + + for (int i = 0; i < nc; i++) { + const float kd = posd->toFloat(i + 1); + float kc = 0; + // ca data doesn't provide first knot (0) + if (i != 0) kc = posc->toFloat(i); + const float kv = posv->toFloat(i + 1); + // check that the knots position is the same for distortion, ca and vignetting, + if (kd != kc || kd != kv) { + throw std::runtime_error("cannot get Fuji correction data: unexpected data"); + } + + fuji_knots[i] = kd; + fuji_distortion[i] = posd->toFloat(i + 12); + + // ca data doesn't provide first knot (0) + if (i == 0) { + fuji_ca_r[i] = 0; + fuji_ca_b[i] = 0; + } else { + fuji_ca_r[i] = posc->toFloat(i + 10); + fuji_ca_b[i] = posc->toFloat(i + 20); + } + fuji_vignetting[i] = posv->toFloat(i + 12); + } + + // Account for the 1.25x crop modes in some Fuji cameras + auto it = exif.findKey(Exiv2::ExifKey("Exif.Fujifilm.CropMode")); + + if (it != exif.end() && (to_long(it) == 2 || to_long(it) == 4)) { + cropf = 1.25f; + } else { + cropf = 1; + } + } else { + throw std::runtime_error("cannot get Fuji correction data"); + } + } + + void setup() + { + std::vector knots_in; + std::vector distortion_in; + std::vector ca_r_in; + std::vector ca_b_in; + + // add a knot with no corrections at 0 value if not existing + int size = nc; + if (fuji_knots[0] > 0.f) { + knots_in.push_back(0); + distortion_in.push_back(1); + ca_r_in.push_back(0); + ca_b_in.push_back(0); + + knots_vig.push_back(0); + vig.push_back(1); + + size++; + } + + knots_in.reserve(size); + vig.reserve(size); + + for (int i = 0; i < nc; i++) { + knots_in.push_back(cropf * fuji_knots[i]); + distortion_in.push_back(fuji_distortion[i] / 100 + 1); + ca_r_in.push_back(fuji_ca_r[i]); + ca_b_in.push_back(fuji_ca_b[i]); + + // vignetting correction is applied before distortion correction. So the + // spline is related to the source image before distortion. + knots_vig.push_back(cropf * fuji_knots[i]); + + vig.push_back(100 / fuji_vignetting[i]); + } + + knots_dist.resize(MAXKNOTS); + dist.resize(MAXKNOTS); + for (int i = 0; i < 3; ++i) { + ca[i].resize(MAXKNOTS); + } + + // convert from spline related to source image (input is source image + // radius) to spline related to dest image (input is dest image radius) + for (int i = 0; i < MAXKNOTS; i++) { + const double rin = static_cast(i) / static_cast(nc - 1); + const double m = interpolateLinearSpline(knots_in, distortion_in, rin); + const double r = rin / m; + knots_dist[i] = r; + + dist[i] = m; + + const double mcar = interpolateLinearSpline(knots_in, ca_r_in, rin); + const double mcab = interpolateLinearSpline(knots_in, ca_b_in, rin); + + ca[0][i] = ca[1][i] = ca[2][i] = 1.f; + ca[0][i] *= mcar + 1; + ca[2][i] *= mcab + 1; + } + } + + double distortionCorrectionFactor(double rout) const override + { + return interpolateLinearSpline(knots_dist, dist, rout); + } + + double caCorrectionFactor(double rout, int channel) const override + { + return interpolateLinearSpline(knots_dist, ca[channel], rout); + } + + double distortionAndCACorrectionFactor(double rout, int channel) const override + { + return distortionCorrectionFactor(rout) * caCorrectionFactor(rout, channel); + } + + double vignettingCorrectionFactor(double r) const override + { + return interpolateLinearSpline(knots_vig, vig, r); + } + + bool hasDistortionCorrection() const override { return true; } + bool hasVignettingCorrection() const override { return true; } + bool hasCACorrection() const override { return true; } +}; + std::unique_ptr MetadataLensCorrectionFinder::findCorrection(const FramesMetaData *meta) { static const std::unordered_set makers = { "SONY", + "FUJIFILM", }; std::string make = Glib::ustring(meta->getMake()).uppercase(); @@ -296,6 +501,8 @@ std::unique_ptr MetadataLensCorrectionFinder::findCorrec try { if (make == "SONY") { correction.reset(new SonyMetadataLensCorrection(meta)); + } else if (make == "FUJIFILM") { + correction.reset(new FujiMetadataLensCorrection(meta)); } } catch (std::exception &exc) { if (settings->verbose) { From 3189efddceb1d28b5b619caec56527ecacc5a974 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:28:19 +0200 Subject: [PATCH 10/12] Add Olympus metadata lens correction --- rtengine/lensmetadata.cc | 131 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/rtengine/lensmetadata.cc b/rtengine/lensmetadata.cc index 006976371..91adadd8a 100644 --- a/rtengine/lensmetadata.cc +++ b/rtengine/lensmetadata.cc @@ -483,11 +483,140 @@ private: bool hasCACorrection() const override { return true; } }; +class OlympusMetadataLensCorrection : public CenterRadiusMetadataLensCorrection +{ +public: + OlympusMetadataLensCorrection(const FramesMetaData *meta) : + CenterRadiusMetadataLensCorrection(meta), has_dist(false), has_ca(false) + { + parse(); + } + +private: + bool has_dist, has_ca; + + double drs; + double dk2; + double dk4; + double dk6; + + double car0; + double car2; + double car4; + double cab0; + double cab2; + double cab4; + + void parse() + { + if (Exiv2::versionNumber() < EXIV2_MAKE_VERSION(0, 27, 4)) { + throw std::runtime_error("cannot get Olympus correction data, too old exiv2 version " + Exiv2::versionString()); + } + + auto &exif = metadata.exifData(); + + std::array distortion; + std::array cacorr; + + auto it = exif.findKey(Exiv2::ExifKey("Exif.OlympusIp.0x150a")); + if (it != exif.end() && it->count() == 4) { + for (int i = 0; i < 4; ++i) { + distortion[i] = it->toFloat(i); + } + has_dist = true; + } + + it = exif.findKey(Exiv2::ExifKey("Exif.OlympusIp.0x150c")); + if (it != exif.end() && it->count() == 6) { + for (int i = 0; i < 6; ++i) { + cacorr[i] = it->toFloat(i); + } + has_ca = true; + } + + if (has_dist) { + drs = distortion[3]; + dk2 = distortion[0]; + dk4 = distortion[1]; + dk6 = distortion[2]; + } + + if (has_ca) { + car0 = cacorr[0]; + car2 = cacorr[1]; + car4 = cacorr[2]; + cab0 = cacorr[3]; + cab2 = cacorr[4]; + cab4 = cacorr[5]; + } + + if (!has_dist && !has_ca) { + throw std::runtime_error("no Olympus correction data"); + } + } + + double distortionCorrectionFactor(double rout) const override + { + // The distortion polynomial maps a radius Rout in the output + // (undistorted) image, where the corner is defined as Rout=1, to a + // radius in the input (distorted) image, where the corner is defined + // as Rin=1. + // Rin = Rout*drs * (1 + dk2 * (Rout*drs)^2 + dk4 * (Rout*drs)^4 + dk6 * (Rout*drs)^6) + // + // cf is Rin / Rout. + + const double rs2 = std::pow(rout * drs, 2); + const double cf = drs * (1 + rs2 * (dk2 + rs2 * (dk4 + rs2 * dk6))); + + return cf; + } + + double caCorrectionFactor(double rout, int channel) const override + { + // ca corrects only red and blue channel + if (channel != 0 && channel != 2) return 1; + + // CA correction is applied as: + // Rin = Rout * ((1 + car0) + car2 * Rout^2 + car4 * Rout^4) + // + // cf is Rin / Rout. + + const double r2 = powf(rout, 2); + if (channel == 0) { + return 1 + (car0 + r2 * (car2 + r2 * car4)); + } else if (channel == 2) { + return 1 + (cab0 + r2 * (cab2 + r2 * cab4)); + } + + return 1; + } + + double distortionAndCACorrectionFactor(double rout, int channel) const override + { + return distortionCorrectionFactor(rout) * caCorrectionFactor(rout, channel); + } + + double vignettingCorrectionFactor(double r) const override + { + return 1; + } + + bool hasDistortionCorrection() const override { return has_dist; } + // Olympus cameras have a shading correction option that fixes vignetting + // already in the raw file. Looks like they don't report vignetting + // correction parameters inside metadata even if shading correction is + // disabled. + bool hasVignettingCorrection() const override { return false; } + bool hasCACorrection() const override { return has_ca; } +}; + std::unique_ptr MetadataLensCorrectionFinder::findCorrection(const FramesMetaData *meta) { static const std::unordered_set makers = { "SONY", "FUJIFILM", + "OLYMPUS", + "OM DIGITAL SOLUTIONS", }; std::string make = Glib::ustring(meta->getMake()).uppercase(); @@ -503,6 +632,8 @@ std::unique_ptr MetadataLensCorrectionFinder::findCorrec correction.reset(new SonyMetadataLensCorrection(meta)); } else if (make == "FUJIFILM") { correction.reset(new FujiMetadataLensCorrection(meta)); + } else if (make == "OLYMPUS" || make == "OM DIGITAL SOLUTIONS") { + correction.reset(new OlympusMetadataLensCorrection(meta)); } } catch (std::exception &exc) { if (settings->verbose) { From c589e9d469cb41bfc24f39051735647d315d0d29 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:29:07 +0200 Subject: [PATCH 11/12] Add Panasonic metadata lens correction --- rtengine/lensmetadata.cc | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/rtengine/lensmetadata.cc b/rtengine/lensmetadata.cc index 91adadd8a..f6b7e7331 100644 --- a/rtengine/lensmetadata.cc +++ b/rtengine/lensmetadata.cc @@ -610,6 +610,95 @@ private: bool hasCACorrection() const override { return has_ca; } }; +/* Panasonic metadata lens correction + * Currently disabled since the algorithm is not stable and works well with only some lenses. + * + * Data extraction and distortion correction formula from: + * https://web.archive.org/web/20120701131817/https://syscall.eu/#pana + * + * TODO(sgotti) + * * CA corrections not yet reverse engineered. + * * From a post on the exiftool forum: + * https://exiftool.org/forum/index.php?topic=9366.0 + * looks like there's another additional tag that provides similar data and it's + * used by newer cameras. + */ +class PanasonicMetadataLensCorrection : public CenterRadiusMetadataLensCorrection +{ +public: + PanasonicMetadataLensCorrection(const FramesMetaData *meta) : + CenterRadiusMetadataLensCorrection(meta), has_dist(false), a(0), b(0), c(0) + { + // Currently disabled since the algorithm is not stable and works well with only some lenses. + throw std::runtime_error("panasonic correction disabled as it's not yet working properly"); + + // parse(); + } + +private: + bool has_dist; + double scale, a, b, c; + + void parse() + { + if (Exiv2::versionNumber() < EXIV2_MAKE_VERSION(0, 27, 4)) { + throw std::runtime_error("cannot get Panasonic correction data, too old exiv2 version " + Exiv2::versionString()); + } + + auto &exif = metadata.exifData(); + + auto it = exif.findKey(Exiv2::ExifKey("Exif.PanasonicRaw.0x0119")); + if (it != exif.end()) { + std::vector buf; + buf.resize(it->value().size()); + it->value().copy(buf.data(), Exiv2::littleEndian); + + const Exiv2::byte *data = buf.data(); + // n is currently unused + // uint32_t n = Exiv2::getShort(data + 24, Exiv2::littleEndian); + scale = 1.0f / (1.0f + Exiv2::getShort(data + 10, Exiv2::littleEndian) / 32768.0f); + a = Exiv2::getShort(data + 16, Exiv2::littleEndian) / 32768.0f; + b = Exiv2::getShort(data + 8, Exiv2::littleEndian) / 32768.0f; + c = Exiv2::getShort(data + 22, Exiv2::littleEndian) / 32768.0f; + + has_dist = true; + } + } + + double distortionCorrectionFactor(double rout) const override + { + const double rs2 = std::pow(rout, 2); + + const double rin = scale * rout * (1 + rs2 * (a + rs2 * (b + rs2 * c))); + const double cf = rout / rin; + + return cf; + } + + double caCorrectionFactor(double rout, int channel) const override + { + return 1; + } + + double distortionAndCACorrectionFactor(double rout, int channel) const override + { + return distortionCorrectionFactor(rout); + } + + double vignettingCorrectionFactor(double r) const override + { + return 1; + } + + bool hasDistortionCorrection() const override { return has_dist; } + // Panasonic cameras have a shading correction option that fixes vignetting + // already in the raw file. Looks like they don't report vignetting + // correction parameters inside metadata even if shading correction is + // disabled. + bool hasVignettingCorrection() const override { return false; } + bool hasCACorrection() const override { return false; } +}; + std::unique_ptr MetadataLensCorrectionFinder::findCorrection(const FramesMetaData *meta) { static const std::unordered_set makers = { @@ -617,6 +706,7 @@ std::unique_ptr MetadataLensCorrectionFinder::findCorrec "FUJIFILM", "OLYMPUS", "OM DIGITAL SOLUTIONS", + "PANASONIC", }; std::string make = Glib::ustring(meta->getMake()).uppercase(); @@ -634,6 +724,8 @@ std::unique_ptr MetadataLensCorrectionFinder::findCorrec correction.reset(new FujiMetadataLensCorrection(meta)); } else if (make == "OLYMPUS" || make == "OM DIGITAL SOLUTIONS") { correction.reset(new OlympusMetadataLensCorrection(meta)); + } else if (make == "PANASONIC") { + correction.reset(new PanasonicMetadataLensCorrection(meta)); } } catch (std::exception &exc) { if (settings->verbose) { From 7deb2754d1f31beff891063b476905d4093e8570 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 13 Jun 2024 20:29:30 +0200 Subject: [PATCH 12/12] Add DNG metadata lens correction --- rtengine/lensmetadata.cc | 306 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 304 insertions(+), 2 deletions(-) diff --git a/rtengine/lensmetadata.cc b/rtengine/lensmetadata.cc index f6b7e7331..89a1ab13b 100644 --- a/rtengine/lensmetadata.cc +++ b/rtengine/lensmetadata.cc @@ -699,6 +699,306 @@ private: bool hasCACorrection() const override { return false; } }; +// Class DNGMetadatalensCorrection handles OpcodeList3 operations: operations to +// be done after demosaicing. +// OpcodeList1 is already handled by rawimagesource.cc. OpcodeList2 is not yet +// handled by rawtherapee. +// TODO(sgotti): dng spec provides clear rules on how and when to process the +// various opcodeList1/2/3 and the order of the various operations that should +// be done. +// Currently we only handle a subset of all the available opcodes and only one +// WarpRectilinar for distortion and FixVignetteRadial for vignetting (that's +// usually the case with Leica DNGs and ADC generated DNGs). +// This should be extended to support more exotic operations lists (i.e. +// multiple WarpRectilinear) +class DNGMetadataLensCorrection : public MetadataLensCorrection +{ +public: + DNGMetadataLensCorrection(const FramesMetaData *meta) : + MetadataLensCorrection(), swap_xy(false) + { + metadata = Exiv2Metadata(meta->getFileName()); + metadata.load(); + + parse(); + } + +private: + Exiv2Metadata metadata; + + bool swap_xy; + int width, height; + + double crx_d; + double cry_d; + double crx_v; + double cry_v; + + double cx_d; + double cy_d; + double m_v; + + double cx_v; + double cy_v; + double m_d; + + int planes; + + bool has_dist, has_ca, has_vign; + std::array, 3> warp_rectilinear; + std::array vignette_radial; + + void initCorrections(int width, int height, const procparams::CoarseTransformParams &coarse, int rawRotationDeg) override + { + if (rawRotationDeg >= 0) { + int rot = (coarse.rotate + rawRotationDeg) % 360; + swap_xy = (rot == 90 || rot == 270); + if (swap_xy) { + std::swap(width, height); + } + } + + this->width = width; + this->height = height; + + setup(); + } + + void parse() + { + std::set processed_opcodes; + + has_dist = has_ca = has_vign = false; + + auto &exif = metadata.exifData(); + + auto it = exif.findKey(Exiv2::ExifKey("Exif.SubImage1.OpcodeList3")); + + if (it != exif.end()) { + std::vector buf; + buf.resize(it->value().size()); + it->value().copy(buf.data(), Exiv2::invalidByteOrder); + + const Exiv2::byte *data = buf.data(); + uint32_t num_entries = Exiv2::getULong(data, Exiv2::bigEndian); + size_t idx = 4; + + for (size_t i = 0; i < num_entries && idx < buf.size(); ++i) { + uint32_t opcodeID = Exiv2::getULong(data + idx, Exiv2::bigEndian); + idx += 4; + idx += 4; // version + uint32_t flags = Exiv2::getULong(data + idx, Exiv2::bigEndian); + idx += 4; + size_t paramSize = Exiv2::getULong(data + idx, Exiv2::bigEndian); + idx += 4; + + if (idx + paramSize > buf.size()) { + throw std::runtime_error("error parsing DNG OpcodeList3"); + } + + if (processed_opcodes.find(opcodeID) != processed_opcodes.end()) { + // we currently handle only one opcode per type and ignore next ones if provided. + if (settings->verbose) { + std::printf("DNG OpcodeList3 %s opcode %d already processed\n", flags & 1 ? "optional" : "mandatory", opcodeID); + } + + idx += paramSize; + continue; + } + + processed_opcodes.insert(opcodeID); + + // we currently handle only one dist correction + if (opcodeID == 1 && !has_dist) { // WarpRectilinear + + planes = Exiv2::getULong(data + idx, Exiv2::bigEndian); + + if ((planes != 1) && (planes != 3)) { + throw std::runtime_error("cannot parse DNG WarpRectilinear"); + } + + for (int p = 0; p < planes; p++) { + for (int i = 0; i < 6; i++) { + warp_rectilinear[p][i] = Exiv2::getDouble(data + idx + 4 + 8 * (i + p * 6), Exiv2::bigEndian); + } + } + + crx_d = Exiv2::getDouble(data + idx + 4 + 8 * (0 + planes * 6), Exiv2::bigEndian); + cry_d = Exiv2::getDouble(data + idx + 4 + 8 * (1 + planes * 6), Exiv2::bigEndian); + + has_dist = true; + if (planes == 3) { + has_ca = true; + } + + // we currently handle only one vignetting correction + } else if (opcodeID == 3 && !has_vign) { // FixVignetteRadial + size_t start = idx; + size_t end = idx + 7 * 8; + if (end > buf.size()) { + throw std::runtime_error("cannot parse DNG FixVignetteRadial"); + } + for (int j = 0; j < 5; j++) { + vignette_radial[j] = Exiv2::getDouble(data + start, Exiv2::bigEndian); + start += 8; + } + crx_v = Exiv2::getDouble(data + start, Exiv2::bigEndian); + start += 8; + cry_v = Exiv2::getDouble(data + start, Exiv2::bigEndian); + has_vign = true; + + } else { + if (settings->verbose) { + std::printf("DNG OpcodeList3 has unsupported %s opcode %d\n", flags & 1 ? "optional" : "mandatory", opcodeID); + } + } + + idx += paramSize; + } + } + + if (!has_dist && !has_vign) { + throw std::runtime_error("no known DNG correction data"); + } + } + + void setup() + { + cx_d = crx_d * width; + cy_d = cry_d * height; + cx_v = crx_v * width; + cy_v = cry_v * height; + double mx_d = std::max(cx_d, width - cx_d); + double my_d = std::max(cy_d, height - cy_d); + m_d = std::sqrt(SQR(mx_d) + SQR(my_d)); + double mx_v = std::max(cx_v, width - cx_v); + double my_v = std::max(cy_v, height - cy_v); + m_v = std::sqrt(SQR(mx_v) + SQR(my_v)); + } + + void correctPlaneDistortion(double &x, double &y, int cx, int cy, int plane) const + { + if (plane < 0 || plane > 2 || plane > planes) { + return; + } + + double xx = x + cx; + double yy = y + cy; + if (swap_xy) { + std::swap(xx, yy); + } + + const double cx1 = cx_d; + const double cy1 = cy_d; + const double m = m_d; + + const double dx = (xx - cx1) / m; + const double dy = (yy - cy1) / m; + const double dx2 = SQR(dx); + const double dy2 = SQR(dy); + const double r2 = dx2 + dy2; + const double f = warp_rectilinear[plane][0] + r2 * (warp_rectilinear[plane][1] + r2 * (warp_rectilinear[plane][2] + r2 * warp_rectilinear[plane][3])); + const double dx_r = f * dx; + const double dy_r = f * dy; + const double dxdy2 = 2 * dx * dy; + const double dx_t = warp_rectilinear[plane][4] * dxdy2 + warp_rectilinear[plane][5] * (r2 + 2 * dx2); + const double dy_t = warp_rectilinear[plane][5] * dxdy2 + warp_rectilinear[plane][4] * (r2 + 2 * dy2); + + x = cx1 + m * (dx_r + dx_t); + y = cy1 + m * (dy_r + dy_t); + + if (swap_xy) { + std::swap(x, y); + } + x -= cx; + y -= cy; + } + + void correctDistortionAndCA(double &x, double &y, int cx, int cy, int channel) const override + { + if (!hasDistortionCorrection() || !hasCACorrection()) { + return; + } + + correctPlaneDistortion(x, y, cx, cy, channel); + } + + void correctDistortion(double &x, double &y, int cx, int cy) const override + { + if (!hasDistortionCorrection()) { + return; + } + + int plane = 1; // 3 planes correction, use plane 1 (green) + if (planes == 1) { + plane = 0; // 1 single plane correction + } + + correctPlaneDistortion(x, y, cx, cy, plane); + } + + void correctCA(double &x, double &y, int cx, int cy, int channel) const override + { + if (!hasCACorrection()) { + return; + } + + // we use plane 0 (red) and plane 2 (blue) for ca correction + if (channel != 0 && channel != 2) return; + if (planes != 3) return; + + double xgreen = x, ygreen = y; + correctPlaneDistortion(xgreen, ygreen, cx, cy, 1); + + double xch = x, ych = y; + correctPlaneDistortion(xch, ych, cx, cy, channel); + + // Calculate diff from green plane + x += xch - xgreen; + y += ych - ygreen; + } + + void processVignetteNChannels(int width, int height, float **rawData, int channels) const + { + if (!hasVignettingCorrection()) { + return; + } + + const double cx = cx_v; + const double cy = cy_v; + const double m2 = 1.f / SQR(m_v); + + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + const double r2 = m2 * (SQR(x - cx) + SQR(y - cy)); + const double g = 1.f + r2 * (vignette_radial[0] + r2 * (vignette_radial[1] + r2 * (vignette_radial[2] + r2 * (vignette_radial[3] + r2 * vignette_radial[4])))); + for (int c = 0; c < channels; ++c) { + rawData[y][x*channels + c] *= g; + } + } + } + } + + void processVignette(int width, int height, float **rawData) const override + { + return processVignetteNChannels(width, height, rawData, 1); + } + + void processVignette3Channels(int width, int height, float **rawData) const override + { + return processVignetteNChannels(width, height, rawData, 3); + } + + bool isCACorrectionAvailable() const + { + return hasCACorrection(); + } + + bool hasDistortionCorrection() const override { return has_dist; } + bool hasVignettingCorrection() const override { return has_vign; } + bool hasCACorrection() const override { return has_ca; } +}; + std::unique_ptr MetadataLensCorrectionFinder::findCorrection(const FramesMetaData *meta) { static const std::unordered_set makers = { @@ -711,14 +1011,16 @@ std::unique_ptr MetadataLensCorrectionFinder::findCorrec std::string make = Glib::ustring(meta->getMake()).uppercase(); - if (makers.find(make) == makers.end()) { + if (!meta->getDNG() && makers.find(make) == makers.end()) { return nullptr; } std::unique_ptr correction; try { - if (make == "SONY") { + if (meta->getDNG()) { + correction.reset(new DNGMetadataLensCorrection(meta)); + } else if (make == "SONY") { correction.reset(new SonyMetadataLensCorrection(meta)); } else if (make == "FUJIFILM") { correction.reset(new FujiMetadataLensCorrection(meta));