Merge master.
[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 boost::shared_ptr;
34
35 MagickImageProxy::MagickImageProxy (boost::filesystem::path path, shared_ptr<Log> log)
36         : ImageProxy (log)
37 {
38         /* Read the file into a Blob */
39         
40         boost::uintmax_t const size = boost::filesystem::file_size (path);
41         FILE* f = fopen_boost (path, "rb");
42         if (!f) {
43                 throw OpenFileError (path);
44         }
45                 
46         uint8_t* data = new uint8_t[size];
47         if (fread (data, 1, size, f) != size) {
48                 delete[] data;
49                 throw ReadFileError (path);
50         }
51         
52         fclose (f);
53         _blob.update (data, size);
54         delete[] data;
55 }
56
57 MagickImageProxy::MagickImageProxy (shared_ptr<cxml::Node>, shared_ptr<Socket> socket, shared_ptr<Log> log)
58         : ImageProxy (log)
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 () const
69 {
70         if (_image) {
71                 return _image;
72         }
73
74         LOG_TIMING ("[%1] MagickImageProxy begins decode and convert of %2 bytes", boost::this_thread::get_id(), _blob.length());
75
76         Magick::Image* magick_image = 0;
77         try {
78                 magick_image = new Magick::Image (_blob);
79         } catch (...) {
80                 throw DecodeError (_("Could not decode image file"));
81         }
82
83         dcp::Size size (magick_image->columns(), magick_image->rows());
84         LOG_TIMING ("[%1] MagickImageProxy decode finished", boost::this_thread::get_id ());
85
86         _image.reset (new Image (PIX_FMT_RGB24, size, true));
87
88         /* Write line-by-line here as _image must be aligned, and write() cannot be told about strides */
89         uint8_t* p = _image->data()[0];
90         for (int i = 0; i < size.height; ++i) {
91                 using namespace MagickCore;
92                 magick_image->write (0, i, size.width, 1, "RGB", CharPixel, p);
93                 p += _image->stride()[0];
94         }
95
96         delete magick_image;
97
98         LOG_TIMING ("[%1] MagickImageProxy completes decode and convert of %2 bytes", boost::this_thread::get_id(), _blob.length());
99
100         return _image;
101 }
102
103 void
104 MagickImageProxy::add_metadata (xmlpp::Node* node) const
105 {
106         node->add_child("Type")->add_child_text (N_("Magick"));
107 }
108
109 void
110 MagickImageProxy::send_binary (shared_ptr<Socket> socket) const
111 {
112         socket->write (_blob.length ());
113         socket->write ((uint8_t *) _blob.data (), _blob.length ());
114 }