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/tiff_decoder.cc
21 * @brief A decoder which reads a numbered set of TIFF files, one per frame.
28 #include <boost/shared_ptr.hpp>
29 #include <boost/filesystem.hpp>
32 #include <libavformat/avformat.h>
35 #include "tiff_decoder.h"
36 #include "exceptions.h"
43 using std::stringstream;
44 using boost::shared_ptr;
46 /** @param f Our Film.
48 * @param j Job that we are associated with, or 0.
50 TIFFDecoder::TIFFDecoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j)
53 string const dir = _film->content_path ();
55 if (!boost::filesystem::is_directory (dir)) {
56 throw DecodeError ("TIFF content must be in a directory");
59 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dir); i != boost::filesystem::directory_iterator(); ++i) {
60 /* Aah, the sweet smell of progress */
61 #if BOOST_FILESYSTEM_VERSION == 3
62 string const ext = boost::filesystem::path(*i).extension().string();
63 string const l = boost::filesystem::path(*i).leaf().generic_string();
65 string const ext = boost::filesystem::path(*i).extension();
66 string const l = i->leaf ();
68 if (ext == ".tif" || ext == ".tiff") {
75 _iter = _files.begin ();
79 TIFFDecoder::frames_per_second () const
86 TIFFDecoder::native_size () const
88 if (_files.empty ()) {
89 throw DecodeError ("no TIFF files found");
92 TIFF* t = TIFFOpen (file_path (_files.front()).c_str (), "r");
94 throw DecodeError ("could not open TIFF file");
98 TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
100 TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);
102 return Size (width, height);
106 TIFFDecoder::audio_channels () const
113 TIFFDecoder::audio_sample_rate () const
119 TIFFDecoder::audio_channel_layout () const
127 if (_iter == _files.end ()) {
131 TIFF* t = TIFFOpen (file_path (*_iter).c_str (), "r");
133 throw DecodeError ("could not open TIFF file");
137 TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
139 TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);
141 int const num_pixels = width * height;
142 uint32_t * raster = (uint32_t *) _TIFFmalloc (num_pixels * sizeof (uint32_t));
144 throw DecodeError ("could not allocate memory to decode TIFF file");
147 if (TIFFReadRGBAImage (t, width, height, raster, 0) < 0) {
148 throw DecodeError ("could not read TIFF data");
151 shared_ptr<CompactImage> image (new CompactImage (PIX_FMT_RGB24, Size (width, height)));
153 uint8_t* p = image->data()[0];
154 for (uint32_t y = 0; y < height; ++y) {
155 for (uint32_t x = 0; x < width; ++x) {
156 uint32_t const i = (height - y - 1) * width + x;
157 *p++ = raster[i] & 0xff;
158 *p++ = (raster[i] & 0xff00) >> 8;
159 *p++ = (raster[i] & 0xff0000) >> 16;
172 /** @param file name within our content directory
173 * @return full path to the file
176 TIFFDecoder::file_path (string f) const
179 s << _film->content_path() << "/" << f;
180 return _film->file (s.str ());
184 TIFFDecoder::pixel_format () const
186 return PIX_FMT_RGB24;
190 TIFFDecoder::time_base_numerator () const
192 return dcp_frame_rate(_film->frames_per_second()).frames_per_second;
197 TIFFDecoder::time_base_denominator () const
203 TIFFDecoder::sample_aspect_ratio_numerator () const
210 TIFFDecoder::sample_aspect_ratio_denominator () const