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 namespace boost;
41 using namespace libdcp;
48 Kumu::GenRandomValue (id);
49 id.EncodeHex (buffer, 64);
50 return string (buffer);
54 libdcp::make_digest (string filename, sigc::signal1<void, float>* progress)
56 int const file_size = filesystem::file_size (filename);
58 Kumu::FileReader reader;
59 if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
60 throw FileError ("could not open file to compute digest", filename);
66 Kumu::ByteString read_buffer (65536);
70 Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read);
72 if (r == Kumu::RESULT_ENDOFFILE) {
74 } else if (ASDCP_FAILURE (r)) {
75 throw FileError ("could not read file to compute digest", filename);
78 SHA1_Update (&sha, read_buffer.Data(), read);
82 (*progress) (0.5 + (0.5 * done / file_size));
86 byte_t byte_buffer[20];
87 SHA1_Final (byte_buffer, &sha);
91 return Kumu::base64encode (byte_buffer, 20, digest, 64);
95 libdcp::content_kind_to_string (ContentKind kind)
107 return "transitional";
114 case PUBLIC_SERVICE_ANNOUNCEMENT:
117 return "advertisement";
124 libdcp::content_kind_from_string (string type)
126 if (type == "feature") {
128 } else if (type == "short") {
130 } else if (type == "trailer" || type == "Trailer") {
132 } else if (type == "test") {
134 } else if (type == "transitional") {
136 } else if (type == "rating") {
138 } else if (type == "teaser") {
140 } else if (type == "policy") {
142 } else if (type == "psa") {
143 return PUBLIC_SERVICE_ANNOUNCEMENT;
144 } else if (type == "advertisement") {
145 return ADVERTISEMENT;
152 libdcp::starts_with (string big, string little)
154 if (little.size() > big.size()) {
158 return big.substr (0, little.length()) == little;
162 libdcp::ends_with (string big, string little)
164 if (little.size() > big.size()) {
168 return big.compare (big.length() - little.length(), little.length(), little) == 0;
172 libdcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
174 opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
175 opj_dparameters_t parameters;
176 opj_set_default_decoder_parameters (¶meters);
177 parameters.cp_reduce = reduce;
178 opj_setup_decoder (decoder, ¶meters);
179 opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
180 opj_image_t* image = opj_decode (decoder, cio);
182 opj_destroy_decompress (decoder);
184 throw DCPReadError ("could not decode JPEG2000 codestream");
189 image->x1 /= pow (2, reduce);
190 image->y1 /= pow (2, reduce);
194 shared_ptr<ARGBFrame>
195 libdcp::xyz_to_rgb (opj_image_t* xyz_frame)
205 int* xyz_x = xyz_frame->comps[0].data;
206 int* xyz_y = xyz_frame->comps[1].data;
207 int* xyz_z = xyz_frame->comps[2].data;
209 shared_ptr<ARGBFrame> argb_frame (new ARGBFrame (xyz_frame->x1, xyz_frame->y1));
211 uint8_t* argb = argb_frame->data ();
213 for (int y = 0; y < xyz_frame->y1; ++y) {
214 uint8_t* argb_line = argb;
215 for (int x = 0; x < xyz_frame->x1; ++x) {
217 assert (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_x < 4096 && *xyz_z < 4096);
220 s.x = lut_in[*xyz_x++];
221 s.y = lut_in[*xyz_y++];
222 s.z = lut_in[*xyz_z++];
225 s.x /= DCI_COEFFICIENT;
226 s.y /= DCI_COEFFICIENT;
227 s.z /= DCI_COEFFICIENT;
230 d.r = ((s.x * color_matrix[0][0]) + (s.y * color_matrix[0][1]) + (s.z * color_matrix[0][2]));
231 d.g = ((s.x * color_matrix[1][0]) + (s.y * color_matrix[1][1]) + (s.z * color_matrix[1][2]));
232 d.b = ((s.x * color_matrix[2][0]) + (s.y * color_matrix[2][1]) + (s.z * color_matrix[2][2]));
234 d.r = min (d.r, 1.0);
235 d.r = max (d.r, 0.0);
237 d.g = min (d.g, 1.0);
238 d.g = max (d.g, 0.0);
240 d.b = min (d.b, 1.0);
241 d.b = max (d.b, 0.0);
244 *argb_line++ = lut_out[(int) (d.b * COLOR_DEPTH)];
245 *argb_line++ = lut_out[(int) (d.g * COLOR_DEPTH)];
246 *argb_line++ = lut_out[(int) (d.r * COLOR_DEPTH)];
250 argb += argb_frame->stride ();
257 libdcp::empty_or_white_space (string s)
259 for (size_t i = 0; i < s.length(); ++i) {
260 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {