7ac807cba27029c013ca0d00e864e1172a80dc02
[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     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
23     including the two.
24
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.
32 */
33
34 /** @file  src/mono_picture_frame.cc
35  *  @brief MonoPictureFrame class.
36  */
37
38 #include "mono_picture_frame.h"
39 #include "exceptions.h"
40 #include "util.h"
41 #include "rgb_xyz.h"
42 #include "colour_conversion.h"
43 #include "compose.hpp"
44 #include "j2k.h"
45 #include "crypto_context.h"
46 #include <asdcp/KM_fileio.h>
47 #include <asdcp/AS_DCP.h>
48
49 using std::string;
50 using boost::shared_ptr;
51 using boost::optional;
52 using namespace dcp;
53
54 /** Make a picture frame from a JPEG2000 file.
55  *  @param path Path to JPEG2000 file.
56  */
57 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
58 {
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");
62         if (!f) {
63                 boost::throw_exception (FileError ("could not open JPEG2000 file", path, errno));
64         }
65
66         fread (j2k_data(), 1, size, f);
67         fclose (f);
68
69         _buffer->Size (size);
70 }
71
72 /** Make a picture frame from a 2D (monoscopic) asset.
73  *  @param reader Reader for the asset's MXF file.
74  *  @param n Frame within the asset, not taking EntryPoint into account.
75  *  @param c Context for decryption, or 0.
76  */
77 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c)
78 {
79         /* XXX: unfortunate guesswork on this buffer size */
80         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
81
82         ASDCP::Result_t const r = reader->ReadFrame (n, *_buffer, c->context(), c->hmac());
83
84         if (ASDCP_FAILURE (r)) {
85                 boost::throw_exception (DCPReadError (String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
86         }
87 }
88
89 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
90 {
91         _buffer = new ASDCP::JP2K::FrameBuffer (size);
92         _buffer->Size (size);
93         memcpy (_buffer->Data(), data, size);
94 }
95
96 /** MonoPictureFrame destructor */
97 MonoPictureFrame::~MonoPictureFrame ()
98 {
99         delete _buffer;
100 }
101
102 /** @return Pointer to JPEG2000 data */
103 uint8_t const *
104 MonoPictureFrame::j2k_data () const
105 {
106         return _buffer->RoData ();
107 }
108
109 /** @return Pointer to JPEG2000 data */
110 uint8_t *
111 MonoPictureFrame::j2k_data ()
112 {
113         return _buffer->Data ();
114 }
115
116 /** @return Size of JPEG2000 data in bytes */
117 int
118 MonoPictureFrame::j2k_size () const
119 {
120         return _buffer->Size ();
121 }
122
123 /** @param reduce a factor by which to reduce the resolution
124  *  of the image, expressed as a power of two (pass 0 for no
125  *  reduction).
126  */
127 shared_ptr<OpenJPEGImage>
128 MonoPictureFrame::xyz_image (int reduce) const
129 {
130         return decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);
131 }