merge the formfield patch from ooo-build
[ooovba.git] / svx / source / gallery2 / codec.cxx
blobcd4573d15e3b2ba6f05ca650b2d6af5d9362cb1a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: codec.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <tools/stream.hxx>
35 #include <tools/zcodec.hxx>
36 #include "codec.hxx"
38 // ----------------
39 // - GalleryCodec -
40 // ----------------
42 GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
43 rStm( rIOStm )
47 // -----------------------------------------------------------------------------
49 GalleryCodec::~GalleryCodec()
53 // -----------------------------------------------------------------------------
55 BOOL GalleryCodec::IsCoded( SvStream& rStm, UINT32& rVersion )
57 const ULONG nPos = rStm.Tell();
58 BOOL bRet;
59 BYTE cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
61 rStm >> cByte1 >> cByte2 >> cByte3 >> cByte4 >> cByte5 >> cByte6;
63 if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
65 rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
66 bRet = TRUE;
68 else
70 rVersion = 0;
71 bRet = FALSE;
74 rStm.Seek( nPos );
76 return bRet;
79 // -----------------------------------------------------------------------------
81 void GalleryCodec::Write( SvStream& rStmToWrite )
83 UINT32 nPos, nCompSize;
85 rStmToWrite.Seek( STREAM_SEEK_TO_END );
86 const UINT32 nSize = rStmToWrite.Tell();
87 rStmToWrite.Seek( 0UL );
89 rStm << 'S' << 'V' << 'R' << 'L' << 'E' << '2';
90 rStm << nSize;
92 nPos = rStm.Tell();
93 rStm.SeekRel( 4UL );
95 ZCodec aCodec;
96 aCodec.BeginCompression();
97 aCodec.Compress( rStmToWrite, rStm );
98 aCodec.EndCompression();
100 nCompSize = rStm.Tell() - nPos - 4UL;
101 rStm.Seek( nPos );
102 rStm << nCompSize;
103 rStm.Seek( STREAM_SEEK_TO_END );
106 // -----------------------------------------------------------------------------
108 void GalleryCodec::Read( SvStream& rStmToRead )
110 UINT32 nVersion = 0;
112 if( IsCoded( rStm, nVersion ) )
114 UINT32 nCompressedSize, nUnCompressedSize;
116 rStm.SeekRel( 6 );
117 rStm >> nUnCompressedSize >> nCompressedSize;
119 // decompress
120 if( 1 == nVersion )
122 BYTE* pCompressedBuffer = new BYTE[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
123 BYTE* pInBuf = pCompressedBuffer;
124 BYTE* pOutBuf = new BYTE[ nUnCompressedSize ];
125 BYTE* pTmpBuf = pOutBuf;
126 BYTE* pLast = pOutBuf + nUnCompressedSize - 1;
127 ULONG nIndex = 0UL, nCountByte, nRunByte;
128 BOOL bEndDecoding = FALSE;
132 nCountByte = *pInBuf++;
134 if ( !nCountByte )
136 nRunByte = *pInBuf++;
138 if ( nRunByte > 2 )
140 // absolutes Fuellen
141 memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
142 pInBuf += nRunByte;
143 nIndex += nRunByte;
145 // WORD-Alignment beachten
146 if ( nRunByte & 1 )
147 pInBuf++;
149 else if ( nRunByte == 1 ) // Ende des Bildes
150 bEndDecoding = TRUE;
152 else
154 const BYTE cVal = *pInBuf++;
156 memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
157 nIndex += nCountByte;
160 while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
162 rStmToRead.Write( pOutBuf, nUnCompressedSize );
164 delete[] pOutBuf;
165 delete[] pCompressedBuffer;
167 else if( 2 == nVersion )
169 ZCodec aCodec;
171 aCodec.BeginCompression();
172 aCodec.Decompress( rStm, rStmToRead );
173 aCodec.EndCompression();