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