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
|
/************************************************************************
TIFF image file format support.
$Id: raster-tiff.cxx 427 2004-09-27 04:45:31Z garland $
************************************************************************/
#include <gfx/gfx.h>
#include <gfx/raster.h>
#include <cstring>
#ifdef HAVE_LIBTIFF
#include <tiffio.h>
namespace gfx
{
////////////////////////////////////////////////////////////////////////
//
// TIFF output
//
static
bool __tiff_write(TIFF *tif, const ByteRaster& img)
{
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, img.width());
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, img.height());
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, img.channels());
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC,
img.channels()==1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
#ifdef HAVE_LIBTIFF_LZW
//
// LZW compression is problematic because it is patented by Unisys.
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
//
// Predictors:
// 1 (default) -- No predictor
// 2 -- Horizontal differencing
TIFFSetField(tif, TIFFTAG_PREDICTOR, 2);
#endif
uint32 scanline_size = img.channels() * img.width();
if( TIFFScanlineSize(tif) != scanline_size )
// ??BUG: Can this mismatch of scanline sizes every occur?
return false;
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, 0));
char *scanline_buf = new char[scanline_size];
const unsigned char *scanline = img.head();
for(int y=0; y<img.height(); y++)
{
memcpy(scanline_buf, scanline, scanline_size);
// NOTE: TIFFWriteScanline modifies the buffer you pass it.
// Thus, we need to copy stuff out of the raster first.
TIFFWriteScanline(tif, scanline_buf, y, 0);
scanline += scanline_size;
}
delete[] scanline_buf;
return true;
}
bool write_tiff_image(const char *filename, const ByteRaster& img)
{
TIFF *tif = TIFFOpen(filename, "w");
if( !tif ) return false;
bool result = __tiff_write(tif, img);
TIFFClose(tif);
return result;
}
////////////////////////////////////////////////////////////////////////
//
// TIFF input
//
static
void unpack_tiff_raster(uint32 *raster, ByteRaster *img, int npixels)
{
unsigned char *pix = img->head();
for(int i=0; i<npixels; i++)
{
*pix++ = TIFFGetR(raster[i]);
if( img->channels() >= 3 )
{
*pix++ = TIFFGetG(raster[i]);
*pix++ = TIFFGetB(raster[i]);
if( img->channels() == 4 )
*pix++ = TIFFGetA(raster[i]);
}
}
}
static
ByteRaster *__tiff_read(TIFF *tif)
{
uint32 w, h;
uint16 nchan;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nchan);
int npixels = w*h;
uint32 *raster = (uint32 *)_TIFFmalloc(npixels * sizeof(uint32));
if( !raster ) return NULL;
TIFFReadRGBAImage(tif, w, h, raster, true);
ByteRaster *img = new ByteRaster(w, h, nchan);
unpack_tiff_raster(raster, img, npixels);
//
// libtiff returned the pixels with the origin in the lower left
// rather than the upper left corner. We fix that by flipping the
// pixels.
//
img->vflip();
_TIFFfree(raster);
return img;
}
ByteRaster *read_tiff_image(const char *filename)
{
TIFF *tif = TIFFOpen(filename, "r");
if( !tif ) return NULL;
ByteRaster *img = __tiff_read(tif);
TIFFClose(tif);
return img;
}
} // namespace gfx
#else
namespace gfx
{
bool write_tiff_image(const char *, const ByteRaster&) { return false; }
ByteRaster *read_tiff_image(const char *) { return NULL; }
}
#endif
|