xref: /aoo41x/main/svx/source/gallery2/codec.cxx (revision f6e50924)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include <tools/stream.hxx>
28 #include <tools/zcodec.hxx>
29 #include "codec.hxx"
30 #include <tools/debug.hxx>
31 
32 // ----------------
33 // - GalleryCodec -
34 // ----------------
DBG_NAME(GalleryCodec)35 DBG_NAME(GalleryCodec)
36 
37 GalleryCodec::GalleryCodec( SvStream& rIOStm ) :
38     rStm( rIOStm )
39 {
40     DBG_CTOR(GalleryCodec,NULL);
41 
42 }
43 
44 // -----------------------------------------------------------------------------
45 
~GalleryCodec()46 GalleryCodec::~GalleryCodec()
47 {
48 
49     DBG_DTOR(GalleryCodec,NULL);
50 }
51 
52 // -----------------------------------------------------------------------------
53 
IsCoded(SvStream & rStm,sal_uInt32 & rVersion)54 sal_Bool GalleryCodec::IsCoded( SvStream& rStm, sal_uInt32& rVersion )
55 {
56 	const sal_uIntPtr	nPos = rStm.Tell();
57 	sal_Bool		bRet;
58 	sal_uInt8		cByte1, cByte2, cByte3, cByte4, cByte5, cByte6;
59 
60 	rStm >> cByte1 >> cByte2 >> cByte3 >> cByte4 >> cByte5 >> cByte6;
61 
62 	if ( cByte1 == 'S' && cByte2 == 'V' && cByte3 == 'R' && cByte4 == 'L' && cByte5 == 'E' && ( cByte6 == '1' || cByte6 == '2' ) )
63 	{
64         rVersion = ( ( cByte6 == '1' ) ? 1 : 2 );
65 		bRet = sal_True;
66 	}
67     else
68     {
69         rVersion = 0;
70 		bRet = sal_False;
71     }
72 
73 	rStm.Seek( nPos );
74 
75 	return bRet;
76 }
77 
78 // -----------------------------------------------------------------------------
79 
Write(SvStream & rStmToWrite)80 void GalleryCodec::Write( SvStream& rStmToWrite )
81 {
82 	sal_uInt32 nPos, nCompSize;
83 
84     rStmToWrite.Seek( STREAM_SEEK_TO_END );
85 	const sal_uInt32 nSize = rStmToWrite.Tell();
86 	rStmToWrite.Seek( 0UL );
87 
88 	rStm << 'S' << 'V' << 'R' << 'L' << 'E' << '2';
89 	rStm << nSize;
90 
91 	nPos = rStm.Tell();
92 	rStm.SeekRel( 4UL );
93 
94     ZCodec aCodec;
95     aCodec.BeginCompression();
96     aCodec.Compress( rStmToWrite, rStm );
97     aCodec.EndCompression();
98 
99 	nCompSize = rStm.Tell() - nPos - 4UL;
100 	rStm.Seek( nPos );
101 	rStm << nCompSize;
102 	rStm.Seek( STREAM_SEEK_TO_END );
103 }
104 
105 // -----------------------------------------------------------------------------
106 
Read(SvStream & rStmToRead)107 void GalleryCodec::Read( SvStream& rStmToRead )
108 {
109 	sal_uInt32 nVersion = 0;
110 
111 	if( IsCoded( rStm, nVersion ) )
112 	{
113 		sal_uInt32	nCompressedSize, nUnCompressedSize;
114 
115 		rStm.SeekRel( 6 );
116 		rStm >> nUnCompressedSize >> nCompressedSize;
117 
118 		// decompress
119         if( 1 == nVersion )
120         {
121             sal_uInt8*   pCompressedBuffer = new sal_uInt8[ nCompressedSize ]; rStm.Read( pCompressedBuffer, nCompressedSize );
122 	        sal_uInt8*	pInBuf = pCompressedBuffer;
123 	        sal_uInt8*	pOutBuf = new sal_uInt8[ nUnCompressedSize ];
124 	        sal_uInt8*	pTmpBuf = pOutBuf;
125 	        sal_uInt8*	pLast = pOutBuf + nUnCompressedSize - 1;
126 	        sal_uIntPtr   nIndex = 0UL, nCountByte, nRunByte;
127 	        sal_Bool    bEndDecoding = sal_False;
128 
129 	        do
130 	        {
131 		        nCountByte = *pInBuf++;
132 
133 		        if ( !nCountByte )
134 		        {
135 			        nRunByte = *pInBuf++;
136 
137 			        if ( nRunByte > 2 )
138 			        {
139 				        // absolutes Fuellen
140 				        memcpy( &pTmpBuf[ nIndex ], pInBuf, nRunByte );
141 				        pInBuf += nRunByte;
142 				        nIndex += nRunByte;
143 
144 				        // WORD-Alignment beachten
145 				        if ( nRunByte & 1 )
146 					        pInBuf++;
147 			        }
148 			        else if ( nRunByte == 1 )   // Ende des Bildes
149 				        bEndDecoding = sal_True;
150 		        }
151 		        else
152 		        {
153 			        const sal_uInt8 cVal = *pInBuf++;
154 
155 			        memset( &pTmpBuf[ nIndex ], cVal, nCountByte );
156 			        nIndex += nCountByte;
157 		        }
158 	        }
159 	        while ( !bEndDecoding && ( pTmpBuf <= pLast ) );
160 
161    		    rStmToRead.Write( pOutBuf, nUnCompressedSize );
162 
163             delete[] pOutBuf;
164             delete[] pCompressedBuffer;
165         }
166         else if( 2 == nVersion )
167         {
168 	        ZCodec aCodec;
169 
170             aCodec.BeginCompression();
171             aCodec.Decompress( rStm, rStmToRead );
172             aCodec.EndCompression();
173         }
174 	}
175 }
176