1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 #include <boost/scoped_array.hpp>
29 GalleryCodec::GalleryCodec( SvStream
& rIOStm
) :
34 GalleryCodec::~GalleryCodec()
38 bool GalleryCodec::IsCoded( SvStream
& rStm
, sal_uInt32
& rVersion
)
40 const sal_uIntPtr nPos
= rStm
.Tell();
42 sal_uInt8 cByte1
, cByte2
, cByte3
, cByte4
, cByte5
, cByte6
;
44 rStm
.ReadUChar( cByte1
).ReadUChar( cByte2
).ReadUChar( cByte3
).ReadUChar( cByte4
).ReadUChar( cByte5
).ReadUChar( cByte6
);
46 if ( cByte1
== 'S' && cByte2
== 'V' && cByte3
== 'R' && cByte4
== 'L' && cByte5
== 'E' && ( cByte6
== '1' || cByte6
== '2' ) )
48 rVersion
= ( ( cByte6
== '1' ) ? 1 : 2 );
62 void GalleryCodec::Write( SvStream
& rStmToWrite
)
64 sal_uInt32 nPos
, nCompSize
;
66 rStmToWrite
.Seek( STREAM_SEEK_TO_END
);
67 const sal_uInt32 nSize
= rStmToWrite
.Tell();
68 rStmToWrite
.Seek( 0UL );
70 rStm
.WriteChar( 'S' ).WriteChar( 'V' ).WriteChar( 'R' ).WriteChar( 'L' ).WriteChar( 'E' ).WriteChar( '2' );
71 rStm
.WriteUInt32( nSize
);
77 aCodec
.BeginCompression();
78 aCodec
.Compress( rStmToWrite
, rStm
);
79 aCodec
.EndCompression();
81 nCompSize
= rStm
.Tell() - nPos
- 4UL;
83 rStm
.WriteUInt32( nCompSize
);
84 rStm
.Seek( STREAM_SEEK_TO_END
);
87 void GalleryCodec::Read( SvStream
& rStmToRead
)
89 sal_uInt32 nVersion
= 0;
91 if( IsCoded( rStm
, nVersion
) )
93 sal_uInt32 nCompressedSize
, nUnCompressedSize
;
96 rStm
.ReadUInt32( nUnCompressedSize
).ReadUInt32( nCompressedSize
);
101 boost::scoped_array
<sal_uInt8
> pCompressedBuffer(new sal_uInt8
[ nCompressedSize
]);
102 rStm
.Read( pCompressedBuffer
.get(), nCompressedSize
);
103 sal_uInt8
* pInBuf
= pCompressedBuffer
.get();
104 boost::scoped_array
<sal_uInt8
> pOutBuf(new sal_uInt8
[ nUnCompressedSize
]);
105 sal_uInt8
* pTmpBuf
= pOutBuf
.get();
106 sal_uInt8
* pLast
= pOutBuf
.get() + nUnCompressedSize
- 1;
107 sal_uIntPtr nIndex
= 0UL, nCountByte
, nRunByte
;
108 bool bEndDecoding
= false;
112 nCountByte
= *pInBuf
++;
116 nRunByte
= *pInBuf
++;
120 // filling absolutely
121 memcpy( &pTmpBuf
[ nIndex
], pInBuf
, nRunByte
);
125 // note WORD alignment
129 else if ( nRunByte
== 1 ) // End of the image
134 const sal_uInt8 cVal
= *pInBuf
++;
136 memset( &pTmpBuf
[ nIndex
], cVal
, nCountByte
);
137 nIndex
+= nCountByte
;
140 while ( !bEndDecoding
&& ( pTmpBuf
<= pLast
) );
142 rStmToRead
.Write( pOutBuf
.get(), nUnCompressedSize
);
144 else if( 2 == nVersion
)
148 aCodec
.BeginCompression();
149 aCodec
.Decompress( rStm
, rStmToRead
);
150 aCodec
.EndCompression();
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */