2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include <libdcp/util.h>
22 #include <libdcp/raw_convert.h>
23 #include "image_proxy.h"
25 #include "exceptions.h"
31 #define LOG_TIMING(...) _log->microsecond_log (String::compose (__VA_ARGS__), Log::TYPE_TIMING);
35 using std::stringstream;
36 using boost::shared_ptr;
38 ImageProxy::ImageProxy (shared_ptr<Log> log)
44 RawImageProxy::RawImageProxy (shared_ptr<Image> image, shared_ptr<Log> log)
51 RawImageProxy::RawImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket, shared_ptr<Log> log)
55 xml->number_child<int> ("Width"), xml->number_child<int> ("Height")
58 _image.reset (new Image (PIX_FMT_RGB24, size, true));
59 _image->read_from_socket (socket);
63 RawImageProxy::image () const
69 RawImageProxy::add_metadata (xmlpp::Node* node) const
71 node->add_child("Type")->add_child_text (N_("Raw"));
72 node->add_child("Width")->add_child_text (libdcp::raw_convert<string> (_image->size().width));
73 node->add_child("Height")->add_child_text (libdcp::raw_convert<string> (_image->size().height));
77 RawImageProxy::send_binary (shared_ptr<Socket> socket) const
79 _image->write_to_socket (socket);
82 MagickImageProxy::MagickImageProxy (boost::filesystem::path path, shared_ptr<Log> log)
85 /* Read the file into a Blob */
87 boost::uintmax_t const size = boost::filesystem::file_size (path);
88 FILE* f = fopen_boost (path, "rb");
90 throw OpenFileError (path);
93 uint8_t* data = new uint8_t[size];
94 if (fread (data, 1, size, f) != size) {
96 throw ReadFileError (path);
100 _blob.update (data, size);
104 MagickImageProxy::MagickImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket, shared_ptr<Log> log)
107 uint32_t const size = socket->read_uint32 ();
108 uint8_t* data = new uint8_t[size];
109 socket->read (data, size);
110 _blob.update (data, size);
115 MagickImageProxy::image () const
121 LOG_TIMING ("MagickImageProxy begins read and decode of %1 bytes", _blob.length());
123 Magick::Image* magick_image = 0;
125 magick_image = new Magick::Image (_blob);
127 throw DecodeError (_("Could not decode image file"));
130 libdcp::Size size (magick_image->columns(), magick_image->rows());
132 _image.reset (new Image (PIX_FMT_RGB24, size, true));
134 using namespace MagickCore;
136 uint8_t* p = _image->data()[0];
137 for (int y = 0; y < size.height; ++y) {
139 for (int x = 0; x < size.width; ++x) {
140 Magick::Color c = magick_image->pixelColor (x, y);
141 *q++ = c.redQuantum() * 255 / QuantumRange;
142 *q++ = c.greenQuantum() * 255 / QuantumRange;
143 *q++ = c.blueQuantum() * 255 / QuantumRange;
145 p += _image->stride()[0];
150 LOG_TIMING ("MagickImageProxy completes read and decode of %1 bytes", _blob.length());
156 MagickImageProxy::add_metadata (xmlpp::Node* node) const
158 node->add_child("Type")->add_child_text (N_("Magick"));
162 MagickImageProxy::send_binary (shared_ptr<Socket> socket) const
164 socket->write (_blob.length ());
165 socket->write ((uint8_t *) _blob.data (), _blob.length ());
168 shared_ptr<ImageProxy>
169 image_proxy_factory (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket, shared_ptr<Log> log)
171 if (xml->string_child("Type") == N_("Raw")) {
172 return shared_ptr<ImageProxy> (new RawImageProxy (xml, socket, log));
173 } else if (xml->string_child("Type") == N_("Magick")) {
174 return shared_ptr<MagickImageProxy> (new MagickImageProxy (xml, socket, log));
177 throw NetworkError (_("Unexpected image type received by server"));