Fix missing version string when Popen communicate returns byte strings.
[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         size_t n = fread (j2k_data(), 1, size, f);
67         if (n != size) {
68                 boost::throw_exception (FileError ("could not read from JPEG2000 file", path, errno));
69         }
70
71         fclose (f);
72
73         _buffer->Size (size);
74 }
75
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.
80  */
81 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c)
82 {
83         /* XXX: unfortunate guesswork on this buffer size */
84         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
85
86         ASDCP::Result_t const r = reader->ReadFrame (n, *_buffer, c->context(), c->hmac());
87
88         if (ASDCP_FAILURE (r)) {
89                 boost::throw_exception (DCPReadError (String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
90         }
91 }
92
93 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
94 {
95         _buffer = new ASDCP::JP2K::FrameBuffer (size);
96         _buffer->Size (size);
97         memcpy (_buffer->Data(), data, size);
98 }
99
100 /** MonoPictureFrame destructor */
101 MonoPictureFrame::~MonoPictureFrame ()
102 {
103         delete _buffer;
104 }
105
106 /** @return Pointer to JPEG2000 data */
107 uint8_t const *
108 MonoPictureFrame::j2k_data () const
109 {
110         return _buffer->RoData ();
111 }
112
113 /** @return Pointer to JPEG2000 data */
114 uint8_t *
115 MonoPictureFrame::j2k_data ()
116 {
117         return _buffer->Data ();
118 }
119
120 /** @return Size of JPEG2000 data in bytes */
121 int
122 MonoPictureFrame::j2k_size () const
123 {
124         return _buffer->Size ();
125 }
126
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
129  *  reduction).
130  */
131 shared_ptr<OpenJPEGImage>
132 MonoPictureFrame::xyz_image (int reduce) const
133 {
134         return decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);
135 }