1
/******************************************************************************
2
* $Id: rmflzw.cpp 11865 2007-08-09 11:53:57Z warmerdam $
4
* Project: Raster Matrix Format
5
* Purpose: Implementation of the ad-hoc compression algorithm used in
6
* GIS "Panorama"/"Integratsia".
7
* Author: Andrey Kiselev, dron@ak4719.spb.edu
9
******************************************************************************
10
* Copyright (c) 2009, Andrey Kiselev <dron@ak4719.spb.edu>
12
* Permission is hereby granted, free of charge, to any person obtaining a
13
* copy of this software and associated documentation files (the "Software"),
14
* to deal in the Software without restriction, including without limitation
15
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
* and/or sell copies of the Software, and to permit persons to whom the
17
* Software is furnished to do so, subject to the following conditions:
19
* The above copyright notice and this permission notice shall be included
20
* in all copies or substantial portions of the Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28
* DEALINGS IN THE SOFTWARE.
29
****************************************************************************/
33
#include "rmfdataset.h"
36
* The encoded data stream is a series of records.
38
* Encoded record consist from the 1-byte record header followed by the
39
* encoded data block. Header specifies the number of elements in the data
40
* block and encoding type. Header format
42
* +---+---+---+---+---+---+---+---+
44
* +---+---+---+---+---+---+---+---+
47
* If count is zero then it means that there are more than 31 elements in this
48
* record. Read the next byte in the stream and increase its value with 32 to
49
* get the count. In this case maximum number of elements is 287.
51
* The "type" field specifies encoding type. It can be either difference
52
* between the previous and the next data value (for the first element the
53
* previous value is zero) or out-of-range codes.
55
* In case of "out of range" or "zero difference" values there are no more
56
* elements in record after the header. Otherwise read as much encoded
57
* elements as count specifies.
61
#define TYPE_OUT 0x00 // Value is out of range
62
#define TYPE_ZERO 0x20 // Zero difference
63
#define TYPE_INT4 0x40 // Difference is 4-bit wide
64
#define TYPE_INT8 0x60 // Difference is 8-bit wide
65
#define TYPE_INT12 0x80 // Difference is 12-bit wide
66
#define TYPE_INT16 0xA0 // Difference is 16-bit wide
67
#define TYPE_INT24 0xC0 // Difference is 24-bit wide
68
#define TYPE_INT32 0xE0 // Difference is 32-bit wide
71
#define RANGE_INT4 0x00000007L // 4-bit
72
#define RANGE_INT12 0x000007FFL // 12-bit
73
#define RANGE_INT24 0x007FFFFFL // 24-bit
76
#define OUT_INT4 ((GInt32)0xFFFFFFF8)
77
#define OUT_INT8 ((GInt32)0xFFFFFF80)
78
#define OUT_INT12 ((GInt32)0xFFFFF800)
79
#define OUT_INT16 ((GInt32)0xFFFF8000)
80
#define OUT_INT24 ((GInt32)0xFF800000)
81
#define OUT_INT32 ((GInt32)0x80000000)
84
#define INV_INT4 0xFFFFFFF0L
85
#define INV_INT12 0xFFFFF000L
86
#define INV_INT24 0xFF000000L
89
/************************************************************************/
91
/************************************************************************/
93
int RMFDataset::DEMDecompress( const GByte* pabyIn, GUInt32 nSizeIn,
94
GByte* pabyOut, GUInt32 nSizeOut )
96
GUInt32 nCount; // Number of encoded data elements to read
99
GInt32 nType; // The encoding type
100
GInt32 iPrev = 0; // The last data value decoded
105
nSizeOut < nSizeIn ||
109
pabyTempIn = (char*)pabyIn;
110
paiOut = (GInt32*)pabyOut;
111
nSizeOut /= sizeof(GInt32);
113
while ( nSizeIn > 0 )
115
// Read number of codes in the record and encoding type
116
nCount = *pabyTempIn & 0x1F;
117
nType = *pabyTempIn++ & 0xE0;
123
nCount = 32 + *((unsigned char*)pabyTempIn++);
130
if ( nSizeOut < nCount )
133
while ( nCount-- > 0 )
138
if ( nSizeOut < nCount )
141
while ( nCount-- > 0 )
142
*paiOut++ = OUT_INT32;
146
if ( nSizeIn < nCount / 2 )
148
if ( nSizeOut < nCount )
150
nSizeIn -= nCount / 2;
152
while ( nCount-- > 0 )
154
nCode = (*pabyTempIn) & 0x0F;
155
if ( nCode > RANGE_INT4 )
157
*paiOut++ = ( nCode == OUT_INT4 ) ?
158
OUT_INT32 : iPrev += nCode;
167
nCode = ((*pabyTempIn++)>>4) & 0x0F;
168
if ( nCode > RANGE_INT4 )
170
*paiOut++ = ( nCode == OUT_INT4 ) ?
171
OUT_INT32 : iPrev += nCode;
176
if ( nSizeIn < nCount )
178
if ( nSizeOut < nCount )
182
while ( nCount-- > 0 )
184
*paiOut++ = ( (nCode = *pabyTempIn++) == OUT_INT8 ) ?
185
OUT_INT32 : iPrev += nCode;
190
if ( nSizeIn < 3 * nCount / 2 )
192
if ( nSizeOut < nCount )
194
nSizeIn -= 3 * nCount / 2;
197
while ( nCount-- > 0 )
199
nCode = *((GInt16*)pabyTempIn++) & 0x0FFF;
200
if ( nCode > RANGE_INT12 )
202
*paiOut++ = ( nCode == OUT_INT12 ) ?
203
OUT_INT32 : iPrev += nCode;
212
nCode = ( (*(GInt16*)pabyTempIn) >> 4 ) & 0x0FFF;
214
if ( nCode > RANGE_INT12 )
216
*paiOut++ = ( nCode == OUT_INT12 ) ?
217
OUT_INT32 : iPrev += nCode;
222
if ( nSizeIn < 2 * nCount )
224
if ( nSizeOut < nCount )
226
nSizeIn -= 2 * nCount;
229
while ( nCount-- > 0 )
231
nCode = *((GInt16*)pabyTempIn);
233
*paiOut++ = ( nCode == OUT_INT16 ) ?
234
OUT_INT32 : iPrev += nCode;
239
if ( nSizeIn < 3 * nCount )
241
if ( nSizeOut < nCount )
243
nSizeIn -= 3 * nCount;
246
while ( nCount-- > 0 )
248
nCode =*((GInt32 *)pabyTempIn) & 0x0FFF;
250
if ( nCode > RANGE_INT24 )
252
*paiOut++ = ( nCode == OUT_INT24 ) ?
253
OUT_INT32 : iPrev += nCode;
258
if ( nSizeIn < 4 * nCount )
260
if ( nSizeOut < nCount )
262
nSizeIn -= 4 * nCount;
265
while ( nCount-- > 0 )
267
nCode = *(GInt32 *)pabyTempIn;
269
*paiOut++ = ( nCode == OUT_INT32 ) ?
270
OUT_INT32 : iPrev += nCode;
276
return ((GByte*)paiOut - pabyOut);