Change default MonoPictureFrame constructor to something more useful.
[libdcp.git] / src / mono_picture_frame.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/mono_picture_frame.cc
21  *  @brief MonoPictureFrame class.
22  */
23
24 #include "mono_picture_frame.h"
25 #include "exceptions.h"
26 #include "util.h"
27 #include "rgb_xyz.h"
28 #include "colour_conversion.h"
29 #include "KM_fileio.h"
30 #include "AS_DCP.h"
31 #include "compose.hpp"
32 #include "j2k.h"
33 #include <openjpeg.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 path Path to 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 (boost::filesystem::path path, int n, ASDCP::AESDecContext* c)
64 {
65         ASDCP::JP2K::MXFReader reader;
66         Kumu::Result_t r = reader.OpenRead (path.string().c_str());
67         if (ASDCP_FAILURE (r)) {
68                 boost::throw_exception (FileError ("could not open MXF file for reading", path, r));
69         }
70
71         /* XXX: unfortunate guesswork on this buffer size */
72         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
73
74         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer, c))) {
75                 boost::throw_exception (DCPReadError (String::compose ("could not read video frame %1 of %2", n, path.string())));
76         }
77 }
78
79 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
80 {
81         _buffer = new ASDCP::JP2K::FrameBuffer (size);
82         _buffer->Size (size);
83         memcpy (_buffer->Data(), data, size);
84 }
85
86 /** MonoPictureFrame destructor */
87 MonoPictureFrame::~MonoPictureFrame ()
88 {
89         delete _buffer;
90 }
91
92 /** @return Pointer to JPEG2000 data */
93 uint8_t const *
94 MonoPictureFrame::j2k_data () const
95 {
96         return _buffer->RoData ();
97 }
98
99 /** @return Pointer to JPEG2000 data */
100 uint8_t *
101 MonoPictureFrame::j2k_data ()
102 {
103         return _buffer->Data ();
104 }
105
106 /** @return Size of JPEG2000 data in bytes */
107 int
108 MonoPictureFrame::j2k_size () const
109 {
110         return _buffer->Size ();
111 }
112
113 /** @param reduce a factor by which to reduce the resolution
114  *  of the image, expressed as a power of two (pass 0 for no
115  *  reduction).
116  */
117 shared_ptr<OpenJPEGImage>
118 MonoPictureFrame::xyz_image (int reduce) const
119 {
120         return decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);
121 }