Add Size operators; install util.h
[libdcp.git] / src / util.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/util.cc
21  *  @brief Utility methods.
22  */
23
24 #include <stdexcept>
25 #include <sstream>
26 #include <iostream>
27 #include <iomanip>
28 #include <boost/filesystem.hpp>
29 #include <openssl/sha.h>
30 #include "KM_util.h"
31 #include "KM_fileio.h"
32 #include "AS_DCP.h"
33 #include "util.h"
34 #include "exceptions.h"
35 #include "types.h"
36 #include "argb_frame.h"
37 #include "lut.h"
38
39 using std::string;
40 using std::stringstream;
41 using std::min;
42 using std::max;
43 using boost::shared_ptr;
44 using namespace libdcp;
45
46 /** Create a UUID.
47  *  @return UUID.
48  */
49 string
50 libdcp::make_uuid ()
51 {
52         char buffer[64];
53         Kumu::UUID id;
54         Kumu::GenRandomValue (id);
55         id.EncodeHex (buffer, 64);
56         return string (buffer);
57 }
58
59
60 /** Create a digest for a file.
61  *  @param filename File name.
62  *  @return Digest.
63  */
64 string
65 libdcp::make_digest (string filename)
66 {
67         Kumu::FileReader reader;
68         if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
69                 throw FileError ("could not open file to compute digest", filename);
70         }
71         
72         SHA_CTX sha;
73         SHA1_Init (&sha);
74         
75         Kumu::ByteString read_buffer (65536);
76         int done = 0;
77         while (1) {
78                 ui32_t read = 0;
79                 Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read);
80                 
81                 if (r == Kumu::RESULT_ENDOFFILE) {
82                         break;
83                 } else if (ASDCP_FAILURE (r)) {
84                         throw FileError ("could not read file to compute digest", filename);
85                 }
86                 
87                 SHA1_Update (&sha, read_buffer.Data(), read);
88                 done += read;
89         }
90
91         byte_t byte_buffer[20];
92         SHA1_Final (byte_buffer, &sha);
93
94         stringstream s;
95         char digest[64];
96         return Kumu::base64encode (byte_buffer, 20, digest, 64);
97 }
98
99 /** Convert a content kind to a string which can be used in a
100  *  <ContentKind> node.
101  *  @param kind ContentKind.
102  *  @return string.
103  */
104 string
105 libdcp::content_kind_to_string (ContentKind kind)
106 {
107         switch (kind) {
108         case FEATURE:
109                 return "feature";
110         case SHORT:
111                 return "short";
112         case TRAILER:
113                 return "trailer";
114         case TEST:
115                 return "test";
116         case TRANSITIONAL:
117                 return "transitional";
118         case RATING:
119                 return "rating";
120         case TEASER:
121                 return "teaser";
122         case POLICY:
123                 return "policy";
124         case PUBLIC_SERVICE_ANNOUNCEMENT:
125                 return "psa";
126         case ADVERTISEMENT:
127                 return "advertisement";
128         }
129
130         assert (false);
131 }
132
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.
137  */
138 libdcp::ContentKind
139 libdcp::content_kind_from_string (string type)
140 {
141         /* XXX: should probably just convert type to lower-case and have done with it */
142         
143         if (type == "feature") {
144                 return FEATURE;
145         } else if (type == "short") {
146                 return SHORT;
147         } else if (type == "trailer" || type == "Trailer") {
148                 return TRAILER;
149         } else if (type == "test") {
150                 return TEST;
151         } else if (type == "transitional") {
152                 return TRANSITIONAL;
153         } else if (type == "rating") {
154                 return RATING;
155         } else if (type == "teaser" || type == "Teaser") {
156                 return TEASER;
157         } else if (type == "policy") {
158                 return POLICY;
159         } else if (type == "psa") {
160                 return PUBLIC_SERVICE_ANNOUNCEMENT;
161         } else if (type == "advertisement") {
162                 return ADVERTISEMENT;
163         }
164
165         assert (false);
166 }
167
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.
176  */
177 opj_image_t *
178 libdcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
179 {
180         opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
181         opj_dparameters_t parameters;
182         opj_set_default_decoder_parameters (&parameters);
183         parameters.cp_reduce = reduce;
184         opj_setup_decoder (decoder, &parameters);
185         opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
186         opj_image_t* image = opj_decode (decoder, cio);
187         if (!image) {
188                 opj_destroy_decompress (decoder);
189                 opj_cio_close (cio);
190                 throw DCPReadError ("could not decode JPEG2000 codestream");
191         }
192
193         opj_cio_close (cio);
194
195         image->x1 = rint (float(image->x1) / pow (2, reduce));
196         image->y1 = rint (float(image->y1) / pow (2, reduce));
197         return image;
198 }
199
200 /** Convert an openjpeg XYZ image to RGB.
201  *  @param xyz_frame Frame in XYZ.
202  *  @return RGB image.
203  */
204 shared_ptr<ARGBFrame>
205 libdcp::xyz_to_rgb (opj_image_t* xyz_frame)
206 {
207         struct {
208                 double x, y, z;
209         } s;
210         
211         struct {
212                 double r, g, b;
213         } d;
214         
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;
218
219         shared_ptr<ARGBFrame> argb_frame (new ARGBFrame (Size (xyz_frame->x1, xyz_frame->y1)));
220
221         uint8_t* argb = argb_frame->data ();
222         
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) {
226
227                         assert (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_x < 4096 && *xyz_z < 4096);
228                         
229                         /* In gamma LUT */
230                         s.x = lut_in[*xyz_x++];
231                         s.y = lut_in[*xyz_y++];
232                         s.z = lut_in[*xyz_z++];
233                         
234                         /* DCI companding */
235                         s.x /= DCI_COEFFICIENT;
236                         s.y /= DCI_COEFFICIENT;
237                         s.z /= DCI_COEFFICIENT;
238                         
239                         /* XYZ to RGB */
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]));
243                         
244                         d.r = min (d.r, 1.0);
245                         d.r = max (d.r, 0.0);
246                         
247                         d.g = min (d.g, 1.0);
248                         d.g = max (d.g, 0.0);
249                         
250                         d.b = min (d.b, 1.0);
251                         d.b = max (d.b, 0.0);
252                         
253                         /* Out gamma LUT */
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)];
257                         *argb_line++ = 0xff;
258                 }
259                 
260                 argb += argb_frame->stride ();
261         }
262
263         return argb_frame;
264 }
265
266 /** @param s A string.
267  *  @return true if the string contains only space, newline or tab characters, or is empty.
268  */
269 bool
270 libdcp::empty_or_white_space (string s)
271 {
272         for (size_t i = 0; i < s.length(); ++i) {
273                 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
274                         return false;
275                 }
276         }
277
278         return true;
279 }
280
281 bool libdcp::operator== (libdcp::Size const & a, libdcp::Size const & b)
282 {
283         return (a.width == b.width && a.height == b.height);
284 }
285
286 bool libdcp::operator!= (libdcp::Size const & a, libdcp::Size const & b)
287 {
288         return !(a == b);
289 }
290