Show black level adjustments in file browser

File browser thumbnails for raw images start with a minimally-processed
images. These images are cached and image adjustments are applied on
top. The black level is "baked-into" the cached image. Therefore, to
reflect the black level adjustments in the thumbnail, one of two options
are required:
    1. Cache an image before the black level is applied and process the
       black level on top of this image.
    2. Recreate the base image with the new black level and cache it.
The first option yields better performance when the user changes the
black level. However, it requires other base adjustments to be applied
every time, such as the camera multipliers. The second option requires
the base image to be recreated every time the black level is changed.
This commit implements the second option. It minimizes code changes, and
therefore possible bugs. It does add a performance penalty when the
black level changes, but the black level adjustment is rarely used.
This commit is contained in:
Lawrence Lee
2023-07-29 17:37:13 -07:00
parent b68a6e9581
commit 6787c53c9b
19 changed files with 141 additions and 34 deletions

View File

@@ -16,6 +16,8 @@
* You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <array>
#include <clocale>
#include <lcms2.h>
@@ -65,6 +67,84 @@ bool checkRawImageThumb (const rtengine::RawImage& raw_image)
return raw_image.get_thumbOffset() + length <= raw_image.get_file()->size;
}
/**
* Apply the black level adjustments in the processing parameters.
*
* @param cblack The original black levels that will be modified.
* @param sensorType Sensor type.
* @param rawParams Subset of processing parameters for raw data.
*/
void adjustBlackLevels(float cblack[4], rtengine::eSensorType sensorType, const rtengine::RAWParams *rawParams)
{
if (!rawParams) {
return;
}
std::array<float, 4> black_adjust{0.f, 0.f, 0.f, 0.f};
switch (sensorType) {
case rtengine::eSensorType::ST_BAYER:
case rtengine::eSensorType::ST_FOVEON:
black_adjust[0] = static_cast<float>(rawParams->bayersensor.black1); // R
black_adjust[1] = static_cast<float>(rawParams->bayersensor.black0); // G1
black_adjust[2] = static_cast<float>(rawParams->bayersensor.black2); // B
black_adjust[3] = static_cast<float>(rawParams->bayersensor.black3); // G2
break;
case rtengine::eSensorType::ST_FUJI_XTRANS:
black_adjust[0] = static_cast<float>(rawParams->xtranssensor.blackred);
black_adjust[1] = static_cast<float>(rawParams->xtranssensor.blackgreen);
black_adjust[2] = static_cast<float>(rawParams->xtranssensor.blackblue);
black_adjust[3] = static_cast<float>(rawParams->xtranssensor.blackgreen);
break;
case rtengine::eSensorType::ST_NONE:
break;
}
for (int i = 0; i < black_adjust.size(); i++) {
cblack[i] = std::max(0.f, cblack[i] + black_adjust[i]);
}
}
/**
* Calculate the new scale multipliers based on new black levels.
*
* @param scale_mul The original scale multipliers to be adjusted.
* @param pre_mul Pre-multipliers.
* @param c_black Updated black levels.
* @param isMono Is the image using mono demosaicing?
* @param ri Pointer to the raw image.
*/
void calculate_scale_mul(float scale_mul[4], const float pre_mul_[4], const float c_black[4], bool isMono, const rtengine::RawImage *ri)
{
std::array<float, 4> c_white;
for (int i = 0; i < c_white.size(); ++i) {
c_white[i] = static_cast<float>(ri->get_white(i));
}
if (isMono || ri->get_colors() == 1) {
for (int c = 0; c < 4; c++) {
scale_mul[c] = 65535.f / (c_white[c] - c_black[c]);
}
} else {
std::array<float, 4> pre_mul;
for (int c = 0; c < 4; c++) {
pre_mul[c] = pre_mul_[c];
}
if (pre_mul[3] == 0) {
pre_mul[3] = pre_mul[1]; // G2 == G1
}
float maxpremul = std::max(std::max(std::max(pre_mul[0], pre_mul[1]), pre_mul[2]), pre_mul[3]);
for (int c = 0; c < 4; c++) {
scale_mul[c] = (pre_mul[c] / maxpremul) * 65535.f / (c_white[c] - c_black[c]);
}
}
}
void scale_colors (rtengine::RawImage *ri, float scale_mul[4], float cblack[4], bool multiThread)
{
@@ -544,7 +624,7 @@ RawMetaDataLocation Thumbnail::loadMetaDataFromRaw (const Glib::ustring& fname)
return rml;
}
Thumbnail* Thumbnail::loadFromRaw (const Glib::ustring& fname, RawMetaDataLocation& rml, eSensorType &sensorType, int &w, int &h, int fixwh, double wbEq, StandardObserver wbObserver, bool rotate, bool forHistogramMatching)
Thumbnail* Thumbnail::loadFromRaw (const Glib::ustring& fname, RawMetaDataLocation& rml, eSensorType &sensorType, int &w, int &h, int fixwh, double wbEq, StandardObserver wbObserver, bool rotate, const RAWParams *rawParams, bool forHistogramMatching)
{
RawImage *ri = new RawImage (fname);
unsigned int tempImageNum = 0;
@@ -587,8 +667,15 @@ Thumbnail* Thumbnail::loadFromRaw (const Glib::ustring& fname, RawMetaDataLocati
tpp->greenMultiplier = ri->get_pre_mul (1);
tpp->blueMultiplier = ri->get_pre_mul (2);
bool isMono =
(ri->getSensorType() == ST_FUJI_XTRANS &&
rawParams->xtranssensor.method == RAWParams::XTransSensor::getMethodString(RAWParams::XTransSensor::Method::MONO)) ||
(ri->getSensorType() == ST_BAYER &&
rawParams->bayersensor.method == RAWParams::BayerSensor::getMethodString(RAWParams::BayerSensor::Method::MONO));
float pre_mul[4], scale_mul[4], cblack[4];
ri->get_colorsCoeff (pre_mul, scale_mul, cblack, false);
adjustBlackLevels(cblack, sensorType, rawParams);
calculate_scale_mul(scale_mul, pre_mul, cblack, isMono, ri);
scale_colors (ri, scale_mul, cblack, forHistogramMatching); // enable multithreading when forHistogramMatching is true
ri->pre_interpolate();