/* SPDX-License-Identifier: BSD-2-Clause */ /* * Copyright (C) 2019, Raspberry Pi (Trading) Limited * * histogram.cpp - histogram calculations */ #include "histogram.h" #include #include "libcamera/internal/log.h" /** * \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. */ /** * \brief Create a cumulative histogram * \param[in] data A pre-sorted histogram to be passed */ Histogram::Histogram(Span data) { cumulative_.reserve(data.size()); cumulative_.push_back(0); for (const uint32_t &value : data) cumulative_.push_back(cumulative_.back() + value); } /** * \fn Histogram::bins() * \brief Retrieve the number of bins currently used by the Histogram * \return Number of bins */ /** * \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 weigthed 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 */ '#n59'>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
# SPDX-License-Identifier: GPL-2.0-only
#
# clang-format configuration file. Intended for clang-format >= 7.
#
# For more information, see:
#
#   Documentation/process/clang-format.rst
#   https://clang.llvm.org/docs/ClangFormat.html
#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
#
---
Language: Cpp
AccessModifierOffset: -8
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
  AfterClass: true
  AfterControlStatement: false
  AfterEnum: false
  AfterFunction: true
  AfterNamespace: false
  AfterObjCDeclaration: false
  AfterStruct: false
  AfterUnion: false
  AfterExternBlock: false
  BeforeCatch: false
  BeforeElse: false
  IndentBraces: false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
  - 'udev_list_entry_foreach'
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
  # Headers matching the name of the component are matched automatically.
  # Priority 1
  # Other library headers (explicit overrides to match before system headers)
  - Regex:           '(<jpeglib.h>|<libudev.h>|<tiffio.h>|<xf86drm.h>|<xf86drmMode.h>|<yaml.h>)'
    Priority:        9
  # Qt includes (match before C++ standard library)
  - Regex:           '<Q([A-Za-z0-9\-_])+>'
    Priority:        9
  # Headers in <> with an extension. (+system libraries)
  - Regex:           '<([A-Za-z0-9\-_])+\.h>'
    Priority:        2
  # System headers 
  - Regex:           '<sys/.*>'
    Priority:        2
  # C++ standard library includes (no extension)
  - Regex:           '<([A-Za-z0-9\-_/])+>'
    Priority:        2
  # Linux headers, as a second group/subset of system headers
  - Regex:           '<linux/.*>'
    Priority:        3
  # Headers for libcamera Base support
  - Regex:           '<libcamera/base/private.h>'
    Priority:        4
  - Regex:           '<libcamera/base/.*\.h>'
    Priority:        5
  # Public API Headers for libcamera, which are not in a subdir (i.e. ipa/,internal/)
  - Regex:           '<libcamera/([A-Za-z0-9\-_])+.h>'
    Priority:        6
  # IPA Interfaces
  - Regex:           '<libcamera/ipa/.*\.h>'
    Priority:        7
  # libcamera Internal headers in ""  
  - Regex:           '"libcamera/internal/.*\.h"'
    Priority:        8
  # Other libraries headers with one group per library (.h or .hpp)
  - Regex:           '<.*/.*\.hp*>'
    Priority:        9
  # local modular includes "path/file.h" (.h or .hpp)
  - Regex:           '"(.*/)+.*\.hp*"'
    Priority:        10
  # Other local headers "file.h" with extension (.h or .hpp)
  - Regex:           '".*.hp*"'
    Priority:        11
  # Any unmatched line, separated from the last group
  - Regex:	     '"*"'
    Priority:        100

IncludeIsMainRegex: '(_test)?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 8
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true

# Taken from git's rules
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60

PointerAlignment: Right
ReflowComments: false
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Always
...