/* SPDX-License-Identifier: BSD-2-Clause */ /* * Copyright (C) 2019, Raspberry Pi Ltd * * histogram calculations */ #include "histogram.h" #include #include /** * \file histogram.h * \brief Class to represent Histograms and manipulate them */ namespace libcamera { namespace ipa { /** * \class Histogram * \brief The base class for creating histograms * * This class stores a cumulative frequency histogram, which is a mapping that * counts the cumulative number of observations in all of the bins up to the * specified bin. It can be used to find quantiles and averages between quantiles. */ /** * \fn Histogram::Histogram() * \brief Construct an empty Histogram * * This empty constructor exists largely to allow Histograms to be embedded in * other classes which may be created before the contents of the Histogram are * known. */ /** * \brief Create a cumulative histogram * \param[in] data A (non-cumulative) histogram */ Histogram::Histogram(Span data) { cumulative_.resize(data.size() + 1); cumulative_[0] = 0; for (const auto &[i, value] : utils::enumerate(data)) cumulative_[i + 1] = cumulative_[i] + value; } /** * \fn Histogram::Histogram(Span data, Transform transform) * \brief Create a cumulative histogram * \param[in] data A (non-cumulative) histogram * \param[in] transform The transformation function to apply to every bin */ /** * \fn Histogram::bins() * \brief Retrieve the number of bins currently used by the Histogram * \return Number of bins */ /** * \fn Histogram::data() * \brief Retrieve the internal data * \return The data */ /** * \fn Histogram::total() * \brief Retrieve the total number of values in the data set * \return Number of values */ /** * \brief Cumulative frequency up to a (fractional) point in a bin * \param[in] bin The bin up to which to cumulate * * With F(p) the cumulative frequency of the histogram, the value is 0 at * the bottom of the histogram, and the maximum is the number of bins. * The pixels are spread evenly throughout the “bin” in which they lie, so that * F(p) is a continuous (monotonically increasing) function. * * \return The cumulative frequency from 0 up to the specified bin */ uint64_t Histogram::cumulativeFrequency(double bin) const { if (bin <= 0) return 0; else if (bin >= bins()) return total(); int b = static_cast(bin); return cumulative_[b] + (bin - b) * (cumulative_[b + 1] - cumulative_[b]); } /** * \brief Return the (fractional) bin of the point through the histogram * \param[in] q the desired point (0 <= q <= 1) * \param[in] first low limit (default is 0) * \param[in] last high limit (default is UINT_MAX) * * A quantile gives us the point p = Q(q) in the range such that a proportion * q of the pixels lie below p. A familiar quantile is Q(0.5) which is the median * of a distribution. * * \return The fractional bin of the point */ double Histogram::quantile(double q, uint32_t first, uint32_t last) const { if (last == UINT_MAX) last = cumulative_.size() - 2; ASSERT(first <= last); uint64_t item = q * total(); /* Binary search to find the right bin */ while (first < last) { int middle = (first + last) / 2; /* Is it between first and middle ? */ if (cumulative_[middle + 1] > item) last = middle; else first = middle + 1; } ASSERT(item >= cumulative_[first] && item <= cumulative_[last + 1]); double frac; if (cumulative_[first + 1] == cumulative_[first]) frac = 0; else frac = (item - cumulative_[first]) / (cumulative_[first + 1] - cumulative_[first]); return first + frac; } /** * \brief Calculate the mean between two quantiles * \param[in] lowQuantile low Quantile * \param[in] highQuantile high Quantile * * Quantiles are not ideal for metering as they suffer several limitations. * Instead, a concept is introduced here: inter-quantile mean. * It returns the mean of all pixels between lowQuantile and highQuantile. * * \return The mean histogram bin value between the two quantiles */ double Histogram::interQuantileMean(double lowQuantile, double highQuantile) const { ASSERT(highQuantile > lowQuantile); /* Proportion of pixels which lies below lowQuantile */ double lowPoint = quantile(lowQuantile); /* Proportion of pixels which lies below highQuantile */ double highPoint = quantile(highQuantile, static_cast(lowPoint)); double sumBinFreq = 0, cumulFreq = 0; for (double p_next = floor(lowPoint) + 1.0; p_next <= ceil(highPoint); lowPoint = p_next, p_next += 1.0) { int bin = floor(lowPoint); double freq = (cumulative_[bin + 1] - cumulative_[bin]) * (std::min(p_next, highPoint) - lowPoint); /* Accumulate weighted bin */ sumBinFreq += bin * freq; /* Accumulate weights */ cumulFreq += freq; } /* add 0.5 to give an average for bin mid-points */ return sumBinFreq / cumulFreq + 0.5; } } /* namespace ipa */ } /* namespace libcamera */ amp;id=624f6d54fff0b5cd370c4e21b9fdb03ab9993cd8'>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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * geometry.cpp - Geometry-related structures
 */

