Files
oak-editor/app/widget/scope/histogram/histogram.cpp
T
Mike-Solar bb40b4923e style: unify identifier naming per updated conventions
Automated with clang-tidy readability-identifier-naming (config added to
.clang-tidy) plus scripted passes, per the updated rules now documented
in CONTRIBUTING.md:

- types (class/struct/enum/alias/template params): PascalCase
- functions, variables, members: snake_case (incl. rational -> Rational)
- private/protected members: trailing underscore; static member
  variables likewise (instance_, available_themes_)
- constants and enum values: snake_case (kLinear -> k_linear,
  F32P -> f32p); ALL_CAPS reserved for macros
- macros: OAK_ prefix (OLIVE_ADD_TEST/OLIVE_ASSERT/OLIVE_CONFIG ->
  OAK_ADD_TEST/OAK_ASSERT/OAK_CONFIG, GL_PREAMBLE -> OAK_GL_PREAMBLE,
  include guards -> OAK_*)
- file names: all lowercase (Current/Plugin/OliveHost/OliveClip/
  OlivePluginInstance -> current/plugin/olivehost/oliveclip/
  oliveplugininstance)
- getters share the member name sans underscore, setters set_foo()
- Qt and third-party (OpenFX) virtual overrides and framework callbacks
  keep their original names (exempt in .clang-tidy)

Manual follow-ups required where automation could not reach:
- string-based QMetaObject/SIGNAL/SLOT references updated to renamed
  methods (AddTask, CreatedFile, DeleteSpecificFile, moveSelectionUp, ...)
- macro bodies referencing renamed methods (OLIVE_CONFIG,
  NODE_DEFAULT_DESTRUCTOR, MANAGEDDISPLAYWIDGET_*)
- self-shadowing locals renamed where signals/methods became same-named
  (size_changed, worker_count, selected_items, import param, filters)
- third_party OFX member/namespace usages restored (OFX::Host::*,
  _created, _clipPrefsDirty, createInstance, clearPersistentMessage)
- STL protocol aliases restored (const_iterator) with .clang-tidy
  ignore rules; qHash overloads restored

Full build and test suite pass: ctest 4/4, ~1960 gtest cases green.
2026-07-19 16:10:54 +08:00

255 lines
8.0 KiB
C++

