From a9be068b1c680550deb8ed20d5f21037714e5ac0 Mon Sep 17 00:00:00 2001 From: Desmis Date: Sat, 19 Jan 2019 09:11:43 +0100 Subject: [PATCH] Improvment to Color and Light contrast --- rtengine/curves.cc | 23 +++++++++++++---------- rtengine/curves.h | 2 +- rtengine/dcrop.cc | 3 ++- rtengine/improccoordinator.cc | 12 ++++++------ rtengine/improccoordinator.h | 1 + rtengine/improcfun.h | 2 +- rtengine/iplocallab.cc | 21 ++++++++++++++++++--- rtengine/simpleprocess.cc | 8 ++++---- 8 files changed, 46 insertions(+), 26 deletions(-) diff --git a/rtengine/curves.cc b/rtengine/curves.cc index 492d36348..722b98ccd 100644 --- a/rtengine/curves.cc +++ b/rtengine/curves.cc @@ -1055,7 +1055,7 @@ void CurveFactory::complexCurve(double ecomp, double black, double hlcompr, doub //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% void CurveFactory::complexCurvelocal(double ecomp, double black, double hlcompr, double hlcomprthresh, double shcompr, double br, double cont, LUTu & histogram, double lumare, - LUTf & hlCurve, LUTf & shCurve, LUTf & outCurve, LUTf & lightCurveloc, + LUTf & hlCurve, LUTf & shCurve, LUTf & outCurve, LUTf & lightCurveloc, float avg, int skip) { @@ -1124,19 +1124,22 @@ void CurveFactory::complexCurvelocal(double ecomp, double black, double hlcompr, if (cont > 0.00001 || cont < -0.00001) { // compute mean luminance of the image with the curve applied - int sum = 0; - float avg = 0; +// int sum = 0; +// float avg = 0; - for (int i = 0; i < 32768; i++) { - avg += lightCurveloc[i] * histogram[i]; - sum += histogram[i]; - } - printf("avg=%f sum=%i lumaref=%f\n", avg/sum, sum, lumare/100.f); +// for (int i = 0; i < 32768; i++) { +// avg += lightCurveloc[i] * histogram[i]; +// sum += histogram[i]; +// } + + int k = avg * 32768; + avg = lightCurveloc[k]; +// printf("avg=%f lumaref=%f\n", avg, lumare/100.f); std::vector contrastcurvePoints; bool lumm = true; if (lumm) { - // avg /= sum; - avg = lumare / 100.f; +// avg /= sum; +// avg = lumare / 100.f; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% contrastcurvePoints.resize(9); contrastcurvePoints.at(0) = double (DCT_NURBS); diff --git a/rtengine/curves.h b/rtengine/curves.h index 76439bd49..0d5270fec 100644 --- a/rtengine/curves.h +++ b/rtengine/curves.h @@ -346,7 +346,7 @@ public: int skip = 1); static void complexCurvelocal(double ecomp, double black, double hlcompr, double hlcomprthresh, double shcompr, double br, double cont, LUTu & histogram, double lumare, - LUTf & hlCurve, LUTf & shCurve, LUTf & outCurve, LUTf & lightCurveloc, + LUTf & hlCurve, LUTf & shCurve, LUTf & outCurve, LUTf & lightCurveloc, float avg, int skip = 1); static void curveBW(const std::vector& curvePointsbw, const std::vector& curvePointsbw2, const LUTu & histogrambw, LUTu & outBeforeCCurveHistogrambw, diff --git a/rtengine/dcrop.cc b/rtengine/dcrop.cc index e428522f2..ac2e2fe44 100644 --- a/rtengine/dcrop.cc +++ b/rtengine/dcrop.cc @@ -895,6 +895,7 @@ void Crop::update(int todo) LUTu lhist16loc2(32770, 0); bool LHutili = parent->LHutili; bool HHutili = parent->HHutili; + float avg = parent->avg; LUTu dummy; bool needslocal = params.locallab.enabled; LocretigainCurve locRETgainCurve; @@ -950,7 +951,7 @@ void Crop::update(int todo) lumare = parent->lumarefs[sp]; sobelre = parent->sobelrefs[sp]; CurveFactory::complexCurvelocal(ecomp, black / 65535., hlcompr, hlcomprthresh, shcompr, br, cont, lhist16loc2, lumare, - hltonecurveloc2, shtonecurveloc2, tonecurveloc2, lightCurveloc2, + hltonecurveloc2, shtonecurveloc2, tonecurveloc2, lightCurveloc2, avg, sca); parent->ipf.Lab_Local(1, sp, (float**)shbuffer, labnCrop, labnCrop, reservCrop, cropx / skip, cropy / skip, skips(parent->fw, skip), skips(parent->fh, skip), skip, locRETgainCurve, lllocalcurve2, diff --git a/rtengine/improccoordinator.cc b/rtengine/improccoordinator.cc index 63c734f5a..be1896b0a 100644 --- a/rtengine/improccoordinator.cc +++ b/rtengine/improccoordinator.cc @@ -120,7 +120,8 @@ ImProcCoordinator::ImProcCoordinator() chromar(0), lumar(0), sobeler(0), - lastspotdup(false), + lastspotdup(false), + avg(0), retistrsav(nullptr) {} @@ -805,22 +806,21 @@ void ImProcCoordinator::updatePreviewImage(int todo, bool panningRelatedChange) double cont = params.locallab.spots.at(sp).contrast; lhist16loc.clear(); - // Reference parameters computation if (params.locallab.spots.at(sp).spotMethod == "exc") { - ipf.calc_ref(sp, reserv, reserv, 0, 0, pW, pH, scale, huerefblu, huere, chromare, lumare, sobelre, lhist16loc); + ipf.calc_ref(sp, reserv, reserv, 0, 0, pW, pH, scale, huerefblu, huere, chromare, lumare, sobelre, lhist16loc, avg); } else { - ipf.calc_ref(sp, nprevl, nprevl, 0, 0, pW, pH, scale, huerefblu, huere, chromare, lumare, sobelre, lhist16loc); + ipf.calc_ref(sp, nprevl, nprevl, 0, 0, pW, pH, scale, huerefblu, huere, chromare, lumare, sobelre, lhist16loc, avg); } - + printf("improc avg=%f\n", avg); huerblu = huerefblurs[sp] = huerefblu; huer = huerefs[sp] = huere; chromar = chromarefs[sp] = chromare; lumar = lumarefs[sp] = lumare ; sobeler = sobelrefs[sp] = sobelre; CurveFactory::complexCurvelocal(ecomp, black / 65535., hlcompr, hlcomprthresh, shcompr, br, cont, lhist16loc, lumar, - hltonecurveloc, shtonecurveloc, tonecurveloc, lightCurveloc, + hltonecurveloc, shtonecurveloc, tonecurveloc, lightCurveloc, avg, sca); if (locallListener) { diff --git a/rtengine/improccoordinator.h b/rtengine/improccoordinator.h index 9eafc28f1..f703ce3f0 100644 --- a/rtengine/improccoordinator.h +++ b/rtengine/improccoordinator.h @@ -255,6 +255,7 @@ protected: LUTf sobelrefs; double huer, huerblu, chromar, lumar, sobeler; bool lastspotdup; + float avg; diff --git a/rtengine/improcfun.h b/rtengine/improcfun.h index 263576fab..73ed17e2e 100644 --- a/rtengine/improcfun.h +++ b/rtengine/improcfun.h @@ -286,7 +286,7 @@ public: //locallab void MSRLocal(int sp, float** luminance, float** templ, const float* const *originalLuminance, const int width, const int height, const LocallabParams &loc, const int skip, const LocretigainCurve &locRETgainCcurve, const int chrome, const int scall, const float krad, float &minCD, float &maxCD, float &mini, float &maxi, float &Tmean, float &Tsigma, float &Tmin, float &Tmax); // void calc_ref (LabImage* original, LabImage* transformed, int cx, int cy, int oW, int oH, int sk, double &huere, double &chromare, double &lumare, double &sobelref); - void calc_ref(int sp, LabImage* original, LabImage* transformed, int cx, int cy, int oW, int oH, int sk, double &huerefblur, double &huere, double &chromare, double &lumare, double &sobelref, LUTu & histogram); + void calc_ref(int sp, LabImage* original, LabImage* transformed, int cx, int cy, int oW, int oH, int sk, double &huerefblur, double &huere, double &chromare, double &lumare, double &sobelref, LUTu & histogram, float &avg); void copy_ref(LabImage* spotbuffer, LabImage* original, LabImage* transformed, int cx, int cy, int sk, const struct local_params & lp, double &huerefspot, double &chromarefspot, double &lumarefspot); void paste_ref(LabImage* spotbuffer, LabImage* transformed, int cx, int cy, int sk, const struct local_params & lp); void Lab_Local(int call, int sp, float** shbuffer, LabImage* original, LabImage* transformed, LabImage* reserved, int cx, int cy, int oW, int oH, int sk, const LocretigainCurve & locRETgainCcurve, LUTf & lllocalcurve, const LocLHCurve & loclhCurve, const LocHHCurve & lochhCurve, const LocCCmaskCurve & locccmasCurve, const LocLLmaskCurve & locllmasCurve, const LocHHmaskCurve & lochhmasCurve, const LocCCmaskexpCurve & locccmasexpCurve, const LocLLmaskexpCurve & locllmasexpCurve, const LocHHmaskexpCurve & lochhmasexpCurve, bool &LHutili, bool &HHutili, LUTf & cclocalcurve, bool & localskutili, LUTf & sklocalcurve, bool & localexutili, LUTf & exlocalcurve, LUTf & hltonecurve, LUTf & shtonecurve, LUTf & tonecurve, LUTf & lightCurveloc, double & huerefblur, double &hueref, double &chromaref, double &lumaref, double &sobelref); diff --git a/rtengine/iplocallab.cc b/rtengine/iplocallab.cc index b4eff3be4..ee001967a 100644 --- a/rtengine/iplocallab.cc +++ b/rtengine/iplocallab.cc @@ -7136,7 +7136,7 @@ void ImProcFunctions::InverseColorLight_Local(const struct local_params & lp, LU } -void ImProcFunctions::calc_ref(int sp, LabImage * original, LabImage * transformed, int cx, int cy, int oW, int oH, int sk, double & huerefblur, double & hueref, double & chromaref, double & lumaref, double & sobelref, LUTu & histogram) +void ImProcFunctions::calc_ref(int sp, LabImage * original, LabImage * transformed, int cx, int cy, int oW, int oH, int sk, double & huerefblur, double & hueref, double & chromaref, double & lumaref, double & sobelref, LUTu & histogram, float &avg) { if (params->locallab.enabled) { //always calculate hueref, chromaref, lumaref before others operations use in normal mode for all modules exceprt denoise @@ -7146,6 +7146,22 @@ void ImProcFunctions::calc_ref(int sp, LabImage * original, LabImage * transform int begx = lp.xc - lp.lxL; int yEn = lp.yc + lp.ly; int xEn = lp.xc + lp.lx; + float avg2 = 0.f; + int nc2 = 0; + + for (int y = 0; y < transformed->H ; y++) //{ + for (int x = 0; x < transformed->W; x++) { + int lox = cx + x; + int loy = cy + y; + + if (lox >= begx && lox < xEn && loy >= begy && loy < yEn) { + avg2 += original->L[y][x]; + nc2++; + } + } + avg2 /= 32768.f; + avg = avg2/nc2; +// printf("calc avg=%f \n", avg); //claculate histogram for area selected #ifdef _OPENMP const int numThreads = min(max(transformed->W * transformed->H / (int)histogram.getSize(), 1), omp_get_max_threads()); @@ -7154,6 +7170,7 @@ void ImProcFunctions::calc_ref(int sp, LabImage * original, LabImage * transform { LUTu lhist16thrloc(histogram.getSize()); lhist16thrloc.clear(); + histogram.clear(); #ifdef _OPENMP #pragma omp for nowait #endif @@ -10242,7 +10259,6 @@ void ImProcFunctions::Lab_Local(int call, int sp, float** shbuffer, LabImage * o int begx = lp.xc - lp.lxL; int yEn = lp.yc + lp.ly; int xEn = lp.xc + lp.lx; - #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,16) #endif @@ -10257,7 +10273,6 @@ void ImProcFunctions::Lab_Local(int call, int sp, float** shbuffer, LabImage * o // buforigchro[loy - begy][lox - begx] = sqrt(SQR(original->a[y][x]) + SQR(original->b[y][x])); } } - const float radius = 3.f / (sk * 1.4f); int spotSi = 1 + 2 * max(1, lp.cir / sk); diff --git a/rtengine/simpleprocess.cc b/rtengine/simpleprocess.cc index 5b8987bf2..53abafeb2 100644 --- a/rtengine/simpleprocess.cc +++ b/rtengine/simpleprocess.cc @@ -1135,14 +1135,14 @@ private: // Reference parameters computation double huere, chromare, lumare, huerefblu, sobelre; - + float avg = 0.f; if (params.locallab.spots.at(sp).spotMethod == "exc") { - ipf.calc_ref(sp, reservView, reservView, 0, 0, fw, fh, 1, huerefblu, huere, chromare, lumare, sobelre, lhist16loc); + ipf.calc_ref(sp, reservView, reservView, 0, 0, fw, fh, 1, huerefblu, huere, chromare, lumare, sobelre, lhist16loc, avg); } else { - ipf.calc_ref(sp, labView, labView, 0, 0, fw, fh, 1, huerefblu, huere, chromare, lumare, sobelre, lhist16loc); + ipf.calc_ref(sp, labView, labView, 0, 0, fw, fh, 1, huerefblu, huere, chromare, lumare, sobelre, lhist16loc, avg); } CurveFactory::complexCurvelocal(ecomp, black / 65535., hlcompr, hlcomprthresh, shcompr, br, cont, lhist16loc, lumare, - hltonecurveloc, shtonecurveloc, tonecurveloc, lightCurveloc, + hltonecurveloc, shtonecurveloc, tonecurveloc, lightCurveloc, avg, 1); ipf.Lab_Local(2, sp, (float**)shbuffer, labView, labView, reservView, 0, 0, fw, fh, 1, locRETgainCurve, lllocalcurve, loclhCurve, lochhCurve, locccmasCurve, locllmasCurve, lochhmasCurve, locccmasexpCurve, locllmasexpCurve, lochhmasexpCurve,