Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <tools/stream.hxx>
22 : #include <tools/zcodec.hxx>
23 : #include <tools/debug.hxx>
24 : #include "codec.hxx"
25 :
26 : // - GalleryCodec -
27 :
28 :
29 0 : GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
30 0 : rStm( rIOStm )
31 : {
32 0 : }
33 :
34 0 : GalleryCodec::~GalleryCodec()
35 : {
36 0 : }
37 :
38 0 : sal_Bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
39 : {
40 0 : const sal_uIntPtr nPos = rStm.Tell();
41 : sal_Bool bRet;
42 : sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
43 :
44 0 : rStm.ReadUChar( cByte1 ).ReadUChar( cByte2 ).ReadUChar( cByte3 ).ReadUChar( cByte4 ).ReadUChar( cByte5 ).ReadUChar( cByte6 );
45 :
46 0 : if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
47 : {
48 0 : rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
49 0 : bRet = sal_True;
50 : }
51 : else
52 : {
53 0 : rVersion = 0;
54 0 : bRet = sal_False;
55 : }
56 :
57 0 : rStm.Seek( nPos );
58 :
59 0 : return bRet;
60 : }
61 :
62 0 : void GalleryCodec::Write( SvStream& rStmToWrite )
63 : {
64 : sal_uInt32 nPos, nCompSize;
65 :
66 0 : rStmToWrite.Seek( STREAM_SEEK_TO_END );
67 0 : const sal_uInt32 nSize = rStmToWrite.Tell();
68 0 : rStmToWrite.Seek( 0UL );
69 :
70 0 : rStm.WriteChar( 'S' ).WriteChar( 'V' ).WriteChar( 'R' ).WriteChar( 'L' ).WriteChar( 'E' ).WriteChar( '2' );
71 0 : rStm.WriteUInt32( nSize );
72 :
73 0 : nPos = rStm.Tell();
74 0 : rStm.SeekRel( 4UL );
75 :
76 0 : ZCodec aCodec;
77 0 : aCodec.BeginCompression();
78 0 : aCodec.Compress( rStmToWrite, rStm );
79 0 : aCodec.EndCompression();
80 :
81 0 : nCompSize = rStm.Tell() - nPos - 4UL;
82 0 : rStm.Seek( nPos );
83 0 : rStm.WriteUInt32( nCompSize );
84 0 : rStm.Seek( STREAM_SEEK_TO_END );
85 0 : }
86 :
87 0 : void GalleryCodec::Read( SvStream& rStmToRead )
88 : {
89 0 : sal_uInt32 nVersion = 0;
90 :
91 0 : if( IsCoded( rStm, nVersion ) )
92 : {
93 : sal_uInt32 nCompressedSize, nUnCompressedSize;
94 :
95 0 : rStm.SeekRel( 6 );
96 0 : rStm.ReadUInt32( nUnCompressedSize ).ReadUInt32( nCompressedSize );
97 :
98 : // decompress
99 0 : if( 1 == nVersion )
100 : {
101 0 : sal_uInt8* pCompressedBuffer = new sal_uInt8[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
102 0 : sal_uInt8* pInBuf = pCompressedBuffer;
103 0 : sal_uInt8* pOutBuf = new sal_uInt8[ nUnCompressedSize ];
104 0 : sal_uInt8* pTmpBuf = pOutBuf;
105 0 : sal_uInt8* pLast = pOutBuf + nUnCompressedSize - 1;
106 0 : sal_uIntPtr nIndex = 0UL, nCountByte, nRunByte;
107 0 : sal_Bool bEndDecoding = sal_False;
108 :
109 0 : do
110 : {
111 0 : nCountByte = *pInBuf++;
112 :
113 0 : if ( !nCountByte )
114 : {
115 0 : nRunByte = *pInBuf++;
116 :
117 0 : if ( nRunByte > 2 )
118 : {
119 : // filling absolutely
120 0 : memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
121 0 : pInBuf += nRunByte;
122 0 : nIndex += nRunByte;
123 :
124 : // note WORD alignment
125 0 : if ( nRunByte & 1 )
126 0 : pInBuf++;
127 : }
128 0 : else if ( nRunByte == 1 ) // End of the image
129 0 : bEndDecoding = sal_True;
130 : }
131 : else
132 : {
133 0 : const sal_uInt8 cVal = *pInBuf++;
134 :
135 0 : memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
136 0 : nIndex += nCountByte;
137 : }
138 : }
139 0 : while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
140 :
141 0 : rStmToRead.Write( pOutBuf, nUnCompressedSize );
142 :
143 0 : delete[] pOutBuf;
144 0 : delete[] pCompressedBuffer;
145 : }
146 0 : else if( 2 == nVersion )
147 : {
148 0 : ZCodec aCodec;
149 :
150 0 : aCodec.BeginCompression();
151 0 : aCodec.Decompress( rStm, rStmToRead );
152 0 : aCodec.EndCompression();
153 : }
154 : }
155 0 : }
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|