Merge master.
[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 "image_proxy.h"
24 #include "image.h"
25 #include "exceptions.h"
26 #include "cross.h"
27 #include "log.h"
28
29 #include "i18n.h"
30
31 #define LOG_TIMING(...) _log->microsecond_log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
32
33 using std::cout;
34 using std::string;
35 using std::stringstream;
36 using boost::shared_ptr;
37
38 ImageProxy::ImageProxy (shared_ptr<Log> log)
39         : _log (log)
40 {
41
42 }
43
44 RawImageProxy::RawImageProxy (shared_ptr<Image> image, shared_ptr<Log> log)
45         : ImageProxy (log)
46         , _image (image)
47 {
48
49 }
50
51 RawImageProxy::RawImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket, shared_ptr<Log> log)
52         : ImageProxy (log)
53 {
54         dcp::Size size (
55                 xml->number_child<int> ("Width"), xml->number_child<int> ("Height")
56                 );
57
58         _image.reset (new Image (static_cast<AVPixelFormat> (xml->number_child<int> ("PixelFormat")), size, true));
59         _image->read_from_socket (socket);
60 }
61
62 shared_ptr<Image>
63 RawImageProxy::image () const
64 {
65         return _image;
66 }
67
68 void
69 RawImageProxy::add_metadata (xmlpp::Node* node) const
70 {
71         node->add_child("Type")->add_child_text (N_("Raw"));
72         node->add_child("Width")->add_child_text (dcp::raw_convert<string> (_image->size().width));
73         node->add_child("Height")->add_child_text (dcp::raw_convert<string> (_image->size().height));
74         node->add_child("PixelFormat")->add_child_text (dcp::raw_convert<string> (_image->pixel_format ()));
75 }
76
77 void
78 RawImageProxy::send_binary (shared_ptr<Socket> socket) const
79 {
80         _image->write_to_socket (socket);
81 }
82
83 MagickImageProxy::MagickImageProxy (boost::filesystem::path path, shared_ptr<Log> log)
84         : ImageProxy (log)
85 {
86         /* Read the file into a Blob */
87         
88         boost::uintmax_t const size = boost::filesystem::file_size (path);
89         FILE* f = fopen_boost (path, "rb");
90         if (!f) {
91                 throw OpenFileError (path);
92         }
93                 
94         uint8_t* data = new uint8_t[size];
95         if (fread (data, 1, size, f) != size) {
96                 delete[] data;
97                 throw ReadFileError (path);
98         }
99         
100         fclose (f);
101         _blob.update (data, size);
102         delete[] data;
103 }
104
105 MagickImageProxy::MagickImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket, shared_ptr<Log> log)
106         : ImageProxy (log)
107 {
108         uint32_t const size = socket->read_uint32 ();
109         uint8_t* data = new uint8_t[size];
110         socket->read (data, size);
111         _blob.update (data, size);
112         delete[] data;
113 }
114
115 shared_ptr<Image>
116 MagickImageProxy::image () const
117 {
118         if (_image) {
119                 return _image;
120         }
121
122         LOG_TIMING ("[%1] MagickImageProxy begins decode and convert of %2 bytes", boost::this_thread::get_id(), _blob.length());
123
124         Magick::Image* magick_image = 0;
125         try {
126                 magick_image = new Magick::Image (_blob);
127         } catch (...) {
128                 throw DecodeError (_("Could not decode image file"));
129         }
130
131         dcp::Size size (magick_image->columns(), magick_image->rows());
132         LOG_TIMING ("[%1] MagickImageProxy decode finished", boost::this_thread::get_id ());
133
134         _image.reset (new Image (PIX_FMT_RGB24, size, true));
135
136         /* Write line-by-line here as _image must be aligned, and write() cannot be told about strides */
137         uint8_t* p = _image->data()[0];
138         for (int i = 0; i < size.height; ++i) {
139                 using namespace MagickCore;
140                 magick_image->write (0, i, size.width, 1, "RGB", CharPixel, p);
141                 p += _image->stride()[0];
142         }
143
144         delete magick_image;
145
146         LOG_TIMING ("[%1] MagickImageProxy completes decode and convert of %2 bytes", boost::this_thread::get_id(), _blob.length());
147
148         return _image;
149 }
150
151 void
152 MagickImageProxy::add_metadata (xmlpp::Node* node) const
153 {
154         node->add_child("Type")->add_child_text (N_("Magick"));
155 }
156
157 void
158 MagickImageProxy::send_binary (shared_ptr<Socket> socket) const
159 {
160         socket->write (_blob.length ());
161         socket->write ((uint8_t *) _blob.data (), _blob.length ());
162 }
163
164 shared_ptr<ImageProxy>
165 image_proxy_factory (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket, shared_ptr<Log> log)
166 {
167         if (xml->string_child("Type") == N_("Raw")) {
168                 return shared_ptr<ImageProxy> (new RawImageProxy (xml, socket, log));
169         } else if (xml->string_child("Type") == N_("Magick")) {
170                 return shared_ptr<MagickImageProxy> (new MagickImageProxy (xml, socket, log));
171         }
172
173         throw NetworkError (_("Unexpected image type received by server"));
174 }