Add Reader classes to permit much more efficient DCP reading.
[libdcp.git] / src / mono_picture_frame.cc
1 /*
2     Copyright (C) 2012-2014 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 */
20
21 /** @file  src/mono_picture_frame.cc
22  *  @brief MonoPictureFrame class.
23  */
24
25 #include "mono_picture_frame.h"
26 #include "exceptions.h"
27 #include "util.h"
28 #include "rgb_xyz.h"
29 #include "colour_conversion.h"
30 #include "KM_fileio.h"
31 #include "AS_DCP.h"
32 #include "compose.hpp"
33 #include "j2k.h"
34
35 using std::string;
36 using boost::shared_ptr;
37 using boost::optional;
38 using namespace dcp;
39
40 /** Make a picture frame from a JPEG2000 file.
41  *  @param path Path to JPEG2000 file.
42  */
43 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
44 {
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");
48         if (!f) {
49                 boost::throw_exception (FileError ("could not open JPEG2000 file", path, errno));
50         }
51
52         fread (j2k_data(), 1, size, f);
53         fclose (f);
54
55         _buffer->Size (size);
56 }
57
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.
62  */
63 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, ASDCP::AESDecContext* c)
64 {
65         /* XXX: unfortunate guesswork on this buffer size */
66         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
67
68         if (ASDCP_FAILURE (reader->ReadFrame (n, *_buffer, c))) {
69                 boost::throw_exception (DCPReadError (String::compose ("could not read video frame %1", n)));
70         }
71 }
72
73 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
74 {
75         _buffer = new ASDCP::JP2K::FrameBuffer (size);
76         _buffer->Size (size);
77         memcpy (_buffer->Data(), data, size);
78 }
79
80 /** MonoPictureFrame destructor */
81 MonoPictureFrame::~MonoPictureFrame ()
82 {
83         delete _buffer;
84 }
85
86 /** @return Pointer to JPEG2000 data */
87 uint8_t const *
88 MonoPictureFrame::j2k_data () const
89 {
90         return _buffer->RoData ();
91 }
92
93 /** @return Pointer to JPEG2000 data */
94 uint8_t *
95 MonoPictureFrame::j2k_data ()
96 {
97         return _buffer->Data ();
98 }
99
100 /** @return Size of JPEG2000 data in bytes */
101 int
102 MonoPictureFrame::j2k_size () const
103 {
104         return _buffer->Size ();
105 }
106
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
109  *  reduction).
110  */
111 shared_ptr<OpenJPEGImage>
112 MonoPictureFrame::xyz_image (int reduce) const
113 {
114         return decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);
115 }