/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
Modifications Copyright (C) 2025 mikesolar
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "histogram.h"
#include <array>
#include <QPainter>
#include <QtMath>
#include <QVector2D>
#include "common/qtutils.h"
#include "node/node.h"
namespace olive
{
#define super ScopeBase
HistogramScope::HistogramScope(QWidget *parent)
: super(parent)
{
}
void HistogramScope::on_init()
{
super::on_init();
ShaderCode secondary_code(
FileFunctions::read_file_as_string(
":/shaders/rgbhistogram_secondary.frag"),
FileFunctions::read_file_as_string(":/shaders/rgbhistogram.vert"));
pipeline_secondary_ = renderer()->create_native_shader(secondary_code);
}
void HistogramScope::on_destroy()
{
pipeline_secondary_.clear();
texture_row_sums_ = nullptr;
super::on_destroy();
}
ShaderCode HistogramScope::generate_shader_code()
{
return ShaderCode(
FileFunctions::read_file_as_string(":/shaders/rgbhistogram.frag"),
FileFunctions::read_file_as_string(":/shaders/default.vert"));
}
void HistogramScope::draw_scope(TexturePtr managed_tex, QVariant pipeline)
{
float histogram_scale = 0.80f;
// This value is eyeballed for usefulness. Until we have a geometry
// shader approach, it is impossible to normalize against a peak
// sum of image values.
float histogram_base = 2.5f;
float histogram_power = 1.0f / histogram_base;
ShaderJob shader_job;
shader_job.insert(QStringLiteral("viewport"),
NodeValue(NodeValue::k_vec2,
QVector2D(width(), height())));
shader_job.insert(QStringLiteral("histogram_scale"),
NodeValue(NodeValue::k_float, histogram_scale));
shader_job.insert(QStringLiteral("histogram_power"),
NodeValue(NodeValue::k_float, histogram_power));
if (!texture_row_sums_ || texture_row_sums_->width() != this->width() ||
texture_row_sums_->height() != this->height()) {
texture_row_sums_ = renderer()->create_texture(
VideoParams(width(), height(), managed_tex->format(),
managed_tex->channel_count()));
}
// Draw managed texture to a sums texture
shader_job.insert(QStringLiteral("ove_maintex"),
NodeValue(NodeValue::k_texture,
QVariant::fromValue(managed_tex)));
renderer()->blit_to_texture(pipeline, shader_job, texture_row_sums_.get());
// Draw sums into a histogram
shader_job.insert(QStringLiteral("ove_maintex"),
NodeValue(NodeValue::k_texture,
QVariant::fromValue(texture_row_sums_)));
renderer()->blit(pipeline_secondary_, shader_job,
texture_row_sums_->params());
// Draw line overlays
QPainter p(paint_device());
QFont font = p.font();
font.setPixelSize(10);
QFontMetrics font_metrics = QFontMetrics(font);
QString label;
std::vector<float> histogram_increments = { 0.00, 0.25, 0.50, 1.0 };
int histogram_steps = histogram_increments.size();
QVector<QLine> histogram_lines(histogram_steps + 1);
int font_x_offset = 0;
int font_y_offset = font_metrics.capHeight() / 2.0f;
p.setCompositionMode(QPainter::CompositionMode_Plus);
p.setPen(QColor(0.0, 0.6 * 255.0, 0.0));
p.setFont(font);
float histogram_dim_x = ceil((width() - 1.0) * histogram_scale);
float histogram_dim_y = ceil((height() - 1.0) * histogram_scale);
float histogram_start_dim_x = ((width() - 1.0) - histogram_dim_x) / 2.0f;
float histogram_start_dim_y = ((height() - 1.0) - histogram_dim_y) / 2.0f;
float histogram_end_dim_x = (width() - 1.0) - histogram_start_dim_x;
// for (int i=0; i <= histogram_steps; i++) {
for (std::vector<float>::iterator it = histogram_increments.begin();
it != histogram_increments.end(); it++) {
histogram_lines[it - histogram_increments.begin()].setLine(
histogram_start_dim_x,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y,
histogram_end_dim_x,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y);
label = QString::number(*it * 100, 'f', 1) + "%";
font_x_offset = QtUtils::q_font_metrics_width(font_metrics, label) + 4;
p.drawText(histogram_start_dim_x - font_x_offset,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y + font_y_offset,
label);
}
p.drawLines(histogram_lines);
}
void HistogramScope::draw_scope_software(QPainter &p, const QImage &image)
{
const float histogram_scale = 0.80f;
const float histogram_base = 2.5f;
const int histogram_dim_x = qCeil((width() - 1.0) * histogram_scale);
const int histogram_dim_y = qCeil((height() - 1.0) * histogram_scale);
const int histogram_start_dim_x =
qFloor(((width() - 1.0) - histogram_dim_x) / 2.0f);
const int histogram_start_dim_y =
qFloor(((height() - 1.0) - histogram_dim_y) / 2.0f);
const int histogram_end_dim_x = width() - 1 - histogram_start_dim_x;
std::array<int, 256> r_counts{};
std::array<int, 256> g_counts{};
std::array<int, 256> b_counts{};
const int src_w = image.width();
const int src_h = image.height();
// Limit analysis resolution to keep CPU usage reasonable.
const int step_x = qMax(1, src_w / 512);
const int step_y = qMax(1, src_h / 512);
for (int sy = 0; sy < src_h; sy += step_y) {
const uchar *src_line = image.constScanLine(sy);
for (int sx = 0; sx < src_w; sx += step_x) {
const uchar *src = src_line + sx * 4;
r_counts[src[0]]++;
g_counts[src[1]]++;
b_counts[src[2]]++;
}
}
int max_count = 1;
for (int i = 0; i < 256; ++i) {
max_count = qMax(max_count, r_counts[i]);
max_count = qMax(max_count, g_counts[i]);
max_count = qMax(max_count, b_counts[i]);
}
auto draw_channel = [&](const std::array<int, 256> &counts,
const QColor &color) {
QPen pen(color);
pen.setWidth(2);
p.setPen(pen);
QVector<QPointF> points;
points.reserve(256);
for (int i = 0; i < 256; ++i) {
float x =
histogram_start_dim_x + (float(i) / 255.0f) * histogram_dim_x;
float normalized = float(counts[i]) / float(max_count);
float y = histogram_start_dim_y +
histogram_dim_y *
(1.0f - pow(normalized, 1.0f / histogram_base));
points.append(QPointF(x, y));
}
p.drawPolyline(points.constData(), points.size());
};
p.setCompositionMode(QPainter::CompositionMode_Plus);
draw_channel(r_counts, QColor(255, 0, 0));
draw_channel(g_counts, QColor(0, 255, 0));
draw_channel(b_counts, QColor(0, 0, 255));
// Draw percentage line overlays
QFont font = p.font();
font.setPixelSize(10);
QFontMetrics font_metrics = QFontMetrics(font);
QString label;
std::vector<float> histogram_increments = { 0.00, 0.25, 0.50, 1.0 };
int histogram_steps = histogram_increments.size();
QVector<QLine> histogram_lines(histogram_steps + 1);
int font_x_offset = 0;
int font_y_offset = font_metrics.capHeight() / 2.0f;
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.setPen(QColor(0.0, 0.6 * 255.0, 0.0));
p.setFont(font);
for (std::vector<float>::iterator it = histogram_increments.begin();
it != histogram_increments.end(); it++) {
histogram_lines[it - histogram_increments.begin()].setLine(
histogram_start_dim_x,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y,
histogram_end_dim_x,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y);
label = QString::number(*it * 100, 'f', 1) + "%";
font_x_offset = QtUtils::q_font_metrics_width(font_metrics, label) + 4;
p.drawText(histogram_start_dim_x - font_x_offset,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y + font_y_offset,
label);
}
p.drawLines(histogram_lines);
}
}