2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 * @brief Utility methods.
28 #include <boost/filesystem.hpp>
29 #include <openssl/sha.h>
31 #include "KM_fileio.h"
34 #include "exceptions.h"
36 #include "argb_frame.h"
40 using std::stringstream;
43 using boost::shared_ptr;
44 using namespace libdcp;
54 Kumu::GenRandomValue (id);
55 id.EncodeHex (buffer, 64);
56 return string (buffer);
60 /** Create a digest for a file.
61 * @param filename File name.
65 libdcp::make_digest (string filename)
67 Kumu::FileReader reader;
68 if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
69 throw FileError ("could not open file to compute digest", filename);
75 Kumu::ByteString read_buffer (65536);
79 Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read);
81 if (r == Kumu::RESULT_ENDOFFILE) {
83 } else if (ASDCP_FAILURE (r)) {
84 throw FileError ("could not read file to compute digest", filename);
87 SHA1_Update (&sha, read_buffer.Data(), read);
91 byte_t byte_buffer[20];
92 SHA1_Final (byte_buffer, &sha);
96 return Kumu::base64encode (byte_buffer, 20, digest, 64);
99 /** Convert a content kind to a string which can be used in a
100 * <ContentKind> node.
101 * @param kind ContentKind.
105 libdcp::content_kind_to_string (ContentKind kind)
117 return "transitional";
124 case PUBLIC_SERVICE_ANNOUNCEMENT:
127 return "advertisement";
133 /** Convert a string from a <ContentKind> node to a libdcp ContentKind.
134 * Reasonably tolerant about varying case.
135 * @param type Content kind string.
136 * @return libdcp ContentKind.
139 libdcp::content_kind_from_string (string type)
141 /* XXX: should probably just convert type to lower-case and have done with it */
143 if (type == "feature") {
145 } else if (type == "short") {
147 } else if (type == "trailer" || type == "Trailer") {
149 } else if (type == "test") {
151 } else if (type == "transitional") {
153 } else if (type == "rating") {
155 } else if (type == "teaser" || type == "Teaser") {
157 } else if (type == "policy") {
159 } else if (type == "psa") {
160 return PUBLIC_SERVICE_ANNOUNCEMENT;
161 } else if (type == "advertisement") {
162 return ADVERTISEMENT;
168 /** Decompress a JPEG2000 image to a bitmap.
169 * @param data JPEG2000 data.
170 * @param size Size of data in bytes.
171 * @param reduce A power of 2 by which to reduce the size of the decoded image;
172 * e.g. 0 reduces by (2^0 == 1), ie keeping the same size.
173 * 1 reduces by (2^1 == 2), ie halving the size of the image.
174 * This is useful for scaling 4K DCP images down to 2K.
175 * @return openjpeg image, which the caller must call opj_image_destroy() on.
178 libdcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
180 opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
181 opj_dparameters_t parameters;
182 opj_set_default_decoder_parameters (¶meters);
183 parameters.cp_reduce = reduce;
184 opj_setup_decoder (decoder, ¶meters);
185 opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
186 opj_image_t* image = opj_decode (decoder, cio);
188 opj_destroy_decompress (decoder);
190 throw DCPReadError ("could not decode JPEG2000 codestream");
195 image->x1 = rint (float(image->x1) / pow (2, reduce));
196 image->y1 = rint (float(image->y1) / pow (2, reduce));
200 /** Convert an openjpeg XYZ image to RGB.
201 * @param xyz_frame Frame in XYZ.
204 shared_ptr<ARGBFrame>
205 libdcp::xyz_to_rgb (opj_image_t* xyz_frame)
215 int* xyz_x = xyz_frame->comps[0].data;
216 int* xyz_y = xyz_frame->comps[1].data;
217 int* xyz_z = xyz_frame->comps[2].data;
219 shared_ptr<ARGBFrame> argb_frame (new ARGBFrame (Size (xyz_frame->x1, xyz_frame->y1)));
221 uint8_t* argb = argb_frame->data ();
223 for (int y = 0; y < xyz_frame->y1; ++y) {
224 uint8_t* argb_line = argb;
225 for (int x = 0; x < xyz_frame->x1; ++x) {
227 assert (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_x < 4096 && *xyz_z < 4096);
230 s.x = lut_in[*xyz_x++];
231 s.y = lut_in[*xyz_y++];
232 s.z = lut_in[*xyz_z++];
235 s.x /= DCI_COEFFICIENT;
236 s.y /= DCI_COEFFICIENT;
237 s.z /= DCI_COEFFICIENT;
240 d.r = ((s.x * color_matrix[0][0]) + (s.y * color_matrix[0][1]) + (s.z * color_matrix[0][2]));
241 d.g = ((s.x * color_matrix[1][0]) + (s.y * color_matrix[1][1]) + (s.z * color_matrix[1][2]));
242 d.b = ((s.x * color_matrix[2][0]) + (s.y * color_matrix[2][1]) + (s.z * color_matrix[2][2]));
244 d.r = min (d.r, 1.0);
245 d.r = max (d.r, 0.0);
247 d.g = min (d.g, 1.0);
248 d.g = max (d.g, 0.0);
250 d.b = min (d.b, 1.0);
251 d.b = max (d.b, 0.0);
254 *argb_line++ = lut_out[(int) (d.b * COLOR_DEPTH)];
255 *argb_line++ = lut_out[(int) (d.g * COLOR_DEPTH)];
256 *argb_line++ = lut_out[(int) (d.r * COLOR_DEPTH)];
260 argb += argb_frame->stride ();
266 /** @param s A string.
267 * @return true if the string contains only space, newline or tab characters, or is empty.
270 libdcp::empty_or_white_space (string s)
272 for (size_t i = 0; i < s.length(); ++i) {
273 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
281 bool libdcp::operator== (libdcp::Size const & a, libdcp::Size const & b)
283 return (a.width == b.width && a.height == b.height);
286 bool libdcp::operator!= (libdcp::Size const & a, libdcp::Size const & b)