98054b8dfed5ebf43fb2ee03231c30070808f2b8
[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 #include <iostream>
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::cout;
34 using boost::shared_ptr;
35 using boost::optional;
36 using boost::dynamic_pointer_cast;
37
38 MagickImageProxy::MagickImageProxy (boost::filesystem::path path)
39 {
40         /* Read the file into a Blob */
41
42         boost::uintmax_t const size = boost::filesystem::file_size (path);
43         FILE* f = fopen_boost (path, "rb");
44         if (!f) {
45                 throw OpenFileError (path);
46         }
47
48         uint8_t* data = new uint8_t[size];
49         if (fread (data, 1, size, f) != size) {
50                 delete[] data;
51                 throw ReadFileError (path);
52         }
53
54         fclose (f);
55         _blob.update (data, size);
56         delete[] data;
57 }
58
59 MagickImageProxy::MagickImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket)
60 {
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);
65         delete[] data;
66 }
67
68 shared_ptr<Image>
69 MagickImageProxy::image (optional<dcp::NoteHandler>) const
70 {
71         boost::mutex::scoped_lock lm (_mutex);
72
73         if (_image) {
74                 return _image;
75         }
76
77         Magick::Image* magick_image = 0;
78         string error;
79         try {
80                 magick_image = new Magick::Image (_blob);
81         } catch (Magick::Exception& e) {
82                 error = e.what ();
83         }
84
85         if (!magick_image) {
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.
89                 */
90                 try {
91                         magick_image = new Magick::Image (_blob, Magick::Geometry (0, 0), "TGA");
92                 } catch (...) {
93
94                 }
95         }
96
97         if (!magick_image) {
98                 /* If we failed both an auto-detect and a forced-Targa we give the error from
99                    the auto-detect.
100                 */
101                 throw DecodeError (String::compose (_("Could not decode image file (%1)"), error));
102         }
103
104         dcp::Size size (magick_image->columns(), magick_image->rows());
105
106         _image.reset (new Image (AV_PIX_FMT_RGB24, size, true));
107
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_HAVE_MAGICKCORE_NAMESPACE
112                 using namespace MagickCore;
113 #endif
114 #ifdef DCPOMATIC_HAVE_MAGICKLIB_NAMESPACE
115                 using namespace MagickLib;
116 #endif
117                 magick_image->write (0, i, size.width, 1, "RGB", CharPixel, p);
118                 p += _image->stride()[0];
119         }
120
121         delete magick_image;
122
123         return _image;
124 }
125
126 void
127 MagickImageProxy::add_metadata (xmlpp::Node* node) const
128 {
129         node->add_child("Type")->add_child_text (N_("Magick"));
130 }
131
132 void
133 MagickImageProxy::send_binary (shared_ptr<Socket> socket) const
134 {
135         socket->write (_blob.length ());
136         socket->write ((uint8_t *) _blob.data (), _blob.length ());
137 }
138
139 bool
140 MagickImageProxy::same (shared_ptr<const ImageProxy> other) const
141 {
142         shared_ptr<const MagickImageProxy> mp = dynamic_pointer_cast<const MagickImageProxy> (other);
143         if (!mp) {
144                 return false;
145         }
146
147         if (_blob.length() != mp->_blob.length()) {
148                 return false;
149         }
150
151         return memcmp (_blob.data(), mp->_blob.data(), _blob.length()) == 0;
152 }
153
154 AVPixelFormat
155 MagickImageProxy::pixel_format () const
156 {
157         return AV_PIX_FMT_RGB24;
158 }