Remove unused variable.
[libdcp.git] / src / stereo_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 #include "stereo_picture_frame.h"
21 #include "exceptions.h"
22 #include "argb_frame.h"
23 #include "util.h"
24 #include "rgb_xyz.h"
25 #include "colour_conversion.h"
26 #include "AS_DCP.h"
27 #include "KM_fileio.h"
28 #include <openjpeg.h>
29
30 #define DCI_GAMMA 2.6
31
32 using std::string;
33 using boost::shared_ptr;
34 using namespace dcp;
35
36 /** Make a picture frame from a 3D (stereoscopic) 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 StereoPictureFrame::StereoPictureFrame (boost::filesystem::path mxf_path, int n)
41 {
42         ASDCP::JP2K::MXFSReader reader;
43         Kumu::Result_t r = reader.OpenRead (mxf_path.string().c_str());
44         if (ASDCP_FAILURE (r)) {
45                 boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path, r));
46         }
47
48         /* XXX: unfortunate guesswork on this buffer size */
49         _buffer = new ASDCP::JP2K::SFrameBuffer (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 StereoPictureFrame::StereoPictureFrame ()
57 {
58         _buffer = new ASDCP::JP2K::SFrameBuffer (4 * Kumu::Megabyte);
59 }
60
61 StereoPictureFrame::~StereoPictureFrame ()
62 {
63         delete _buffer;
64 }
65
66 /** @param eye Eye to return (EYE_LEFT or EYE_RIGHT).
67  *  @param reduce a factor by which to reduce the resolution
68  *  of the image, expressed as a power of two (pass 0 for no
69  *  reduction).
70  *
71  *  @return An ARGB representation of one of the eyes (left or right)
72  *  of this frame.  This is ARGB in the Cairo sense, so that each
73  *  pixel takes up 4 bytes; the first byte is blue, second green,
74  *  third red and fourth alpha (always 255).
75  *
76  */
77 shared_ptr<ARGBFrame>
78 StereoPictureFrame::argb_frame (Eye eye, int reduce) const
79 {
80         shared_ptr<XYZFrame> xyz_frame;
81         switch (eye) {
82         case LEFT:
83                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Left.RoData()), _buffer->Left.Size(), reduce);
84                 break;
85         case RIGHT:
86                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Right.RoData()), _buffer->Right.Size(), reduce);
87                 break;
88         }
89         
90         return xyz_to_rgba (xyz_frame, ColourConversion::xyz_to_srgb ());
91 }
92
93 void
94 StereoPictureFrame::rgb_frame (Eye eye, uint16_t* buffer) const
95 {
96         shared_ptr<XYZFrame> xyz_frame;
97         switch (eye) {
98         case LEFT:
99                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Left.RoData()), _buffer->Left.Size(), 0);
100                 break;
101         case RIGHT:
102                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Right.RoData()), _buffer->Right.Size(), 0);
103                 break;
104         }
105         
106         return xyz_to_rgb (xyz_frame, ColourConversion::xyz_to_srgb (), buffer);
107 }
108
109 uint8_t const *
110 StereoPictureFrame::left_j2k_data () const
111 {
112         return _buffer->Left.RoData ();
113 }
114
115 uint8_t*
116 StereoPictureFrame::left_j2k_data ()
117 {
118         return _buffer->Left.Data ();
119 }
120
121 int
122 StereoPictureFrame::left_j2k_size () const
123 {
124         return _buffer->Left.Size ();
125 }
126
127 uint8_t const *
128 StereoPictureFrame::right_j2k_data () const
129 {
130         return _buffer->Right.RoData ();
131 }
132
133 uint8_t*
134 StereoPictureFrame::right_j2k_data ()
135 {
136         return _buffer->Right.Data ();
137 }
138
139 int
140 StereoPictureFrame::right_j2k_size () const
141 {
142         return _buffer->Right.Size ();
143 }