summaryrefslogtreecommitdiff
path: root/src/lib/tiff_decoder.cc
blob: 9058fcc2a5ba6ab82c8b94081c9f60e2b3517fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file src/tiff_decoder.cc
 *  @brief A decoder which reads a numbered set of TIFF files, one per frame.
 */

#include <vector>
#include <string>
#include <iostream>
#include <stdint.h>
#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>
#include <tiffio.h>
extern "C" {
#include <libavformat/avformat.h>
}
#include "util.h"
#include "tiff_decoder.h"
#include "exceptions.h"
#include "image.h"
#include "options.h"
#include "film.h"

using namespace std;
using namespace boost;

/** @param f Our Film.
 *  @param o Options.
 *  @param j Job that we are associated with, or 0.
 *  @param minimal true to do the bare minimum of work; just run through the content.  Useful for acquiring
 *  accurate frame counts as quickly as possible.  This generates no video or audio output.
 *  @param ignore_length Ignore the content's claimed length when computing progress.
 */
TIFFDecoder::TIFFDecoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal, bool ignore_length)
	: Decoder (f, o, j, minimal, ignore_length)
{
	string const dir = _film->content_path ();
	
	if (!filesystem::is_directory (dir)) {
		throw DecodeError ("TIFF content must be in a directory");
	}

	for (filesystem::directory_iterator i = filesystem::directory_iterator (dir); i != filesystem::directory_iterator(); ++i) {
		/* Aah, the sweet smell of progress */
#if BOOST_FILESYSTEM_VERSION == 3
		string const ext = filesystem::path(*i).extension().string();
		string const l = filesystem::path(*i).leaf().generic_string();
#else
		string const ext = filesystem::path(*i).extension();
		string const l = i->leaf ();
#endif
		if (ext == ".tif" || ext == ".tiff") {
			_files.push_back (l);
		}
	}

	_files.sort ();

	_iter = _files.begin ();

}

float
TIFFDecoder::frames_per_second () const
{
	/* We don't know */
	return 0;
}

Size
TIFFDecoder::native_size () const
{
	if (_files.empty ()) {
		throw DecodeError ("no TIFF files found");
	}
	
	TIFF* t = TIFFOpen (file_path (_files.front()).c_str (), "r");
	if (t == 0) {
		throw DecodeError ("could not open TIFF file");
	}

	uint32_t width;
	TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
	uint32_t height;
	TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);

	return Size (width, height);
}

int
TIFFDecoder::audio_channels () const
{
	/* No audio */
	return 0;
}

int
TIFFDecoder::audio_sample_rate () const
{
	return 0;
}

AVSampleFormat
TIFFDecoder::audio_sample_format () const
{
	return AV_SAMPLE_FMT_NONE;
}


int64_t
TIFFDecoder::audio_channel_layout () const
{
	return 0;
}

bool
TIFFDecoder::do_pass ()
{
	if (_iter == _files.end ()) {
		return true;
	}

	TIFF* t = TIFFOpen (file_path (*_iter).c_str (), "r");
	if (t == 0) {
		throw DecodeError ("could not open TIFF file");
	}

	uint32_t width;
	TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
	uint32_t height;
	TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);

	int const num_pixels = width * height;
	uint32_t * raster = (uint32_t *) _TIFFmalloc (num_pixels * sizeof (uint32_t));
	if (raster == 0) {
		throw DecodeError ("could not allocate memory to decode TIFF file");
	}

	if (TIFFReadRGBAImage (t, width, height, raster, 0) < 0) {
		throw DecodeError ("could not read TIFF data");
	}

	RGBFrameImage image (Size (width, height));

	uint8_t* p = image.data()[0];
	for (uint32_t y = 0; y < height; ++y) {
		for (uint32_t x = 0; x < width; ++x) {
			uint32_t const i = (height - y - 1) * width + x;
			*p++ = raster[i] & 0xff;
			*p++ = (raster[i] & 0xff00) >> 8;
			*p++ = (raster[i] & 0xff0000) >> 16;
		}
	}

	_TIFFfree (raster);
	TIFFClose (t);

	process_video (image.frame ());

	++_iter;
	return false;
}

/** @param file name within our content directory
 *  @return full path to the file
 */
string
TIFFDecoder::file_path (string f) const
{
	stringstream s;
	s << _film->content_path() << "/" << f;
	return _film->file (s.str ());
}

PixelFormat
TIFFDecoder::pixel_format () const
{
	return PIX_FMT_RGB24;
}

int
TIFFDecoder::time_base_numerator () const
{
	return rint (_film->frames_per_second());
}


int
TIFFDecoder::time_base_denominator () const
{
	return 1;
}

int
TIFFDecoder::sample_aspect_ratio_numerator () const
{
	/* XXX */
	return 1;
}

int
TIFFDecoder::sample_aspect_ratio_denominator () const
{
	/* XXX */
	return 1;
}