Tidying.
[libdcp.git] / src / mono_picture_frame.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/mono_picture_frame.cc
36  *  @brief MonoPictureFrame class
37  */
38
39
40 #include "mono_picture_frame.h"
41 #include "exceptions.h"
42 #include "util.h"
43 #include "rgb_xyz.h"
44 #include "colour_conversion.h"
45 #include "compose.hpp"
46 #include "j2k.h"
47 #include "crypto_context.h"
48 #include <asdcp/KM_fileio.h>
49 #include <asdcp/AS_DCP.h>
50
51
52 using std::make_shared;
53 using std::string;
54 using std::shared_ptr;
55 using boost::optional;
56 using namespace dcp;
57
58
59 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
60 {
61         auto const size = boost::filesystem::file_size (path);
62         _buffer.reset(new ASDCP::JP2K::FrameBuffer(size));
63         auto f = fopen_boost (path, "rb");
64         if (!f) {
65                 boost::throw_exception (FileError("could not open JPEG2000 file", path, errno));
66         }
67
68         size_t n = fread (data(), 1, size, f);
69         fclose (f);
70
71         if (n != size) {
72                 boost::throw_exception (FileError("could not read from JPEG2000 file", path, errno));
73         }
74
75         _buffer->Size (size);
76 }
77
78
79 /** Make a picture frame from a 2D (monoscopic) asset.
80  *  @param reader Reader for the asset's MXF file.
81  *  @param n Frame within the asset, not taking EntryPoint into account.
82  *  @param c Context for decryption, or 0.
83  */
84 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c)
85 {
86         /* XXX: unfortunate guesswork on this buffer size */
87         _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(4 * Kumu::Megabyte);
88
89         auto const r = reader->ReadFrame (n, *_buffer, c->context(), c->hmac());
90
91         if (ASDCP_FAILURE(r)) {
92                 boost::throw_exception (ReadError(String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
93         }
94 }
95
96
97 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
98 {
99         _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(size);
100         _buffer->Size (size);
101         memcpy (_buffer->Data(), data, size);
102 }
103
104
105 uint8_t const *
106 MonoPictureFrame::data () const
107 {
108         return _buffer->RoData ();
109 }
110
111
112 uint8_t *
113 MonoPictureFrame::data ()
114 {
115         return _buffer->Data ();
116 }
117
118
119 int
120 MonoPictureFrame::size () const
121 {
122         return _buffer->Size ();
123 }
124
125
126 shared_ptr<OpenJPEGImage>
127 MonoPictureFrame::xyz_image (int reduce) const
128 {
129         return decompress_j2k (const_cast<uint8_t*>(_buffer->RoData()), _buffer->Size(), reduce);
130 }