Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <tools/stream.hxx>
31 : : #include <tools/zcodec.hxx>
32 : : #include "codec.hxx"
33 : :
34 : : // ----------------
35 : : // - GalleryCodec -
36 : : // ----------------
37 : :
38 : 0 : GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
39 : 0 : rStm( rIOStm )
40 : : {
41 : 0 : }
42 : :
43 : : // -----------------------------------------------------------------------------
44 : :
45 : 0 : GalleryCodec::~GalleryCodec()
46 : : {
47 : 0 : }
48 : :
49 : : // -----------------------------------------------------------------------------
50 : :
51 : 0 : sal_Bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
52 : : {
53 : 0 : const sal_uIntPtr nPos = rStm.Tell();
54 : : sal_Bool bRet;
55 : : sal_uInt8 cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
56 : :
57 [ # # ][ # # ]: 0 : rStm >> cByte1 >> cByte2 >> cByte3 >> cByte4 >> cByte5 >> cByte6;
[ # # ][ # # ]
[ # # ][ # # ]
58 : :
59 [ # # ][ # # ]: 0 : if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
60 : : {
61 [ # # ]: 0 : rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
62 : 0 : bRet = sal_True;
63 : : }
64 : : else
65 : : {
66 : 0 : rVersion = 0;
67 : 0 : bRet = sal_False;
68 : : }
69 : :
70 [ # # ]: 0 : rStm.Seek( nPos );
71 : :
72 : 0 : return bRet;
73 : : }
74 : :
75 : : // -----------------------------------------------------------------------------
76 : :
77 : 0 : void GalleryCodec::Write( SvStream& rStmToWrite )
78 : : {
79 : : sal_uInt32 nPos, nCompSize;
80 : :
81 [ # # ]: 0 : rStmToWrite.Seek( STREAM_SEEK_TO_END );
82 : 0 : const sal_uInt32 nSize = rStmToWrite.Tell();
83 [ # # ]: 0 : rStmToWrite.Seek( 0UL );
84 : :
85 [ # # ][ # # ]: 0 : rStm << 'S' << 'V' << 'R' << 'L' << 'E' << '2';
[ # # ][ # # ]
[ # # ][ # # ]
86 [ # # ]: 0 : rStm << nSize;
87 : :
88 : 0 : nPos = rStm.Tell();
89 [ # # ]: 0 : rStm.SeekRel( 4UL );
90 : :
91 [ # # ]: 0 : ZCodec aCodec;
92 [ # # ]: 0 : aCodec.BeginCompression();
93 [ # # ]: 0 : aCodec.Compress( rStmToWrite, rStm );
94 [ # # ]: 0 : aCodec.EndCompression();
95 : :
96 : 0 : nCompSize = rStm.Tell() - nPos - 4UL;
97 [ # # ]: 0 : rStm.Seek( nPos );
98 [ # # ]: 0 : rStm << nCompSize;
99 [ # # ][ # # ]: 0 : rStm.Seek( STREAM_SEEK_TO_END );
100 : 0 : }
101 : :
102 : : // -----------------------------------------------------------------------------
103 : :
104 : 0 : void GalleryCodec::Read( SvStream& rStmToRead )
105 : : {
106 : 0 : sal_uInt32 nVersion = 0;
107 : :
108 [ # # ][ # # ]: 0 : if( IsCoded( rStm, nVersion ) )
109 : : {
110 : : sal_uInt32 nCompressedSize, nUnCompressedSize;
111 : :
112 [ # # ]: 0 : rStm.SeekRel( 6 );
113 [ # # ][ # # ]: 0 : rStm >> nUnCompressedSize >> nCompressedSize;
114 : :
115 : : // decompress
116 [ # # ]: 0 : if( 1 == nVersion )
117 : : {
118 [ # # ][ # # ]: 0 : sal_uInt8* pCompressedBuffer = new sal_uInt8[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
119 : 0 : sal_uInt8* pInBuf = pCompressedBuffer;
120 [ # # ]: 0 : sal_uInt8* pOutBuf = new sal_uInt8[ nUnCompressedSize ];
121 : 0 : sal_uInt8* pTmpBuf = pOutBuf;
122 : 0 : sal_uInt8* pLast = pOutBuf + nUnCompressedSize - 1;
123 : 0 : sal_uIntPtr nIndex = 0UL, nCountByte, nRunByte;
124 : 0 : sal_Bool bEndDecoding = sal_False;
125 : :
126 [ # # ][ # # ]: 0 : do
[ # # ]
127 : : {
128 : 0 : nCountByte = *pInBuf++;
129 : :
130 [ # # ]: 0 : if ( !nCountByte )
131 : : {
132 : 0 : nRunByte = *pInBuf++;
133 : :
134 [ # # ]: 0 : if ( nRunByte > 2 )
135 : : {
136 : : // absolutes Fuellen
137 : 0 : memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
138 : 0 : pInBuf += nRunByte;
139 : 0 : nIndex += nRunByte;
140 : :
141 : : // WORD-Alignment beachten
142 [ # # ]: 0 : if ( nRunByte & 1 )
143 : 0 : pInBuf++;
144 : : }
145 [ # # ]: 0 : else if ( nRunByte == 1 ) // Ende des Bildes
146 : 0 : bEndDecoding = sal_True;
147 : : }
148 : : else
149 : : {
150 : 0 : const sal_uInt8 cVal = *pInBuf++;
151 : :
152 : 0 : memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
153 : 0 : nIndex += nCountByte;
154 : : }
155 : : }
156 : : while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
157 : :
158 [ # # ]: 0 : rStmToRead.Write( pOutBuf, nUnCompressedSize );
159 : :
160 [ # # ]: 0 : delete[] pOutBuf;
161 [ # # ]: 0 : delete[] pCompressedBuffer;
162 : : }
163 [ # # ]: 0 : else if( 2 == nVersion )
164 : : {
165 [ # # ]: 0 : ZCodec aCodec;
166 : :
167 [ # # ]: 0 : aCodec.BeginCompression();
168 [ # # ]: 0 : aCodec.Decompress( rStm, rStmToRead );
169 [ # # ][ # # ]: 0 : aCodec.EndCompression();
170 : : }
171 : : }
172 : 0 : }
173 : :
174 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|