Rename XYZFrame -> XYZImage and ARGBFrame -> ARGBImage.
[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 "argb_image.h"
27 #include "util.h"
28 #include "rgb_xyz.h"
29 #include "colour_conversion.h"
30 #include "KM_fileio.h"
31 #include "AS_DCP.h"
32 #include <openjpeg.h>
33
34 #define DCI_GAMMA 2.6
35
36 using std::string;
37 using boost::shared_ptr;
38 using boost::optional;
39 using namespace dcp;
40
41 /** Make a picture frame from a JPEG2000 file.
42  *  @param path Path to JPEG2000 file.
43  */
44 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
45 {
46         boost::uintmax_t const size = boost::filesystem::file_size (path);
47         _buffer = new ASDCP::JP2K::FrameBuffer (size);
48         FILE* f = fopen_boost (path, "r");
49         if (!f) {
50                 boost::throw_exception (FileError ("could not open JPEG2000 file", path, errno));
51         }
52
53         fread (j2k_data(), 1, size, f);
54         fclose (f);
55
56         _buffer->Size (size);
57 }
58
59 /** Make a picture frame from a 2D (monoscopic) asset.
60  *  @param mxf_path Path to the asset's MXF file.
61  *  @param n Frame within the asset, not taking EntryPoint into account.
62  *  @param c Context for decryption, or 0.
63  */
64 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path mxf_path, int n, ASDCP::AESDecContext* c)
65 {
66         ASDCP::JP2K::MXFReader reader;
67         Kumu::Result_t r = reader.OpenRead (mxf_path.string().c_str());
68         if (ASDCP_FAILURE (r)) {
69                 boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path, r));
70         }
71
72         /* XXX: unfortunate guesswork on this buffer size */
73         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
74
75         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer, c))) {
76                 boost::throw_exception (DCPReadError ("could not read video frame"));
77         }
78 }
79
80 MonoPictureFrame::MonoPictureFrame ()
81 {
82         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
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  *  @param srgb_gamma Reciprocal of output gamma to use after
116  *  the conversion from XYZ to RGB.
117  *
118  *  @return An ARGB representation of this frame.  This is ARGB in the
119  *  Cairo sense, so that each pixel takes up 4 bytes; the first byte
120  *  is blue, second green, third red and fourth alpha (always 255).
121  */
122 shared_ptr<ARGBImage>
123 MonoPictureFrame::argb_image (int reduce) const
124 {
125         return xyz_to_rgba (
126                 decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce),
127                 ColourConversion::xyz_to_srgb ()
128                 );
129 }
130
131 void
132 MonoPictureFrame::rgb_frame (shared_ptr<Image> rgb, optional<NoteHandler> note) const
133 {
134         xyz_to_rgb (
135                 decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), 0),
136                 ColourConversion::xyz_to_srgb (),
137                 rgb,
138                 note
139                 );
140 }