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