summaryrefslogtreecommitdiff
path: root/src/ipa/rkisp1/algorithms/cproc.cpp
blob: bca5ab6907d64cc12d27097bad2b536e185782ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2021-2022, Ideas On Board
 *
 * cproc.cpp - RkISP1 Color Processing control
 */

#include "cproc.h"

#include <algorithm>
#include <cmath>

#include <libcamera/base/log.h>

#include <libcamera/control_ids.h>

/**
 * \file cproc.h
 */

namespace libcamera {

namespace ipa::rkisp1::algorithms {

/**
 * \class ColorProcessing
 * \brief RkISP1 Color Processing control
 *
 * The ColorProcessing algorithm is responsible for applying brightness,
 * contrast and saturation corrections. The values are directly provided
 * through requests by the corresponding controls.
 */

LOG_DEFINE_CATEGORY(RkISP1CProc)

/**
 * \copydoc libcamera::ipa::Algorithm::queueRequest
 */
void ColorProcessing::queueRequest(IPAContext &context,
				   [[maybe_unused]] const uint32_t frame,
				   const ControlList &controls)
{
	auto &cproc = context.frameContext.cproc;

	const auto &brightness = controls.get(controls::Brightness);
	if (brightness) {
		cproc.brightness = std::clamp<int>(std::lround(*brightness * 128), -128, 127);
		cproc.updateParams = true;

		LOG(RkISP1CProc, Debug) << "Set brightness to " << *brightness;
	}

	const auto &contrast = controls.get(controls::Contrast);
	if (contrast) {
		cproc.contrast = std::clamp<int>(std::lround(*contrast * 128), 0, 255);
		cproc.updateParams = true;

		LOG(RkISP1CProc, Debug) << "Set contrast to " << *contrast;
	}

	const auto saturation = controls.get(controls::Saturation);
	if (saturation) {
		cproc.saturation = std::clamp<int>(std::lround(*saturation * 128), 0, 255);
		cproc.updateParams = true;

		LOG(RkISP1CProc, Debug) << "Set saturation to " << *saturation;
	}
}

/**
 * \copydoc libcamera::ipa::Algorithm::prepare
 */
void ColorProcessing::prepare(IPAContext &context,
			      rkisp1_params_cfg *params)
{
	auto &cproc = context.frameContext.cproc;

	/* Check if the algorithm configuration has been updated. */
	if (!cproc.updateParams)
		return;

	cproc.updateParams = false;

	params->others.cproc_config.brightness = cproc.brightness;
	params->others.cproc_config.contrast = cproc.contrast;
	params->others.cproc_config.sat = cproc.saturation;

	params->module_en_update |= RKISP1_CIF_ISP_MODULE_CPROC;
	params->module_ens |= RKISP1_CIF_ISP_MODULE_CPROC;
	params->module_cfg_update |= RKISP1_CIF_ISP_MODULE_CPROC;
}

REGISTER_IPA_ALGORITHM(ColorProcessing, "ColorProcessing")

} /* namespace ipa::rkisp1::algorithms */

} /* namespace libcamera */