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 <tools/stream.hxx>
22 : #include "giflzwc.hxx"
23 :
24 :
25 : // - GIFImageDataOutputStream -
26 :
27 :
28 : class GIFImageDataOutputStream
29 : {
30 : private:
31 :
32 : void FlushBlockBuf();
33 : inline void FlushBitsBufsFullBytes();
34 :
35 : SvStream& rStream;
36 : sal_uInt8* pBlockBuf;
37 : sal_uInt8 nBlockBufSize;
38 : sal_uLong nBitsBuf;
39 : sal_uInt16 nBitsBufSize;
40 :
41 : public:
42 :
43 : GIFImageDataOutputStream( SvStream & rGIF, sal_uInt8 nLZWDataSize );
44 : ~GIFImageDataOutputStream();
45 :
46 : inline void WriteBits( sal_uInt16 nCode, sal_uInt16 nCodeLen );
47 : };
48 :
49 :
50 :
51 0 : inline void GIFImageDataOutputStream::FlushBitsBufsFullBytes()
52 : {
53 0 : while (nBitsBufSize>=8)
54 : {
55 0 : if( nBlockBufSize==255 )
56 0 : FlushBlockBuf();
57 :
58 0 : pBlockBuf[nBlockBufSize++] = (sal_uInt8) nBitsBuf;
59 0 : nBitsBuf >>= 8;
60 0 : nBitsBufSize -= 8;
61 : }
62 0 : }
63 :
64 :
65 :
66 0 : inline void GIFImageDataOutputStream::WriteBits( sal_uInt16 nCode, sal_uInt16 nCodeLen )
67 : {
68 0 : if( nBitsBufSize+nCodeLen>32 )
69 0 : FlushBitsBufsFullBytes();
70 :
71 0 : nBitsBuf |= (sal_uLong) nCode << nBitsBufSize;
72 0 : nBitsBufSize = nBitsBufSize + nCodeLen;
73 0 : }
74 :
75 :
76 :
77 0 : GIFImageDataOutputStream::GIFImageDataOutputStream( SvStream & rGIF, sal_uInt8 nLZWDataSize ) :
78 0 : rStream(rGIF)
79 : {
80 0 : pBlockBuf = new sal_uInt8[ 255 ];
81 0 : nBlockBufSize = 0;
82 0 : nBitsBufSize = 0;
83 0 : nBitsBuf = 0;
84 0 : rStream.WriteUChar( nLZWDataSize );
85 0 : }
86 :
87 :
88 :
89 :
90 0 : GIFImageDataOutputStream::~GIFImageDataOutputStream()
91 : {
92 0 : WriteBits(0,7);
93 0 : FlushBitsBufsFullBytes();
94 0 : FlushBlockBuf();
95 0 : rStream.WriteUChar( (sal_uInt8)0 );
96 0 : delete[] pBlockBuf;
97 0 : }
98 :
99 :
100 :
101 0 : void GIFImageDataOutputStream::FlushBlockBuf()
102 : {
103 0 : if( nBlockBufSize )
104 : {
105 0 : rStream.WriteUChar( (sal_uInt8) nBlockBufSize );
106 0 : rStream.Write( pBlockBuf,nBlockBufSize );
107 0 : nBlockBufSize = 0;
108 : }
109 0 : }
110 :
111 :
112 : // - GIFLZWCTreeNode -
113 :
114 :
115 : struct GIFLZWCTreeNode
116 : {
117 :
118 : GIFLZWCTreeNode* pBrother; // next node which has the same father
119 : GIFLZWCTreeNode* pFirstChild; // first
120 : sal_uInt16 nCode; // the code for the string of pixel values which comes about
121 : sal_uInt16 nValue; // the pixel value
122 : };
123 :
124 :
125 : // - GIFLZWCompressor -
126 :
127 :
128 0 : GIFLZWCompressor::GIFLZWCompressor()
129 : : pIDOS(NULL), pTable(NULL), pPrefix(NULL), nDataSize(0), nClearCode(0),
130 0 : nEOICode(0), nTableSize(0), nCodeSize(0)
131 : {
132 0 : }
133 :
134 :
135 :
136 0 : GIFLZWCompressor::~GIFLZWCompressor()
137 : {
138 0 : if (pIDOS!=NULL) EndCompression();
139 0 : }
140 :
141 :
142 :
143 0 : void GIFLZWCompressor::StartCompression( SvStream& rGIF, sal_uInt16 nPixelSize )
144 : {
145 0 : if( !pIDOS )
146 : {
147 : sal_uInt16 i;
148 :
149 0 : nDataSize = nPixelSize;
150 :
151 0 : if( nDataSize < 2 )
152 0 : nDataSize=2;
153 :
154 0 : nClearCode=1<<nDataSize;
155 0 : nEOICode=nClearCode+1;
156 0 : nTableSize=nEOICode+1;
157 0 : nCodeSize=nDataSize+1;
158 :
159 0 : pIDOS=new GIFImageDataOutputStream(rGIF,(sal_uInt8)nDataSize);
160 0 : pTable=new GIFLZWCTreeNode[4096];
161 :
162 0 : for (i=0; i<4096; i++)
163 : {
164 0 : pTable[i].pBrother = pTable[i].pFirstChild = NULL;
165 0 : pTable[i].nValue = (sal_uInt8) ( pTable[i].nCode = i );
166 : }
167 :
168 0 : pPrefix = NULL;
169 0 : pIDOS->WriteBits( nClearCode,nCodeSize );
170 : }
171 0 : }
172 :
173 :
174 :
175 0 : void GIFLZWCompressor::Compress( HPBYTE pSrc, sal_uLong nSize )
176 : {
177 0 : if( pIDOS )
178 : {
179 : GIFLZWCTreeNode* p;
180 : sal_uInt16 i;
181 : sal_uInt8 nV;
182 :
183 0 : if( !pPrefix && nSize )
184 : {
185 0 : pPrefix=pTable+(*pSrc++);
186 0 : nSize--;
187 : }
188 :
189 0 : while( nSize )
190 : {
191 0 : nSize--;
192 0 : nV=*pSrc++;
193 0 : for( p=pPrefix->pFirstChild; p!=NULL; p=p->pBrother )
194 : {
195 0 : if (p->nValue==nV)
196 0 : break;
197 : }
198 :
199 0 : if( p)
200 0 : pPrefix=p;
201 : else
202 : {
203 0 : pIDOS->WriteBits(pPrefix->nCode,nCodeSize);
204 :
205 0 : if (nTableSize==4096)
206 : {
207 0 : pIDOS->WriteBits(nClearCode,nCodeSize);
208 :
209 0 : for (i=0; i<nClearCode; i++)
210 0 : pTable[i].pFirstChild=NULL;
211 :
212 0 : nCodeSize=nDataSize+1;
213 0 : nTableSize=nEOICode+1;
214 : }
215 : else
216 : {
217 0 : if(nTableSize==(sal_uInt16)(1<<nCodeSize))
218 0 : nCodeSize++;
219 :
220 0 : p=pTable+(nTableSize++);
221 0 : p->pBrother=pPrefix->pFirstChild;
222 0 : pPrefix->pFirstChild=p;
223 0 : p->nValue=nV;
224 0 : p->pFirstChild=NULL;
225 : }
226 :
227 0 : pPrefix=pTable+nV;
228 : }
229 : }
230 : }
231 0 : }
232 :
233 :
234 :
235 0 : void GIFLZWCompressor::EndCompression()
236 : {
237 0 : if( pIDOS )
238 : {
239 0 : if( pPrefix )
240 0 : pIDOS->WriteBits(pPrefix->nCode,nCodeSize);
241 :
242 0 : pIDOS->WriteBits( nEOICode,nCodeSize );
243 0 : delete[] pTable;
244 0 : delete pIDOS;
245 0 : pIDOS=NULL;
246 : }
247 0 : }
248 :
249 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|