Remove LUT parent class.
[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_frame.h"
27 #include "gamma_lut.h"
28 #include "util.h"
29 #include "rgb_xyz.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 namespace dcp;
39
40 /** Make a picture frame from a 2D (monoscopic) asset.
41  *  @param mxf_path Path to the asset's MXF file.
42  *  @param n Frame within the asset, not taking EntryPoint into account.
43  *  @param c Context for decryption, or 0.
44  */
45 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path mxf_path, int n, ASDCP::AESDecContext* c)
46 {
47         ASDCP::JP2K::MXFReader reader;
48         Kumu::Result_t r = reader.OpenRead (mxf_path.string().c_str());
49         if (ASDCP_FAILURE (r)) {
50                 boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path, r));
51         }
52
53         /* XXX: unfortunate guesswork on this buffer size */
54         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
55
56         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer, c))) {
57                 boost::throw_exception (DCPReadError ("could not read video frame"));
58         }
59 }
60
61 /** MonoPictureFrame destructor */
62 MonoPictureFrame::~MonoPictureFrame ()
63 {
64         delete _buffer;
65 }
66
67 /** @return Pointer to JPEG2000 data */
68 uint8_t const *
69 MonoPictureFrame::j2k_data () const
70 {
71         return _buffer->RoData ();
72 }
73
74 /** @return Size of JPEG2000 data in bytes */
75 int
76 MonoPictureFrame::j2k_size () const
77 {
78         return _buffer->Size ();
79 }
80
81 /** @param reduce a factor by which to reduce the resolution
82  *  of the image, expressed as a power of two (pass 0 for no
83  *  reduction).
84  *  @param srgb_gamma Reciprocal of output gamma to use after
85  *  the conversion from XYZ to RGB.
86  *
87  *  @return An ARGB representation of this frame.  This is ARGB in the
88  *  Cairo sense, so that each pixel takes up 4 bytes; the first byte
89  *  is blue, second green, third red and fourth alpha (always 255).
90  */
91 shared_ptr<ARGBFrame>
92 MonoPictureFrame::argb_frame (int reduce, float srgb_gamma) const
93 {
94         return xyz_to_rgb (
95                 decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce),
96                 GammaLUT::cache.get (12, DCI_GAMMA), GammaLUT::cache.get (12, 1 / srgb_gamma)
97                 );
98 }