2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 /** @file src/mono_picture_frame.cc
36 * @brief MonoPictureFrame class
40 #include "colour_conversion.h"
41 #include "compose.hpp"
42 #include "crypto_context.h"
43 #include "exceptions.h"
45 #include "j2k_transcode.h"
46 #include "mono_picture_frame.h"
49 #include <asdcp/KM_fileio.h>
50 #include <asdcp/AS_DCP.h>
53 using std::make_shared;
55 using std::shared_ptr;
56 using boost::optional;
60 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
62 auto const size = boost::filesystem::file_size (path);
63 _buffer.reset(new ASDCP::JP2K::FrameBuffer(size));
66 boost::throw_exception (FileError("could not open JPEG2000 file", path, errno));
69 if (f.read(data(), 1, size) != size) {
70 boost::throw_exception (FileError("could not read from JPEG2000 file", path, errno));
77 /** Make a picture frame from a 2D (monoscopic) asset.
78 * @param reader Reader for the asset's MXF file.
79 * @param n Frame within the asset, not taking EntryPoint into account.
80 * @param c Context for decryption, or 0.
81 * @param check_hmac true to check the HMAC and give an error if it is not as expected.
83 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c, bool check_hmac)
85 /* XXX: unfortunate guesswork on this buffer size */
86 _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(4 * Kumu::Megabyte);
88 auto const r = reader->ReadFrame (n, *_buffer, c->context(), check_hmac ? c->hmac() : nullptr);
90 if (ASDCP_FAILURE(r)) {
91 boost::throw_exception (ReadError(String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
96 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
98 _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(size);
100 memcpy (_buffer->Data(), data, size);
105 MonoPictureFrame::data () const
107 return _buffer->RoData ();
112 MonoPictureFrame::data ()
114 return _buffer->Data ();
119 MonoPictureFrame::size () const
121 return _buffer->Size ();
125 shared_ptr<OpenJPEGImage>
126 MonoPictureFrame::xyz_image (int reduce) const
128 return decompress_j2k (const_cast<uint8_t*>(_buffer->RoData()), _buffer->Size(), reduce);