1
/************************************************************************************
2
TerraLib - a library for developing GIS applications.
3
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
5
This code is part of the TerraLib library.
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version.
11
You should have received a copy of the GNU Lesser General Public
12
License along with this library.
14
The authors reassure the license terms regarding the warranties.
15
They specifically disclaim any warranties, including, but not limited to,
16
the implied warranties of merchantability and fitness for a particular purpose.
17
The library provided hereunder is on an "as is" basis, and the authors have no
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
20
indirect, special, incidental, or consequential damages arising out of the use
21
of this library and its documentation.
22
*************************************************************************************/
23
/*! \file TeDecoderTIFF.h
24
This file contais functions to deal with raster images in Tiff/GeoTIFF format.
27
#ifndef __TERRALIB_INTERNAL_DECODERTIFF_H
28
#define __TERRALIB_INTERNAL_DECODERTIFF_H
33
#include "geo_normalize.h"
34
#include "geovalues.h"
36
#include "geo_tiffp.h" /* external TIFF interface */
37
#include "geo_keyp.h" /* private interface */
41
#include <sys/types.h>
43
#include "TeDecoderVirtualMemory.h"
48
#define MONOIMAGE 0 /* Image type monochrome */
49
#define RGBIMAGE 1 /* Image type color image */
50
#define PALLETEIMAGE 2 /* Image type pallete image */
53
//! Implements a decoder to a raster in TIFF (Tagged Image File Format) format
55
Decoder TIFF is based on geotiff libray (http://remotesensing.org/geotiff/geotiff.html).
56
This imposes some restrictions to this decoder. Using libtiff TIFF image files may not
57
be opened for both reading and writing; there is no support for altering the contents of
58
a TIFF file. So this decoder is limited to create new tiff files or to read existing ones.
60
class TeDecoderTIFF : public TeDecoder
62
TIFF *tif; // TIFF directory structure
63
GTIF *gtif; // GEOTIFF directory structure
65
unsigned char *TBuffer_;
66
unsigned char *TBufferTile_;
68
bool isGeoTiff_; // flag that indicates georeference
69
bool isTiled_; // flag indicating if image is tiled
70
unsigned short photom_; // photometric interpretation
71
unsigned short planar_; // storage organization : PLANARCONFIG_CONTIG or PLANARCONFIG_SEPARATE
72
unsigned short compress_; // compression flag: COMPRESSION_NONE, COMPRESSION_CCITTRLE, COMPRESSION_CCITTFAX3 and others
73
short TImage; // MONOIMAGE, RGBIMAGE, PALLETE
74
int nBands_; // number of bands
76
unsigned long bytesperline_;
77
unsigned long tilew_,tileh_;
78
unsigned long rowtilesize_;
79
unsigned long bytespertile_;
80
unsigned long rowsperstrip_;
81
int TCurLine_; // current line in memory
82
int TCurTile_; // current tile in memory
85
bool first; // Flag to test the first access to To8Bits() function
86
unsigned char Lut[256]; // Lut to convert nbits/pixel to 8bits/pixel
87
unsigned short mask; // mask bits 0000...1 for 1 bit
88
// 000.1111 for 2 bits and on
95
// if PHOTOMETRIC_YCBCR:
96
float *YCbCrCoeffs, *refBlackWhite; // Chrominance coefficients
97
unsigned short YCbCrHorizSampling, // Horizontal Smapling
98
YCbCrVertSampling; // Vertical and
103
//! Reads TIFF Directory and fills tif structure
104
void ReadTiffDirectory();
106
//! Reads the georeferencing keys of a geotiff data
109
//! Reads and inteprets a .tfw file associated to a tiff
110
bool DecodeTFW(const string& filename);
112
//! Sets the georeferencing keys of a geotiff data
115
//! Write a dummy image (when creating a tiff file)
116
bool WriteDummyImage();
118
//! Reads a line of monochrome image
120
\param lin line number to read
121
\return true if line is secessfully read and false otherwise
123
bool ReadMonoImage(unsigned long lin);
125
//! Reads a line of color image
126
/*! \param lin line number to read
127
\return true if line is secessfully read and false otherwise
129
bool ReadRGBImage(unsigned long lin);
131
//! Reads color image if planar = PLANARCONFIG_CONTIG
132
/*! \param lin line number to read
133
\return non 0 if line is secessfully written and 0 otherwise
135
bool ReadRGBContig(unsigned long lin);
137
//! Reads color image if planar = PHOTOMETRIC_PALETTE
138
/*! \param lin line number to read
139
\return non 0 if line is secessfully written and 0 otherwise
141
bool ReadPaletteContig(unsigned long lin);
143
bool ReadTileImageContig(unsigned long lin, unsigned char *line);
145
//! Allocates the internal buffers
146
bool AllocateBuffer();
148
//! Allocates the internal buffers
149
void DeallocateBuffer();
151
//! Reads Lut associated to a raster pallete
154
//! Saves the associated LUT for pallete raster
157
//! Converts buf to 8 bits per pixel into ret buffer
159
// buf - a line of image stored in TIFF
161
// ret - image line converted to 8 bits per pixel
163
// image line buffer (ret) must be allocated
164
// previously before call this function
165
void To8Bits(u_char *buf,u_char *ret,short vmax=0, short vmin = 0,short nx=0);
167
//! Prints to standard output contents of TIFF directory
170
// Returns true if tiff data is compressed
171
bool IsCompressed() { return compress_ != COMPRESSION_NONE; }
175
//! Construtor from parameters
176
TeDecoderTIFF( const TeRasterParams& );
178
//! Normal destructor
181
//! Initializes raster structure
184
//! Clear internal parameters
187
TeCoord2D index2Coord (TeCoord2D& pt);
188
TeCoord2D coord2Index (TeCoord2D& pt);
190
bool setElement (int col,int lin, double val, int band=0);
191
bool getElement (int col,int lin, double &val,int band=0);
194
//! Implements a tiff decoder factory
195
class TeDecoderTIFFFactory : public TeDecoderFactory
199
TeDecoderTIFFFactory(const string& name) : TeDecoderFactory(name) {}
201
virtual TeDecoder* build (const TeRasterParams& arg)
202
{ return new TeDecoderTIFF(arg); }