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 MonoJ2KPictureFrame class
40 #include "colour_conversion.h"
41 #include "compose.hpp"
42 #include "crypto_context.h"
43 #include "exceptions.h"
45 #include "filesystem.h"
46 #include "j2k_transcode.h"
47 #include "mono_j2k_picture_frame.h"
50 #include <asdcp/KM_fileio.h>
51 #include <asdcp/AS_DCP.h>
54 using std::make_shared;
56 using std::shared_ptr;
57 using boost::optional;
61 MonoJ2KPictureFrame::MonoJ2KPictureFrame (boost::filesystem::path path)
63 auto const size = filesystem::file_size(path);
64 _buffer.reset(new ASDCP::JP2K::FrameBuffer(size));
67 boost::throw_exception (FileError("could not open JPEG2000 file", path, errno));
70 if (f.read(data(), 1, size) != size) {
71 boost::throw_exception (FileError("could not read from JPEG2000 file", path, errno));
78 /** Make a picture frame from a 2D (monoscopic) asset.
79 * @param reader Reader for the asset's MXF file.
80 * @param n Frame within the asset, not taking EntryPoint into account.
81 * @param c Context for decryption, or 0.
82 * @param check_hmac true to check the HMAC and give an error if it is not as expected.
84 MonoJ2KPictureFrame::MonoJ2KPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c, bool check_hmac)
86 /* XXX: unfortunate guesswork on this buffer size */
87 _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(4 * Kumu::Megabyte);
89 auto const r = reader->ReadFrame (n, *_buffer, c->context(), check_hmac ? c->hmac() : nullptr);
91 if (ASDCP_FAILURE(r)) {
92 boost::throw_exception (ReadError(String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
97 MonoJ2KPictureFrame::MonoJ2KPictureFrame (uint8_t const * data, int size)
99 _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(size);
100 _buffer->Size (size);
101 memcpy (_buffer->Data(), data, size);
106 MonoJ2KPictureFrame::data () const
108 return _buffer->RoData ();
113 MonoJ2KPictureFrame::data ()
115 return _buffer->Data ();
120 MonoJ2KPictureFrame::size () const
122 return _buffer->Size ();
126 shared_ptr<OpenJPEGImage>
127 MonoJ2KPictureFrame::xyz_image (int reduce) const
129 return decompress_j2k (const_cast<uint8_t*>(_buffer->RoData()), _buffer->Size(), reduce);