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