ac7d7bf19acd8095eb1fc5da12f0a7026018dd85
[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 "util.h"
23 #include "rgb_xyz.h"
24 #include "colour_conversion.h"
25 #include "AS_DCP.h"
26 #include "KM_fileio.h"
27 #include <openjpeg.h>
28
29 #define DCI_GAMMA 2.6
30
31 using std::string;
32 using boost::shared_ptr;
33 using namespace dcp;
34
35 /** Make a picture frame from a 3D (stereoscopic) asset.
36  *  @param mxf_path Path to the asset's MXF file.
37  *  @param n Frame within the asset, not taking EntryPoint into account.
38  */
39 StereoPictureFrame::StereoPictureFrame (boost::filesystem::path mxf_path, int n)
40 {
41         ASDCP::JP2K::MXFSReader reader;
42         Kumu::Result_t r = reader.OpenRead (mxf_path.string().c_str());
43         if (ASDCP_FAILURE (r)) {
44                 boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path, r));
45         }
46
47         /* XXX: unfortunate guesswork on this buffer size */
48         _buffer = new ASDCP::JP2K::SFrameBuffer (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 StereoPictureFrame::StereoPictureFrame ()
56 {
57         _buffer = new ASDCP::JP2K::SFrameBuffer (4 * Kumu::Megabyte);
58 }
59
60 StereoPictureFrame::~StereoPictureFrame ()
61 {
62         delete _buffer;
63 }
64
65 /** @param eye Eye to return (EYE_LEFT or EYE_RIGHT).
66  *  @param reduce a factor by which to reduce the resolution
67  *  of the image, expressed as a power of two (pass 0 for no
68  *  reduction).
69  */
70 shared_ptr<XYZImage>
71 StereoPictureFrame::xyz_image (Eye eye, int reduce) const
72 {
73         switch (eye) {
74         case LEFT:
75                 return decompress_j2k (const_cast<uint8_t*> (_buffer->Left.RoData()), _buffer->Left.Size(), reduce);
76         case RIGHT:
77                 return decompress_j2k (const_cast<uint8_t*> (_buffer->Right.RoData()), _buffer->Right.Size(), reduce);
78         }
79         
80         return shared_ptr<XYZImage> ();
81 }
82
83 uint8_t const *
84 StereoPictureFrame::left_j2k_data () const
85 {
86         return _buffer->Left.RoData ();
87 }
88
89 uint8_t*
90 StereoPictureFrame::left_j2k_data ()
91 {
92         return _buffer->Left.Data ();
93 }
94
95 int
96 StereoPictureFrame::left_j2k_size () const
97 {
98         return _buffer->Left.Size ();
99 }
100
101 uint8_t const *
102 StereoPictureFrame::right_j2k_data () const
103 {
104         return _buffer->Right.RoData ();
105 }
106
107 uint8_t*
108 StereoPictureFrame::right_j2k_data ()
109 {
110         return _buffer->Right.Data ();
111 }
112
113 int
114 StereoPictureFrame::right_j2k_size () const
115 {
116         return _buffer->Right.Size ();
117 }