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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/* This file is part of ksquirrel-libs (http://ksquirrel.sf.net)
Copyright (c) 2004 Dmitry Baryshev <ksquirrel@tut.by>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later
version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
as32 with this library; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <tiffio.h>
#include "ksquirrel-libs/fmt_types.h"
#include "ksquirrel-libs/fmt_utils.h"
#include "ksquirrel-libs/fileio.h"
#include "ksquirrel-libs/error.h"
#include "fmt_codec_tiff_defs.h"
#include "fmt_codec_tiff.h"
#include "../xpm/codec_tiff.xpm"
/*
*
* The TIFF specification was originally released in
* 1986 by Aldus Corporation as a standard method of storing
* black-and-white images created by scanners and desktop publishing
* applications. This first public release of TIFF was
* the third major revision of the TIFF format, and
* although it was not assigned a specific version number, this release
* may be thought of as TIFF Revision 3.0. The first
* widely used revision of TIFF, 4.0, was released in
* April 1987. TIFF 4.0 added support for uncompressed
* RGB color images and was quickly followed by the
* release of TIFF Revision 5.0 in August
* 1988. TIFF 5.0 was the first revision to add the
* capability of storing palette color images and support for the
* LZW compression algorithm. TIFF 6.0 was released in June
* 1992 and added support for CMYK and YCbCr color
*
*/
fmt_codec::fmt_codec() : fmt_codec_base()
{
compr[COMPRESSION_NONE] = "None";
compr[COMPRESSION_CCITTRLE] = "CCITTRLE";
compr[COMPRESSION_CCITTFAX3] = "CCITTFAX3";
compr[COMPRESSION_CCITT_T4] = "CCITT_T4";
compr[COMPRESSION_CCITTFAX4] = "CCITTFAX4";
compr[COMPRESSION_CCITT_T6] = "CCITT_T6";
compr[COMPRESSION_LZW] = "LZW";
compr[COMPRESSION_OJPEG] = "OJPEG";
compr[COMPRESSION_JPEG] = "JPEG";
compr[COMPRESSION_NEXT] = "NEXT";
compr[COMPRESSION_CCITTRLEW] = "CCITTRLEW";
compr[COMPRESSION_PACKBITS] = "PACKBITS";
compr[COMPRESSION_THUNDERSCAN] = "THUNDERSCAN";
compr[COMPRESSION_IT8CTPAD] = "IT8CTPAD";
compr[COMPRESSION_IT8LW] = "IT8LW";
compr[COMPRESSION_IT8MP] = "IT8MP";
compr[COMPRESSION_IT8BL] = "IT8BL";
compr[COMPRESSION_PIXARFILM] = "PIXARFILM";
compr[COMPRESSION_PIXARLOG] = "PIXARLOG";
compr[COMPRESSION_DEFLATE] = "DEFLATE";
compr[COMPRESSION_ADOBE_DEFLATE] = "Adobe DEFLATE";
compr[COMPRESSION_DCS] = "DCS";
compr[COMPRESSION_JBIG] = "JBIG";
compr[COMPRESSION_SGILOG] = "SGILOG";
compr[COMPRESSION_SGILOG24] = "SGILOG24";
compr[COMPRESSION_JP2000] = "JP2000";
}
fmt_codec::~fmt_codec()
{}
void fmt_codec::options(codec_options *o)
{
o->version = "1.0.1";
o->name = "Tagged Image File Format";
o->filter = "*.tif *.tiff ";
o->config = std::string(TIFF_UI);
o->mime = "";
o->mimetype = "image/tiff";
o->pixmap = codec_tiff;
o->readable = true;
o->canbemultiple = true;
o->writestatic = true;
o->writeanimated = false;
o->needtempfile = false;
}
void fmt_codec::fill_default_settings()
{
settings_value val;
val.type = settings_value::v_int;
val.iVal = 1;
m_settings["pages"] = val;
}
s32 fmt_codec::read_init(const std::string &file)
{
currentImage = -1;
if((ftiff = TIFFOpen(file.c_str(), "r")) == NULL)
return SQE_R_BADFILE;
TIFFSetWarningHandler(NULL);
TIFFSetErrorHandler(NULL);
finfo.animated = false;
fmt_settings::iterator it = m_settings.find("pages");
pages = (it == m_settings.end() || (*it).second.type != settings_value::v_int) ?
1 : (*it).second.iVal;
if(pages < 1 || pages > 1000)
pages = 1;
dircount = 0;
while(TIFFReadDirectory(ftiff))
dircount++;
TIFFSetDirectory(ftiff, 0);
return SQE_OK;
}
s32 fmt_codec::read_next()
{
currentImage++;
if(currentImage == pages)
return SQE_NOTOK;
if(dircount)
{
if(currentImage == dircount)
return SQE_NOTOK;
}
else
if(currentImage)
return SQE_NOTOK;
if(dircount != 1 && dircount != 0)
if(!TIFFReadDirectory(ftiff))
return SQE_R_BADFILE;
if(currentImage)
TIFFRGBAImageEnd(&img);
fmt_image image;
s32 bps, spp;
TIFFGetField(ftiff, TIFFTAG_IMAGEWIDTH, &image.w);
TIFFGetField(ftiff, TIFFTAG_IMAGELENGTH, &image.h);
memset(&img, 0, sizeof(TIFFRGBAImage));
TIFFRGBAImageBegin(&img, ftiff, 1, 0);
bps = img.bitspersample;
spp = img.samplesperpixel;
s16 cmp;
TIFFGetField(ftiff, TIFFTAG_COMPRESSION, &cmp);
std::map<s32, std::string>::iterator it = compr.find(cmp);
image.bpp = bps * spp;
image.compression = (it == compr.end() ? "Unknown" : (*it).second);
image.hasalpha = true;
image.colorspace = fmt_utils::colorSpaceByBpp(image.bpp);
finfo.image.push_back(image);
return SQE_OK;
}
s32 fmt_codec::read_next_pass()
{
return SQE_OK;
}
s32 fmt_codec::read_scanline(RGBA *scan)
{
fmt_image *im = image(currentImage);
const s32 W = im->w * sizeof(RGBA);
uint32 buf[W];
TIFFRGBAImageGet(&img, buf, im->w, 1);
memcpy(scan, buf, W);
img.row_offset++;
return SQE_OK;
}
void fmt_codec::read_close()
{
TIFFRGBAImageEnd(&img);
TIFFClose(ftiff);
finfo.meta.clear();
finfo.image.clear();
}
void fmt_codec::getwriteoptions(fmt_writeoptionsabs *opt)
{
opt->interlaced = false;
opt->compression_scheme = CompressionRLE;
opt->compression_min = 0;
opt->compression_max = 0;
opt->compression_def = 0;
opt->passes = 1;
opt->needflip = false;
opt->palette_flags = 0 | fmt_image::pure32;
}
s32 fmt_codec::write_init(const std::string &file, const fmt_image &image, const fmt_writeoptions &opt)
{
if(!image.w || !image.h || file.empty())
return SQE_W_WRONGPARAMS;
writeimage = image;
writeopt = opt;
out = TIFFOpen(file.c_str(), "w");
if(!out)
return SQE_W_NOFILE;
return SQE_OK;
}
s32 fmt_codec::write_next()
{
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, writeimage.w);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, writeimage.h);
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(out, TIFFTAG_COMPRESSION, (writeopt.compression_scheme == CompressionRLE ? COMPRESSION_PACKBITS : COMPRESSION_NONE));
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, (uint32) -1));
line = -1;
return SQE_OK;
}
s32 fmt_codec::write_next_pass()
{
return SQE_OK;
}
s32 fmt_codec::write_scanline(RGBA *scan)
{
++line;
if(TIFFWriteScanline(out, (u8 *)scan, line, 0) < 0)
return SQE_W_ERROR;
return SQE_OK;
}
void fmt_codec::write_close()
{
TIFFClose(out);
}
std::string fmt_codec::extension(const s32 /*bpp*/)
{
return std::string("tiff");
}
#include "fmt_codec_cd_func.h"
|