Hand-apply bd7102b476c631b1fa9067f18ce938d86073f6c8; single-file hashes.
[dcpomatic.git] / src / lib / encoded_data.cc
1 /*
2     Copyright (C) 2012-2015 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 #include "encoded_data.h"
21 #include "cross.h"
22 #include "exceptions.h"
23 #include "film.h"
24 #include "dcpomatic_socket.h"
25
26 #include "i18n.h"
27
28 using boost::shared_ptr;
29
30 EncodedData::EncodedData (int s)
31         : _data (new uint8_t[s])
32         , _size (s)
33 {
34
35 }
36
37 EncodedData::EncodedData (uint8_t const * d, int s)
38         : _data (new uint8_t[s])
39         , _size (s)
40 {
41         memcpy (_data, d, s);
42 }
43
44 EncodedData::EncodedData (boost::filesystem::path file)
45 {
46         _size = boost::filesystem::file_size (file);
47         _data = new uint8_t[_size];
48
49         FILE* f = fopen_boost (file, "rb");
50         if (!f) {
51                 throw FileError (_("could not open file for reading"), file);
52         }
53         
54         size_t const r = fread (_data, 1, _size, f);
55         if (r != size_t (_size)) {
56                 fclose (f);
57                 throw FileError (_("could not read encoded data"), file);
58         }
59                 
60         fclose (f);
61 }
62
63
64 EncodedData::~EncodedData ()
65 {
66         delete[] _data;
67 }
68
69 /** Write this data to a J2K file.
70  *  @param Film Film.
71  *  @param frame DCP frame index.
72  */
73 void
74 EncodedData::write (shared_ptr<const Film> film, int frame, Eyes eyes) const
75 {
76         boost::filesystem::path const tmp_j2c = film->j2c_path (frame, eyes, true);
77
78         FILE* f = fopen_boost (tmp_j2c, "wb");
79         
80         if (!f) {
81                 throw WriteFileError (tmp_j2c, errno);
82         }
83
84         fwrite (_data, 1, _size, f);
85         fclose (f);
86
87         boost::filesystem::path const real_j2c = film->j2c_path (frame, eyes, false);
88
89         /* Rename the file from foo.j2c.tmp to foo.j2c now that it is complete */
90         boost::filesystem::rename (tmp_j2c, real_j2c);
91 }
92
93 void
94 EncodedData::write_info (shared_ptr<const Film> film, int frame, Eyes eyes, dcp::FrameInfo info) const
95 {
96         FILE* file = fopen_boost (film->info_file(), "ab");
97         if (!file) {
98                 throw OpenFileError (film->info_file ());
99         }
100         write_frame_info (file, frame, eyes, info);
101         fclose (file);
102 }
103
104 /** Send this data to a socket.
105  *  @param socket Socket
106  */
107 void
108 EncodedData::send (shared_ptr<Socket> socket)
109 {
110         socket->write (_size);
111         socket->write (_data, _size);
112 }
113
114 LocallyEncodedData::LocallyEncodedData (uint8_t* d, int s)
115         : EncodedData (s)
116 {
117         memcpy (_data, d, s);
118 }
119
120 /** @param s Size of data in bytes */
121 RemotelyEncodedData::RemotelyEncodedData (int s)
122         : EncodedData (s)
123 {
124
125 }