From aa2beb4c724c6d8ba301ce7426d512ecc50faed5 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Wed, 11 May 2016 00:04:11 +0200 Subject: [PATCH] Floessie suggested some C++11 related improvements which I add with this commit --- rtengine/LUT.h | 26 +++--- rtengine/curves.cc | 207 +++++++++++++++++---------------------------- 2 files changed, 92 insertions(+), 141 deletions(-) diff --git a/rtengine/LUT.h b/rtengine/LUT.h index 00c7b1858..6676e38a7 100644 --- a/rtengine/LUT.h +++ b/rtengine/LUT.h @@ -59,17 +59,8 @@ #ifndef LUT_H_ #define LUT_H_ -// bit representations of flags -#define LUT_CLIP_BELOW 1 -#define LUT_CLIP_ABOVE 2 - -#define LUTf LUT -#define LUTi LUT -#define LUTu LUT -#define LUTd LUT -#define LUTuc LUT - #include +#include #ifndef NDEBUG #include #include @@ -78,6 +69,21 @@ #include #include "rt_math.h" +// Bit representations of flags +enum { + LUT_CLIP_BELOW = 1 << 0, + LUT_CLIP_ABOVE = 1 << 1 +}; + +template +class LUT; + +using LUTf = LUT; +using LUTi = LUT; +using LUTu = LUT; +using LUTd = LUT; +using LUTuc = LUT; + template class LUT { diff --git a/rtengine/curves.cc b/rtengine/curves.cc index 724ec7280..568f1e8ea 100644 --- a/rtengine/curves.cc +++ b/rtengine/curves.cc @@ -16,12 +16,13 @@ * You should have received a copy of the GNU General Public License * along with RawTherapee. If not, see . */ +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include #ifdef _OPENMP #include #endif @@ -43,7 +44,7 @@ using namespace std; namespace rtengine { -Curve::Curve () : N(0), x(nullptr), y(nullptr), ypp(nullptr), hashSize(1000 /* has to be initialized to the maximum value */ ) {} +Curve::Curve () : N(0), x(nullptr), y(nullptr), ypp(nullptr), hashSize(1000 /* has to be initialized to the maximum value */) {} void Curve::AddPolygons () { @@ -297,10 +298,10 @@ void CurveFactory::curveBW ( const std::vector& curvePointsbw, const std void CurveFactory::curveCL ( bool & clcutili, const std::vector& clcurvePoints, LUTf & clCurve, int skip) { bool needed = false; - DiagonalCurve* dCurve = nullptr; + std::unique_ptr dCurve; if (!clcurvePoints.empty() && clcurvePoints[0] != 0) { - dCurve = new DiagonalCurve (clcurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(clcurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (dCurve && !dCurve->isIdentity()) { needed = true; @@ -308,22 +309,18 @@ void CurveFactory::curveCL ( bool & clcutili, const std::vector& clcurve } } - fillCurveArray(dCurve, clCurve, skip, needed); - - if (dCurve) { - delete dCurve; - } + fillCurveArray(dCurve.get(), clCurve, skip, needed); } void CurveFactory::mapcurve ( bool & mapcontlutili, const std::vector& mapcurvePoints, LUTf & mapcurve, int skip, const LUTu & histogram, LUTu & outBeforeCurveHistogram) { bool needed = false; - DiagonalCurve* dCurve = nullptr; + std::unique_ptr dCurve; outBeforeCurveHistogram.clear(); bool histNeeded = false; if (!mapcurvePoints.empty() && mapcurvePoints[0] != 0) { - dCurve = new DiagonalCurve (mapcurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(mapcurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (outBeforeCurveHistogram) { histNeeded = true; @@ -339,22 +336,18 @@ void CurveFactory::mapcurve ( bool & mapcontlutili, const std::vector& m histogram.compressTo(outBeforeCurveHistogram, 32768); } - fillCurveArray(dCurve, mapcurve, skip, needed); - - if (dCurve) { - delete dCurve; - } + fillCurveArray(dCurve.get(), mapcurve, skip, needed); } void CurveFactory::curveDehaContL ( bool & dehacontlutili, const std::vector& dehaclcurvePoints, LUTf & dehaclCurve, int skip, const LUTu & histogram, LUTu & outBeforeCurveHistogram) { bool needed = false; - DiagonalCurve* dCurve = nullptr; + std::unique_ptr dCurve; outBeforeCurveHistogram.clear(); bool histNeeded = false; if (!dehaclcurvePoints.empty() && dehaclcurvePoints[0] != 0) { - dCurve = new DiagonalCurve (dehaclcurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(dehaclcurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (outBeforeCurveHistogram) { histNeeded = true; @@ -370,21 +363,17 @@ void CurveFactory::curveDehaContL ( bool & dehacontlutili, const std::vector& wavclcurvePoints, LUTf & wavclCurve, /*LUTu & histogramwavcl, LUTu & outBeforeWavCLurveHistogram,*/int skip) { bool needed = false; - DiagonalCurve* dCurve = nullptr; + std::unique_ptr dCurve; if (!wavclcurvePoints.empty() && wavclcurvePoints[0] != 0) { - dCurve = new DiagonalCurve (wavclcurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(wavclcurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (dCurve && !dCurve->isIdentity()) { needed = true; @@ -392,32 +381,24 @@ void CurveFactory::curveWavContL ( bool & wavcontlutili, const std::vector& curvePoints, LUTf & ToningCurve, int skip) { bool needed = false; - DiagonalCurve* dCurve = nullptr; + std::unique_ptr dCurve; if (!curvePoints.empty() && curvePoints[0] != 0) { - dCurve = new DiagonalCurve (curvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(curvePoints, CURVES_MIN_POLY_POINTS / skip)); if (dCurve && !dCurve->isIdentity()) { needed = true; } } - fillCurveArray(dCurve, ToningCurve, skip, needed); - - if (dCurve) { - delete dCurve; - } + fillCurveArray(dCurve.get(), ToningCurve, skip, needed); } @@ -430,12 +411,12 @@ void CurveFactory::complexsgnCurve (float adjustr, bool & autili, bool & butili { bool needed = false; - DiagonalCurve* dCurve = nullptr; + std::unique_ptr dCurve; int k = 48000; //32768*1.41 // create a curve if needed if (!acurvePoints.empty() && acurvePoints[0] != 0) { - dCurve = new DiagonalCurve (acurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(acurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (dCurve && !dCurve->isIdentity()) { needed = true; @@ -443,19 +424,16 @@ void CurveFactory::complexsgnCurve (float adjustr, bool & autili, bool & butili } } - fillCurveArray(dCurve, aoutCurve, skip, needed); + fillCurveArray(dCurve.get(), aoutCurve, skip, needed); - if (dCurve) { - delete dCurve; - dCurve = nullptr; - } + dCurve = nullptr; //----------------------------------------------------- needed = false; if (!bcurvePoints.empty() && bcurvePoints[0] != 0) { - dCurve = new DiagonalCurve (bcurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(bcurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (dCurve && !dCurve->isIdentity()) { needed = true; @@ -463,12 +441,9 @@ void CurveFactory::complexsgnCurve (float adjustr, bool & autili, bool & butili } } - fillCurveArray(dCurve, boutCurve, skip, needed); + fillCurveArray(dCurve.get(), boutCurve, skip, needed); - if (dCurve) { - delete dCurve; - dCurve = nullptr; - } + dCurve = nullptr; //----------------------------------------------- needed = false; @@ -478,7 +453,7 @@ void CurveFactory::complexsgnCurve (float adjustr, bool & autili, bool & butili } if (!cccurvePoints.empty() && cccurvePoints[0] != 0) { - dCurve = new DiagonalCurve (cccurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(cccurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (outBeforeCCurveHistogram) { histogramC.compressTo(outBeforeCCurveHistogram, k * adjustr); @@ -490,12 +465,9 @@ void CurveFactory::complexsgnCurve (float adjustr, bool & autili, bool & butili } } - fillCurveArray(dCurve, satCurve, skip, needed); + fillCurveArray(dCurve.get(), satCurve, skip, needed); - if (dCurve) { - delete dCurve; - dCurve = nullptr; - } + dCurve = nullptr; //---------------------------- needed = false; @@ -505,7 +477,7 @@ void CurveFactory::complexsgnCurve (float adjustr, bool & autili, bool & butili } if (!lccurvePoints.empty() && lccurvePoints[0] != 0) { - dCurve = new DiagonalCurve (lccurvePoints, CURVES_MIN_POLY_POINTS / skip); + dCurve = std::unique_ptr(new DiagonalCurve(lccurvePoints, CURVES_MIN_POLY_POINTS / skip)); if (outBeforeLCurveHistogram) { histogramLC.compressTo(outBeforeLCurveHistogram, k * adjustr); @@ -517,11 +489,7 @@ void CurveFactory::complexsgnCurve (float adjustr, bool & autili, bool & butili } } - fillCurveArray(dCurve, lhskCurve, skip, needed); - - if (dCurve) { - delete dCurve; - } + fillCurveArray(dCurve.get(), lhskCurve, skip, needed); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -558,36 +526,35 @@ SSEFUNCTION void CurveFactory::complexCurve (double ecomp, double black, double // tone curve base. a: slope (from exp.comp.), b: black, def_mul: max. x value (can be>1), hr,sr: highlight,shadow recovery //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - DiagonalCurve* brightcurve = nullptr; + std::unique_ptr brightcurve; // check if brightness curve is needed if (br > 0.00001 || br < -0.00001) { - std::vector brightcurvePoints; - brightcurvePoints.resize(9); - brightcurvePoints.at(0) = double(DCT_NURBS); + std::vector brightcurvePoints(9); + brightcurvePoints[0] = DCT_NURBS; - brightcurvePoints.at(1) = 0.; //black point. Value in [0 ; 1] range - brightcurvePoints.at(2) = 0.; //black point. Value in [0 ; 1] range + brightcurvePoints[1] = 0.; //black point. Value in [0 ; 1] range + brightcurvePoints[2] = 0.; //black point. Value in [0 ; 1] range if(br > 0) { - brightcurvePoints.at(3) = 0.1; //toe point - brightcurvePoints.at(4) = 0.1 + br / 150.0; //value at toe point + brightcurvePoints[3] = 0.1; //toe point + brightcurvePoints[4] = 0.1 + br / 150.0; //value at toe point - brightcurvePoints.at(5) = 0.7; //shoulder point - brightcurvePoints.at(6) = min(1.0, 0.7 + br / 300.0); //value at shoulder point + brightcurvePoints[5] = 0.7; //shoulder point + brightcurvePoints[6] = min(1.0, 0.7 + br / 300.0); //value at shoulder point } else { - brightcurvePoints.at(3) = max(0.0, 0.1 - br / 150.0); //toe point - brightcurvePoints.at(4) = 0.1; //value at toe point + brightcurvePoints[3] = max(0.0, 0.1 - br / 150.0); //toe point + brightcurvePoints[4] = 0.1; //value at toe point - brightcurvePoints.at(5) = 0.7 - br / 300.0; //shoulder point - brightcurvePoints.at(6) = 0.7; //value at shoulder point + brightcurvePoints[5] = 0.7 - br / 300.0; //shoulder point + brightcurvePoints[6] = 0.7; //value at shoulder point } - brightcurvePoints.at(7) = 1.; // white point - brightcurvePoints.at(8) = 1.; // value at white point + brightcurvePoints[7] = 1.; // white point + brightcurvePoints[8] = 1.; // value at white point - brightcurve = new DiagonalCurve (brightcurvePoints, CURVES_MIN_POLY_POINTS / skip); + brightcurve = std::unique_ptr(new DiagonalCurve(brightcurvePoints, CURVES_MIN_POLY_POINTS / skip)); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -688,9 +655,7 @@ SSEFUNCTION void CurveFactory::complexCurve (double ecomp, double black, double dcurve[i] = CLIPD(val); } - if (brightcurve) { - delete brightcurve; - } + brightcurve = nullptr; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -712,23 +677,22 @@ SSEFUNCTION void CurveFactory::complexCurve (double ecomp, double black, double avg /= sum; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - std::vector contrastcurvePoints; - contrastcurvePoints.resize(9); - contrastcurvePoints.at(0) = double(DCT_NURBS); + std::vector contrastcurvePoints(9); + contrastcurvePoints[0] = DCT_NURBS; - contrastcurvePoints.at(1) = 0; //black point. Value in [0 ; 1] range - contrastcurvePoints.at(2) = 0; //black point. Value in [0 ; 1] range + contrastcurvePoints[1] = 0; //black point. Value in [0 ; 1] range + contrastcurvePoints[2] = 0; //black point. Value in [0 ; 1] range - contrastcurvePoints.at(3) = avg - avg * (0.6 - contr / 250.0); //toe point - contrastcurvePoints.at(4) = avg - avg * (0.6 + contr / 250.0); //value at toe point + contrastcurvePoints[3] = avg - avg * (0.6 - contr / 250.0); //toe point + contrastcurvePoints[4] = avg - avg * (0.6 + contr / 250.0); //value at toe point - contrastcurvePoints.at(5) = avg + (1 - avg) * (0.6 - contr / 250.0); //shoulder point - contrastcurvePoints.at(6) = avg + (1 - avg) * (0.6 + contr / 250.0); //value at shoulder point + contrastcurvePoints[5] = avg + (1 - avg) * (0.6 - contr / 250.0); //shoulder point + contrastcurvePoints[6] = avg + (1 - avg) * (0.6 + contr / 250.0); //value at shoulder point - contrastcurvePoints.at(7) = 1.; // white point - contrastcurvePoints.at(8) = 1.; // value at white point + contrastcurvePoints[7] = 1.; // white point + contrastcurvePoints[8] = 1.; // value at white point - DiagonalCurve contrastcurve(contrastcurvePoints, CURVES_MIN_POLY_POINTS / skip); + const DiagonalCurve contrastcurve(contrastcurvePoints, CURVES_MIN_POLY_POINTS / skip); //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // apply contrast enhancement @@ -742,11 +706,10 @@ SSEFUNCTION void CurveFactory::complexCurve (double ecomp, double black, double // create second curve if needed bool histNeeded = false; - DiagonalCurve* tcurve = nullptr; customToneCurve2.Reset(); if (!curvePoints2.empty() && curvePoints2[0] > DCT_Linear && curvePoints2[0] < DCT_Unchanged) { - DiagonalCurve tcurve(curvePoints2, CURVES_MIN_POLY_POINTS / skip); + const DiagonalCurve tcurve(curvePoints2, CURVES_MIN_POLY_POINTS / skip); if (!tcurve.isIdentity()) { customToneCurve2.Set(tcurve, gamma_, start, slope, mul, add); @@ -765,7 +728,7 @@ SSEFUNCTION void CurveFactory::complexCurve (double ecomp, double black, double customToneCurve1.Reset(); if (!curvePoints.empty() && curvePoints[0] > DCT_Linear && curvePoints[0] < DCT_Unchanged) { - DiagonalCurve tcurve(curvePoints, CURVES_MIN_POLY_POINTS / skip); + const DiagonalCurve tcurve(curvePoints, CURVES_MIN_POLY_POINTS / skip); if (!tcurve.isIdentity()) { customToneCurve1.Set(tcurve, gamma_, start, slope, mul, add); @@ -811,11 +774,6 @@ SSEFUNCTION void CurveFactory::complexCurve (double ecomp, double black, double outBeforeCCurveHistogram[hi] += histogram[i] ; } } - - if (tcurve) { - delete tcurve; - } - } @@ -953,11 +911,11 @@ void CurveFactory::complexLCurve (double br, double contr, const std::vector tcurve; bool histNeeded = false; if (!curvePoints.empty() && curvePoints[0] != 0) { - tcurve = new DiagonalCurve (curvePoints, CURVES_MIN_POLY_POINTS / skip); + tcurve = std::unique_ptr(new DiagonalCurve (curvePoints, CURVES_MIN_POLY_POINTS / skip)); if (outBeforeCCurveHistogram) { histNeeded = true; @@ -965,7 +923,6 @@ void CurveFactory::complexLCurve (double br, double contr, const std::vectorisIdentity()) { - delete tcurve; tcurve = nullptr; } @@ -1004,10 +961,6 @@ void CurveFactory::complexLCurve (double br, double contr, const std::vector& curvePoints, LUTf & outC { // create a curve if needed - DiagonalCurve* tcurve = nullptr; + std::unique_ptr tcurve; if (!curvePoints.empty() && curvePoints[0] != 0) { - tcurve = new DiagonalCurve (curvePoints, CURVES_MIN_POLY_POINTS / skip); + tcurve = std::unique_ptr(new DiagonalCurve(curvePoints, CURVES_MIN_POLY_POINTS / skip)); } if (tcurve && tcurve->isIdentity()) { - delete tcurve; tcurve = nullptr; } @@ -1041,8 +993,6 @@ void CurveFactory::RGBCurve (const std::vector& curvePoints, LUTf & outC val = tcurve->getVal(val); outCurve[i] = Color::igammatab_srgb[val * 65535.f]; } - - delete tcurve; } else { // let the LUTf empty for identity curves outCurve.reset(); } @@ -1146,17 +1096,16 @@ void OpacityCurve::Set(const Curve *pCurve) void OpacityCurve::Set(const std::vector &curvePoints, bool &opautili) { - FlatCurve* tcurve = nullptr; + std::unique_ptr tcurve; if (!curvePoints.empty() && curvePoints[0] > FCT_Linear && curvePoints[0] < FCT_Unchanged) { - tcurve = new FlatCurve (curvePoints, false, CURVES_MIN_POLY_POINTS / 2); + tcurve = std::unique_ptr(new FlatCurve(curvePoints, false, CURVES_MIN_POLY_POINTS / 2)); tcurve->setIdentityValue(0.); } if (tcurve) { - Set(tcurve); + Set(tcurve.get()); opautili = true; - delete tcurve; tcurve = nullptr; } } @@ -1530,16 +1479,14 @@ void ColorGradientCurve::SetXYZ(const Curve *pCurve, const double xyz_rgb[3][3], void ColorGradientCurve::SetXYZ(const std::vector &curvePoints, const double xyz_rgb[3][3], const double rgb_xyz[3][3], float satur, float lumin) { - FlatCurve* tcurve = nullptr; + std::unique_ptr tcurve; if (!curvePoints.empty() && curvePoints[0] > FCT_Linear && curvePoints[0] < FCT_Unchanged) { - tcurve = new FlatCurve (curvePoints, false, CURVES_MIN_POLY_POINTS / 2); + tcurve = std::unique_ptr(new FlatCurve(curvePoints, false, CURVES_MIN_POLY_POINTS / 2)); } if (tcurve) { - SetXYZ(tcurve, xyz_rgb, rgb_xyz, satur, lumin); - delete tcurve; - tcurve = nullptr; + SetXYZ(tcurve.get(), xyz_rgb, rgb_xyz, satur, lumin); } } @@ -1631,16 +1578,14 @@ void ColorGradientCurve::SetRGB(const Curve *pCurve, const double xyz_rgb[3][3], void ColorGradientCurve::SetRGB(const std::vector &curvePoints, const double xyz_rgb[3][3], const double rgb_xyz[3][3]) { - FlatCurve* tcurve = nullptr; + std::unique_ptr tcurve; if (!curvePoints.empty() && curvePoints[0] > FCT_Linear && curvePoints[0] < FCT_Unchanged) { - tcurve = new FlatCurve (curvePoints, false, CURVES_MIN_POLY_POINTS / 2); + tcurve = std::unique_ptr(new FlatCurve(curvePoints, false, CURVES_MIN_POLY_POINTS / 2)); } if (tcurve) { - SetRGB(tcurve, xyz_rgb, rgb_xyz); - delete tcurve; - tcurve = nullptr; + SetRGB(tcurve.get(), xyz_rgb, rgb_xyz); } }