libdcp
mono_picture_frame.cc
Go to the documentation of this file.
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 "j2k_transcode.h"
45 #include "mono_picture_frame.h"
46 #include "rgb_xyz.h"
47 #include "util.h"
48 #include <asdcp/KM_fileio.h>
49 #include <asdcp/AS_DCP.h>
50 
51 
52 using std::make_shared;
53 using std::string;
54 using std::shared_ptr;
55 using boost::optional;
56 using namespace dcp;
57 
58 
59 MonoPictureFrame::MonoPictureFrame (boost::filesystem::path path)
60 {
61  auto const size = boost::filesystem::file_size (path);
62  _buffer.reset(new ASDCP::JP2K::FrameBuffer(size));
63  auto f = fopen_boost (path, "rb");
64  if (!f) {
65  boost::throw_exception (FileError("could not open JPEG2000 file", path, errno));
66  }
67 
68  size_t n = fread (data(), 1, size, f);
69  fclose (f);
70 
71  if (n != size) {
72  boost::throw_exception (FileError("could not read from JPEG2000 file", path, errno));
73  }
74 
75  _buffer->Size (size);
76 }
77 
78 
85 MonoPictureFrame::MonoPictureFrame (ASDCP::JP2K::MXFReader* reader, int n, shared_ptr<DecryptionContext> c, bool check_hmac)
86 {
87  /* XXX: unfortunate guesswork on this buffer size */
88  _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(4 * Kumu::Megabyte);
89 
90  auto const r = reader->ReadFrame (n, *_buffer, c->context(), check_hmac ? c->hmac() : nullptr);
91 
92  if (ASDCP_FAILURE(r)) {
93  boost::throw_exception (ReadError(String::compose ("could not read video frame %1 (%2)", n, static_cast<int>(r))));
94  }
95 }
96 
97 
98 MonoPictureFrame::MonoPictureFrame (uint8_t const * data, int size)
99 {
100  _buffer = make_shared<ASDCP::JP2K::FrameBuffer>(size);
101  _buffer->Size (size);
102  memcpy (_buffer->Data(), data, size);
103 }
104 
105 
106 uint8_t const *
108 {
109  return _buffer->RoData ();
110 }
111 
112 
113 uint8_t *
115 {
116  return _buffer->Data ();
117 }
118 
119 
120 int
122 {
123  return _buffer->Size ();
124 }
125 
126 
127 shared_ptr<OpenJPEGImage>
128 MonoPictureFrame::xyz_image (int reduce) const
129 {
130  return decompress_j2k (const_cast<uint8_t*>(_buffer->RoData()), _buffer->Size(), reduce);
131 }
An exception related to a file.
Definition: exceptions.h:56
std::shared_ptr< OpenJPEGImage > xyz_image(int reduce=0) const
MonoPictureFrame(boost::filesystem::path path)
uint8_t const * data() const override
int size() const override
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.
MonoPictureFrame 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)
FILE * fopen_boost(boost::filesystem::path, std::string)
Definition: util.cc:232
Conversion between RGB and XYZ.
Utility methods and classes.