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