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