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 << 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 << (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 << (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 : {
130 0 : pIDOS=NULL;
131 0 : }
132 :
133 : // ------------------------------------------------------------------------
134 :
135 0 : GIFLZWCompressor::~GIFLZWCompressor()
136 : {
137 0 : if (pIDOS!=NULL) EndCompression();
138 0 : }
139 :
140 : // ------------------------------------------------------------------------
141 :
142 0 : void GIFLZWCompressor::StartCompression( SvStream& rGIF, sal_uInt16 nPixelSize )
143 : {
144 0 : if( !pIDOS )
145 : {
146 : sal_uInt16 i;
147 :
148 0 : nDataSize = nPixelSize;
149 :
150 0 : if( nDataSize < 2 )
151 0 : nDataSize=2;
152 :
153 0 : nClearCode=1<<nDataSize;
154 0 : nEOICode=nClearCode+1;
155 0 : nTableSize=nEOICode+1;
156 0 : nCodeSize=nDataSize+1;
157 :
158 0 : pIDOS=new GIFImageDataOutputStream(rGIF,(sal_uInt8)nDataSize);
159 0 : pTable=new GIFLZWCTreeNode[4096];
160 :
161 0 : for (i=0; i<4096; i++)
162 : {
163 0 : pTable[i].pBrother = pTable[i].pFirstChild = NULL;
164 0 : pTable[i].nValue = (sal_uInt8) ( pTable[i].nCode = i );
165 : }
166 :
167 0 : pPrefix = NULL;
168 0 : pIDOS->WriteBits( nClearCode,nCodeSize );
169 : }
170 0 : }
171 :
172 : // ------------------------------------------------------------------------
173 :
174 0 : void GIFLZWCompressor::Compress( HPBYTE pSrc, sal_uLong nSize )
175 : {
176 0 : if( pIDOS )
177 : {
178 : GIFLZWCTreeNode* p;
179 : sal_uInt16 i;
180 : sal_uInt8 nV;
181 :
182 0 : if( !pPrefix && nSize )
183 : {
184 0 : pPrefix=pTable+(*pSrc++);
185 0 : nSize--;
186 : }
187 :
188 0 : while( nSize )
189 : {
190 0 : nSize--;
191 0 : nV=*pSrc++;
192 0 : for( p=pPrefix->pFirstChild; p!=NULL; p=p->pBrother )
193 : {
194 0 : if (p->nValue==nV)
195 0 : break;
196 : }
197 :
198 0 : if( p)
199 0 : pPrefix=p;
200 : else
201 : {
202 0 : pIDOS->WriteBits(pPrefix->nCode,nCodeSize);
203 :
204 0 : if (nTableSize==4096)
205 : {
206 0 : pIDOS->WriteBits(nClearCode,nCodeSize);
207 :
208 0 : for (i=0; i<nClearCode; i++)
209 0 : pTable[i].pFirstChild=NULL;
210 :
211 0 : nCodeSize=nDataSize+1;
212 0 : nTableSize=nEOICode+1;
213 : }
214 : else
215 : {
216 0 : if(nTableSize==(sal_uInt16)(1<<nCodeSize))
217 0 : nCodeSize++;
218 :
219 0 : p=pTable+(nTableSize++);
220 0 : p->pBrother=pPrefix->pFirstChild;
221 0 : pPrefix->pFirstChild=p;
222 0 : p->nValue=nV;
223 0 : p->pFirstChild=NULL;
224 : }
225 :
226 0 : pPrefix=pTable+nV;
227 : }
228 : }
229 : }
230 0 : }
231 :
232 : // ------------------------------------------------------------------------
233 :
234 0 : void GIFLZWCompressor::EndCompression()
235 : {
236 0 : if( pIDOS )
237 : {
238 0 : if( pPrefix )
239 0 : pIDOS->WriteBits(pPrefix->nCode,nCodeSize);
240 :
241 0 : pIDOS->WriteBits( nEOICode,nCodeSize );
242 0 : delete[] pTable;
243 0 : delete pIDOS;
244 0 : pIDOS=NULL;
245 : }
246 0 : }
247 :
248 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|