Thumbs sort of have subs.
[dcpomatic.git] / src / lib / tiff_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/tiff_encoder.h
21  *  @brief An encoder that writes TIFF files (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 <tiffio.h>
32 #include "tiff_encoder.h"
33 #include "film.h"
34 #include "film_state.h"
35 #include "options.h"
36 #include "exceptions.h"
37 #include "image.h"
38 #include "subtitle.h"
39
40 using namespace std;
41 using namespace boost;
42
43 /** @param s FilmState of the film that we are encoding.
44  *  @param o Options.
45  *  @param l Log.
46  */
47 TIFFEncoder::TIFFEncoder (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
48         : Encoder (s, o, l)
49 {
50         
51 }
52
53 void
54 TIFFEncoder::process_video (shared_ptr<Image> image, int frame, shared_ptr<Subtitle> sub)
55 {
56         shared_ptr<Image> scaled = image->scale_and_convert_to_rgb (_opt->out_size, _opt->padding, _fs->scaler);
57         string tmp_file = _opt->frame_out_path (frame, true);
58         TIFF* output = TIFFOpen (tmp_file.c_str (), "w");
59         if (output == 0) {
60                 throw CreateFileError (tmp_file);
61         }
62                                                 
63         TIFFSetField (output, TIFFTAG_IMAGEWIDTH, _opt->out_size.width);
64         TIFFSetField (output, TIFFTAG_IMAGELENGTH, _opt->out_size.height);
65         TIFFSetField (output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
66         TIFFSetField (output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
67         TIFFSetField (output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
68         TIFFSetField (output, TIFFTAG_BITSPERSAMPLE, 8);
69         TIFFSetField (output, TIFFTAG_SAMPLESPERPIXEL, 3);
70         
71         if (TIFFWriteEncodedStrip (output, 0, scaled->data()[0], _opt->out_size.width * _opt->out_size.height * 3) == 0) {
72                 throw WriteFileError (tmp_file, 0);
73         }
74
75         TIFFClose (output);
76
77         filesystem::rename (tmp_file, _opt->frame_out_path (frame, false));
78
79         if (sub) {
80                 float const x_scale = float (_opt->out_size.width) / _fs->size.width;
81                 float const y_scale = float (_opt->out_size.height) / _fs->size.height;
82
83                 string tmp_metadata_file = _opt->frame_out_path (frame, false, ".sub");
84                 ofstream metadata (tmp_metadata_file.c_str ());
85                 
86                 list<shared_ptr<SubtitleImage> > images = sub->images ();
87                 int n = 0;
88                 for (list<shared_ptr<SubtitleImage> >::iterator i = images.begin(); i != images.end(); ++i) {
89                         stringstream ext;
90                         ext << ".sub." << n << ".tiff";
91                         
92                         string tmp_sub_file = _opt->frame_out_path (frame, true, ext.str ());
93                         output = TIFFOpen (tmp_sub_file.c_str(), "w");
94                         if (output == 0) {
95                                 throw CreateFileError (tmp_file);
96                         }
97
98                         Size new_size = (*i)->image()->size ();
99                         new_size.width *= x_scale;
100                         new_size.height *= y_scale;
101                         shared_ptr<Image> scaled = (*i)->image()->scale (new_size, _fs->scaler);
102                         
103                         TIFFSetField (output, TIFFTAG_IMAGEWIDTH, scaled->size().width);
104                         TIFFSetField (output, TIFFTAG_IMAGELENGTH, scaled->size().height);
105                         TIFFSetField (output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
106                         TIFFSetField (output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
107                         TIFFSetField (output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
108                         TIFFSetField (output, TIFFTAG_BITSPERSAMPLE, 8);
109                         TIFFSetField (output, TIFFTAG_SAMPLESPERPIXEL, 4);
110                 
111                         if (TIFFWriteEncodedStrip (output, 0, scaled->data()[0], scaled->size().width * scaled->size().height * 4) == 0) {
112                                 throw WriteFileError (tmp_file, 0);
113                         }
114                 
115                         TIFFClose (output);
116                         filesystem::rename (tmp_sub_file, _opt->frame_out_path (frame, false, ext.str ()));
117
118                         metadata << "image " << n << "\n"
119                                  << "x " << (*i)->position().x << "\n"
120                                  << "y " << (*i)->position().y << "\n";
121
122                         metadata.close ();
123                         filesystem::rename (tmp_metadata_file, _opt->frame_out_path (frame, false, ".sub"));
124                 }
125
126         }
127         
128         frame_done (frame);
129 }