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