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