From de602fa3915e3ef652d12e613feafe0432e94801 Mon Sep 17 00:00:00 2001 From: Oliver Duis Date: Tue, 20 Sep 2011 19:22:53 +0200 Subject: [PATCH] Fixed blend matrix does not work correctly/causes crashes, new V2 see issue 1010 --- rtengine/imagefloat.cc | 7 ------ rtengine/imagefloat.h | 1 - rtengine/rawimagesource.cc | 47 ++++++++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/rtengine/imagefloat.cc b/rtengine/imagefloat.cc index 9b7264286..5fb02d6f4 100644 --- a/rtengine/imagefloat.cc +++ b/rtengine/imagefloat.cc @@ -36,13 +36,6 @@ Imagefloat::Imagefloat (int w, int h) allocate (w, h); } -// Copy other image -Imagefloat::Imagefloat (Imagefloat& other) { - unaligned=NULL; data=NULL; - allocate (other.width, other.height); - memcpy(data, other.data, width*height*3); -} - Imagefloat::~Imagefloat () { if (data!=NULL) { diff --git a/rtengine/imagefloat.h b/rtengine/imagefloat.h index 26a54dbbf..011378ed0 100644 --- a/rtengine/imagefloat.h +++ b/rtengine/imagefloat.h @@ -53,7 +53,6 @@ class Imagefloat : public ImageIO, public IImagefloat { Imagefloat (); Imagefloat (int width, int height); - Imagefloat (Imagefloat& other); ~Imagefloat (); Imagefloat* copy (); diff --git a/rtengine/rawimagesource.cc b/rtengine/rawimagesource.cc index 8f45796bb..7f3eb492c 100644 --- a/rtengine/rawimagesource.cc +++ b/rtengine/rawimagesource.cc @@ -1627,7 +1627,7 @@ void RawImageSource::colorSpaceConversion (Imagefloat* im, ColorManagementParams } } else { Imagefloat* imgPreLCMS=NULL; - if (cmp.blendCMSMatrix) imgPreLCMS=new Imagefloat(*im); + if (cmp.blendCMSMatrix) imgPreLCMS=im->copy(); // use supplied input profile // color space transform is expecting data in the range (0,1) @@ -1661,6 +1661,7 @@ void RawImageSource::colorSpaceConversion (Imagefloat* im, ColorManagementParams cmsDeleteTransform(hTransform); // restore normalization to the range (0,65535) and blend matrix colors if LCMS is clipping + const float RecoverTresh = 65535.0 * 0.98; // just the last few percent highlights are merged #pragma omp parallel for for ( int h = 0; h < im->height; ++h ) for ( int w = 0; w < im->width; ++w ) { @@ -1670,21 +1671,43 @@ void RawImageSource::colorSpaceConversion (Imagefloat* im, ColorManagementParams if (cmp.blendCMSMatrix) { // Red - if (im->r[h][w]>65534.9) { - float f = mat[0][0]*imgPreLCMS->r[h][w] + mat[0][1]*imgPreLCMS->g[h][w] + mat[0][2]*imgPreLCMS->b[h][w]; - if (f>im->r[h][w]) im->r[h][w]=f; - } + float red=im->r[h][w]; + if (red>RecoverTresh) { + float matrixRed = mat[0][0]*imgPreLCMS->r[h][w] + mat[0][1]*imgPreLCMS->g[h][w] + mat[0][2]*imgPreLCMS->b[h][w]; - // Green - if (im->g[h][w]>65534.9) { - float f = mat[1][0]*imgPreLCMS->r[h][w] + mat[1][1]*imgPreLCMS->g[h][w] + mat[1][2]*imgPreLCMS->b[h][w]; - if (f>im->g[h][w]) im->g[h][w]=f; + if (red>=65535.0) + im->r[h][w] = matrixRed; + else { + float fac = (red - RecoverTresh) / (65535.0 - RecoverTresh); + im->r[h][w] = (1.0-fac) * red + fac * matrixRed; + } } + // Green + float green=im->g[h][w]; + if (green>RecoverTresh) { + float matrixGreen = mat[1][0]*imgPreLCMS->r[h][w] + mat[1][1]*imgPreLCMS->g[h][w] + mat[1][2]*imgPreLCMS->b[h][w]; + + if (green>=65535.0) + im->g[h][w] = matrixGreen; + else { + float fac = (green - RecoverTresh) / (65535.0 - RecoverTresh); + im->g[h][w] = (1.0-fac) * green + fac * matrixGreen; + } + } + + // Blue - if (im->b[h][w]>65534.9) { - float f = mat[2][0]*imgPreLCMS->r[h][w] + mat[2][1]*imgPreLCMS->g[h][w] + mat[2][2]*imgPreLCMS->b[h][w]; - if (f>im->b[h][w]) im->b[h][w]=f; + float blue=im->b[h][w]; + if (blue>RecoverTresh) { + float matrixBlue = mat[2][0]*imgPreLCMS->r[h][w] + mat[2][1]*imgPreLCMS->g[h][w] + mat[2][2]*imgPreLCMS->b[h][w]; + + if (blue>=65535.0) + im->b[h][w] = matrixBlue; + else { + float fac = (blue - RecoverTresh) / (65535.0 - RecoverTresh); + im->b[h][w] = (1.0-fac) * blue + fac * matrixBlue; + } } } }