2 Copyright (C) 2014-2015 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.
20 #include "magick_image_proxy.h"
22 #include "exceptions.h"
23 #include "dcpomatic_socket.h"
25 #include "compose.hpp"
27 #include <libxml++/libxml++.h>
34 using boost::shared_ptr;
35 using boost::optional;
36 using boost::dynamic_pointer_cast;
38 MagickImageProxy::MagickImageProxy (boost::filesystem::path path)
40 /* Read the file into a Blob */
42 boost::uintmax_t const size = boost::filesystem::file_size (path);
43 FILE* f = fopen_boost (path, "rb");
45 throw OpenFileError (path);
48 uint8_t* data = new uint8_t[size];
49 if (fread (data, 1, size, f) != size) {
51 throw ReadFileError (path);
55 _blob.update (data, size);
59 MagickImageProxy::MagickImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket)
61 uint32_t const size = socket->read_uint32 ();
62 uint8_t* data = new uint8_t[size];
63 socket->read (data, size);
64 _blob.update (data, size);
69 MagickImageProxy::image (optional<dcp::NoteHandler>) const
71 boost::mutex::scoped_lock lm (_mutex);
77 Magick::Image* magick_image = 0;
80 magick_image = new Magick::Image (_blob);
81 } catch (Magick::Exception& e) {
86 /* ImageMagick cannot auto-detect Targa files, it seems, so try here with an
87 explicit format. I can't find it documented that passing a (0, 0) geometry
88 is allowed, but it seems to work.
91 magick_image = new Magick::Image (_blob, Magick::Geometry (0, 0), "TGA");
98 /* If we failed both an auto-detect and a forced-Targa we give the error from
101 throw DecodeError (String::compose (_("Could not decode image file (%1)"), error));
104 dcp::Size size (magick_image->columns(), magick_image->rows());
106 _image.reset (new Image (PIX_FMT_RGB24, size, true));
108 /* Write line-by-line here as _image must be aligned, and write() cannot be told about strides */
109 uint8_t* p = _image->data()[0];
110 for (int i = 0; i < size.height; ++i) {
111 #ifdef DCPOMATIC_IMAGE_MAGICK
112 using namespace MagickCore;
114 using namespace MagickLib;
116 magick_image->write (0, i, size.width, 1, "RGB", CharPixel, p);
117 p += _image->stride()[0];
126 MagickImageProxy::add_metadata (xmlpp::Node* node) const
128 node->add_child("Type")->add_child_text (N_("Magick"));
132 MagickImageProxy::send_binary (shared_ptr<Socket> socket) const
134 socket->write (_blob.length ());
135 socket->write ((uint8_t *) _blob.data (), _blob.length ());
139 MagickImageProxy::same (shared_ptr<const ImageProxy> other) const
141 shared_ptr<const MagickImageProxy> mp = dynamic_pointer_cast<const MagickImageProxy> (other);
146 if (_blob.length() != mp->_blob.length()) {
150 return memcmp (_blob.data(), mp->_blob.data(), _blob.length()) == 0;