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