2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 /** @file src/openjpeg_image.cc
36 * @brief OpenJPEGImage class
40 #include "openjpeg_image.h"
41 #include "dcp_assert.h"
49 OpenJPEGImage::OpenJPEGImage (opj_image_t* image)
52 DCP_ASSERT (_opj_image->numcomps == 3);
56 OpenJPEGImage::OpenJPEGImage (OpenJPEGImage const & other)
58 _opj_image = reinterpret_cast<opj_image_t*>(malloc(sizeof(opj_image_t)));
59 DCP_ASSERT (_opj_image);
60 memcpy (_opj_image, other._opj_image, sizeof (opj_image_t));
62 int const data_size = _opj_image->x1 * _opj_image->y1 * 4;
64 _opj_image->comps = reinterpret_cast<opj_image_comp_t*> (malloc (_opj_image->numcomps * sizeof (opj_image_comp_t)));
65 DCP_ASSERT (_opj_image->comps);
66 memcpy (_opj_image->comps, other._opj_image->comps, _opj_image->numcomps * sizeof (opj_image_comp_t));
67 for (unsigned int i = 0; i < _opj_image->numcomps; ++i) {
68 _opj_image->comps[i].data = reinterpret_cast<OPJ_INT32*> (malloc (data_size));
69 DCP_ASSERT (_opj_image->comps[i].data);
70 memcpy (_opj_image->comps[i].data, other._opj_image->comps[i].data, data_size);
73 _opj_image->icc_profile_buf = reinterpret_cast<OPJ_BYTE*> (malloc (_opj_image->icc_profile_len));
74 DCP_ASSERT (_opj_image->icc_profile_buf);
75 memcpy (_opj_image->icc_profile_buf, other._opj_image->icc_profile_buf, _opj_image->icc_profile_len);
79 OpenJPEGImage::OpenJPEGImage (Size size)
85 OpenJPEGImage::OpenJPEGImage (uint8_t const * data_16, dcp::Size size, int stride)
90 for (int y = 0; y < size.height; ++y) {
91 uint16_t const * p = reinterpret_cast<uint16_t const *> (data_16 + y * stride);
92 for (int x = 0; x < size.width; ++x) {
93 /* Truncate 16-bit to 12-bit */
94 _opj_image->comps[0].data[jn] = *p++ >> 4;
95 _opj_image->comps[1].data[jn] = *p++ >> 4;
96 _opj_image->comps[2].data[jn] = *p++ >> 4;
104 OpenJPEGImage::create (Size size)
106 opj_image_cmptparm_t cmptparm[3];
108 for (int i = 0; i < 3; ++i) {
111 cmptparm[i].w = size.width;
112 cmptparm[i].h = size.height;
115 cmptparm[i].prec = 12;
116 cmptparm[i].bpp = 12;
117 cmptparm[i].sgnd = 0;
120 /* XXX: is this _SRGB right? */
121 _opj_image = opj_image_create (3, &cmptparm[0], OPJ_CLRSPC_SRGB);
122 if (_opj_image == nullptr) {
123 throw std::runtime_error ("could not create libopenjpeg image");
128 _opj_image->x1 = size.width;
129 _opj_image->y1 = size.height;
133 OpenJPEGImage::~OpenJPEGImage ()
135 opj_image_destroy (_opj_image);
140 OpenJPEGImage::data (int c) const
142 DCP_ASSERT (c >= 0 && c < 3);
143 return _opj_image->comps[c].data;
148 OpenJPEGImage::size () const
150 /* XXX: this may not be right; x0 and y0 can presumably be non-zero */
151 return dcp::Size (_opj_image->x1, _opj_image->y1);
156 OpenJPEGImage::precision (int component) const
158 return _opj_image->comps[component].prec;
163 OpenJPEGImage::factor (int component) const
165 return _opj_image->comps[component].factor;
170 OpenJPEGImage::srgb () const
172 return _opj_image->color_space == OPJ_CLRSPC_SRGB;