Use a new ImageProxy to hold J2K data from DCP content.
[dcpomatic.git] / src / lib / image_proxy.cc
1 /*
2     Copyright (C) 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 <Magick++.h>
21 #include <dcp/util.h>
22 #include <dcp/raw_convert.h>
23 #include <dcp/mono_picture_frame.h>
24 #include <dcp/stereo_picture_frame.h>
25 #include "image_proxy.h"
26 #include "image.h"
27 #include "exceptions.h"
28 #include "cross.h"
29 #include "log.h"
30
31 #include "i18n.h"
32
33 #define LOG_TIMING(...) _log->microsecond_log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
34
35 using std::cout;
36 using std::string;
37 using std::stringstream;
38 using boost::shared_ptr;
39
40 ImageProxy::ImageProxy (shared_ptr<Log> log)
41         : _log (log)
42 {
43
44 }
45
46 RawImageProxy::RawImageProxy (shared_ptr<Image> image, shared_ptr<Log> log)
47         : ImageProxy (log)
48         , _image (image)
49 {
50
51 }
52
53 RawImageProxy::RawImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket, shared_ptr<Log> log)
54         : ImageProxy (log)
55 {
56         dcp::Size size (
57                 xml->number_child<int> ("Width"), xml->number_child<int> ("Height")
58                 );
59
60         _image.reset (new Image (static_cast<AVPixelFormat> (xml->number_child<int> ("PixelFormat")), size, true));
61         _image->read_from_socket (socket);
62 }
63
64 shared_ptr<Image>
65 RawImageProxy::image () const
66 {
67         return _image;
68 }
69
70 void
71 RawImageProxy::add_metadata (xmlpp::Node* node) const
72 {
73         node->add_child("Type")->add_child_text (N_("Raw"));
74         node->add_child("Width")->add_child_text (dcp::raw_convert<string> (_image->size().width));
75         node->add_child("Height")->add_child_text (dcp::raw_convert<string> (_image->size().height));
76         node->add_child("PixelFormat")->add_child_text (dcp::raw_convert<string> (_image->pixel_format ()));
77 }
78
79 void
80 RawImageProxy::send_binary (shared_ptr<Socket> socket) const
81 {
82         _image->write_to_socket (socket);
83 }
84
85 MagickImageProxy::MagickImageProxy (boost::filesystem::path path, shared_ptr<Log> log)
86         : ImageProxy (log)
87 {
88         /* Read the file into a Blob */
89         
90         boost::uintmax_t const size = boost::filesystem::file_size (path);
91         FILE* f = fopen_boost (path, "rb");
92         if (!f) {
93                 throw OpenFileError (path);
94         }
95                 
96         uint8_t* data = new uint8_t[size];
97         if (fread (data, 1, size, f) != size) {
98                 delete[] data;
99                 throw ReadFileError (path);
100         }
101         
102         fclose (f);
103         _blob.update (data, size);
104         delete[] data;
105 }
106
107 MagickImageProxy::MagickImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket, shared_ptr<Log> log)
108         : ImageProxy (log)
109 {
110         uint32_t const size = socket->read_uint32 ();
111         uint8_t* data = new uint8_t[size];
112         socket->read (data, size);
113         _blob.update (data, size);
114         delete[] data;
115 }
116
117 shared_ptr<Image>
118 MagickImageProxy::image () const
119 {
120         if (_image) {
121                 return _image;
122         }
123
124         LOG_TIMING ("[%1] MagickImageProxy begins decode and convert of %2 bytes", boost::this_thread::get_id(), _blob.length());
125
126         Magick::Image* magick_image = 0;
127         try {
128                 magick_image = new Magick::Image (_blob);
129         } catch (...) {
130                 throw DecodeError (_("Could not decode image file"));
131         }
132
133         dcp::Size size (magick_image->columns(), magick_image->rows());
134         LOG_TIMING ("[%1] MagickImageProxy decode finished", boost::this_thread::get_id ());
135
136         _image.reset (new Image (PIX_FMT_RGB24, size, true));
137
138         /* Write line-by-line here as _image must be aligned, and write() cannot be told about strides */
139         uint8_t* p = _image->data()[0];
140         for (int i = 0; i < size.height; ++i) {
141                 using namespace MagickCore;
142                 magick_image->write (0, i, size.width, 1, "RGB", CharPixel, p);
143                 p += _image->stride()[0];
144         }
145
146         delete magick_image;
147
148         LOG_TIMING ("[%1] MagickImageProxy completes decode and convert of %2 bytes", boost::this_thread::get_id(), _blob.length());
149
150         return _image;
151 }
152
153 void
154 MagickImageProxy::add_metadata (xmlpp::Node* node) const
155 {
156         node->add_child("Type")->add_child_text (N_("Magick"));
157 }
158
159 void
160 MagickImageProxy::send_binary (shared_ptr<Socket> socket) const
161 {
162         socket->write (_blob.length ());
163         socket->write ((uint8_t *) _blob.data (), _blob.length ());
164 }
165
166 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::MonoPictureFrame> frame, dcp::Size size, shared_ptr<Log> log)
167         : ImageProxy (log)
168         , _mono (frame)
169         , _size (size)
170 {
171         
172 }
173
174 J2KImageProxy::J2KImageProxy (shared_ptr<const dcp::StereoPictureFrame> frame, dcp::Size size, dcp::Eye eye, shared_ptr<Log> log)
175         : ImageProxy (log)
176         , _stereo (frame)
177         , _size (size)
178         , _eye (eye)
179 {
180
181 }
182
183 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket, shared_ptr<Log> log)
184         : ImageProxy (log)
185 {
186         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
187         if (xml->optional_number_child<int> ("Eye")) {
188                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
189                 int const left_size = xml->number_child<int> ("LeftSize");
190                 int const right_size = xml->number_child<int> ("RightSize");
191                 _stereo.reset (new dcp::StereoPictureFrame ());
192                 socket->read (_stereo->left_j2k_data(), left_size);
193                 socket->read (_stereo->right_j2k_data(), right_size);
194         } else {
195                 int const size = xml->number_child<int> ("Size");
196                 _mono.reset (new dcp::MonoPictureFrame ());
197                 socket->read (_mono->j2k_data (), size);
198         }
199 }
200
201 shared_ptr<Image>
202 J2KImageProxy::image ()
203 {
204         shared_ptr<Image> image (new Image (PIX_FMT_RGB24, _size, false));
205
206         if (_mono) {
207                 _mono->rgb_frame (image->data()[0]);
208         } else {
209                 _stereo->rgb_frame (image->data()[0], _eye);
210         }
211
212         return shared_ptr<Image> (new Image (image, true));
213 }
214
215 void
216 J2KImageProxy::add_metadata (xmlpp::Node* node) const
217 {
218         node->add_child("Width")->add_child_text (dcp::raw_convert<string> (_size.width));
219         node->add_child("Height")->add_child_text (dcp::raw_convert<string> (_size.height));
220         if (_stereo) {
221                 node->add_child("Eye")->add_child_text (dcp::raw_convert<string> (_eye));
222                 node->add_child("LeftSize")->add_child_text (dcp::raw_convert<string> (_stereo->left_j2k_size ()));
223                 node->add_child("RightSize")->add_child_text (dcp::raw_convert<string> (_stereo->right_j2k_size ()));
224         } else {
225                 node->add_child("Size")->add_child_text (dcp::raw_convert<string> (_mono->j2k_size ()));
226         }
227 }
228
229 void
230 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
231 {
232         if (_mono) {
233                 socket->write (_mono->j2k_data(), size);
234         } else {
235                 socket->write (_stereo->left_j2k_data(), _stereo->left_j2k_size ());
236                 socket->write (_stereo->right_j2k_data(), _stereo->right_j2k_size ());
237         }
238 }
239
240 shared_ptr<ImageProxy>
241 image_proxy_factory (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket, shared_ptr<Log> log)
242 {
243         if (xml->string_child("Type") == N_("Raw")) {
244                 return shared_ptr<ImageProxy> (new RawImageProxy (xml, socket, log));
245         } else if (xml->string_child("Type") == N_("Magick")) {
246                 return shared_ptr<MagickImageProxy> (new MagickImageProxy (xml, socket, log));
247         }
248
249         throw NetworkError (_("Unexpected image type received by server"));
250 }