1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: codec.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <tools/stream.hxx>
35 #include <tools/zcodec.hxx>
42 GalleryCodec::GalleryCodec( SvStream
& rIOStm
) :
47 // -----------------------------------------------------------------------------
49 GalleryCodec::~GalleryCodec()
53 // -----------------------------------------------------------------------------
55 BOOL
GalleryCodec::IsCoded( SvStream
& rStm
, UINT32
& rVersion
)
57 const ULONG nPos
= rStm
.Tell();
59 BYTE cByte1
, cByte2
, cByte3
, cByte4
, cByte5
, cByte6
;
61 rStm
>> cByte1
>> cByte2
>> cByte3
>> cByte4
>> cByte5
>> cByte6
;
63 if ( cByte1
== 'S' && cByte2
== 'V' && cByte3
== 'R' && cByte4
== 'L' && cByte5
== 'E' && ( cByte6
== '1' || cByte6
== '2' ) )
65 rVersion
= ( ( cByte6
== '1' ) ? 1 : 2 );
79 // -----------------------------------------------------------------------------
81 void GalleryCodec::Write( SvStream
& rStmToWrite
)
83 UINT32 nPos
, nCompSize
;
85 rStmToWrite
.Seek( STREAM_SEEK_TO_END
);
86 const UINT32 nSize
= rStmToWrite
.Tell();
87 rStmToWrite
.Seek( 0UL );
89 rStm
<< 'S' << 'V' << 'R' << 'L' << 'E' << '2';
96 aCodec
.BeginCompression();
97 aCodec
.Compress( rStmToWrite
, rStm
);
98 aCodec
.EndCompression();
100 nCompSize
= rStm
.Tell() - nPos
- 4UL;
103 rStm
.Seek( STREAM_SEEK_TO_END
);
106 // -----------------------------------------------------------------------------
108 void GalleryCodec::Read( SvStream
& rStmToRead
)
112 if( IsCoded( rStm
, nVersion
) )
114 UINT32 nCompressedSize
, nUnCompressedSize
;
117 rStm
>> nUnCompressedSize
>> nCompressedSize
;
122 BYTE
* pCompressedBuffer
= new BYTE
[ nCompressedSize
]; rStm
.Read( pCompressedBuffer
, nCompressedSize
);
123 BYTE
* pInBuf
= pCompressedBuffer
;
124 BYTE
* pOutBuf
= new BYTE
[ nUnCompressedSize
];
125 BYTE
* pTmpBuf
= pOutBuf
;
126 BYTE
* pLast
= pOutBuf
+ nUnCompressedSize
- 1;
127 ULONG nIndex
= 0UL, nCountByte
, nRunByte
;
128 BOOL bEndDecoding
= FALSE
;
132 nCountByte
= *pInBuf
++;
136 nRunByte
= *pInBuf
++;
141 memcpy( &pTmpBuf
[ nIndex
], pInBuf
, nRunByte
);
145 // WORD-Alignment beachten
149 else if ( nRunByte
== 1 ) // Ende des Bildes
154 const BYTE cVal
= *pInBuf
++;
156 memset( &pTmpBuf
[ nIndex
], cVal
, nCountByte
);
157 nIndex
+= nCountByte
;
160 while ( !bEndDecoding
&& ( pTmpBuf
<= pLast
) );
162 rStmToRead
.Write( pOutBuf
, nUnCompressedSize
);
165 delete[] pCompressedBuffer
;
167 else if( 2 == nVersion
)
171 aCodec
.BeginCompression();
172 aCodec
.Decompress( rStm
, rStmToRead
);
173 aCodec
.EndCompression();