Obey requests to change the video range of RGB content.
[dcpomatic.git] / test / video_level_test.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file  test/video_level_test.cc
23  *  @brief Test that video level ranges are handled correctly.
24  *  @ingroup specific
25  */
26
27
28 #include "lib/ffmpeg_image_proxy.h"
29 #include "lib/image.h"
30 #include "test.h"
31 #include <boost/test/unit_test.hpp>
32
33
34 using boost::shared_ptr;
35
36
37 static
38 shared_ptr<Image>
39 grey_image (dcp::Size size, uint8_t pixel)
40 {
41         shared_ptr<Image> grey(new Image(AV_PIX_FMT_RGB24, size, true));
42         for (int y = 0; y < size.height; ++y) {
43                 uint8_t* p = grey->data()[0] + y * grey->stride()[0];
44                 for (int x = 0; x < size.width; ++x) {
45                         *p++ = pixel;
46                         *p++ = pixel;
47                         *p++ = pixel;
48                 }
49         }
50
51         return grey;
52 }
53
54
55 BOOST_AUTO_TEST_CASE (ffmpeg_image_full_range_not_changed)
56 {
57         dcp::Size size(640, 480);
58         uint8_t const grey_pixel = 128;
59         boost::filesystem::path const file = "build/test/ffmpeg_image_full_range_not_changed.png";
60
61         write_image (grey_image(size, grey_pixel), file);
62
63         FFmpegImageProxy proxy (file, VIDEO_RANGE_FULL);
64         ImageProxy::Result result = proxy.image ();
65         BOOST_REQUIRE (!result.error);
66
67         for (int y = 0; y < size.height; ++y) {
68                 uint8_t* p = result.image->data()[0] + y * result.image->stride()[0];
69                 for (int x = 0; x < size.width; ++x) {
70                         BOOST_REQUIRE (*p++ == grey_pixel);
71                 }
72         }
73 }
74
75
76 BOOST_AUTO_TEST_CASE (ffmpeg_image_video_range_expanded)
77 {
78         dcp::Size size(640, 480);
79         uint8_t const grey_pixel = 128;
80         uint8_t const expanded_grey_pixel = static_cast<uint8_t>((grey_pixel - 16) * 256.0 / 219);
81         boost::filesystem::path const file = "build/test/ffmpeg_image_video_range_expanded.png";
82
83         write_image (grey_image(size, grey_pixel), file);
84
85         FFmpegImageProxy proxy (file, VIDEO_RANGE_VIDEO);
86         ImageProxy::Result result = proxy.image ();
87         BOOST_REQUIRE (!result.error);
88
89         for (int y = 0; y < size.height; ++y) {
90                 uint8_t* p = result.image->data()[0] + y * result.image->stride()[0];
91                 for (int x = 0; x < size.width; ++x) {
92                         BOOST_REQUIRE_EQUAL (*p++, expanded_grey_pixel);
93                 }
94         }
95 }
96
97