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/>.
21 /** @file src/mono_picture_frame.cc
22 * @brief MonoPictureFrame class.
25 #include "mono_picture_frame.h"
26 #include "exceptions.h"
29 #include "colour_conversion.h"
30 #include "KM_fileio.h"
32 #include "compose.hpp"
36 using boost::shared_ptr;
37 using boost::optional;
40 /** Make a picture frame from a JPEG2000 file.
41 * @param path Path to JPEG2000 file.
43 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
45 boost::uintmax_t const size = boost::filesystem::file_size (path);
46 _buffer = new ASDCP::JP2K::FrameBuffer (size);
47 FILE* f = fopen_boost (path, "rb");
49 boost::throw_exception (FileError ("could not open JPEG2000 file", path, errno));
52 fread (j2k_data(), 1, size, f);
58 /** Make a picture frame from a 2D (monoscopic) asset.
59 * @param reader Reader for the asset's MXF file.
60 * @param n Frame within the asset, not taking EntryPoint into account.
61 * @param c Context for decryption, or 0.
63 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, ASDCP::AESDecContext* c)
65 /* XXX: unfortunate guesswork on this buffer size */
66 _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
68 if (ASDCP_FAILURE (reader->ReadFrame (n, *_buffer, c))) {
69 boost::throw_exception (DCPReadError (String::compose ("could not read video frame %1", n)));
73 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
75 _buffer = new ASDCP::JP2K::FrameBuffer (size);
77 memcpy (_buffer->Data(), data, size);
80 /** MonoPictureFrame destructor */
81 MonoPictureFrame::~MonoPictureFrame ()
86 /** @return Pointer to JPEG2000 data */
88 MonoPictureFrame::j2k_data () const
90 return _buffer->RoData ();
93 /** @return Pointer to JPEG2000 data */
95 MonoPictureFrame::j2k_data ()
97 return _buffer->Data ();
100 /** @return Size of JPEG2000 data in bytes */
102 MonoPictureFrame::j2k_size () const
104 return _buffer->Size ();
107 /** @param reduce a factor by which to reduce the resolution
108 * of the image, expressed as a power of two (pass 0 for no
111 shared_ptr<OpenJPEGImage>
112 MonoPictureFrame::xyz_image (int reduce) const
114 return decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);