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>
23 #include <tools/debug.hxx>
29 DBG_NAME(GalleryCodec
)
31 GalleryCodec::GalleryCodec( SvStream
& 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();
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 );
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';
87 aCodec
.BeginCompression();
88 aCodec
.Compress( rStmToWrite
, rStm
);
89 aCodec
.EndCompression();
91 nCompSize
= rStm
.Tell() - nPos
- 4UL;
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
;
108 rStm
>> nUnCompressedSize
>> nCompressedSize
;
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
++;
127 nRunByte
= *pInBuf
++;
132 memcpy( &pTmpBuf
[ nIndex
], pInBuf
, nRunByte
);
136 // WORD-Alignment beachten
140 else if ( nRunByte
== 1 ) // Ende des Bildes
141 bEndDecoding
= sal_True
;
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
);
156 delete[] pCompressedBuffer
;
158 else if( 2 == nVersion
)
162 aCodec
.BeginCompression();
163 aCodec
.Decompress( rStm
, rStmToRead
);
164 aCodec
.EndCompression();
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */