summaryrefslogtreecommitdiff
path: root/src/ipa/raspberrypi/controller/rpi/geq.cpp
blob: 106f0a40dfc13a93b5d7c63a123505130c16137b (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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi Ltd
 *
 * geq.cpp - GEQ (green equalisation) control algorithm
 */

#include <libcamera/base/log.h>

#include "../device_status.h"
#include "../lux_status.h"
#include "../pwl.h"

#include "geq.h"

using namespace RPiController;
using namespace libcamera;

LOG_DEFINE_CATEGORY(RPiGeq)

/*
 * We use the lux status so that we can apply stronger settings in darkness (if
 * necessary).
 */

#define NAME "rpi.geq"

Geq::Geq(Controller *controller)
	: Algorithm(controller)
{
}

char const *Geq::name() const
{
	return NAME;
}

int Geq::read(boost::property_tree::ptree const &params)
{
	config_.offset = params.get<uint16_t>("offset", 0);
	config_.slope = params.get<double>("slope", 0.0);
	if (config_.slope < 0.0 || config_.slope >= 1.0) {
		LOG(RPiGeq, Error) << "Bad slope value";
		return -EINVAL;
	}

	if (params.get_child_optional("strength")) {
		int ret = config_.strength.read(params.get_child("strength"));
		if (ret)
			return ret;
	}

	return 0;
}

void Geq::prepare(Metadata *imageMetadata)
{
	LuxStatus luxStatus = {};
	luxStatus.lux = 400;
	if (imageMetadata->get("lux.status", luxStatus))
		LOG(RPiGeq, Warning) << "no lux data found";
	DeviceStatus deviceStatus;
	deviceStatus.analogueGain = 1.0; /* in case not found */
	if (imageMetadata->get("device.status", deviceStatus))
		LOG(RPiGeq, Warning)
			<< "no device metadata - use analogue gain of 1x";
	GeqStatus geqStatus = {};
	double strength = config_.strength.empty()
			? 1.0
			: config_.strength.eval(config_.strength.domain().clip(luxStatus.lux));
	strength *= deviceStatus.analogueGain;
	double offset = config_.offset * strength;
	double slope = config_.slope * strength;
	geqStatus.offset = std::min(65535.0, std::max(0.0, offset));
	geqStatus.slope = std::min(.99999, std::max(0.0, slope));
	LOG(RPiGeq, Debug)
		<< "offset " << geqStatus.offset << " slope "
		<< geqStatus.slope << " (analogue gain "
		<< deviceStatus.analogueGain << " lux "
		<< luxStatus.lux << ")";
	imageMetadata->set("geq.status", geqStatus);
}

/* Register algorithm with the system. */
static Algorithm *create(Controller *controller)
{
	return (Algorithm *)new Geq(controller);
}
static RegisterAlgorithm reg(NAME, &create);