libcamera/src/ipa/simple/algorithms/awb.cpp
Milan Zamazal 94e849bcf7 libcamera: software_isp: Use RGB type to represent gains
Rather than using a custom struct to represent RGB values, let's use the
corresponding type and its facilities.

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

77 lines
2 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Red Hat Inc.
*
* Auto white balance
*/
#include "awb.h"
#include <numeric>
#include <stdint.h>
#include <libcamera/base/log.h>
#include "libipa/colours.h"
#include "simple/ipa_context.h"
namespace libcamera {
LOG_DEFINE_CATEGORY(IPASoftAwb)
namespace ipa::soft::algorithms {
int Awb::configure(IPAContext &context,
[[maybe_unused]] const IPAConfigInfo &configInfo)
{
auto &gains = context.activeState.awb.gains;
gains = { { 1.0, 1.0, 1.0 } };
return 0;
}
void Awb::process(IPAContext &context,
[[maybe_unused]] const uint32_t frame,
[[maybe_unused]] IPAFrameContext &frameContext,
const SwIspStats *stats,
[[maybe_unused]] ControlList &metadata)
{
const SwIspStats::Histogram &histogram = stats->yHistogram;
const uint8_t blackLevel = context.activeState.blc.level;
/*
* Black level must be subtracted to get the correct AWB ratios, they
* would be off if they were computed from the whole brightness range
* rather than from the sensor range.
*/
const uint64_t nPixels = std::accumulate(
histogram.begin(), histogram.end(), 0);
const uint64_t offset = blackLevel * nPixels;
const uint64_t sumR = stats->sumR_ - offset / 4;
const uint64_t sumG = stats->sumG_ - offset / 2;
const uint64_t sumB = stats->sumB_ - offset / 4;
/*
* Calculate red and blue gains for AWB.
* Clamp max gain at 4.0, this also avoids 0 division.
*/
auto &gains = context.activeState.awb.gains;
gains = { {
sumR <= sumG / 4 ? 4.0 : static_cast<double>(sumG) / sumR,
1.0,
sumB <= sumG / 4 ? 4.0 : static_cast<double>(sumG) / sumB,
} };
RGB<double> rgbGains{ { 1 / gains.r(), 1 / gains.g(), 1 / gains.b() } };
context.activeState.awb.temperatureK = estimateCCT(rgbGains);
LOG(IPASoftAwb, Debug)
<< "gain R/B: " << gains << "; temperature: "
<< context.activeState.awb.temperatureK;
}
REGISTER_IPA_ALGORITHM(Awb, "Awb")
} /* namespace ipa::soft::algorithms */
} /* namespace libcamera */