Move things round a bit.
[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 <boost/filesystem.hpp>
30 #include <tiffio.h>
31 #include "tiff_encoder.h"
32 #include "film.h"
33 #include "film_state.h"
34 #include "options.h"
35 #include "exceptions.h"
36 #include "image.h"
37
38 using namespace std;
39 using namespace boost;
40
41 /** @param s FilmState of the film that we are encoding.
42  *  @param o Options.
43  *  @param l Log.
44  */
45 TIFFEncoder::TIFFEncoder (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
46         : Encoder (s, o, l)
47 {
48         
49 }
50
51 void
52 TIFFEncoder::process_video (shared_ptr<Image> image, int frame)
53 {
54         shared_ptr<Image> scaled = image->scale_and_convert_to_rgb (_opt->out_size, _opt->padding, _fs->scaler);
55         string tmp_file = _opt->frame_out_path (frame, true);
56         TIFF* output = TIFFOpen (tmp_file.c_str (), "w");
57         if (output == 0) {
58                 throw CreateFileError (tmp_file);
59         }
60                                                 
61         TIFFSetField (output, TIFFTAG_IMAGEWIDTH, _opt->out_size.width);
62         TIFFSetField (output, TIFFTAG_IMAGELENGTH, _opt->out_size.height);
63         TIFFSetField (output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
64         TIFFSetField (output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
65         TIFFSetField (output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
66         TIFFSetField (output, TIFFTAG_BITSPERSAMPLE, 8);
67         TIFFSetField (output, TIFFTAG_SAMPLESPERPIXEL, 3);
68         
69         if (TIFFWriteEncodedStrip (output, 0, scaled->data()[0], _opt->out_size.width * _opt->out_size.height * 3) == 0) {
70                 throw WriteFileError (tmp_file, 0);
71         }
72
73         TIFFClose (output);
74
75         boost::filesystem::rename (tmp_file, _opt->frame_out_path (frame, false));
76         frame_done ();
77 }