2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/imagemagick_encoder.cc
21 * @brief An encoder that writes image files using ImageMagick (and does nothing with audio).
30 #include <boost/filesystem.hpp>
31 #include <Magick++/Image.h>
32 #include "imagemagick_encoder.h"
34 #include "film_state.h"
36 #include "exceptions.h"
41 using namespace boost;
43 /** @param s FilmState of the film that we are encoding.
47 ImageMagickEncoder::ImageMagickEncoder (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
54 ImageMagickEncoder::process_video (shared_ptr<Image> image, int frame, shared_ptr<Subtitle> sub)
56 shared_ptr<Image> scaled = image->scale_and_convert_to_rgb (_opt->out_size, _opt->padding, _fs->scaler);
58 string tmp_file = _opt->frame_out_path (frame, true);
59 Magick::Image thumb (_opt->out_size.width, _opt->out_size.height, "RGB", MagickCore::CharPixel, scaled->data()[0]);
61 thumb.write (tmp_file);
62 filesystem::rename (tmp_file, _opt->frame_out_path (frame, false));
65 float const x_scale = float (_opt->out_size.width) / _fs->size.width;
66 float const y_scale = float (_opt->out_size.height) / _fs->size.height;
68 string tmp_metadata_file = _opt->frame_out_path (frame, false, ".sub");
69 ofstream metadata (tmp_metadata_file.c_str ());
71 list<shared_ptr<SubtitleImage> > images = sub->images ();
73 for (list<shared_ptr<SubtitleImage> >::iterator i = images.begin(); i != images.end(); ++i) {
75 ext << ".sub." << n << ".png";
77 Size new_size = (*i)->image()->size ();
78 new_size.width *= x_scale;
79 new_size.height *= y_scale;
80 shared_ptr<Image> scaled = (*i)->image()->scale (new_size, _fs->scaler);
82 string tmp_sub_file = _opt->frame_out_path (frame, true, ext.str ());
83 Magick::Image sub_thumb (scaled->size().width, scaled->size().height, "RGBA", MagickCore::CharPixel, scaled->data()[0]);
84 sub_thumb.magick ("PNG");
85 sub_thumb.write (tmp_sub_file);
86 filesystem::rename (tmp_sub_file, _opt->frame_out_path (frame, false, ext.str ()));
88 metadata << "image " << n << "\n"
89 << "x " << (*i)->position().x << "\n"
90 << "y " << (*i)->position().y << "\n";
93 filesystem::rename (tmp_metadata_file, _opt->frame_out_path (frame, false, ".sub"));