Give Film a container; move crop into video content; other bits.
[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_content.h"
24 #include "imagemagick_decoder.h"
25 #include "image.h"
26 #include "film.h"
27 #include "exceptions.h"
28
29 #include "i18n.h"
30
31 using std::cout;
32 using boost::shared_ptr;
33 using libdcp::Size;
34
35 ImageMagickDecoder::ImageMagickDecoder (shared_ptr<const Film> f, shared_ptr<const ImageMagickContent> c)
36         : Decoder (f)
37         , VideoDecoder (f, c)
38         , _imagemagick_content (c)
39         , _position (0)
40 {
41
42 }
43
44 libdcp::Size
45 ImageMagickDecoder::native_size () const
46 {
47         if (!_native_size) {
48                 using namespace MagickCore;
49                 Magick::Image* image = new Magick::Image (_imagemagick_content->file().string());
50                 _native_size = libdcp::Size (image->columns(), image->rows());
51                 delete image;
52         }
53
54         return _native_size.get ();
55 }
56
57 int
58 ImageMagickDecoder::video_length () const
59 {
60         return _imagemagick_content->video_length ();
61 }
62
63 bool
64 ImageMagickDecoder::pass ()
65 {
66         if (_position < 0 || _position >= _imagemagick_content->video_length ()) {
67                 return true;
68         }
69
70         if (_image) {
71                 emit_video (_image, true, double (_position) / 24);
72                 _position++;
73                 return false;
74         }
75
76         Magick::Image* magick_image = new Magick::Image (_imagemagick_content->file().string ());
77         _native_size = libdcp::Size (magick_image->columns(), magick_image->rows());
78         
79         _image.reset (new SimpleImage (PIX_FMT_RGB24, _native_size.get(), false));
80
81         using namespace MagickCore;
82         
83         uint8_t* p = _image->data()[0];
84         for (int y = 0; y < _native_size->height; ++y) {
85                 for (int x = 0; x < _native_size->width; ++x) {
86                         Magick::Color c = magick_image->pixelColor (x, y);
87                         *p++ = c.redQuantum() * 255 / QuantumRange;
88                         *p++ = c.greenQuantum() * 255 / QuantumRange;
89                         *p++ = c.blueQuantum() * 255 / QuantumRange;
90                 }
91         }
92
93         delete magick_image;
94
95         _image = _image->crop (_imagemagick_content->crop(), true);
96         emit_video (_image, false, double (_position) / 24);
97
98         ++_position;
99         return false;
100 }
101
102 PixelFormat
103 ImageMagickDecoder::pixel_format () const
104 {
105         /* XXX: always true? */
106         return PIX_FMT_RGB24;
107 }
108
109 bool
110 ImageMagickDecoder::seek (double t)
111 {
112         int const f = t * _imagemagick_content->video_frame_rate ();
113
114         if (f >= _imagemagick_content->video_length()) {
115                 _position = 0;
116                 return true;
117         }
118
119         _position = f;
120         return false;
121 }