# SPDX-License-Identifier: CC0-1.0 libcamera_gst_sources = [ 'gstlibcamera-utils.cpp', 'gstlibcamera.cpp', 'gstlibcameraallocator.cpp', 'gstlibcamerapad.cpp', 'gstlibcamerapool.cpp', 'gstlibcameraprovider.cpp', 'gstlibcamerasrc.cpp', ] libcamera_gst_cpp_args = [ '-DVERSION="@0@"'.format(libcamera_git_version), '-DPACKAGE="@0@"'.format(meson.project_name()), ] glib_dep = dependency('glib-2.0', required : get_option('gstreamer')) gst_dep_version = '>=1.14.0' gstvideo_dep = dependency('gstreamer-video-1.0', version : gst_dep_version, required : get_option('gstreamer')) gstallocator_dep = dependency('gstreamer-allocators-1.0', version : gst_dep_version, required : get_option('gstreamer')) if glib_dep.found() and gstvideo_dep.found() and gstallocator_dep.found() # The G_DECLARE_FINAL_TYPE macro creates static inline functions that were # not marked as possibly unused prior to GLib v2.63.0. This causes clang to # complain about the ones we are not using. Silence the -Wunused-function # warning in that case. if cc.get_id() == 'clang' and glib_dep.version().version_compare('<2.63.0') libcamera_gst_cpp_args += [ '-Wno-unused-function' ] endif libcamera_gst = shared_library('gstlibcamera', libcamera_gst_sources, cpp_args : libcamera_gst_cpp_args, dependencies : [libcamera_dep, gstvideo_dep, gstallocator_dep], install: true, install_dir : '@0@/gstreamer-1.0'.format(get_option('libdir')), ) endif '/libcamera/vivid.git/tree/src/ipa/raspberrypi/controller/rpi/contrast.cpp?h=pobrn/rebase&id=7e91f951604c17a091b8c05efa1d01eddbcdfdbf'>treecommitdiff
path: root/src/ipa/raspberrypi/controller/rpi/contrast.cpp
blob: ae55aad567399772be70c7edfc163902f591e1d3 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi (Trading) Limited
 *
 * contrast.cpp - contrast (gamma) control algorithm
 */
#include <stdint.h>

#include <libcamera/base/log.h>

#include "../contrast_status.h"
#include "../histogram.hpp"

#include "contrast.hpp"

using namespace RPiController;
using namespace libcamera;

LOG_DEFINE_CATEGORY(RPiContrast)

// This is a very simple control algorithm which simply retrieves the results of
// AGC and AWB via their "status" metadata, and applies digital gain to the
// colour channels in accordance with those instructions. We take care never to
// apply less than unity gains, as that would cause fully saturated pixels to go
// off-white.

#define NAME "rpi.contrast"

Contrast::Contrast(Controller *controller)
	: ContrastAlgorithm(controller), brightness_(0.0), contrast_(1.0)
{
}

char const *Contrast::Name() const
{
	return NAME;
}

void Contrast::Read(boost::property_tree::ptree const &params)
{
	// enable adaptive enhancement by default
	config_.ce_enable = params.get<int>("ce_enable", 1);
	// the point near the bottom of the histogram to move
	config_.lo_histogram = params.get<double>("lo_histogram", 0.01);
	// where in the range to try and move it to
	config_.lo_level = params.get<double>("lo_level", 0.015);
	// but don't move by more than this
	config_.lo_max = params.get<double>("lo_max", 500);
	// equivalent values for the top of the histogram...
	config_.hi_histogram = params.get<double>("hi_histogram", 0.95);
	config_.hi_level = params.get<double>("hi_level", 0.95);
	config_.hi_max = params.get<double>("hi_max", 2000);
	config_.gamma_curve.Read(params.get_child("gamma_curve"));
}

void Contrast::SetBrightness(double brightness)
{
	brightness_ = brightness;
}

void Contrast::SetContrast(double contrast)
{
	contrast_ = contrast;
}

static void fill_in_status(ContrastStatus &status, double brightness,
			   double contrast, Pwl &gamma_curve)
{
	status.brightness = brightness;
	status.contrast = contrast;
	for (int i = 0; i < CONTRAST_NUM_POINTS - 1; i++) {
		int x = i < 16 ? i * 1024
			       : (i < 24 ? (i - 16) * 2048 + 16384
					 : (i - 24) * 4096 + 32768);
		status.points[i].x = x;
		status.points[i].y = std::min(65535.0, gamma_curve.Eval(x));
	}
	status.points[CONTRAST_NUM_POINTS - 1].x = 65535;
	status.points[CONTRAST_NUM_POINTS - 1].y = 65535;
}

void Contrast::Initialise()
{
	// Fill in some default values as Prepare will run before Process gets
	// called.
	fill_in_status(status_, brightness_, contrast_, config_.gamma_curve);
}

void Contrast::Prepare(Metadata *image_metadata)
{
	std::unique_lock<std::mutex> lock(mutex_);
	image_metadata->Set("contrast.status", status_);
}

Pwl compute_stretch_curve(Histogram const &histogram,
			  ContrastConfig const &config)
{
	Pwl enhance;
	enhance.Append(0, 0);
	// If the start of the histogram is rather empty, try to pull it down a
	// bit.
	double hist_lo = histogram.Quantile(config.lo_histogram) *
			 (65536 / NUM_HISTOGRAM_BINS);
	double level_lo = config.lo_level * 65536;
	LOG(RPiContrast, Debug)
		<< "Move histogram point " << hist_lo << " to " << level_lo;