c171b2c7f8a6048b45f18fe10b45d5fdf601aa0a
[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 namespace std;
40 using namespace boost;
41 using namespace libdcp;
42
43 string
44 libdcp::make_uuid ()
45 {
46         char buffer[64];
47         Kumu::UUID id;
48         Kumu::GenRandomValue (id);
49         id.EncodeHex (buffer, 64);
50         return string (buffer);
51 }
52
53 string
54 libdcp::make_digest (string filename, sigc::signal1<void, float>* progress)
55 {
56         int const file_size = filesystem::file_size (filename);
57         
58         Kumu::FileReader reader;
59         if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
60                 throw FileError ("could not open file to compute digest", filename);
61         }
62         
63         SHA_CTX sha;
64         SHA1_Init (&sha);
65         
66         Kumu::ByteString read_buffer (65536);
67         int done = 0;
68         while (1) {
69                 ui32_t read = 0;
70                 Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read);
71                 
72                 if (r == Kumu::RESULT_ENDOFFILE) {
73                         break;
74                 } else if (ASDCP_FAILURE (r)) {
75                         throw FileError ("could not read file to compute digest", filename);
76                 }
77                 
78                 SHA1_Update (&sha, read_buffer.Data(), read);
79                 done += read;
80
81                 if (progress) {
82                         (*progress) (0.5 + (0.5 * done / file_size));
83                 }
84         }
85
86         byte_t byte_buffer[20];
87         SHA1_Final (byte_buffer, &sha);
88
89         stringstream s;
90         char digest[64];
91         return Kumu::base64encode (byte_buffer, 20, digest, 64);
92 }
93
94 string
95 libdcp::content_kind_to_string (ContentKind kind)
96 {
97         switch (kind) {
98         case FEATURE:
99                 return "feature";
100         case SHORT:
101                 return "short";
102         case TRAILER:
103                 return "trailer";
104         case TEST:
105                 return "test";
106         case TRANSITIONAL:
107                 return "transitional";
108         case RATING:
109                 return "rating";
110         case TEASER:
111                 return "teaser";
112         case POLICY:
113                 return "policy";
114         case PUBLIC_SERVICE_ANNOUNCEMENT:
115                 return "psa";
116         case ADVERTISEMENT:
117                 return "advertisement";
118         }
119
120         assert (false);
121 }
122
123 libdcp::ContentKind
124 libdcp::content_kind_from_string (string type)
125 {
126         if (type == "feature") {
127                 return FEATURE;
128         } else if (type == "short") {
129                 return SHORT;
130         } else if (type == "trailer") {
131                 return TRAILER;
132         } else if (type == "test") {
133                 return TEST;
134         } else if (type == "transitional") {
135                 return TRANSITIONAL;
136         } else if (type == "rating") {
137                 return RATING;
138         } else if (type == "teaser") {
139                 return TEASER;
140         } else if (type == "policy") {
141                 return POLICY;
142         } else if (type == "psa") {
143                 return PUBLIC_SERVICE_ANNOUNCEMENT;
144         } else if (type == "advertisement") {
145                 return ADVERTISEMENT;
146         }
147
148         assert (false);
149 }
150                 
151 bool
152 libdcp::ends_with (string big, string little)
153 {
154         if (little.size() > big.size()) {
155                 return false;
156         }
157
158         return big.compare (big.length() - little.length(), little.length(), little) == 0;
159 }
160
161 opj_image_t *
162 libdcp::decompress_j2k (uint8_t* data, int64_t size, int reduce)
163 {
164         opj_dinfo_t* decoder = opj_create_decompress (CODEC_J2K);
165         opj_dparameters_t parameters;
166         opj_set_default_decoder_parameters (&parameters);
167         parameters.cp_reduce = reduce;
168         opj_setup_decoder (decoder, &parameters);
169         opj_cio_t* cio = opj_cio_open ((opj_common_ptr) decoder, data, size);
170         opj_image_t* image = opj_decode (decoder, cio);
171         if (!image) {
172                 opj_destroy_decompress (decoder);
173                 opj_cio_close (cio);
174                 throw DCPReadError ("could not decode JPEG2000 codestream");
175         }
176
177         opj_cio_close (cio);
178         return image;
179 }
180
181 shared_ptr<ARGBFrame>
182 libdcp::xyz_to_rgb (opj_image_t* xyz_frame)
183 {
184         struct {
185                 double x, y, z;
186         } s;
187         
188         struct {
189                 double r, g, b;
190         } d;
191         
192         int* xyz_x = xyz_frame->comps[0].data;
193         int* xyz_y = xyz_frame->comps[1].data;
194         int* xyz_z = xyz_frame->comps[2].data;
195
196         shared_ptr<ARGBFrame> argb_frame (new ARGBFrame (xyz_frame->x1, xyz_frame->y1));
197         
198         uint8_t* argb = argb_frame->data ();
199         
200         for (int y = 0; y < xyz_frame->y1; ++y) {
201                 uint8_t* argb_line = argb;
202                 for (int x = 0; x < xyz_frame->x1; ++x) {
203                         
204                         assert (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_x < 4096 && *xyz_z < 4096);
205                         
206                         /* In gamma LUT */
207                         s.x = lut_in[*xyz_x++];
208                         s.y = lut_in[*xyz_y++];
209                         s.z = lut_in[*xyz_z++];
210                         
211                         /* DCI companding */
212                         s.x /= DCI_COEFFICIENT;
213                         s.y /= DCI_COEFFICIENT;
214                         s.z /= DCI_COEFFICIENT;
215                         
216                         /* XYZ to RGB */
217                         d.r = ((s.x * color_matrix[0][0]) + (s.y * color_matrix[0][1]) + (s.z * color_matrix[0][2]));
218                         d.g = ((s.x * color_matrix[1][0]) + (s.y * color_matrix[1][1]) + (s.z * color_matrix[1][2]));
219                         d.b = ((s.x * color_matrix[2][0]) + (s.y * color_matrix[2][1]) + (s.z * color_matrix[2][2]));
220                         
221                         d.r = min (d.r, 1.0);
222                         d.r = max (d.r, 0.0);
223                         
224                         d.g = min (d.g, 1.0);
225                         d.g = max (d.g, 0.0);
226                         
227                         d.b = min (d.b, 1.0);
228                         d.b = max (d.b, 0.0);
229                         
230                         /* Out gamma LUT */
231                         *argb_line++ = lut_out[(int) (d.b * COLOR_DEPTH)];
232                         *argb_line++ = lut_out[(int) (d.g * COLOR_DEPTH)];
233                         *argb_line++ = lut_out[(int) (d.r * COLOR_DEPTH)];
234                         *argb_line++ = 0xff;
235                 }
236                 
237                 argb += argb_frame->stride ();
238         }
239
240         return argb_frame;
241 }