6b1436918e21b286b39b5b3e9018dea010751f0d
[dcpomatic.git] / src / lib / magick_image_proxy.cc
1 /*
2     Copyright (C) 2014-2015 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_image_proxy.h"
21 #include "cross.h"
22 #include "exceptions.h"
23 #include "dcpomatic_socket.h"
24 #include "image.h"
25 #include "compose.hpp"
26 #include <Magick++.h>
27 #include <libxml++/libxml++.h>
28
29 #include "i18n.h"
30
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::optional;
35 using boost::dynamic_pointer_cast;
36
37 MagickImageProxy::MagickImageProxy (boost::filesystem::path path)
38 {
39         /* Read the file into a Blob */
40
41         boost::uintmax_t const size = boost::filesystem::file_size (path);
42         FILE* f = fopen_boost (path, "rb");
43         if (!f) {
44                 throw OpenFileError (path);
45         }
46
47         uint8_t* data = new uint8_t[size];
48         if (fread (data, 1, size, f) != size) {
49                 delete[] data;
50                 throw ReadFileError (path);
51         }
52
53         fclose (f);
54         _blob.update (data, size);
55         delete[] data;
56 }
57
58 MagickImageProxy::MagickImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket)
59 {
60         uint32_t const size = socket->read_uint32 ();
61         uint8_t* data = new uint8_t[size];
62         socket->read (data, size);
63         _blob.update (data, size);
64         delete[] data;
65 }
66
67 shared_ptr<Image>
68 MagickImageProxy::image (optional<dcp::NoteHandler>) const
69 {
70         boost::mutex::scoped_lock lm (_mutex);
71
72         if (_image) {
73                 return _image;
74         }
75
76         Magick::Image* magick_image = 0;
77         string error;
78         try {
79                 magick_image = new Magick::Image (_blob);
80         } catch (Magick::Exception& e) {
81                 error = e.what ();
82         }
83
84         if (!magick_image) {
85                 /* ImageMagick cannot auto-detect Targa files, it seems, so try here with an
86                    explicit format.  I can't find it documented that passing a (0, 0) geometry
87                    is allowed, but it seems to work.
88                 */
89                 try {
90                         magick_image = new Magick::Image (_blob, Magick::Geometry (0, 0), "TGA");
91                 } catch (...) {
92
93                 }
94         }
95
96         if (!magick_image) {
97                 /* If we failed both an auto-detect and a forced-Targa we give the error from
98                    the auto-detect.
99                 */
100                 throw DecodeError (String::compose (_("Could not decode image file (%1)"), error));
101         }
102
103         dcp::Size size (magick_image->columns(), magick_image->rows());
104
105         _image.reset (new Image (PIX_FMT_RGB24, size, true));
106
107         /* Write line-by-line here as _image must be aligned, and write() cannot be told about strides */
108         uint8_t* p = _image->data()[0];
109         for (int i = 0; i < size.height; ++i) {
110 #ifdef DCPOMATIC_IMAGE_MAGICK
111                 using namespace MagickCore;
112 #else
113                 using namespace MagickLib;
114 #endif
115                 magick_image->write (0, i, size.width, 1, "RGB", CharPixel, p);
116                 p += _image->stride()[0];
117         }
118
119         delete magick_image;
120
121         return _image;
122 }
123
124 void
125 MagickImageProxy::add_metadata (xmlpp::Node* node) const
126 {
127         node->add_child("Type")->add_child_text (N_("Magick"));
128 }
129
130 void
131 MagickImageProxy::send_binary (shared_ptr<Socket> socket) const
132 {
133         socket->write (_blob.length ());
134         socket->write ((uint8_t *) _blob.data (), _blob.length ());
135 }
136
137 bool
138 MagickImageProxy::same (shared_ptr<const ImageProxy> other) const
139 {
140         shared_ptr<const MagickImageProxy> mp = dynamic_pointer_cast<const MagickImageProxy> (other);
141         if (!mp) {
142                 return false;
143         }
144
145         if (_blob.length() != mp->_blob.length()) {
146                 return false;
147         }
148
149         return memcmp (_blob.data(), mp->_blob.data(), _blob.length()) == 0;
150 }