2 Copyright (C) 2012-2014 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.
34 /** @file src/mono_picture_frame.cc
35 * @brief MonoPictureFrame class.
38 #include "mono_picture_frame.h"
39 #include "exceptions.h"
42 #include "colour_conversion.h"
43 #include "compose.hpp"
45 #include "crypto_context.h"
46 #include <asdcp/KM_fileio.h>
47 #include <asdcp/AS_DCP.h>
50 using boost::shared_ptr;
51 using boost::optional;
54 /** Make a picture frame from a JPEG2000 file.
55 * @param path Path to JPEG2000 file.
57 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
59 boost::uintmax_t const size = boost::filesystem::file_size (path);
60 _buffer = new ASDCP::JP2K::FrameBuffer (size);
61 FILE* f = fopen_boost (path, "rb");
63 boost::throw_exception (FileError ("could not open JPEG2000 file", path, errno));
66 size_t n = fread (j2k_data(), 1, size, f);
68 boost::throw_exception (FileError ("could not read from JPEG2000 file", path, errno));
76 /** Make a picture frame from a 2D (monoscopic) asset.
77 * @param reader Reader for the asset's MXF file.
78 * @param n Frame within the asset, not taking EntryPoint into account.
79 * @param c Context for decryption, or 0.
81 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c)
83 /* XXX: unfortunate guesswork on this buffer size */
84 _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
86 ASDCP::Result_t const r = reader->ReadFrame (n, *_buffer, c->context(), c->hmac());
88 if (ASDCP_FAILURE (r)) {
89 boost::throw_exception (ReadError (String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
93 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
95 _buffer = new ASDCP::JP2K::FrameBuffer (size);
97 memcpy (_buffer->Data(), data, size);
100 /** MonoPictureFrame destructor */
101 MonoPictureFrame::~MonoPictureFrame ()
106 /** @return Pointer to JPEG2000 data */
108 MonoPictureFrame::j2k_data () const
110 return _buffer->RoData ();
113 /** @return Pointer to JPEG2000 data */
115 MonoPictureFrame::j2k_data ()
117 return _buffer->Data ();
120 /** @return Size of JPEG2000 data in bytes */
122 MonoPictureFrame::j2k_size () const
124 return _buffer->Size ();
127 /** @param reduce a factor by which to reduce the resolution
128 * of the image, expressed as a power of two (pass 0 for no
131 shared_ptr<OpenJPEGImage>
132 MonoPictureFrame::xyz_image (int reduce) const
134 return decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);