xref: /aoo41x/main/svx/source/gallery2/codec.cxx (revision f6e50924)
1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <tools/stream.hxx>
28cdf0e10cSrcweir #include <tools/zcodec.hxx>
29cdf0e10cSrcweir #include "codec.hxx"
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ----------------
33cdf0e10cSrcweir // - GalleryCodec -
34cdf0e10cSrcweir // ----------------
DBG_NAME(GalleryCodec)35cdf0e10cSrcweir DBG_NAME(GalleryCodec)
36cdf0e10cSrcweir 
37cdf0e10cSrcweir GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
38cdf0e10cSrcweir     rStm( rIOStm )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     DBG_CTOR(GalleryCodec,NULL);
41cdf0e10cSrcweir 
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // -----------------------------------------------------------------------------
45cdf0e10cSrcweir 
~GalleryCodec()46cdf0e10cSrcweir GalleryCodec::~GalleryCodec()
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     DBG_DTOR(GalleryCodec,NULL);
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir // -----------------------------------------------------------------------------
53cdf0e10cSrcweir 
IsCoded(SvStream & rStm,sal_uInt32 & rVersion)54cdf0e10cSrcweir sal_Bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	const sal_uIntPtr	nPos = rStm.Tell();
57cdf0e10cSrcweir 	sal_Bool		bRet;
58cdf0e10cSrcweir 	sal_uInt8		cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	rStm >> cByte1 >> cByte2 >> cByte3 >> cByte4 >> cByte5 >> cByte6;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir         rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
65cdf0e10cSrcweir 		bRet = sal_True;
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir     else
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         rVersion = 0;
70cdf0e10cSrcweir 		bRet = sal_False;
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	rStm.Seek( nPos );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	return bRet;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir // -----------------------------------------------------------------------------
79cdf0e10cSrcweir 
Write(SvStream & rStmToWrite)80cdf0e10cSrcweir void GalleryCodec::Write( SvStream& rStmToWrite )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir 	sal_uInt32 nPos, nCompSize;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     rStmToWrite.Seek( STREAM_SEEK_TO_END );
85cdf0e10cSrcweir 	const sal_uInt32 nSize = rStmToWrite.Tell();
86cdf0e10cSrcweir 	rStmToWrite.Seek( 0UL );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	rStm << 'S' << 'V' << 'R' << 'L' << 'E' << '2';
89cdf0e10cSrcweir 	rStm << nSize;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	nPos = rStm.Tell();
92cdf0e10cSrcweir 	rStm.SeekRel( 4UL );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     ZCodec aCodec;
95cdf0e10cSrcweir     aCodec.BeginCompression();
96cdf0e10cSrcweir     aCodec.Compress( rStmToWrite, rStm );
97cdf0e10cSrcweir     aCodec.EndCompression();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	nCompSize = rStm.Tell() - nPos - 4UL;
100cdf0e10cSrcweir 	rStm.Seek( nPos );
101cdf0e10cSrcweir 	rStm << nCompSize;
102cdf0e10cSrcweir 	rStm.Seek( STREAM_SEEK_TO_END );
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir // -----------------------------------------------------------------------------
106cdf0e10cSrcweir 
Read(SvStream & rStmToRead)107cdf0e10cSrcweir void GalleryCodec::Read( SvStream& rStmToRead )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	sal_uInt32 nVersion = 0;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	if( IsCoded( rStm, nVersion ) )
112cdf0e10cSrcweir 	{
113cdf0e10cSrcweir 		sal_uInt32	nCompressedSize, nUnCompressedSize;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 		rStm.SeekRel( 6 );
116cdf0e10cSrcweir 		rStm >> nUnCompressedSize >> nCompressedSize;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 		// decompress
119cdf0e10cSrcweir         if( 1 == nVersion )
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             sal_uInt8*   pCompressedBuffer = new sal_uInt8[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
122cdf0e10cSrcweir 	        sal_uInt8*	pInBuf = pCompressedBuffer;
123cdf0e10cSrcweir 	        sal_uInt8*	pOutBuf = new sal_uInt8[ nUnCompressedSize ];
124cdf0e10cSrcweir 	        sal_uInt8*	pTmpBuf = pOutBuf;
125cdf0e10cSrcweir 	        sal_uInt8*	pLast = pOutBuf + nUnCompressedSize - 1;
126cdf0e10cSrcweir 	        sal_uIntPtr   nIndex = 0UL, nCountByte, nRunByte;
127cdf0e10cSrcweir 	        sal_Bool    bEndDecoding = sal_False;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	        do
130cdf0e10cSrcweir 	        {
131cdf0e10cSrcweir 		        nCountByte = *pInBuf++;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		        if ( !nCountByte )
134cdf0e10cSrcweir 		        {
135cdf0e10cSrcweir 			        nRunByte = *pInBuf++;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 			        if ( nRunByte > 2 )
138cdf0e10cSrcweir 			        {
139cdf0e10cSrcweir 				        // absolutes Fuellen
140cdf0e10cSrcweir 				        memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
141cdf0e10cSrcweir 				        pInBuf += nRunByte;
142cdf0e10cSrcweir 				        nIndex += nRunByte;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 				        // WORD-Alignment beachten
145cdf0e10cSrcweir 				        if ( nRunByte & 1 )
146cdf0e10cSrcweir 					        pInBuf++;
147cdf0e10cSrcweir 			        }
148cdf0e10cSrcweir 			        else if ( nRunByte == 1 )   // Ende des Bildes
149cdf0e10cSrcweir 				        bEndDecoding = sal_True;
150cdf0e10cSrcweir 		        }
151cdf0e10cSrcweir 		        else
152cdf0e10cSrcweir 		        {
153cdf0e10cSrcweir 			        const sal_uInt8 cVal = *pInBuf++;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			        memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
156cdf0e10cSrcweir 			        nIndex += nCountByte;
157cdf0e10cSrcweir 		        }
158cdf0e10cSrcweir 	        }
159cdf0e10cSrcweir 	        while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir    		    rStmToRead.Write( pOutBuf, nUnCompressedSize );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             delete[] pOutBuf;
164cdf0e10cSrcweir             delete[] pCompressedBuffer;
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         else if( 2 == nVersion )
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir 	        ZCodec aCodec;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir             aCodec.BeginCompression();
171cdf0e10cSrcweir             aCodec.Decompress( rStm, rStmToRead );
172cdf0e10cSrcweir             aCodec.EndCompression();
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 	}
175cdf0e10cSrcweir }
176