xref: /AOO41X/main/svx/source/gallery2/codec.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <tools/stream.hxx>
32*cdf0e10cSrcweir #include <tools/zcodec.hxx>
33*cdf0e10cSrcweir #include "codec.hxx"
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir // ----------------
37*cdf0e10cSrcweir // - GalleryCodec -
38*cdf0e10cSrcweir // ----------------
39*cdf0e10cSrcweir DBG_NAME(GalleryCodec)
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
42*cdf0e10cSrcweir     rStm( rIOStm )
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir     DBG_CTOR(GalleryCodec,NULL);
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir }
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir // -----------------------------------------------------------------------------
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir GalleryCodec::~GalleryCodec()
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     DBG_DTOR(GalleryCodec,NULL);
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir // -----------------------------------------------------------------------------
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir sal_Bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	const sal_uIntPtr	nPos = rStm.Tell();
61*cdf0e10cSrcweir 	sal_Bool		bRet;
62*cdf0e10cSrcweir 	sal_uInt8		cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	rStm >> cByte1 >> cByte2 >> cByte3 >> cByte4 >> cByte5 >> cByte6;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
67*cdf0e10cSrcweir 	{
68*cdf0e10cSrcweir         rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
69*cdf0e10cSrcweir 		bRet = sal_True;
70*cdf0e10cSrcweir 	}
71*cdf0e10cSrcweir     else
72*cdf0e10cSrcweir     {
73*cdf0e10cSrcweir         rVersion = 0;
74*cdf0e10cSrcweir 		bRet = sal_False;
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	rStm.Seek( nPos );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	return bRet;
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir // -----------------------------------------------------------------------------
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir void GalleryCodec::Write( SvStream& rStmToWrite )
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir 	sal_uInt32 nPos, nCompSize;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     rStmToWrite.Seek( STREAM_SEEK_TO_END );
89*cdf0e10cSrcweir 	const sal_uInt32 nSize = rStmToWrite.Tell();
90*cdf0e10cSrcweir 	rStmToWrite.Seek( 0UL );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	rStm << 'S' << 'V' << 'R' << 'L' << 'E' << '2';
93*cdf0e10cSrcweir 	rStm << nSize;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 	nPos = rStm.Tell();
96*cdf0e10cSrcweir 	rStm.SeekRel( 4UL );
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     ZCodec aCodec;
99*cdf0e10cSrcweir     aCodec.BeginCompression();
100*cdf0e10cSrcweir     aCodec.Compress( rStmToWrite, rStm );
101*cdf0e10cSrcweir     aCodec.EndCompression();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	nCompSize = rStm.Tell() - nPos - 4UL;
104*cdf0e10cSrcweir 	rStm.Seek( nPos );
105*cdf0e10cSrcweir 	rStm << nCompSize;
106*cdf0e10cSrcweir 	rStm.Seek( STREAM_SEEK_TO_END );
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir // -----------------------------------------------------------------------------
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir void GalleryCodec::Read( SvStream& rStmToRead )
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir 	sal_uInt32 nVersion = 0;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	if( IsCoded( rStm, nVersion ) )
116*cdf0e10cSrcweir 	{
117*cdf0e10cSrcweir 		sal_uInt32	nCompressedSize, nUnCompressedSize;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 		rStm.SeekRel( 6 );
120*cdf0e10cSrcweir 		rStm >> nUnCompressedSize >> nCompressedSize;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 		// decompress
123*cdf0e10cSrcweir         if( 1 == nVersion )
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir             sal_uInt8*   pCompressedBuffer = new sal_uInt8[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
126*cdf0e10cSrcweir 	        sal_uInt8*	pInBuf = pCompressedBuffer;
127*cdf0e10cSrcweir 	        sal_uInt8*	pOutBuf = new sal_uInt8[ nUnCompressedSize ];
128*cdf0e10cSrcweir 	        sal_uInt8*	pTmpBuf = pOutBuf;
129*cdf0e10cSrcweir 	        sal_uInt8*	pLast = pOutBuf + nUnCompressedSize - 1;
130*cdf0e10cSrcweir 	        sal_uIntPtr   nIndex = 0UL, nCountByte, nRunByte;
131*cdf0e10cSrcweir 	        sal_Bool    bEndDecoding = sal_False;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	        do
134*cdf0e10cSrcweir 	        {
135*cdf0e10cSrcweir 		        nCountByte = *pInBuf++;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		        if ( !nCountByte )
138*cdf0e10cSrcweir 		        {
139*cdf0e10cSrcweir 			        nRunByte = *pInBuf++;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 			        if ( nRunByte > 2 )
142*cdf0e10cSrcweir 			        {
143*cdf0e10cSrcweir 				        // absolutes Fuellen
144*cdf0e10cSrcweir 				        memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
145*cdf0e10cSrcweir 				        pInBuf += nRunByte;
146*cdf0e10cSrcweir 				        nIndex += nRunByte;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 				        // WORD-Alignment beachten
149*cdf0e10cSrcweir 				        if ( nRunByte & 1 )
150*cdf0e10cSrcweir 					        pInBuf++;
151*cdf0e10cSrcweir 			        }
152*cdf0e10cSrcweir 			        else if ( nRunByte == 1 )   // Ende des Bildes
153*cdf0e10cSrcweir 				        bEndDecoding = sal_True;
154*cdf0e10cSrcweir 		        }
155*cdf0e10cSrcweir 		        else
156*cdf0e10cSrcweir 		        {
157*cdf0e10cSrcweir 			        const sal_uInt8 cVal = *pInBuf++;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 			        memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
160*cdf0e10cSrcweir 			        nIndex += nCountByte;
161*cdf0e10cSrcweir 		        }
162*cdf0e10cSrcweir 	        }
163*cdf0e10cSrcweir 	        while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir    		    rStmToRead.Write( pOutBuf, nUnCompressedSize );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir             delete[] pOutBuf;
168*cdf0e10cSrcweir             delete[] pCompressedBuffer;
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir         else if( 2 == nVersion )
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir 	        ZCodec aCodec;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir             aCodec.BeginCompression();
175*cdf0e10cSrcweir             aCodec.Decompress( rStm, rStmToRead );
176*cdf0e10cSrcweir             aCodec.EndCompression();
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir 	}
179*cdf0e10cSrcweir }
180