Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "lzwdecom.hxx"
22 :
23 : #define MAX_TABLE_SIZE 4096
24 :
25 2 : LZWDecompressor::LZWDecompressor()
26 : : pIStream(NULL)
27 : , nTableSize(0)
28 : , bEOIFound(false)
29 : , bInvert(false)
30 : , bFirst(true)
31 : , nOldCode(0)
32 : , pOutBufData(NULL)
33 : , nOutBufDataLen(0)
34 : , nInputBitsBuf(0)
35 2 : , nInputBitsBufSize(0)
36 : {
37 : sal_uInt16 i;
38 :
39 2 : pTable=new LZWTableEntry[MAX_TABLE_SIZE];
40 2 : pOutBuf=new sal_uInt8[MAX_TABLE_SIZE];
41 8194 : for (i=0; i<MAX_TABLE_SIZE; i++)
42 : {
43 8192 : pTable[i].nPrevCode=0;
44 8192 : pTable[i].nDataCount=1;
45 8192 : pTable[i].nData=(sal_uInt8)i;
46 : }
47 2 : }
48 :
49 :
50 2 : LZWDecompressor::~LZWDecompressor()
51 : {
52 2 : delete[] pOutBuf;
53 2 : delete[] pTable;
54 2 : }
55 :
56 :
57 87 : void LZWDecompressor::StartDecompression(SvStream & rIStream)
58 : {
59 87 : pIStream=&rIStream;
60 :
61 87 : nTableSize=258;
62 :
63 87 : bEOIFound=false;
64 :
65 87 : nOutBufDataLen=0;
66 :
67 87 : pIStream->ReadUChar( nInputBitsBuf );
68 :
69 87 : nInputBitsBufSize=8;
70 :
71 87 : if ( bFirst )
72 : {
73 2 : bInvert = nInputBitsBuf == 1;
74 2 : bFirst = false;
75 : }
76 :
77 87 : if ( bInvert )
78 0 : nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 );
79 87 : }
80 :
81 :
82 513 : sal_uLong LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uLong nMaxCount)
83 : {
84 : sal_uLong nCount;
85 :
86 513 : if (pIStream==NULL) return 0;
87 :
88 513 : nCount=0;
89 : for (;;) {
90 :
91 13890 : if (pIStream->GetError()) break;
92 :
93 13890 : if (((sal_uLong)nOutBufDataLen)>=nMaxCount) {
94 512 : nOutBufDataLen = nOutBufDataLen - (sal_uInt16)nMaxCount;
95 512 : nCount+=nMaxCount;
96 22707 : while (nMaxCount>0) {
97 21683 : *(pTarget++)=*(pOutBufData++);
98 21683 : nMaxCount--;
99 : }
100 512 : break;
101 : }
102 :
103 13378 : nMaxCount-=(sal_uLong)nOutBufDataLen;
104 13378 : nCount+=nOutBufDataLen;
105 988116 : while (nOutBufDataLen>0) {
106 961360 : *(pTarget++)=*(pOutBufData++);
107 961360 : nOutBufDataLen--;
108 : }
109 :
110 13378 : if (bEOIFound) break;
111 :
112 13377 : DecompressSome();
113 :
114 : }
115 :
116 13377 : return nCount;
117 : }
118 :
119 :
120 13463 : sal_uInt16 LZWDecompressor::GetNextCode()
121 : {
122 : sal_uInt16 nBits,nCode;
123 :
124 13463 : if (nTableSize<511) nBits=9;
125 0 : else if (nTableSize<1023) nBits=10;
126 0 : else if (nTableSize<2047) nBits=11;
127 0 : else nBits=12;
128 :
129 13463 : nCode=0;
130 26926 : do {
131 26926 : if (nInputBitsBufSize<=nBits)
132 : {
133 15130 : nCode=(nCode<<nInputBitsBufSize) | nInputBitsBuf;
134 15130 : nBits = nBits - nInputBitsBufSize;
135 15130 : pIStream->ReadUChar( nInputBitsBuf );
136 15130 : if ( bInvert )
137 0 : nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 );
138 15130 : nInputBitsBufSize=8;
139 : }
140 : else
141 : {
142 11796 : nCode=(nCode<<nBits) | (nInputBitsBuf>>(nInputBitsBufSize-nBits));
143 11796 : nInputBitsBufSize = nInputBitsBufSize - nBits;
144 11796 : nInputBitsBuf&=0x00ff>>(8-nInputBitsBufSize);
145 11796 : nBits=0;
146 : }
147 : } while (nBits>0);
148 :
149 13463 : return nCode;
150 : }
151 :
152 :
153 13290 : void LZWDecompressor::AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData)
154 : {
155 13290 : if (nTableSize >= MAX_TABLE_SIZE)
156 : {
157 : //It might be possible to force emit a 256 to flush the buffer and try
158 : //to continue later?
159 : SAL_WARN("filter.tiff", "Too much data at scanline");
160 0 : bEOIFound = true;
161 13290 : return;
162 : }
163 :
164 983517 : while (pTable[nCodeFirstData].nDataCount>1)
165 956937 : nCodeFirstData=pTable[nCodeFirstData].nPrevCode;
166 :
167 13290 : pTable[nTableSize].nPrevCode=nPrevCode;
168 13290 : pTable[nTableSize].nDataCount=pTable[nPrevCode].nDataCount+1;
169 13290 : pTable[nTableSize].nData=pTable[nCodeFirstData].nData;
170 :
171 13290 : nTableSize++;
172 : }
173 :
174 :
175 13377 : void LZWDecompressor::DecompressSome()
176 : {
177 : sal_uInt16 i,nCode;
178 :
179 13377 : nCode=GetNextCode();
180 13377 : if (nCode==256)
181 : {
182 86 : nTableSize=258;
183 86 : nCode=GetNextCode();
184 86 : if (nCode==257)
185 : {
186 0 : bEOIFound=true;
187 : }
188 : }
189 13291 : else if (nCode<nTableSize)
190 560 : AddToTable(nOldCode,nCode);
191 12731 : else if (nCode==nTableSize)
192 12730 : AddToTable(nOldCode,nOldCode);
193 : else
194 : {
195 1 : bEOIFound=true;
196 : }
197 :
198 13377 : if (bEOIFound)
199 13378 : return;
200 :
201 13376 : nOldCode=nCode;
202 :
203 13376 : nOutBufDataLen=pTable[nCode].nDataCount;
204 13376 : pOutBufData=pOutBuf+nOutBufDataLen;
205 996419 : for (i=0; i<nOutBufDataLen; i++)
206 : {
207 983043 : *(--pOutBufData)=pTable[nCode].nData;
208 983043 : nCode=pTable[nCode].nPrevCode;
209 : }
210 : }
211 :
212 :
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|