libdcp
mono_j2k_picture_frame.cc
1 /*
2  Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3 
4  This file is part of libdcp.
5 
6  libdcp is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  libdcp is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with libdcp. If not, see <http://www.gnu.org/licenses/>.
18 
19  In addition, as a special exception, the copyright holders give
20  permission to link the code of portions of this program with the
21  OpenSSL library under certain conditions as described in each
22  individual source file, and distribute linked combinations
23  including the two.
24 
25  You must obey the GNU General Public License in all respects
26  for all of the code used other than OpenSSL. If you modify
27  file(s) with this exception, you may extend this exception to your
28  version of the file(s), but you are not obligated to do so. If you
29  do not wish to do so, delete this exception statement from your
30  version. If you delete this exception statement from all source
31  files in the program, then also delete it here.
32 */
33 
34 
40 #include "colour_conversion.h"
41 #include "compose.hpp"
42 #include "crypto_context.h"
43 #include "exceptions.h"
44 #include "file.h"
45 #include "filesystem.h"
46 #include "j2k_transcode.h"
47 #include "mono_j2k_picture_frame.h"
48 #include "rgb_xyz.h"
49 #include "util.h"
50 #include <asdcp/KM_fileio.h>
51 #include <asdcp/AS_DCP.h>
52 
53 
54 using std::make_shared;
55 using std::string;
56 using std::shared_ptr;
57 using boost::optional;
58 using namespace dcp;
59 
60 
61 MonoJ2KPictureFrame::MonoJ2KPictureFrame (boost::filesystem::path path)
62 {
63  auto const size = filesystem::file_size(path);
64  _buffer.reset(new ASDCP::JP2K::FrameBuffer(size));
65  File f(path, "rb");
66  if (!f) {
67  boost::throw_exception (FileError("could not open JPEG2000 file", path, errno));
68  }
69 
70  if (f.read(data(), 1, size) != size) {
71  boost::throw_exception (FileError("could not read from JPEG2000 file", path, errno));
72  }
73 
74  _buffer->Size (size);
75 }
76 
77 
84 MonoJ2KPictureFrame::MonoJ2KPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c, bool check_hmac)
85 {
86  /* XXX: unfortunate guesswork on this buffer size */
87  _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(4 * Kumu::Megabyte);
88 
89  auto const r = reader->ReadFrame (n, *_buffer, c->context(), check_hmac ? c->hmac() : nullptr);
90 
91  if (ASDCP_FAILURE(r)) {
92  boost::throw_exception (ReadError(String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
93  }
94 }
95 
96 
97 MonoJ2KPictureFrame::MonoJ2KPictureFrame (uint8_t const * data, int size)
98 {
99  _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(size);
100  _buffer->Size (size);
101  memcpy (_buffer->Data(), data, size);
102 }
103 
104 
105 uint8_t const *
107 {
108  return _buffer->RoData ();
109 }
110 
111 
112 uint8_t *
114 {
115  return _buffer->Data ();
116 }
117 
118 
119 int
121 {
122  return _buffer->Size ();
123 }
124 
125 
126 shared_ptr<OpenJPEGImage>
128 {
129  return decompress_j2k (const_cast<uint8_t*>(_buffer->RoData()), _buffer->Size(), reduce);
130 }
An exception related to a file.
Definition: exceptions.h:56
Definition: file.h:49
size_t read(void *ptr, size_t size, size_t nmemb)
Definition: file.cc:114
std::shared_ptr< OpenJPEGImage > xyz_image(int reduce=0) const
uint8_t const * data() const override
MonoJ2KPictureFrame(boost::filesystem::path path)
Any error that occurs when reading data from a DCP.
Definition: exceptions.h:106
ColourConversion class.
Exceptions thrown by libdcp.
Methods to encode and decode JPEG2000.
MonoJ2KPictureFrame class.
Namespace for everything in libdcp.
Definition: array_data.h:50
std::shared_ptr< OpenJPEGImage > decompress_j2k(uint8_t const *data, int64_t size, int reduce)
Conversion between RGB and XYZ.
Utility methods and classes.