fd4e5b18df91aa00aeb4ea638f13c6258d5cb657
[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 <Magick++.h>
22 #include "imagemagick_decoder.h"
23 #include "image.h"
24 #include "film.h"
25
26 using namespace std;
27 using namespace boost;
28
29 ImageMagickDecoder::ImageMagickDecoder (
30         boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j)
31         : Decoder (f, o, j)
32         , _done (false)
33 {
34         _magick_image = new Magick::Image (_film->content_path ());
35 }
36
37 Size
38 ImageMagickDecoder::native_size () const
39 {
40         return Size (_magick_image->columns(), _magick_image->rows());
41 }
42
43 bool
44 ImageMagickDecoder::pass ()
45 {
46         using namespace MagickCore;
47         
48         if (_done) {
49                 return true;
50         }
51
52         Size size = native_size ();
53         CompactImage image (PIX_FMT_RGB24, size);
54
55         uint8_t* p = image.data()[0];
56         for (int y = 0; y < size.height; ++y) {
57                 for (int x = 0; x < size.width; ++x) {
58                         Magick::Color c = _magick_image->pixelColor (x, y);
59                         *p++ = c.redQuantum() * 255 / QuantumRange;
60                         *p++ = c.greenQuantum() * 255 / QuantumRange;
61                         *p++ = c.blueQuantum() * 255 / QuantumRange;
62                 }
63
64         }
65         
66         process_video ((AVFrame const *) image.picture());
67
68         _done = true;
69         return false;
70 }
71
72 PixelFormat
73 ImageMagickDecoder::pixel_format () const
74 {
75         return PIX_FMT_RGB24;
76 }
77