From d1ae563da6068da4132bd50e098d442dc4ec1bb0 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 3 Sep 2023 18:33:15 +0200 Subject: Add PixelQuanta::round taking floats. --- src/lib/pixel_quanta.cc | 13 +++++++++++++ src/lib/pixel_quanta.h | 1 + test/pixel_quanta_test.cc | 34 ++++++++++++++++++++++++++++++++++ test/wscript | 1 + 4 files changed, 49 insertions(+) create mode 100644 test/pixel_quanta_test.cc diff --git a/src/lib/pixel_quanta.cc b/src/lib/pixel_quanta.cc index 7c1d285cf..30894ddb7 100644 --- a/src/lib/pixel_quanta.cc +++ b/src/lib/pixel_quanta.cc @@ -60,6 +60,19 @@ PixelQuanta::round (dcp::Size size) const } +dcp::Size +PixelQuanta::round(float x_, float y_) const +{ + auto round_one = [](float v, int q) { + return q == 1 + ? static_cast(std::lround(v)) + : (static_cast(v) + static_cast(v) % q); + }; + + return { round_one(x_, x), round_one(y_, y) }; +} + + PixelQuanta max (PixelQuanta const& a, PixelQuanta const& b) { diff --git a/src/lib/pixel_quanta.h b/src/lib/pixel_quanta.h index 37d2321f4..bef23b2b1 100644 --- a/src/lib/pixel_quanta.h +++ b/src/lib/pixel_quanta.h @@ -55,6 +55,7 @@ public: int round_x (int x_) const; int round_y (int y_) const; dcp::Size round (dcp::Size size) const; + dcp::Size round(float x, float y) const; int x; int y; diff --git a/test/pixel_quanta_test.cc b/test/pixel_quanta_test.cc new file mode 100644 index 000000000..b07981577 --- /dev/null +++ b/test/pixel_quanta_test.cc @@ -0,0 +1,34 @@ +/* + Copyright (C) 2023 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic 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 2 of the License, or + (at your option) any later version. + + DCP-o-matic 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 DCP-o-matic. If not, see . + +*/ + + +#include "lib/pixel_quanta.h" +#include + + +BOOST_AUTO_TEST_CASE(pixel_quanta_test) +{ + BOOST_CHECK(PixelQuanta(1, 1).round(9.3, 5.9) == dcp::Size(9, 6)); + BOOST_CHECK(PixelQuanta(1, 1).round(11.7, 5.1) == dcp::Size(12, 5)); + BOOST_CHECK(PixelQuanta(2, 2).round(9.3, 5.9) == dcp::Size(10, 6)); + BOOST_CHECK(PixelQuanta(2, 2).round(8.9, 11.9) == dcp::Size(8, 12)); + +} + diff --git a/test/wscript b/test/wscript index f40568e3c..f992cfb63 100644 --- a/test/wscript +++ b/test/wscript @@ -123,6 +123,7 @@ def build(bld): optimise_stills_test.cc overlap_video_test.cc pixel_formats_test.cc + pixel_quanta_test.cc player_test.cc playlist_test.cc pulldown_detect_test.cc -- cgit v1.2.3