Use boost::signals2; fix bugs with x-thread signalling.
[dcpomatic.git] / src / lib / imagemagick_encoder.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 /** @file src/imagemagick_encoder.cc
21  *  @brief An encoder that writes image files using ImageMagick (and does nothing with audio).
22  */
23
24 #include <stdexcept>
25 #include <vector>
26 #include <sstream>
27 #include <iomanip>
28 #include <iostream>
29 #include <fstream>
30 #include <boost/filesystem.hpp>
31 #include <Magick++/Image.h>
32 #include "imagemagick_encoder.h"
33 #include "film.h"
34 #include "options.h"
35 #include "exceptions.h"
36 #include "image.h"
37 #include "subtitle.h"
38
39 using std::string;
40 using std::ofstream;
41 using boost::shared_ptr;
42
43 /** @param f Film that we are encoding.
44  *  @param o Options.
45  */
46 ImageMagickEncoder::ImageMagickEncoder (shared_ptr<const Film> f, shared_ptr<const Options> o)
47         : Encoder (f, o)
48 {
49         
50 }
51
52 void
53 ImageMagickEncoder::process_video (shared_ptr<Image> image, int frame, shared_ptr<Subtitle> sub)
54 {
55         shared_ptr<Image> scaled = image->scale_and_convert_to_rgb (_opt->out_size, _opt->padding, _film->scaler());
56         shared_ptr<Image> compact (new CompactImage (scaled));
57
58         string tmp_file = _opt->frame_out_path (frame, true);
59         Magick::Image thumb (compact->size().width, compact->size().height, "RGB", MagickCore::CharPixel, compact->data()[0]);
60         thumb.magick ("PNG");
61         thumb.write (tmp_file);
62         boost::filesystem::rename (tmp_file, _opt->frame_out_path (frame, false));
63
64         if (sub) {
65                 float const x_scale = float (_opt->out_size.width) / _film->size().width;
66                 float const y_scale = float (_opt->out_size.height) / _film->size().height;
67
68                 string tmp_metadata_file = _opt->frame_out_path (frame, false, ".sub");
69                 ofstream metadata (tmp_metadata_file.c_str ());
70                 
71                 Size new_size = sub->image()->size ();
72                 new_size.width *= x_scale;
73                 new_size.height *= y_scale;
74                 shared_ptr<Image> scaled = sub->image()->scale (new_size, _film->scaler());
75                 shared_ptr<Image> compact (new CompactImage (scaled));
76                 
77                 string tmp_sub_file = _opt->frame_out_path (frame, true, ".sub.png");
78                 Magick::Image sub_thumb (compact->size().width, compact->size().height, "RGBA", MagickCore::CharPixel, compact->data()[0]);
79                 sub_thumb.magick ("PNG");
80                 sub_thumb.write (tmp_sub_file);
81                 boost::filesystem::rename (tmp_sub_file, _opt->frame_out_path (frame, false, ".sub.png"));
82
83                 metadata << "x " << sub->position().x << "\n"
84                          << "y " << sub->position().y << "\n";
85
86                 metadata.close ();
87                 boost::filesystem::rename (tmp_metadata_file, _opt->frame_out_path (frame, false, ".sub"));
88         }
89         
90         frame_done (frame);
91 }