Statistics
| Revision:

svn-gvsig-desktop / branches / v05 / libraries / libjni-ecw / include / ECW.h @ 40081

History | View | Annotate | Download (35.7 KB)

1 3538 nacho
/**********************************************************
2
** Copyright 1998 Earth Resource Mapping Ltd.
3
** This document contains proprietary source code of
4
** Earth Resource Mapping Ltd, and can only be used under
5
** one of the three licenses as described in the
6
** license.txt file supplied with this distribution.
7
** See separate license.txt file for license details
8
** and conditions.
9
**
10
** This software is covered by US patent #6,442,298,
11
** #6,102,897 and #6,633,688.  Rights to use these patents
12
** is included in the license agreements.
13
**
14
** FILE:           erswave.h
15
** CREATED:        1998
16
** AUTHOR:         SNS
17
** PURPOSE:        Definitions for Wavelet compressed imagery
18
** EDITS:
19
** [01] sns        10sep98        Created file
20
** [02] sns        12Feb99        Modified from ECW to NCS
21
** [03] sns 22Mar99        Adding additional encoding techniques
22
** [04] sns 01Apr99        Updated to use Windows native file IO
23
** [05] sns        02apr99 Modified to use IEEE4 for decode instead of UINT32
24
**                                        so can better handle sub-integer errors, and later compress
25
**                                        non-integer datasets. As the number of multiplies for decompression
26
**                                        is quite low, going to IEEE4's from INT32's should not impact performance too much
27
** [06] sns 02Apr99        Updated to use fast floating point conversions on x86 platforms
28
** [07] sns 03Apr99        Adding RUN_ZERO and HUFFMAN encoding techniques
29
** [08] sns        06Apr99        Summing TAPs to exactly 1.0
30
** [09] sns 06Apr99 Adding 11 tap filter for speed of compression
31
** [10] sns 09Apr99 Added user control of compression block size
32
** [11] sns 08May99 Added on/off texture noise control flag to region structures
33
** [12] sns 21May99 Restructuring Compression front-end. Also using _RC_NEAR not _RC_CHOP
34
** [13] sns 21May99 Restructured QMF struct to prevent mis-aligned fetches
35
** [14] sns 04Jun99 Added internal random number generator instead of using rand()
36
** [15] sns 17Jun99 Changes to split Compression logic out to a separate SDK library
37
** [16] sjc 30Apr00 Merged Mac SDK port
38
** [17] ddi 14Nov00 Modified erw_decompress_read_region_line_bil() to take NCSEcwReadLineType parameter,
39
**                                        and moved NCSEcwReadLineType from ncsecw.h
40
** [18] rar        17-Jan-01 Mac port changes
41
** [19] rar 18Sep01 Modified erw_decompress_open so that if the Block Table is uncompressed (RAW)
42
**                  it is not passed through unpack_data (which just does a memcpy anyway)
43
** [20] sjc 30Apr03 Added "Low Memory" compression
44
** [21] jx  12Feb04 nCounter is added to QmfRegionStruct to fix rounding error in erw_decompress_read_region_line_bil().
45
** [22] jx  17Feb04 changed varibles: current_line, start_line, increment_y and increment_x to IEEE8 to fix rounding error in erw_decompress_read_region_line_bil().
46
** [23] tfl 05Jul04 Added #define for JPEG2000 file extensions
47
**
48
** NOTES
49
**
50
**        A wavelet compressed file consists of a normal .ERS file,
51
**        containing map projection details and so forth, and the
52
**        ER Mapper Compressed Wavelet (.ECW) file, which contains
53
**        the following structure:
54
**
55
**        ECW Header format is:
56
**
57
**        UINT8        ERW_HEADER_ID_TAG 'e'
58
**        UINT8        VERSION NUMBER (1..255)
59
**        UINT8        blocking format (BLOCKING_LEVEL = blocking individual levels)
60
**        UINT8        compress format        (COMPRESS_UINT8 = rescale back to 0..255 for each band.
61
**                                                         COMPRESS_YIQ = YIQ encoding of a 3 band RGB file. Number of bands must also = 3)
62
**        UINT8        number of QMF levels (excluding file fake level)
63
**        UINT8        Number of subbands (sidebands). Currently always 4.
64
**        UINT32        X size of the file
65
**        UINT32        Y size of the file
66
**        UINT16        Number of bands of data (must be 1 or more)
67
**        UINT16        quantization scale factor
68
**        UINT16        X block size
69
**        UINT16        Y block size
70
**
71
**                Then for each level (other than the largest level, being the fake file level), the
72
**                following is written out:
73
**        UINT8                Level number
74
**        UINT32                x_size
75
**        UINT32                y_size
76
**        IEEE4                binsizes for each band in the file (so 1 for greyscale, 3 for RGB or YIQ)
77
**                                (note that currently all sidebands for a band have the same binsize)
78
**
79
**                Next, the block offsets for all blocks, relative to the start of all level blocks (not relative to
80
**                the start of the file), are output.  As these can be a large number, they are output
81
**                as a compressed set. A block may contain multiple subbands. We only record the total
82
**                block length, as we always need all subbands.
83
**                Blocks are output to disk in the order of levels, so all level 0 blocks will be on disk
84
**                first, then level 1, and so on. The block offsets are stored in a single array, in
85
**                the same way. Because we want to read and write this array, for all levels, as a single
86
**                operation to/from disk, because we want to optimize compression by writing the entire
87
**                group, only one array for all QMF levels is allocated, and each QMF points into part of
88
**                that single array. The array is attached to the top level (which points to the first
89
**                entries), and is freed at that level.
90
**                NOTE WELL: We need to know the size of each block, which is the different between block X
91
**                and block X+1 offsets. Therefor, we have to write one more block offset than actually present,
92
**                which contains the offset to the end of the file (e.g. the file length). This could also
93
**                be used as a file consistency check. Eitherway, it must be there. So the block offset
94
**                buffers are allocated with +1 for this reason.
95
**
96
**                The length of the compressed block is written first.
97
**                Blocks are written with the offset to each subband other than the first,
98
*                then the ENCODED DATA for each subband, so the data stream is:
99
**        UINT32        Length of the compressed block
100
**        UINT8        encoding_style
101
**        <compressed stream of UINT64 block_offsets>
102
**
103
**                Then, the blocks of sideband data are output.
104
**                A block is defined as containing (level : MAX_SIDEBAND ? MAX_SIDEBAND-1) sidebands (e.g. 3 for
105
**                levels greater than zero, and 4 for the level 0).
106
**
107
**                As noted above, each block contains multiple sidebands. Each sideband is encoded in the
108
**                ENCODED DATA format as follows.
109
**                Blocks are written with the offset to each subband other than the first,
110
**                then the ENCODED DATA for each subband, so the data stream is:
111
**        UINT32        Offset to HL_SUBBAND        ONLY OUTPUT IF LEVEL 0 (as there will be a LL SUBBAND output)
112
**        UINT32        Offset to HH_SUBBAND
113
**                Then for each subband (4 for 1st level, 3 for all others):
114
**        UINT8        encoding_style
115
**        <compressed stream of UINT64 block_offsets>
116
**
117
**        After this, other routines write each compressed QMF level out to file.
118
********************************************************/
119
120
#ifndef ECW_H
121
#define ECW_H
122
123
#include "NCSJPCDefs.h"
124
125
#ifndef NCSTYPES_H
126
#include "NCSTypes.h"
127
#endif
128
129
#ifdef MACINTOSH
130
#include <fenv.h>
131
#endif
132
#include <float.h>
133
#include <stdio.h>
134
135
136
#ifndef NCSUTIL_H
137
#include "NCSUtil.h"
138
#endif
139
140
typedef struct {
141
        NCS_FILE_HANDLE        hFile;
142
        void        *pClientData;
143
} ECWFILE;
144
#define NULL_ECWFILE        { NCS_NULL_FILE_HANDLE, NULL }
145
146
#include "NCSECWCompress.h"
147
148
//#define        SAFE_FREE(ptr)                if( ptr ) free(ptr);
149
#define        NCS_SAFE_FREE(ptr)                if( ptr ) NCSFree(ptr);
150
151
#ifdef __cplusplus
152
extern "C" {
153
#endif
154
155
156
// FIXME! Move all range encoding code over to using UINT16/UINT32
157
#if !defined(MACINTOSH)
158
typedef UINT16 uint2;
159
#endif
160
typedef UINT32 uint4;
161
#if defined(WIN32)
162
typedef UINT32 uint;
163
#endif
164
165
#define ERS_WAVELET_DATASET_EXT        ".ecw"                /* compressed wavelet format file */
166
#define ERS_JP2_DATASET_EXTS        { ".jp2" , ".j2k" , ".j2c" , ".jpc", ".jpx", ".jpf" }  /*[23]*/
167
#define ECW_HEADER_ID_TAG   'e'                                /* [02] first byte of a compressed file must be this */
168
169
// FIXME! Move all code over to using IEEE4 not FLOAT
170
typedef IEEE4 FLOAT;
171
172
173
174
/* Maximum amount of RAM to use while copying a file. More is faster */
175
#ifdef MACINTOSH // MDT 4 Meg is too big for Mac -- use 64K        /**[16]**/
176
177
#define MAX_FILE_COPY_MEMORY        (1024*64)
178
179
#else        /* MACINTOSH */
180
181
#define MAX_FILE_COPY_MEMORY        (1024*1024) * 4        // [02] 4MB is reasonable
182
183
#endif        /* MACINTOSH */
184
185
/* Maximum number of pyramid levels.
186
** We want enough room for terrabyte sized files
187
** This doesn't need to be larger than log2(MAX_IMAGE_DIM/FILTER_SIZE).
188
*/
189
#define MAX_LEVELS 20
190
191
//#define SCALE_FACTOR 1                // [05] was 256, no longer needed
192
193
/*
194
**        QMF sideband images are sub-blocked, so that sub-sections of sideband images
195
**        can be retrieved without having to read the entire sideband image.
196
**        So a block will be BLOCK_SIZE_X * BLOCK_SIZE_Y symbols in size.
197
**        The total must be less than or equal to 64K result (to ensure that encoders
198
**        have a fixed maximum to encode).  Smaller blocks means faster load time over
199
**        internet (for NCS technology), but more inefficient compression and overhead.
200
**        The best size is 1024(x)x64(y), which is fast, good compression, and small size.
201
**        Note that the compressor can compress an entire file without blocking; this
202
**        is a convenience definition related to decompression of images, it is not
203
**        essential for operation of the compressor.
204
**
205
**        The X block size is typically greater than the Y block size. This is because
206
**        once a block is found, it is fast to read it, and because increasing Y block
207
**        size increases the amount of memory required to store blocks prior to encoding
208
**        Both numbers must be even numbers.
209
**
210
**  [07] Note: The ENCODE_RUN_ZERO decoder currently limits block size in X to 2^15
211
**        in size - which should never be a problem as this is a huge value for a single
212
**        block, but is something  to be aware of. The RUN_ZERO unpack code is easily enhanced for larger
213
**        block sizes, at a slight expense in performance.
214
**
215
**        [10] Block size can now be set during compression via command line. This is the default.
216
*/
217
218
219
220
//#define        MIN_QMF_SIZE        128        // minimum size for the smallest LL
221
// Made smaller, for fast NCS initial loads
222
#define        MIN_QMF_SIZE        64                // minimum size for the smallest LL
223
224
225
/* Use these to set up nr_[x|y]_blocks in the qmf, then use those values */
226
227
#define QMF_LEVEL_NR_X_BLOCKS(p_qmf)        \
228
                        (((p_qmf)->x_size + ((p_qmf)->x_block_size - 1)) / (p_qmf)->x_block_size)
229
230
#define QMF_LEVEL_NR_Y_BLOCKS(p_qmf)        \
231
                        (((p_qmf)->y_size + ((p_qmf)->y_block_size - 1)) / (p_qmf)->y_block_size)
232
233
// size of filter tap bank, must be an odd number 3 or greater
234
// Filters are hardcode as defines, making multiplications later much faster.
235
// Compiler will detect and optimize symetric numbers
236
237
//#define FILTER_SIZE        15                // 15 tap filter banks.
238
#define FILTER_SIZE 11                        // 11 tap filter banks
239
240
#if FILTER_SIZE == 15
241
// Lowpass sums to 1.0
242
#define        LO_FILTER_0                (IEEE4) -0.0012475221
243
#define        LO_FILTER_1                (IEEE4) -0.0024950907
244
#define        LO_FILTER_2                (IEEE4)  0.0087309530
245
#define        LO_FILTER_3                (IEEE4)  0.0199579580
246
#define        LO_FILTER_4                (IEEE4) -0.0505290000
247
#define        LO_FILTER_5                (IEEE4) -0.1205509700
248
#define        LO_FILTER_6                (IEEE4)  0.2930455800
249
#define        LO_FILTER_7                (IEEE4)  0.7061761836                // [08]
250
#define        LO_FILTER_8                (IEEE4)  0.2930455800
251
#define        LO_FILTER_9                (IEEE4) -0.1205509700
252
#define        LO_FILTER_10        (IEEE4) -0.0505290000
253
#define        LO_FILTER_11        (IEEE4)  0.0199579580
254
#define        LO_FILTER_12        (IEEE4)  0.0087309530
255
#define        LO_FILTER_13        (IEEE4) -0.0024950907
256
#define        LO_FILTER_14        (IEEE4) -0.0012475221
257
// Highpass sums to 0.0
258
#define        HI_FILTER_0                (IEEE4)  0.0012475221
259
#define        HI_FILTER_1                (IEEE4) -0.0024950907
260
#define        HI_FILTER_2                (IEEE4) -0.0087309530
261
#define        HI_FILTER_3                (IEEE4)  0.0199579580
262
#define        HI_FILTER_4                (IEEE4)  0.0505290000
263
#define        HI_FILTER_5                (IEEE4) -0.1205509700
264
#define        HI_FILTER_6                (IEEE4) -0.2930455800
265
#define        HI_FILTER_7                (IEEE4)  0.7061762272                // [08]
266
#define        HI_FILTER_8                (IEEE4) -0.2930455800
267
#define        HI_FILTER_9                (IEEE4) -0.1205509700
268
#define        HI_FILTER_10        (IEEE4)  0.0505290000
269
#define        HI_FILTER_11        (IEEE4)  0.0199579580
270
#define        HI_FILTER_12        (IEEE4) -0.0087309530
271
#define        HI_FILTER_13        (IEEE4) -0.0024950907
272
#define        HI_FILTER_14        (IEEE4)  0.0012475221
273
#endif
274
275
#if FILTER_SIZE == 11
276
// Lowpass sums to 1.0
277
#define        LO_FILTER_0                (IEEE4)  0.007987761489921101
278
#define        LO_FILTER_1                (IEEE4)  0.02011649866148413
279
#define        LO_FILTER_2                (IEEE4) -0.05015758257647976
280
#define        LO_FILTER_3                (IEEE4) -0.12422330961337678
281
#define        LO_FILTER_4                (IEEE4)  0.29216982108655865
282
#define        LO_FILTER_5                (IEEE4)  0.7082136219037853
283
#define        LO_FILTER_6                (IEEE4)  0.29216982108655865
284
#define        LO_FILTER_7                (IEEE4) -0.12422330961337678
285
#define        LO_FILTER_8                (IEEE4) -0.05015758257647976
286
#define        LO_FILTER_9                (IEEE4)  0.02011649866148413
287
#define        LO_FILTER_10        (IEEE4)  0.007987761489921101
288
// Highpass sums to 0.0
289
#define        HI_FILTER_0                (IEEE4) -0.007987761489921101
290
#define        HI_FILTER_1                (IEEE4)  0.02011649866148413
291
#define        HI_FILTER_2                (IEEE4)  0.05015758257647976
292
#define        HI_FILTER_3                (IEEE4) -0.12422330961337678
293
#define        HI_FILTER_4                (IEEE4) -0.29216982108655865
294
#define        HI_FILTER_5                (IEEE4)  0.7082136219037853
295
#define        HI_FILTER_6                (IEEE4) -0.29216982108655865
296
#define        HI_FILTER_7                (IEEE4) -0.12422330961337678
297
#define        HI_FILTER_8                (IEEE4)  0.05015758257647976
298
#define        HI_FILTER_9                (IEEE4)  0.02011649866148413
299
#define        HI_FILTER_10        (IEEE4) -0.007987761489921101
300
#endif
301
302
/* The type of the quantized images. Must be SIGNED, and capable of holding    values  in the range [-MAX_BINS, MAX_BINS] */
303
typedef INT16 BinIndexType;
304
305
/* The type used to represent the binsizes. Should be UNSIGNED. If this is
306
   changed, be sure to change the places where
307
   binsizes are written or read from files.  */
308
typedef UINT16 BinValueType;
309
310
typedef UINT8 Byte;
311
312
/*
313
** Number of possible values for a symbol.  This must be at least
314
**(MAX_BINS * 4)  (one sign bit, one tag bit)...
315
*/
316
#define NUM_SYMBOL_VALUES 65536
317
#define SIGN_MASK        0x4000
318
#define RUN_MASK        0x8000
319
#define MAX_BIN_VALUE        16383        // 0x3fff
320
#define MIN_BIN_VALUE        -16383        // Prior to masking
321
#define VALUE_MASK        0x3fff                /* without the sign or run bits set */
322
#define MAX_RUN_LENGTH 0x7fff        /* can't be longer than this or would run into mask bits */
323
324
/*
325
**        The blocking format used for a file. Currently only one supported -
326
**        block each level.
327
*/
328
329
typedef enum {
330
        BLOCKING_LEVEL        = 1
331
} BlockingFormat;
332
333
/*
334
**        The encoding format for a sideband within a block. More encoding
335
**        standards might be added later.
336
*/
337
#ifdef NOTDEF
338
typedef enum {
339
        ENCODE_INVALID        = 0,        // invalid encoding style
340
        ENCODE_RAW                = 1,        // data is not encoded, and is in a raw format
341
        ENCODE_HUFFMAN        = 2,        // zero's run length encoded, and then huffman encoded
342
        ENCODE_RANGE        = 3,        // range encoding
343
        ENCODE_RANGE8        = 4,        // 8 bit difference then range encoding
344
        ENCODE_ZEROS        = 5,        // encode the entire block as zero's
345
        ENCODE_RUN_ZERO        = 6                // Run length encoded as zero's
346
} EncodeFormat;
347
#endif
348
typedef UINT16 EncodeFormat;
349
#define        ENCODE_INVALID        0
350
#define        ENCODE_RAW                1
351
#define        ENCODE_HUFFMAN        2
352
#define        ENCODE_RANGE        3
353
#define        ENCODE_RANGE8        4
354
#define        ENCODE_ZEROS        5
355
#define        ENCODE_RUN_ZERO        6
356
357
#define MAX_SIDEBAND        4
358
359
typedef enum {
360
        LL_SIDEBAND = 0,        // must be in order as used for array offsets
361
        LH_SIDEBAND = 1,
362
        HL_SIDEBAND = 2,
363
        HH_SIDEBAND = 3
364
} Sideband;
365
366
/*
367
**        Some basic information about a QMF file, pointed to by all levels,
368
**        designed to make life easier for higher level applications.
369
**        NOTE!! NCSECWClient.h has this structure, defined as "NCSFileViewFileInfo".
370
**        Any changes must be kept in synch between the two structures
371
**
372
*/
373
374
typedef NCSFileViewFileInfo ECWFileInfo;
375
typedef NCSFileViewFileInfoEx ECWFileInfoEx;
376
377
/*
378
**
379
**        The QmfLevelBandStruct is only allocated for COMPRESSION. Because the bin size is
380
**        needed for decompression, it is pulled out of the band structure (binsize can be
381
**        different for each band) and put into the main level structure. So the bin_size
382
**        is the only multi-band value in the QMF level structure; the rest of the band
383
**        specific information is held here at the band level. This also makes the QMF
384
**        structure much smaller for decompression.
385
**
386
**        There is a multi-band, compression only, buffer allocated at the QMF level,
387
**        which is a pointer to the p_input_ll_line for all bands. This is so recursion,
388
**        including the top level, is easier to structure.
389
*/
390
391
typedef struct qmf_level_band_struct {
392
        //
393
        // These buffers are not allocated for the largest (file level) QMF level
394
        //
395
        IEEE4        *p_p_lo_lines[FILTER_SIZE+1];        // pointer to enough larger input lines for lowpass input
396
        IEEE4        *p_p_hi_lines[FILTER_SIZE+1];        // pointet to enough larger input lines for highpass input
397
        IEEE4        *p_low_high_block;                                // the block of memory indexed into by the above. Used for free()
398
        // this is allocated with enough room on the X sides to handle reflection
399
        IEEE4        *p_input_ll_line;                                // pointer to a single input line needed for this level
400
401
        // This points to the Y_BLOCK_SIZE buffer of output lines, quantized, stored for
402
        // LL (smallest level 0 only), LH, HL and HH. The index into the block for a given line
403
        // is (for maximum memory access performance):
404
        // (y_line_offset * x_line_size)
405
                /* These are used when p_file_qmf->bLowMemCompress is TRUE [20]*/
406
        UINT32 **p_p_ll_lengths;
407
        UINT8 ***p_p_p_ll_segs;
408
        UINT32 **p_p_lh_lengths;
409
        UINT8 ***p_p_p_lh_segs;
410
        UINT32 **p_p_hl_lengths;
411
        UINT8 ***p_p_p_hl_segs;
412
        UINT32 **p_p_hh_lengths;
413
        UINT8 ***p_p_p_hh_segs;
414
                /* These are used when p_file_qmf->bLowMemCompress is TRUE (1 line) or FALSE (Y_BLOCK_SIZE lines) [20]*/
415
        INT16        *p_quantized_output_ll_block;        // Only allocated for level 0 LL, NULL for all other levels
416
        INT16        *p_quantized_output_lh_block;        // LH block
417
        INT16        *p_quantized_output_hl_block;        // HL block
418
        INT16        *p_quantized_output_hh_block;        // HH block
419
420
        UINT32        packed_length[MAX_SIDEBAND];        // the packed length for a single set of sidebands for this band
421
} QmfLevelBandStruct;
422
423
/*
424
**        One level of a QMF tree
425
**
426
** The QMF levels are as follows:
427
**        QMF level (level 0..file_level-1)
428
**                Normal QMF levels. Contain temporary buffers for line processing, and a temporary file
429
**                pointer for writing the compressed sidebands to.
430
**        File level:
431
**                A place-holder level. Contains no buffers, but does contain information about the
432
**                file to be read from.
433
*/
434
435
typedef struct qmf_level_struct {
436
        UINT16        level;
437
        UINT8        nr_levels;                        // Number of levels, excluding file level
438
        UINT8        version;                        // [13] Top-level only: compressed file version
439
        UINT8        nr_sidebands;                // [13] Top-level only: set to MAX_SIDEBANDS during write of a file
440
        UINT8        bPAD1;                                // [13] pad to long-word
441
        UINT16        nr_bands;                        // [13] number of bands (not subbands) in the file, e.g. 3 for a RGB file
442
        UINT32        x_size, y_size;
443
        UINT32        next_output_line;        // COMPRESSION ONLY: next output line to construct. Will go from 0 to x_size - 1
444
        UINT32        next_input_line;        // COMPRESSION ONLY: next line of input to read. 2 lines read for every output line
445
        struct        qmf_level_struct        *p_larger_qmf;
446
        struct        qmf_level_struct        *p_smaller_qmf;
447
        struct        qmf_level_struct        *p_top_qmf;                /* pointer to top (smallest) level QMF */
448
        struct        qmf_level_struct        *p_file_qmf;        /* pointer to the fake (largest) file level QMF */
449
450
        UINT32        *p_band_bin_size;                        /* bin size for each band during quantization. Each sideband for a band has same binsize */
451
        UINT16        x_block_size;                                /* size of a block in X direction */
452
        UINT16        y_block_size;                                /* size of a block in Y direction */
453
        UINT32        nr_x_blocks;                                /* number of blocks in the X direction */
454
        UINT32        nr_y_blocks;                                /* number of blocks in the Y direction */
455
        UINT32        nFirstBlockNumber;                        /* [02] First block number for this level, 0 for level 0 */
456
457
        // These values are valid for the top (smallest) level QMF only
458
        BlockingFormat blocking_format;                // typically BLOCKING_LEVEL
459
        CompressFormat compress_format;                // typically COMPRESS_UINT8 or COMPRESS_YIQ
460
        IEEE4        compression_factor;                        // a number between 1 and 1000
461
462
463
        // These are SHARED entries by all QMFs. They all point to the same data,
464
        // and only the top level QMF allocates and frees these. The p_a_block points
465
        // to a single block (valid compression only);
466
        // all QMF's point to the same block. The p_block_offsets is
467
        // an single array for all levels. It is allocated at p_top_qmf, and freed
468
        // at p_top_qmf only.  The other levels point to offsets in the array
469
        // There is +1 block than actually present, to calculate the final block offset.
470
        // These offsets are relative to the start of the first block, not the start of the file
471
        UINT64        first_block_offset;                        /* offset from the start of the file to the first block */
472
        UINT64        *p_block_offsets;                        /* offset for each block for this level of the file */
473
        NCS_FILE_HANDLE        outfile;                                        /* output file */
474
        UINT32        next_block_offset;                        /* incremented during writing */
475
        UINT8        *p_a_block;                                        /* pointer to a single block during packing/unpacking */
476
        BOOLEAN bRawBlockTable;                                /* used to indicated weather the block table was store as
477
                                                                                   RAW or compressed.  Required when freeing the memory
478
                                                                                   allocated for the Block Table in delete_qmf_levels */ //[19]
479
        BOOLEAN        bLowMemCompress;                        /* Use "Low Memory" compression techniques [20]*/
480
481
        // COMPRESSION ONLY: nr_bands of the following pointers, which point into the
482
        // p_input_ll_line offset at [FILTERSIZE/2] for this QMF. Used for recursive calls.
483
        IEEE4        **p_p_input_ll_line;                // pointer to nr_bands, a single input line needed for this level
484
        //
485
        // These buffers are not allocated for the largest (file level) QMF level
486
        // These are also ONLY allocated for compression, not for decompression. There is one
487
        // per band being compressed
488
        QmfLevelBandStruct        *p_bands;
489
        INT16        next_output_block_y_line;        // a number going from 0 to Y_BLOCK_SIZE-1
490
        //        The place to write the compressed sidebands for this level to. Each level has MAX_SIDEBAND files
491
        //        We don't encode the LL sideband image, so the [LL_SIDEBAND] item is normally unused except for debug
492
        char        *tmp_fname;                                        // temporary file to write this level's blocks, so we can set up block order
493
        NCS_FILE_HANDLE        tmp_file;                                        // file pointer to the above block image disk file
494
495
        //        if we are doing transmission encoding instead of blocked encoding
496
        IEEE4                scale_factor;                        // scale factor, for decompression only
497
        UINT64                file_offset;                        // offset to start of this level in the file
498
499
        //        [02] Addition information for NCS usage. Valid for the TopQmf (smallest level) only
500
        UINT8        *pHeaderMemImage;                // Pointer to in memory copy of header if not NULL
501
        UINT32        nHeaderMemImageLen;                // Length of the above in memory header structure
502
503
        ECWFILE        hEcwFile;                                // input ECW, TopQmf level only, for decompression only
504
        BOOLEAN        bEcwFileOpen;                        // TRUE if above file handle is valid (open), otherwise FALSE
505
506
        ECWFileInfoEx        *pFileInfo;                        // Valid at top level only
507
508
        // Compression specific information
509
        struct EcwCompressionTask        *pCompressionTask;        // [15]
510
} QmfLevelStruct;
511
512
//
513
//        Region to decode/decompress within a compressed file for a given line.
514
//
515
//
516
// For a region, we keep local line buffers for each level within the region's depth. There
517
// may be less of these than QMF's, if we are extracting a reduced resolution view of the data.
518
//
519
// The upsampling logic results in 4x4 output values from 2x2 input values, so we keep
520
// line 0 and line 1 of each level's LL.  During processing, line 1 is rolled
521
// to line 1, and line 1 is requested from the smaller level recursively.
522
//
523
// Note that LH, HL and HH are loaded from the compressed file, LL is regenerated recursively
524
// from smaller levels.
525
//
526
// the line0 and line1 are +1 larger in X than required by this level; this handles
527
// different level sizes correctly. For example, we might expand expand a 11x12
528
// image up to a 22 x 23 image.
529
530
// The following returns an index into the line buffer, for the indicated
531
// band number (0..p_qmf->nr_bands), sideband (LL_SIDEBAND..HH_SIDEBAND) and line (0 or 1)
532
//
533
// NOTE WELL!! The "used_bands" is the value used for all band loops during decompression.
534
// This is so that (later) we can set up the decompress logic to uncompress only some bands.
535
// Only the unpack() routines will need to be modified for this to work. For now, though,
536
// we have to unpack all bands.
537
538
// Used to compute initial indexes for pointers
539
#define DECOMP_LEVEL_LINE01(p_level, band, sideband, line)        \
540
                ((p_level)->buffer_ptr + (((2 * (band * MAX_SIDEBAND + sideband)) + (line))        * ((p_level)->level_size_x+2)))
541
// Used to compute index for a given band and subband
542
#define        DECOMP_INDEX        \
543
                (band * MAX_SIDEBAND + sideband)
544
545
typedef struct {
546
        UINT32        used_bands;
547
        IEEE4        **p_p_line0;                        // [05] this level's LL, LH, HL and HH for line N+0. Must be signed
548
        IEEE4        **p_p_line1;                        // [05] this level's LL, LH, HL and HH for line N+1. Must be signed
549
        IEEE4        **p_p_line1_ll_sideband;// [05] This level's LL sideband with x_reflection for line 1, all bands
550
551
        UINT32  start_read_lines;                // [11] starting read_lines state for ResetView()
552
        UINT32        read_lines;                                // # of lines of above level to read. Starts as 2, then will be 0 or 1
553
        // The following is the next line of this level to read.
554
        // So a level with sidebands of 32x32 could have this number range from 0..31,
555
        // as this level would be called up to 64 times to generate the 0..64 lines for the larger level
556
        // In other words, this is from 0 .. (# lines-1) in the side bands for this level.
557
        UINT32        current_line;                        // line currently being read at this level
558
        UINT32        start_line;                                // [11] starting line for ResetView()
559
        // this is the next block line to read (can be from 0..y_block_size). Only valid if have_blocks = TRUE
560
        INT16        next_input_block_y_line;// a number going from 0 to Y_BLOCK_SIZE-1
561
        UINT8        have_blocks;                        // true if the next Y block number is valid
562
        UINT32        start_x_block;                        // first X block, from 0..p_pqm->x_size/p_qmf->x_block_size
563
        UINT32        x_block_count;                        // count of X blocks across
564
565
        void        *p_x_blocks;                        // pointer to a set of decompressions in progress for a set of X blocks
566
        UINT32        first_block_skip;                // how many symbols to skip at start of line in the first block in the set of X blocks
567
        UINT32        last_block_skip;                // how many symbols to skip at end of line in the last block in the X set
568
569
        QmfLevelStruct        *p_qmf;                                // handy pointer to the QMF for this level
570
        struct qmf_region_struct *p_region;        // handy pointer to the region that contains these levels
571
        // the reflection amounts. Need to add these to start/end/size to get true size
572
        // Always 0 or 1, so can be shorter byte values
573
        UINT8        reflect_start_x, reflect_end_x;
574
        UINT8        reflect_start_y, reflect_end_y;
575
        // NOTE WELL: These start/end/size values EXCLUDE the reflection values
576
        UINT32        level_start_y, level_end_y, level_size_y;
577
        UINT32        level_start_x, level_end_x, level_size_x;
578
        UINT32        output_level_start_y, output_level_end_y, output_level_size_y;
579
        UINT32        output_level_start_x, output_level_end_x, output_level_size_x;
580
581
        IEEE4        *buffer_ptr;                        // [05] Band/sideband/line[01] buffer.
582
                                                                        // for speed we allocate a single buffer, and point into it.
583
                                                                        // So a single malloc/free per level
584
} QmfRegionLevelStruct;
585
586
// One of these is created for each start_region()/end_region() call pair
587
typedef struct qmf_region_struct {
588
        QmfLevelStruct        *p_top_qmf;                // pointer to the smallest level in the QMF
589
        QmfLevelStruct        *p_largest_qmf;        // pointer to the largest level we have to read for this region resolution
590
        // region location and size to read
591
        UINT32        start_x, start_y;
592
        UINT32        end_x, end_y;
593
        UINT32        number_x, number_y;
594
595
        UINT32        random_value;                        // [14] random value for texture generation
596
        // floating point versions of current line and incrememtn. This is because
597
        // we read power of 2 lines, but need to feed non-power of two lines to caller
598
        UINT32        read_line;                                // non-zero if have to read line(s), 0 if not (duplicating line)
599
        //[22]
600
        IEEE8        current_line;                        // the current output line being worked on
601
        IEEE8        start_line;                                // [11] starting line for ResetView()
602
        IEEE8        increment_y;                        // the increment of input lines to add for each output line. Will be <= 1.0
603
        IEEE8        increment_x;                        // the increment of input pixels to add for each output pixel. Will be <= 1.0
604
        // recursive information. This array is p_largest_level->level+1 in size
605
        // The in_line[0|1] points are set up for the size of each level, so increase in size for each level
606
        QmfRegionLevelStruct        *p_levels;
607
        IEEE4        **p_p_ll_line;                        // [05] the power of two size, INT32 format, output line. Must be signed
608
        IEEE4        *p_ll_buffer;                        // [05] the buffer that the above indexes into
609
        UINT32        used_bands;                                // bands actually read - may be different from p_qmf->nr_bands in future versions
610
        // The band_list is allocated and freed by the caller. We do NOT allocate or free it
611
        UINT32        nr_bands_requested;                // the number of bands requested from caller; may be differed to used_bands
612
        UINT32        *band_list;                                // array of used_bands, indicating actual band numbers to use
613
        struct NCSFileViewStruct *pNCSFileView;        // [02] pointer to the NCS File View for this QMF Region
614
        BOOLEAN        bAddTextureNoise;                // [11] TRUE if add texture noise during decompression
615
        UINT32 nCounter;                // [21] to fix rounding error in erw_decompress_read_region_line_bil()
616
} QmfRegionStruct;
617
618
619
/*
620
**        QMF tree management functions
621
*/
622
623
// QMF tree management routines used by both build/extract
624
625
#ifdef ECW_COMPRESS        // [15]
626
extern NCSError setup_qmf_tree(QmfLevelStruct **pp_qmf, struct EcwCompressionTask *pCompressionTask,
627
                                                  UINT32 nBlockSizeX, UINT32 nBlockSizeY,
628
                                                  UINT16 *num_levels, UINT32 x_size, UINT32 y_size, UINT32 nr_bands,
629
                                                  UINT32 nInputBands, CompressHint eCompressHint,
630
                                                  IEEE4 compression_factor, CompressFormat compress_format, int compress );
631
#endif
632
633
extern void delete_qmf_levels(QmfLevelStruct *p_qmf);
634
extern UINT32 get_qmf_tree_nr_blocks( QmfLevelStruct *p_top_level );
635
636
QmfLevelStruct *new_qmf_level(UINT32 nBlockSizeX, UINT32 nBlockSizeY,
637
                                                UINT16 level, UINT32 x_size, UINT32 y_size, UINT32 nr_bands,
638
                                                QmfLevelStruct *p_smaller_qmf, QmfLevelStruct *p_larger_qmf, int compress);
639
640
NCSError allocate_qmf_buffers(QmfLevelStruct *p_top_qmf, int compress);
641
642
643
644
// Data unpack routines
645
646
#if defined( MACINTOSH ) && TARGET_API_MAC_OS8
647
extern int unpack_ecw_block(QmfLevelStruct *pQmfLevel, UINT32 nBlockX, UINT32 nBlockY,
648
                                         Handle *ppUnpackedECWBlock, UINT32        *pUnpackedLength,
649
                                         UINT8 *pPackedBlock);
650
int align_ecw_block(NCSFile *pFile, NCSBlockId nBlockID,
651
                                         Handle *ppAlignedECWBlock, UINT32        *pAlignedLength,
652
                                         UINT8 *pPackedBlock, UINT32 nPackedLength);
653
#else
654
extern int unpack_ecw_block(QmfLevelStruct *pQmfLevel, UINT32 nBlockX, UINT32 nBlockY,
655
                                         UINT8 **ppUnpackedECWBlock, UINT32        *pUnpackedLength,
656
                                         UINT8 *pPackedECWBlock);
657
int align_ecw_block(NCSFile *pFile, NCSBlockId nBlockID,
658
                                         UINT8 **ppAlignedECWBlock, UINT32        *pAlignedLength,
659
                                         UINT8 *pPackedBlock, UINT32 nPackedLength);
660
#endif //MACINTOSH
661
662
extern int        unpack_data(UINT8 **p_raw,
663
                                                UINT8  *p_packed, UINT32 raw_length,
664
                                                UINT8 nSizeOfEncodeFormat);
665
666
667
int unpack_init_lines( QmfRegionLevelStruct *p_level );
668
669
int unpack_start_line_block( QmfRegionLevelStruct *p_level, UINT32 x_block,
670
                                                        UINT8 *p_packed_block, UINT32        lines_to_skip);
671
672
int        unpack_line( QmfRegionLevelStruct *p_level );
673
674
void unpack_finish_lines( QmfRegionLevelStruct *p_level );
675
676
void unpack_free_lines( QmfRegionLevelStruct *p_level );
677
678
/*
679
**        Encoder functions
680
*/
681
682
#ifdef ECW_COMPRESS
683
684
extern NCSError        build_qmf_compress_file(QmfLevelStruct *p_top_qmf, NCS_FILE_HANDLE outfile);
685
686
extern NCSError        build_qmf_level_qencode_line(QmfLevelStruct *p_qmf, UINT32 y, IEEE4 **p_p_ll_line);
687
688
extern NCSError qencode_qmf(QmfLevelStruct *p_top_qmf, int num_levels,
689
                          FLOAT compression_factor, NCS_FILE_HANDLE outfile);
690
691
extern NCSError        pack_data(QmfLevelStruct *p_top_qmf,
692
                                          UINT8 **p_packed,        UINT32 *packed_length,
693
                                          UINT8 *p_raw,                UINT32 raw_length, UINT8 symbol_size_hint, BOOLEAN bTryHuffman);
694
695
696
/*
697
**        File IO functions
698
*/
699
700
// writes the preamble header in the compressed ERW file
701
NCSError write_compressed_preamble(QmfLevelStruct *p_top_qmf, NCS_FILE_HANDLE outfile);
702
// convert the block table to LSB offsets
703
NCSError convert_block_table(QmfLevelStruct *p_top_qmf, NCS_FILE_HANDLE outfile);
704
// writes the level to the compressed ERW file
705
NCSError write_compressed_level(QmfLevelStruct *p_qmf, NCS_FILE_HANDLE outfile);
706
707
#endif        /* ECW_COMPRESS */
708
709
/*
710
**        Decoder functions
711
*/
712
713
// Visible functions that can be called
714
715
extern QmfLevelStruct *erw_decompress_open(
716
                        char *p_input_filename,                // File to open
717
                        UINT8        *pMemImage,                        // if non-NULL, open the memory image not the file
718
                        BOOLEAN bReadOffsets,                // TRUE if the client wants the block Offset Table
719
                        BOOLEAN bReadMemImage );        // TRUE if the client wants a Memory Image of the Header
720
721
722
extern QmfRegionStruct *erw_decompress_start_region( QmfLevelStruct *p_top_qmf,
723
                                 UINT32        nr_bands_requested,                        // number of bands to read
724
                                 UINT32        *p_band_list,                                // list of bands to be read. Caller allocs/frees this
725
                                 UINT32 start_x, UINT32 start_y,
726
                                 UINT32 end_x, UINT32 end_y,
727
                                 UINT32 number_x, UINT32 number_y);
728
729
typedef enum {                                                                                                                /**[17]**/
730
        NCSECW_READLINE_INVALID        = 0,        // invalid type                                        /**[17]**/
731
        NCSECW_READLINE_RGB                = 1,        // RGB Triplet                                        /**[17]**/
732
        NCSECW_READLINE_BGR                = 2,        // BGR Triplet                                        /**[17]**/
733
        NCSECW_READLINE_RGBA        = 3,        // RGBA 32-bit                                        /**[17]**/
734
        NCSECW_READLINE_BGRA        = 4,        // BGRA 32-bit                                        /**[17]**/
735
        NCSECW_READLINE_IEEE4        = 5,        // single band float                        /**[17]**/
736
        NCSECW_READLINE_UINT8        = 6,        // single band unsigned 8bit        /**[17]**/
737
        NCSECW_READLINE_UINT16        = 7                // single band unsigned 16bit        /**[17]**/
738
} NCSEcwReadLineType;                                                                                                /**[17]**/
739
740
int erw_decompress_read_region_line_bil ( QmfRegionStruct *p_region, UINT8  **p_p_output_line, NCSEcwReadLineType nOutputType); // [17]
741
int erw_decompress_read_region_line_rgb ( QmfRegionStruct *p_region, UINT8  *pRGBTriplets);
742
int erw_decompress_read_region_line_bgr ( QmfRegionStruct *p_region, UINT8  *pRGBTriplets);
743
int erw_decompress_read_region_line_rgba( QmfRegionStruct *p_region, UINT32 *pRGBAPixels);
744
int erw_decompress_read_region_line_bgra( QmfRegionStruct *p_region, UINT32 *pBGRAPixels);
745
746
747
void erw_decompress_end_region( QmfRegionStruct *p_region );
748
extern void erw_decompress_close( QmfLevelStruct *p_top_qmf );
749
750
// handy argument parsing for stand alone program
751
extern int setup_decompress(int argc, char *argv[],
752
                        char **p_p_input_erw_filename,
753
                        char **p_p_output_bil_filename);
754
755
// Internal functions. Do not call these
756
757
extern int qdecode_qmf_level_line( QmfRegionStruct *p_region, UINT16 level, UINT32 y_line,
758
                                                                  IEEE4 **p_p_output_line);
759
760
/*
761
**        GUI functions (gui_io.c)
762
*/
763
void gui_percent( UINT32 percent );
764
int gui_cancel( void );
765
void gui_msg_ok( char *msg, char *title );
766
767
768
/*
769
**        ECW Header File IO routines
770
**        Use these only for small numbers of header bytes, as they are quite slow
771
*/
772
773
#ifdef ECW_COMPRESS                // [02] only compression needs the ermapper.lib library
774
int write_int8(UINT8   value, FILE *stream);
775
int write_int16(UINT16 value, FILE *stream);
776
int write_int32(UINT32 value, FILE *stream);
777
int write_int64(UINT64 value, FILE *stream);
778
int write_ieee4(IEEE4  value, FILE *stream);
779
#endif
780
/*
781
**        ECW File reading Abstraction Routines
782
**        These are non-ER Mapper specific (as ECW gets linked stand alone),
783
**        and CAN NOT use fopen() to due # of file limitations on platforms like Windows
784
*/
785
786
/*
787
**        The Unix vs WIN32 calls are so different, we wrap the reads with functions,
788
**        that return FALSE if all went well, and TRUE if there was an error.
789
**        Note that the ECWFILE *can* be NULL (windows madness), so you will have to
790
**        keep a separate variable to decide if the file is open or not.
791
*/
792
BOOLEAN EcwFileOpenForRead(char *szFilename, ECWFILE *pFile);                // Opens file for binary reading
793
BOOLEAN EcwFileClose(ECWFILE hFile);                                                                // Closes file
794
BOOLEAN EcwFileRead(ECWFILE hFile, void *pBuffer, UINT32 nLength);        // reads nLength bytes into existing pBuffer
795
BOOLEAN EcwFileSetPos(ECWFILE hFile, UINT64 nOffset);                                // Seeks to specified location in file
796
BOOLEAN EcwFileGetPos(ECWFILE hFile, UINT64 *pOffset);                                // Returns current file position
797
BOOLEAN EcwFileReadUint8(ECWFILE hFile, UINT8 *sym);
798
BOOLEAN EcwFileReadUint16(ECWFILE hFile, UINT16 *sym16);
799
BOOLEAN EcwFileReadUint32(ECWFILE hFile, UINT32 *sym32);
800
BOOLEAN EcwFileReadUint64(ECWFILE hFile, UINT64 *sym);
801
BOOLEAN EcwFileReadIeee8(ECWFILE hFile, IEEE8 *fvalue);
802
BOOLEAN EcwFileReadIeee4(ECWFILE hFile, IEEE4 *fsym32);
803
void sread_ieee8(IEEE8 *sym, UINT8 *p_s);
804
805
806
UINT32 sread_int32(UINT8 *p_s);
807
UINT16 sread_int16(UINT8 *p_s);
808
809
/*
810
**        Memory functions
811
*/
812
813
extern char *check_malloc (int size);
814
extern void check_free (char *ptr);
815
extern FILE *check_fopen (char *filename, char *read_write_flag);
816
char *concatenate(char *string1, char *string2);
817
818
//        For MAC Port --
819
820
#ifdef MACINTOSH
821
extern void *vmalloc( size_t cb );
822
extern void *vcalloc( size_t c, size_t cb );
823
#else
824
#define vmalloc(p) malloc(p)
825
#define vcalloc(p,q) calloc(p,q)
826
#endif
827
828
#ifdef __cplusplus
829
}
830
#endif
831
832
#endif        // ECW_H