Bump version
[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 #include "i18n.h"
29
30 using std::cout;
31 using boost::shared_ptr;
32 using libdcp::Size;
33
34 ImageMagickDecoder::ImageMagickDecoder (
35         boost::shared_ptr<Film> f, DecodeOptions o)
36         : Decoder (f, o)
37         , VideoDecoder (f, o)
38 {
39         if (boost::filesystem::is_directory (_film->content_path())) {
40                 for (
41                         boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (_film->content_path());
42                         i != boost::filesystem::directory_iterator();
43                         ++i) {
44
45                         if (still_image_file (i->path().string())) {
46                                 _files.push_back (i->path().string());
47                         }
48                 }
49         } else {
50                 _files.push_back (_film->content_path ());
51         }
52
53         _iter = _files.begin ();
54 }
55
56 libdcp::Size
57 ImageMagickDecoder::native_size () const
58 {
59         if (_files.empty ()) {
60                 throw DecodeError (_("no still image files found"));
61         }
62
63         /* Look at the first file and assume its size holds for all */
64         using namespace MagickCore;
65         Magick::Image* image = new Magick::Image (_film->content_path ());
66         libdcp::Size const s = libdcp::Size (image->columns(), image->rows());
67         delete image;
68
69         return s;
70 }
71
72 bool
73 ImageMagickDecoder::pass ()
74 {
75         if (_iter == _files.end()) {
76                 if (video_frame() >= _film->still_duration_in_frames()) {
77                         return true;
78                 }
79
80                 emit_video (_image, true, double (video_frame()) / frames_per_second());
81                 return false;
82         }
83         
84         Magick::Image* magick_image = new Magick::Image (_film->content_path ());
85         
86         libdcp::Size size = native_size ();
87         shared_ptr<Image> image (new SimpleImage (PIX_FMT_RGB24, size, false));
88
89         using namespace MagickCore;
90         
91         uint8_t* p = image->data()[0];
92         for (int y = 0; y < size.height; ++y) {
93                 for (int x = 0; x < size.width; ++x) {
94                         Magick::Color c = magick_image->pixelColor (x, y);
95                         *p++ = c.redQuantum() * 255 / QuantumRange;
96                         *p++ = c.greenQuantum() * 255 / QuantumRange;
97                         *p++ = c.blueQuantum() * 255 / QuantumRange;
98                 }
99         }
100
101         delete magick_image;
102
103         _image = image->crop (_film->crop(), true);
104
105         emit_video (_image, false, double (video_frame()) / frames_per_second());
106
107         ++_iter;
108         return false;
109 }
110
111 PixelFormat
112 ImageMagickDecoder::pixel_format () const
113 {
114         /* XXX: always true? */
115         return PIX_FMT_RGB24;
116 }
117
118 bool
119 ImageMagickDecoder::seek_to_last ()
120 {
121         if (_iter == _files.end()) {
122                 _iter = _files.begin();
123         } else {
124                 --_iter;
125         }
126
127         return false;
128 }
129
130 bool
131 ImageMagickDecoder::seek (double t)
132 {
133         int const f = t * frames_per_second();
134         
135         _iter = _files.begin ();
136         for (int i = 0; i < f; ++i) {
137                 if (_iter == _files.end()) {
138                         return true;
139                 }
140                 ++_iter;
141         }
142         
143         return false;
144 }
145
146 void
147 ImageMagickDecoder::film_changed (Film::Property p)
148 {
149         if (p == Film::CROP) {
150                 OutputChanged ();
151         }
152 }
153
154 float
155 ImageMagickDecoder::frames_per_second () const
156 {
157         return _film->source_frame_rate ();
158 }