Merge master; at least partially.
[libdcp.git] / src / picture_frame.cc
1 /*
2     Copyright (C) 2012 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 #include <openjpeg.h>
21 #include "AS_DCP.h"
22 #include "KM_fileio.h"
23 #include "picture_frame.h"
24 #include "exceptions.h"
25 #include "argb_frame.h"
26 #include "lut.h"
27 #include "util.h"
28 #include "gamma_lut.h"
29
30 #define DCI_GAMMA 2.6
31
32 using std::string;
33 using boost::shared_ptr;
34 using namespace libdcp;
35
36 /** Make a picture frame from a 2D (monoscopic) asset.
37  *  @param mxf_path Path to the asset's MXF file.
38  *  @param n Frame within the asset, not taking EntryPoint into account.
39  */
40 MonoPictureFrame::MonoPictureFrame (string mxf_path, int n)
41 {
42         ASDCP::JP2K::MXFReader reader;
43         if (ASDCP_FAILURE (reader.OpenRead (mxf_path.c_str()))) {
44                 boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path));
45         }
46
47         /* XXX: unfortunate guesswork on this buffer size */
48         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
49
50         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer))) {
51                 boost::throw_exception (DCPReadError ("could not read video frame"));
52         }
53 }
54
55 MonoPictureFrame::~MonoPictureFrame ()
56 {
57         delete _buffer;
58 }
59
60 uint8_t const *
61 MonoPictureFrame::j2k_data () const
62 {
63         return _buffer->RoData ();
64 }
65
66 int
67 MonoPictureFrame::j2k_size () const
68 {
69         return _buffer->Size ();
70 }
71
72 /** @param reduce a factor by which to reduce the resolution
73  *  of the image, expressed as a power of two (pass 0 for no
74  *  reduction).
75  *
76  *  @return An ARGB representation of this frame.  This is ARGB in the
77  *  Cairo sense, so that each pixel takes up 4 bytes; the first byte
78  *  is blue, second green, third red and fourth alpha (always 255).
79  *
80  */
81 shared_ptr<ARGBFrame>
82 MonoPictureFrame::argb_frame (int reduce, float srgb_gamma) const
83 {
84         opj_image_t* xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);
85         assert (xyz_frame->numcomps == 3);
86         shared_ptr<ARGBFrame> f = xyz_to_rgb (xyz_frame, GammaLUT::cache.get (12, DCI_GAMMA), GammaLUT::cache.get (12, 1 / srgb_gamma));
87         opj_image_destroy (xyz_frame);
88         return f;
89 }
90
91 /** Make a picture frame from a 3D (stereoscopic) asset.
92  *  @param mxf_path Path to the asset's MXF file.
93  *  @param n Frame within the asset, not taking EntryPoint into account.
94  */
95 StereoPictureFrame::StereoPictureFrame (string mxf_path, int n)
96 {
97         ASDCP::JP2K::MXFSReader reader;
98         if (ASDCP_FAILURE (reader.OpenRead (mxf_path.c_str()))) {
99                 boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path));
100         }
101
102         /* XXX: unfortunate guesswork on this buffer size */
103         _buffer = new ASDCP::JP2K::SFrameBuffer (4 * Kumu::Megabyte);
104
105         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer))) {
106                 boost::throw_exception (DCPReadError ("could not read video frame"));
107         }
108 }
109
110 StereoPictureFrame::~StereoPictureFrame ()
111 {
112         delete _buffer;
113 }
114
115 /** @param reduce a factor by which to reduce the resolution
116  *  of the image, expressed as a power of two (pass 0 for no
117  *  reduction).
118  *
119  *  @param eye Eye to return (EYE_LEFT or EYE_RIGHT).
120  *
121  *  @return An ARGB representation of one of the eyes (left or right)
122  *  of this frame.  This is ARGB in the Cairo sense, so that each
123  *  pixel takes up 4 bytes; the first byte is blue, second green,
124  *  third red and fourth alpha (always 255).
125  *
126  */
127 shared_ptr<ARGBFrame>
128 StereoPictureFrame::argb_frame (Eye eye, int reduce, float srgb_gamma) const
129 {
130         opj_image_t* xyz_frame = 0;
131         switch (eye) {
132         case LEFT:
133                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Left.RoData()), _buffer->Left.Size(), reduce);
134                 break;
135         case RIGHT:
136                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Right.RoData()), _buffer->Right.Size(), reduce);
137                 break;
138         }
139         
140         assert (xyz_frame->numcomps == 3);
141         shared_ptr<ARGBFrame> f = xyz_to_rgb (xyz_frame, GammaLUT::cache.get (12, DCI_GAMMA), GammaLUT::cache.get (12, 1 / srgb_gamma));
142         opj_image_destroy (xyz_frame);
143         return f;
144 }
145
146 uint8_t const *
147 StereoPictureFrame::left_j2k_data () const
148 {
149         return _buffer->Left.RoData ();
150 }
151
152 int
153 StereoPictureFrame::left_j2k_size () const
154 {
155         return _buffer->Left.Size ();
156 }
157
158 uint8_t const *
159 StereoPictureFrame::right_j2k_data () const
160 {
161         return _buffer->Right.RoData ();
162 }
163
164 int
165 StereoPictureFrame::right_j2k_size () const
166 {
167         return _buffer->Right.Size ();
168 }