#include <libcamera/geometry.h>

#include <sstream>
#include <stdint.h>

/**
 * \file geometry.h
 * \brief Data structures related to geometric objects
 */

namespace libcamera {

/**
 * \struct Size
 * \brief Describe a two-dimensional size
 *
 * The Size structure defines a two-dimensional size with integer precision.
 */

/**
 * \fn Size::Size()
 * \brief Construct a Size with width and height set to 0
 */

/**
 * \fn Size::Size(unsigned int width, unsigned int height)
 * \brief Construct a Size with given \a width and \a height
 * \param[in] width The Size width
 * \param[in] height The Size height
 */

/**
 * \var Size::width
 * \brief The Size width
 */

/**
 * \var Size::height
 * \brief The Size height
 */

/**
 * \fn bool Size::isNull() const
 * \brief Check if the size is null
 * \return True if both the width and height are 0, or false otherwise
 */

/**
 * \brief Assemble and return a string describing the size
 * \return A string describing the size
 */
const std::string Size::toString() const
{
	return std::to_string(width) + "x" + std::to_string(height);
}

/**
 * \fn Size::alignDownTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size down horizontally and vertically in place
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 *
 * This functions rounds the width and height down to the nearest multiple of
 * \a hAlignment and \a vAlignment respectively.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::alignUpTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size up horizontally and vertically in place
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 *
 * This functions rounds the width and height up to the nearest multiple of
 * \a hAlignment and \a vAlignment respectively.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::boundTo(const Size &bound)
 * \brief Bound the size to \a bound in place
 * \param[in] bound The maximum size
 *
 * This function sets the width and height to the minimum of this size and the
 * \a bound size.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::expandTo(const Size &expand)
 * \brief Expand the size to \a expand
 * \param[in] expand The minimum size
 *
 * This function sets the width and height to the maximum of this size and the
 * \a expand size.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::alignedDownTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size down horizontally and vertically
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 * \return A Size whose width and height are equal to the width and height of
 * this size rounded down to the nearest multiple of \a hAlignment and
 * \a vAlignment respectively
 */

/**
 * \fn Size::alignedUpTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size up horizontally and vertically
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 * \return A Size whose width and height are equal to the width and height of
 * this size rounded up to the nearest multiple of \a hAlignment and
 * \a vAlignment respectively
 */

/**
 * \fn Size::boundedTo(const Size &bound)
 * \brief Bound the size to \a bound
 * \param[in] bound The maximum size
 * \return A Size whose width and height are the minimum of the width and
 * height of this size and the \a bound size
 */

/**
 * \fn Size::expandedTo(const Size &expand)
 * \brief Expand the size to \a expand
 * \param[in] expand The minimum size
 * \return A Size whose width and height are the maximum of the width and
 * height of this size and the \a expand size
 */

/**
 * \brief Compare sizes for equality
 * \return True if the two sizes are equal, false otherwise
 */
bool operator==(const Size &lhs, const Size &rhs)
{
	return lhs.width == rhs.width && lhs.height == rhs.height;
}

/**
 * \brief Compare sizes for smaller than order
 *
 * Sizes are compared on three criteria, in the following order.
 *
 * - A size with smaller width and smaller height is smaller.
 * - A size with smaller area is smaller.
 * - A size with smaller width is smaller.
 *
 * \return True if \a lhs is smaller than \a rhs, false otherwise
 */
bool operator<(const Size &lhs, const Size &rhs)
{