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