Fix missing version string when Popen communicate returns byte strings.
[libdcp.git] / src / openjpeg_image.cc
1 /*
2     Copyright (C) 2012-2015 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 /** @file  src/openjpeg_image.cc
35  *  @brief OpenJPEGImage class.
36  */
37
38 #include "openjpeg_image.h"
39 #include "dcp_assert.h"
40 #include <openjpeg.h>
41 #include <stdexcept>
42
43 using namespace dcp;
44
45 #ifdef LIBDCP_OPENJPEG1
46 #define OPJ_CLRSPC_SRGB CLRSPC_SRGB
47 #endif
48
49 /** Construct an OpenJPEGImage, taking ownership of the opj_image_t */
50 OpenJPEGImage::OpenJPEGImage (opj_image_t* image)
51         : _opj_image (image)
52 {
53         DCP_ASSERT (_opj_image->numcomps == 3);
54 }
55
56 #ifdef LIBDCP_OPENJPEG1
57 typedef int32_t OPJ_INT32;
58 typedef uint8_t OPJ_BYTE;
59 #endif
60
61 OpenJPEGImage::OpenJPEGImage (OpenJPEGImage const & other)
62 {
63         _opj_image = reinterpret_cast<opj_image_t*> (malloc (sizeof (opj_image_t)));
64         DCP_ASSERT (_opj_image);
65         memcpy (_opj_image, other._opj_image, sizeof (opj_image_t));
66
67         int const data_size = _opj_image->x1 * _opj_image->y1 * 4;
68
69         _opj_image->comps = reinterpret_cast<opj_image_comp_t*> (malloc (_opj_image->numcomps * sizeof (opj_image_comp_t)));
70         DCP_ASSERT (_opj_image->comps);
71         memcpy (_opj_image->comps, other._opj_image->comps, _opj_image->numcomps * sizeof (opj_image_comp_t));
72         for (unsigned int i = 0; i < _opj_image->numcomps; ++i) {
73                 _opj_image->comps[i].data = reinterpret_cast<OPJ_INT32*> (malloc (data_size));
74                 DCP_ASSERT (_opj_image->comps[i].data);
75                 memcpy (_opj_image->comps[i].data, other._opj_image->comps[i].data, data_size);
76         }
77
78         _opj_image->icc_profile_buf = reinterpret_cast<OPJ_BYTE*> (malloc (_opj_image->icc_profile_len));
79         DCP_ASSERT (_opj_image->icc_profile_buf);
80         memcpy (_opj_image->icc_profile_buf, other._opj_image->icc_profile_buf, _opj_image->icc_profile_len);
81 }
82
83 /** Construct a new OpenJPEGImage with undefined contents.
84  *  @param size Size for the frame in pixels.
85  */
86 OpenJPEGImage::OpenJPEGImage (Size size)
87 {
88         create (size);
89 }
90
91 /** @param data_16 XYZ/RGB image data in packed 16:16:16, 48bpp with
92  *  the 2-byte value for each component stored as little-endian.
93  */
94 OpenJPEGImage::OpenJPEGImage (uint8_t const * data_16, dcp::Size size, int stride)
95 {
96         create (size);
97
98         int jn = 0;
99         for (int y = 0; y < size.height; ++y) {
100                 uint16_t const * p = reinterpret_cast<uint16_t const *> (data_16 + y * stride);
101                 for (int x = 0; x < size.width; ++x) {
102                         /* Truncate 16-bit to 12-bit */
103                         _opj_image->comps[0].data[jn] = *p++ >> 4;
104                         _opj_image->comps[1].data[jn] = *p++ >> 4;
105                         _opj_image->comps[2].data[jn] = *p++ >> 4;
106                         ++jn;
107                 }
108         }
109 }
110
111 void
112 OpenJPEGImage::create (Size size)
113 {
114         opj_image_cmptparm_t cmptparm[3];
115
116         for (int i = 0; i < 3; ++i) {
117                 cmptparm[i].dx = 1;
118                 cmptparm[i].dy = 1;
119                 cmptparm[i].w = size.width;
120                 cmptparm[i].h = size.height;
121                 cmptparm[i].x0 = 0;
122                 cmptparm[i].y0 = 0;
123                 cmptparm[i].prec = 12;
124                 cmptparm[i].bpp = 12;
125                 cmptparm[i].sgnd = 0;
126         }
127
128         /* XXX: is this _SRGB right? */
129         _opj_image = opj_image_create (3, &cmptparm[0], OPJ_CLRSPC_SRGB);
130         if (_opj_image == 0) {
131                 throw std::runtime_error ("could not create libopenjpeg image");
132         }
133
134         _opj_image->x0 = 0;
135         _opj_image->y0 = 0;
136         _opj_image->x1 = size.width;
137         _opj_image->y1 = size.height;
138 }
139
140 /** OpenJPEGImage destructor */
141 OpenJPEGImage::~OpenJPEGImage ()
142 {
143         opj_image_destroy (_opj_image);
144 }
145
146 /** @param c Component index (0, 1 or 2)
147  *  @return Pointer to the data for component c.
148  */
149 int *
150 OpenJPEGImage::data (int c) const
151 {
152         DCP_ASSERT (c >= 0 && c < 3);
153         return _opj_image->comps[c].data;
154 }
155
156 /** @return Size of the image in pixels */
157 dcp::Size
158 OpenJPEGImage::size () const
159 {
160         /* XXX: this may not be right; x0 and y0 can presumably be non-zero */
161         return dcp::Size (_opj_image->x1, _opj_image->y1);
162 }
163
164 int
165 OpenJPEGImage::precision (int component) const
166 {
167         return _opj_image->comps[component].prec;
168 }
169
170 int
171 OpenJPEGImage::factor (int component) const
172 {
173         return _opj_image->comps[component].factor;
174 }
175
176 bool
177 OpenJPEGImage::srgb () const
178 {
179         return _opj_image->color_space == OPJ_CLRSPC_SRGB;
180 }