fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svx / source / gallery2 / codec.cxx
blob85bd091db52306395e3e87b29bc7715e83c641a0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <tools/stream.hxx>
22 #include <tools/zcodec.hxx>
23 #include <tools/debug.hxx>
24 #include "codec.hxx"
26 // ----------------
27 // - GalleryCodec -
28 // ----------------
29 DBG_NAME(GalleryCodec)
31 GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
32 rStm( rIOStm )
34 DBG_CTOR(GalleryCodec,NULL);
37 // -----------------------------------------------------------------------------
39 GalleryCodec::~GalleryCodec()
41 DBG_DTOR(GalleryCodec,NULL);
44 // -----------------------------------------------------------------------------
46 sal_Bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
48 const sal_uIntPtr nPos = rStm.Tell();
49 sal_Bool bRet;
50 sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
52 rStm >> cByte1 >> cByte2 >> cByte3 >> cByte4 >> cByte5 >> cByte6;
54 if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
56 rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
57 bRet = sal_True;
59 else
61 rVersion = 0;
62 bRet = sal_False;
65 rStm.Seek( nPos );
67 return bRet;
70 // -----------------------------------------------------------------------------
72 void GalleryCodec::Write( SvStream& rStmToWrite )
74 sal_uInt32 nPos, nCompSize;
76 rStmToWrite.Seek( STREAM_SEEK_TO_END );
77 const sal_uInt32 nSize = rStmToWrite.Tell();
78 rStmToWrite.Seek( 0UL );
80 rStm << 'S' << 'V' << 'R' << 'L' << 'E' << '2';
81 rStm << nSize;
83 nPos = rStm.Tell();
84 rStm.SeekRel( 4UL );
86 ZCodec aCodec;
87 aCodec.BeginCompression();
88 aCodec.Compress( rStmToWrite, rStm );
89 aCodec.EndCompression();
91 nCompSize = rStm.Tell() - nPos - 4UL;
92 rStm.Seek( nPos );
93 rStm << nCompSize;
94 rStm.Seek( STREAM_SEEK_TO_END );
97 // -----------------------------------------------------------------------------
99 void GalleryCodec::Read( SvStream& rStmToRead )
101 sal_uInt32 nVersion = 0;
103 if( IsCoded( rStm, nVersion ) )
105 sal_uInt32 nCompressedSize, nUnCompressedSize;
107 rStm.SeekRel( 6 );
108 rStm >> nUnCompressedSize >> nCompressedSize;
110 // decompress
111 if( 1 == nVersion )
113 sal_uInt8* pCompressedBuffer = new sal_uInt8[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
114 sal_uInt8* pInBuf = pCompressedBuffer;
115 sal_uInt8* pOutBuf = new sal_uInt8[ nUnCompressedSize ];
116 sal_uInt8* pTmpBuf = pOutBuf;
117 sal_uInt8* pLast = pOutBuf + nUnCompressedSize - 1;
118 sal_uIntPtr nIndex = 0UL, nCountByte, nRunByte;
119 sal_Bool bEndDecoding = sal_False;
123 nCountByte = *pInBuf++;
125 if ( !nCountByte )
127 nRunByte = *pInBuf++;
129 if ( nRunByte > 2 )
131 // absolutes Fuellen
132 memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
133 pInBuf += nRunByte;
134 nIndex += nRunByte;
136 // WORD-Alignment beachten
137 if ( nRunByte & 1 )
138 pInBuf++;
140 else if ( nRunByte == 1 ) // Ende des Bildes
141 bEndDecoding = sal_True;
143 else
145 const sal_uInt8 cVal = *pInBuf++;
147 memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
148 nIndex += nCountByte;
151 while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
153 rStmToRead.Write( pOutBuf, nUnCompressedSize );
155 delete[] pOutBuf;
156 delete[] pCompressedBuffer;
158 else if( 2 == nVersion )
160 ZCodec aCodec;
162 aCodec.BeginCompression();
163 aCodec.Decompress( rStm, rStmToRead );
164 aCodec.EndCompression();
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */