~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/terralib/kernel/TeDecoderTIFF.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
Import upstream version 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
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.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
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.
 
25
*/
 
26
 
 
27
#ifndef  __TERRALIB_INTERNAL_DECODERTIFF_H
 
28
#define  __TERRALIB_INTERNAL_DECODERTIFF_H
 
29
 
 
30
// LibTIFF includes
 
31
#include "geotiff.h"
 
32
#include "xtiffio.h"
 
33
#include "geo_normalize.h"
 
34
#include "geovalues.h"
 
35
#include "tiffiop.h"
 
36
#include "geo_tiffp.h" /* external TIFF interface */
 
37
#include "geo_keyp.h"  /* private interface       */
 
38
 
 
39
#include <iostream>
 
40
#include <stdio.h>
 
41
#include <sys/types.h>
 
42
 
 
43
#include "TeDecoderVirtualMemory.h"
 
44
 
 
45
#include <map>
 
46
using namespace std;
 
47
 
 
48
#define MONOIMAGE       0       /* Image type monochrome */
 
49
#define RGBIMAGE        1       /* Image type color image */
 
50
#define PALLETEIMAGE 2  /* Image type pallete image */
 
51
 
 
52
 
 
53
//! Implements a decoder to a raster in TIFF (Tagged Image File Format) format
 
54
/*!
 
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.
 
59
*/
 
60
class TeDecoderTIFF : public TeDecoder
 
61
{
 
62
        TIFF    *tif;       // TIFF directory structure
 
63
        GTIF    *gtif;          // GEOTIFF directory structure
 
64
 
 
65
        unsigned char  *TBuffer_;
 
66
        unsigned char  *TBufferTile_;
 
67
 
 
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
 
75
        int                             nbitsperpixel_;
 
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
 
83
 
 
84
 
 
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
 
89
 
 
90
        double *transMatrix_;
 
91
        double *pixelScale_;
 
92
        double *tiePoints_;
 
93
 
 
94
 
 
95
        // if PHOTOMETRIC_YCBCR:        
 
96
        float   *YCbCrCoeffs,   *refBlackWhite; // Chrominance coefficients
 
97
        unsigned short YCbCrHorizSampling,              // Horizontal Smapling
 
98
                           YCbCrVertSampling;           // Vertical and
 
99
                                                                                                        
 
100
                                                                                                        
 
101
 
 
102
 
 
103
        //!     Reads TIFF Directory and fills tif structure
 
104
        void ReadTiffDirectory();
 
105
 
 
106
        //! Reads the georeferencing keys of a geotiff data
 
107
        bool GetGeoTIFF ();
 
108
 
 
109
        //! Reads and inteprets a .tfw file associated to a tiff
 
110
        bool DecodeTFW(const string& filename);
 
111
 
 
112
        //! Sets the georeferencing keys of a geotiff data
 
113
        void SetGeoKeys();
 
114
 
 
115
        //! Write a dummy image (when creating a tiff file)
 
116
        bool WriteDummyImage();
 
117
 
 
118
        //!     Reads a line of monochrome image
 
119
        /*!             \param buf image
 
120
                        \param lin line number to read
 
121
                        \return true if line is secessfully read and false otherwise
 
122
        */
 
123
        bool ReadMonoImage(unsigned long lin);
 
124
 
 
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
 
128
        */
 
129
        bool ReadRGBImage(unsigned long lin);
 
130
 
 
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
 
134
        */
 
135
        bool ReadRGBContig(unsigned long lin);
 
136
 
 
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
 
140
        */
 
141
        bool ReadPaletteContig(unsigned long lin);
 
142
 
 
143
        bool ReadTileImageContig(unsigned long lin, unsigned char *line);
 
144
 
 
145
        //!     Allocates the internal buffers
 
146
        bool AllocateBuffer();
 
147
 
 
148
        //!     Allocates the internal buffers
 
149
        void DeallocateBuffer();
 
150
 
 
151
        //!     Reads Lut associated to a raster pallete
 
152
        bool ReadLut();
 
153
 
 
154
        //!     Saves the associated LUT for pallete raster 
 
155
        void SaveLut();
 
156
 
 
157
        //! Converts buf to 8 bits per pixel into ret buffer
 
158
        //              Input:
 
159
        //                      buf - a line of image stored in TIFF
 
160
        //              Output:
 
161
        //                      ret - image line converted to 8 bits per pixel
 
162
        //              Preconditions:
 
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);
 
166
 
 
167
        //!     Prints to standard output contents of TIFF directory
 
168
        void Print();
 
169
        
 
170
        //      Returns true if tiff data is compressed
 
171
        bool IsCompressed() { return compress_ != COMPRESSION_NONE; }
 
172
 
 
173
public:
 
174
 
 
175
        //! Construtor from parameters 
 
176
        TeDecoderTIFF( const TeRasterParams& );
 
177
 
 
178
        //!     Normal destructor
 
179
        ~TeDecoderTIFF();
 
180
 
 
181
        //! Initializes raster structure
 
182
        void init();
 
183
 
 
184
        //! Clear internal parameters
 
185
        bool clear();
 
186
 
 
187
        TeCoord2D index2Coord (TeCoord2D& pt);
 
188
        TeCoord2D coord2Index (TeCoord2D& pt);
 
189
 
 
190
        bool setElement (int col,int lin, double val, int band=0);
 
191
        bool getElement (int col,int lin, double &val,int band=0);
 
192
};
 
193
 
 
194
//! Implements a tiff decoder factory
 
195
class TeDecoderTIFFFactory : public TeDecoderFactory
 
196
{
 
197
public:
 
198
 
 
199
        TeDecoderTIFFFactory(const string& name) : TeDecoderFactory(name) {}
 
200
 
 
201
        virtual TeDecoder* build (const TeRasterParams& arg)
 
202
        {  return new TeDecoderTIFF(arg); }
 
203
};
 
204
 
 
205
#endif