Remove some unnecessary includes.
[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
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 path Path to 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 (boost::filesystem::path path, int n, ASDCP::AESDecContext* c)
63 {
64         ASDCP::JP2K::MXFReader reader;
65         Kumu::Result_t r = reader.OpenRead (path.string().c_str());
66         if (ASDCP_FAILURE (r)) {
67                 boost::throw_exception (FileError ("could not open MXF file for reading", path, r));
68         }
69
70         /* XXX: unfortunate guesswork on this buffer size */
71         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
72
73         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer, c))) {
74                 boost::throw_exception (DCPReadError (String::compose ("could not read video frame %1 of %2", n, path.string())));
75         }
76 }
77
78 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
79 {
80         _buffer = new ASDCP::JP2K::FrameBuffer (size);
81         _buffer->Size (size);
82         memcpy (_buffer->Data(), data, size);
83 }
84
85 /** MonoPictureFrame destructor */
86 MonoPictureFrame::~MonoPictureFrame ()
87 {
88         delete _buffer;
89 }
90
91 /** @return Pointer to JPEG2000 data */
92 uint8_t const *
93 MonoPictureFrame::j2k_data () const
94 {
95         return _buffer->RoData ();
96 }
97
98 /** @return Pointer to JPEG2000 data */
99 uint8_t *
100 MonoPictureFrame::j2k_data ()
101 {
102         return _buffer->Data ();
103 }
104
105 /** @return Size of JPEG2000 data in bytes */
106 int
107 MonoPictureFrame::j2k_size () const
108 {
109         return _buffer->Size ();
110 }
111
112 /** @param reduce a factor by which to reduce the resolution
113  *  of the image, expressed as a power of two (pass 0 for no
114  *  reduction).
115  */
116 shared_ptr<OpenJPEGImage>
117 MonoPictureFrame::xyz_image (int reduce) const
118 {
119         return decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);
120 }