From 71f3520ea315fb65ff151c99aa0b64fc8dccdb1d Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 25 May 2021 00:32:39 +0200 Subject: Clamp results correctly when shifting video levels, and account for that in tests. --- src/lib/image.cc | 4 ++-- src/lib/util.h | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib/image.cc b/src/lib/image.cc index 4859ebe14..eff53a2aa 100644 --- a/src/lib/image.cc +++ b/src/lib/image.cc @@ -1433,7 +1433,7 @@ Image::video_range_to_full_range () for (int y = 0; y < lines; ++y) { uint8_t* q = p; for (int x = 0; x < line_size()[0]; ++x) { - *q = int((*q - 16) * factor); + *q = clamp(lrintf((*q - 16) * factor), 0L, 255L); ++q; } p += stride()[0]; @@ -1450,7 +1450,7 @@ Image::video_range_to_full_range () uint16_t* q = p; int const line_size_pixels = line_size()[c] / 2; for (int x = 0; x < line_size_pixels; ++x) { - *q = int((*q - 256) * factor); + *q = clamp(lrintf((*q - 256) * factor), 0L, 4095L); ++q; } } diff --git a/src/lib/util.h b/src/lib/util.h index fa0d9fdf2..013eabe12 100644 --- a/src/lib/util.h +++ b/src/lib/util.h @@ -150,4 +150,12 @@ list_to_vector (std::list v) extern double db_to_linear (double db); extern double linear_to_db (double linear); + +template +T clamp (T val, T minimum, T maximum) +{ + return std::max(std::min(val, maximum), minimum); +} + + #endif -- cgit v1.2.3