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