7faf74c6196c42b72a82356136a303218a8509e3
[dcpomatic.git] / src / lib / imagemagick_decoder.cc
1 /*
2     Copyright (C) 2012 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 <iostream>
21 #include <boost/filesystem.hpp>
22 #include <Magick++.h>
23 #include "imagemagick_decoder.h"
24 #include "image.h"
25 #include "film.h"
26 #include "exceptions.h"
27
28 using std::cout;
29 using boost::shared_ptr;
30
31 ImageMagickDecoder::ImageMagickDecoder (
32         boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j)
33         : Decoder (f, o, j)
34 {
35         if (boost::filesystem::is_directory (_film->content_path())) {
36                 for (
37                         boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (_film->content_path());
38                         i != boost::filesystem::directory_iterator();
39                         ++i) {
40
41                         if (still_image_file (i->path().string())) {
42                                 _files.push_back (i->path().string());
43                         }
44                 }
45         } else {
46                 _files.push_back (_film->content_path ());
47         }
48
49         _iter = _files.begin ();
50 }
51
52 Size
53 ImageMagickDecoder::native_size () const
54 {
55         if (_files.empty ()) {
56                 throw DecodeError ("no still image files found");
57         }
58
59         /* Look at the first file and assume its size holds for all */
60         using namespace MagickCore;
61         Magick::Image* image = new Magick::Image (_film->content_path ());
62         Size const s = Size (image->columns(), image->rows());
63         delete image;
64
65         return s;
66 }
67
68 bool
69 ImageMagickDecoder::pass ()
70 {
71         if (_iter == _files.end()) {
72                 return true;
73         }
74         
75         using namespace MagickCore;
76
77         Magick::Image* magick_image = new Magick::Image (_film->content_path ());
78         
79         Size size = native_size ();
80         shared_ptr<CompactImage> image (new CompactImage (PIX_FMT_RGB24, size));
81
82         uint8_t* p = image->data()[0];
83         for (int y = 0; y < size.height; ++y) {
84                 for (int x = 0; x < size.width; ++x) {
85                         Magick::Color c = magick_image->pixelColor (x, y);
86                         *p++ = c.redQuantum() * 255 / QuantumRange;
87                         *p++ = c.greenQuantum() * 255 / QuantumRange;
88                         *p++ = c.blueQuantum() * 255 / QuantumRange;
89                 }
90         }
91
92         delete magick_image;
93         
94         emit_video (image);
95
96         ++_iter;
97         return false;
98 }
99
100 PixelFormat
101 ImageMagickDecoder::pixel_format () const
102 {
103         /* XXX: always true? */
104         return PIX_FMT_RGB24;
105 }
106