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