From 7f1d6d67bc9bbc521de39f5990bfc60b19dc249e Mon Sep 17 00:00:00 2001 From: heckflosse Date: Fri, 3 Aug 2018 17:43:37 +0200 Subject: [PATCH] ahd demosaic review changes --- rtengine/ahd_demosaic_RT.cc | 67 ++++++++++++++++++------------------- rtengine/demosaic_algos.cc | 35 ------------------- rtengine/rawimagesource.h | 1 - 3 files changed, 32 insertions(+), 71 deletions(-) diff --git a/rtengine/ahd_demosaic_RT.cc b/rtengine/ahd_demosaic_RT.cc index 75e3af970..a25ca1363 100644 --- a/rtengine/ahd_demosaic_RT.cc +++ b/rtengine/ahd_demosaic_RT.cc @@ -23,6 +23,7 @@ // Optimized for speed and reduced memory usage 2018 Ingo Weyrich // +#include #include "rtengine.h" #include "rawimagesource.h" #include "rt_math.h" @@ -52,14 +53,14 @@ void RawImageSource::ahd_demosaic() constexpr float d65_white[3] = { 0.950456, 1, 1.088754 }; - volatile double progress = 0.0; + double progress = 0.0; if (plistener) { plistener->setProgressStr (Glib::ustring::compose(M("TP_RAW_DMETHOD_PROGRESSBAR"), RAWParams::BayerSensor::getMethodString(RAWParams::BayerSensor::Method::AHD))); plistener->setProgress (0.0); } - for (int i = 0; i < 0x10000; i++) { - double r = (double)i / 65535.0; + for (int i = 0; i < 65536; i++) { + const double r = i / 65535.0; cbrt[i] = r > 0.008856 ? std::cbrt(r) : 7.787 * r + 16 / 116.0; } @@ -78,13 +79,10 @@ void RawImageSource::ahd_demosaic() #endif { int progresscounter = 0; - float (*pix), (*rix)[3]; - float ldiff[2][4], abdiff[2][4]; - float (*lix)[3]; - char *buffer = (char *) malloc (12 * TS * TS * sizeof(float) + 2 * TS * TS * sizeof(uint16_t)); /* 1053 kB per core */ - float (*rgb)[TS][TS][3] = (float(*)[TS][TS][3]) buffer; - float (*lab)[TS][TS][3] = (float(*)[TS][TS][3])(buffer + 6 * TS * TS * sizeof(float)); - uint16_t (*homo)[TS][TS] = (uint16_t(*)[TS][TS])(buffer + 12 * TS * TS * sizeof(float)); + float *buffer = new float[13 * TS * TS]; /* 1053 kB per core */ + auto rgb = (float(*)[TS][TS][3]) buffer; + auto lab = (float(*)[TS][TS][3])(buffer + 6 * TS * TS); + auto homo = (uint16_t(*)[TS][TS])(buffer + 12 * TS * TS); #ifdef _OPENMP #pragma omp for collapse(2) schedule(dynamic) nowait @@ -94,7 +92,7 @@ void RawImageSource::ahd_demosaic() // Interpolate green horizontally and vertically: for (int row = top; row < top + TS && row < height - 2; row++) { for (int col = left + (FC(row, left) & 1); col < std::min(left + TS, width - 2); col += 2) { - pix = &(rawData[row][col]); + auto pix = &rawData[row][col]; float val0 = 0.25f * ((pix[-1] + pix[0] + pix[1]) * 2 - pix[-2] - pix[2]) ; rgb[0][row - top][col - left][1] = median(val0, pix[-1], pix[1]); @@ -109,9 +107,9 @@ void RawImageSource::ahd_demosaic() for (int row = top + 1; row < top + TS - 1 && row < height - 3; row++) { int cng = FC(row + 1, FC(row + 1, 0) & 1); for (int col = left + 1; col < std::min(left + TS - 1, width - 3); col++) { - pix = &(rawData[row][col]); - rix = &rgb[d][row - top][col - left]; - lix = &lab[d][row - top][col - left]; + auto pix = &rawData[row][col]; + auto rix = &rgb[d][row - top][col - left]; + auto lix = lab[d][row - top][col - left]; if (FC(row, col) == 1) { rix[0][2 - cng] = CLIP(pix[0] + (0.5f * (pix[-1] + pix[1] - rix[-1][1] - rix[1][1] ) )); @@ -125,23 +123,21 @@ void RawImageSource::ahd_demosaic() - rix[+TS - 1][1] - rix[+TS + 1][1]))); rix[0][2 - cng] = pix[0]; } - float xyz0 = 0.f; - float xyz1 = 0.f; - float xyz2 = 0.f; + float xyz[3] = {}; for(unsigned int c = 0; c < 3; ++c) { - xyz0 += xyz_cam[0][c] * rix[0][c]; - xyz1 += xyz_cam[1][c] * rix[0][c]; - xyz2 += xyz_cam[2][c] * rix[0][c]; + xyz[0] += xyz_cam[0][c] * rix[0][c]; + xyz[1] += xyz_cam[1][c] * rix[0][c]; + xyz[2] += xyz_cam[2][c] * rix[0][c]; } - xyz0 = cbrt[xyz0]; - xyz1 = cbrt[xyz1]; - xyz2 = cbrt[xyz2]; + xyz[0] = cbrt[xyz[0]]; + xyz[1] = cbrt[xyz[1]]; + xyz[2] = cbrt[xyz[2]]; - lix[0][0] = 116.f * xyz1 - 16.f; - lix[0][1] = 500.f * (xyz0 - xyz1); - lix[0][2] = 200.f * (xyz1 - xyz2); + lix[0] = 116.f * xyz[1] - 16.f; + lix[1] = 500.f * (xyz[0] - xyz[1]); + lix[2] = 200.f * (xyz[1] - xyz[2]); } } @@ -149,10 +145,11 @@ void RawImageSource::ahd_demosaic() for (int row = top + 2; row < top + TS - 2 && row < height - 4; row++) { int tr = row - top; + float ldiff[2][4], abdiff[2][4]; for (int col = left + 2, tc = 2; col < left + TS - 2 && col < width - 4; col++, tc++) { for (int d = 0; d < 2; d++) { - lix = &lab[d][tr][tc]; + auto lix = &lab[d][tr][tc]; for (int i = 0; i < 4; i++) { ldiff[d][i] = std::fabs(lix[0][0] - lix[dir[i]][0]); @@ -161,15 +158,15 @@ void RawImageSource::ahd_demosaic() } } - float leps = min(max(ldiff[0][0], ldiff[0][1]), - max(ldiff[1][2], ldiff[1][3])); - float abeps = min(max(abdiff[0][0], abdiff[0][1]), - max(abdiff[1][2], abdiff[1][3])); + float leps = std::min(std::max(ldiff[0][0], ldiff[0][1]), + std::max(ldiff[1][2], ldiff[1][3])); + float abeps = std::min(std::max(abdiff[0][0], abdiff[0][1]), + std::max(abdiff[1][2], abdiff[1][3])); for (int d = 0; d < 2; d++) { homo[d][tr][tc] = 0; for (int i = 0; i < 4; i++) { - homo[d][tr][tc] += ((ldiff[d][i] <= leps) * (abdiff[d][i] <= abeps)); + homo[d][tr][tc] += (ldiff[d][i] <= leps) * (abdiff[d][i] <= abeps); } } } @@ -208,8 +205,8 @@ void RawImageSource::ahd_demosaic() #pragma omp critical (ahdprogress) #endif { - progress += (double)32 * ((TS - 32) * (TS - 32)) / (height * width); - progress = progress > 1.0 ? 1.0 : progress; + progress += 32.0 * SQR(TS - 32) / (height * width); + progress = std::min(progress, 1.0); plistener->setProgress(progress); } } @@ -217,7 +214,7 @@ void RawImageSource::ahd_demosaic() } } - free (buffer); + delete [] buffer; } if(plistener) { plistener->setProgress (1.0); diff --git a/rtengine/demosaic_algos.cc b/rtengine/demosaic_algos.cc index 5955c758b..78b5490b5 100644 --- a/rtengine/demosaic_algos.cc +++ b/rtengine/demosaic_algos.cc @@ -682,41 +682,6 @@ void RawImageSource::ppg_demosaic() free (image); } -void RawImageSource::border_interpolate(unsigned int border, float (*image)[3], unsigned int start, unsigned int end) -{ - unsigned row, col, y, x, f; - float sum[8]; - unsigned int width = W, height = H; - unsigned int colors = 3; - - if (end == 0 ) { - end = H; - } - - for (row = start; row < end; row++) - for (col = 0; col < width; col++) { - if (col == border && row >= border && row < height - border) { - col = width - border; - } - - memset (sum, 0, sizeof sum); - - for (y = row - 1; y != row + 2; y++) - for (x = col - 1; x != col + 2; x++) - if (y < height && x < width) { - f = fc(y, x); - sum[f] += image[y * width + x][f]; - sum[f + 4]++; - } - - f = fc(row, col); - - FORCC if (c != f && sum[c + 4]) { - image[row * width + col][c] = sum[c] / sum[c + 4]; - } - } -} - void RawImageSource::border_interpolate(unsigned int border, float (*image)[4], unsigned int start, unsigned int end) { unsigned row, col, y, x, f; diff --git a/rtengine/rawimagesource.h b/rtengine/rawimagesource.h index aff7f4445..53d3e0dc1 100644 --- a/rtengine/rawimagesource.h +++ b/rtengine/rawimagesource.h @@ -273,7 +273,6 @@ protected: void dcb_demosaic(int iterations, bool dcb_enhance); void ahd_demosaic(); void rcd_demosaic(); - void border_interpolate(unsigned int border, float (*image)[3], unsigned int start = 0, unsigned int end = 0); void border_interpolate(unsigned int border, float (*image)[4], unsigned int start = 0, unsigned int end = 0); void border_interpolate2(int winw, int winh, int lborders, const array2D &rawData, array2D &red, array2D &green, array2D &blue); void dcb_initTileLimits(int &colMin, int &rowMin, int &colMax, int &rowMax, int x0, int y0, int border);