Files
external_libcamera/src/ipa/simple/algorithms/lut.cpp
Milan Zamazal 148ac13043 libcamera: software_isp: lut: Remove maybe_unused on a used argument
`params' argument of Lut::prepare is actually used, let's remove
maybe_unused from it.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2025-03-26 10:45:01 +00:00

118 lines
3.7 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Red Hat Inc.
*
* Color lookup tables construction
*/
#include "lut.h"
#include <algorithm>
#include <cmath>
#include <optional>
#include <stdint.h>
#include <libcamera/base/log.h>
#include <libcamera/control_ids.h>
#include "simple/ipa_context.h"
namespace libcamera {
LOG_DEFINE_CATEGORY(IPASoftLut)
namespace ipa::soft::algorithms {
int Lut::init(IPAContext &context,
[[maybe_unused]] const YamlObject &tuningData)
{
context.ctrlMap[&controls::Contrast] = ControlInfo(0.0f, 2.0f, 1.0f);
return 0;
}
int Lut::configure(IPAContext &context,
[[maybe_unused]] const IPAConfigInfo &configInfo)
{
/* Gamma value is fixed */
context.configuration.gamma = 0.5;
context.activeState.knobs.contrast = std::optional<double>();
updateGammaTable(context);
return 0;
}
void Lut::queueRequest(typename Module::Context &context,
[[maybe_unused]] const uint32_t frame,
[[maybe_unused]] typename Module::FrameContext &frameContext,
const ControlList &controls)
{
const auto &contrast = controls.get(controls::Contrast);
if (contrast.has_value()) {
context.activeState.knobs.contrast = contrast;
LOG(IPASoftLut, Debug) << "Setting contrast to " << contrast.value();
}
}
void Lut::updateGammaTable(IPAContext &context)
{
auto &gammaTable = context.activeState.gamma.gammaTable;
const auto blackLevel = context.activeState.blc.level;
const unsigned int blackIndex = blackLevel * gammaTable.size() / 256;
const auto contrast = context.activeState.knobs.contrast.value_or(1.0);
std::fill(gammaTable.begin(), gammaTable.begin() + blackIndex, 0);
const float divisor = gammaTable.size() - blackIndex - 1.0;
for (unsigned int i = blackIndex; i < gammaTable.size(); i++) {
double normalized = (i - blackIndex) / divisor;
/* Convert 0..2 to 0..infinity; avoid actual inifinity at tan(pi/2) */
double contrastExp = tan(std::clamp(contrast * M_PI_4, 0.0, M_PI_2 - 0.00001));
/* Apply simple S-curve */
if (normalized < 0.5)
normalized = 0.5 * std::pow(normalized / 0.5, contrastExp);
else
normalized = 1.0 - 0.5 * std::pow((1.0 - normalized) / 0.5, contrastExp);
gammaTable[i] = UINT8_MAX *
std::pow(normalized, context.configuration.gamma);
}
context.activeState.gamma.blackLevel = blackLevel;
context.activeState.gamma.contrast = contrast;
}
void Lut::prepare(IPAContext &context,
[[maybe_unused]] const uint32_t frame,
[[maybe_unused]] IPAFrameContext &frameContext,
DebayerParams *params)
{
/*
* Update the gamma table if needed. This means if black level changes
* and since the black level gets updated only if a lower value is
* observed, it's not permanently prone to minor fluctuations or
* rounding errors.
*/
if (context.activeState.gamma.blackLevel != context.activeState.blc.level ||
context.activeState.gamma.contrast != context.activeState.knobs.contrast)
updateGammaTable(context);
auto &gains = context.activeState.awb.gains;
auto &gammaTable = context.activeState.gamma.gammaTable;
const unsigned int gammaTableSize = gammaTable.size();
for (unsigned int i = 0; i < DebayerParams::kRGBLookupSize; i++) {
const double div = static_cast<double>(DebayerParams::kRGBLookupSize) /
gammaTableSize;
/* Apply gamma after gain! */
const RGB<double> lutGains = (gains * i / div).min(gammaTableSize - 1);
params->red[i] = gammaTable[static_cast<unsigned int>(lutGains.r())];
params->green[i] = gammaTable[static_cast<unsigned int>(lutGains.g())];
params->blue[i] = gammaTable[static_cast<unsigned int>(lutGains.b())];
}
}
REGISTER_IPA_ALGORITHM(Lut, "Lut")
} /* namespace ipa::soft::algorithms */
} /* namespace libcamera */