Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <vcl/graph.hxx>
22 : #include <vcl/bmpacc.hxx>
23 : #include <vcl/fltcall.hxx>
24 : #include <vcl/FilterConfigItem.hxx>
25 :
26 : //============================ RASWriter ==================================
27 :
28 : class RASWriter {
29 :
30 : private:
31 :
32 : SvStream & m_rOStm;
33 :
34 : sal_Bool mbStatus;
35 : BitmapReadAccess* mpAcc;
36 :
37 : sal_uLong mnWidth, mnHeight;
38 : sal_uInt16 mnColors, mnDepth;
39 :
40 : sal_uLong mnRepCount;
41 : sal_uInt8 mnRepVal;
42 :
43 : com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
44 :
45 : void ImplCallback( sal_uLong nCurrentYPos );
46 : sal_Bool ImplWriteHeader();
47 : void ImplWritePalette();
48 : void ImplWriteBody();
49 : void ImplPutByte( sal_uInt8 ); // RLE decoding
50 :
51 : public:
52 : RASWriter(SvStream &rStream);
53 : ~RASWriter();
54 :
55 : sal_Bool WriteRAS( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
56 : };
57 :
58 : //=================== Methoden von RASWriter ==============================
59 :
60 0 : RASWriter::RASWriter(SvStream &rStream)
61 : : m_rOStm(rStream)
62 : , mbStatus(sal_True)
63 : , mpAcc(NULL)
64 0 : , mnRepCount( 0xffffffff )
65 : {
66 0 : }
67 :
68 :
69 :
70 0 : RASWriter::~RASWriter()
71 : {
72 0 : }
73 :
74 :
75 :
76 0 : void RASWriter::ImplCallback( sal_uLong nYPos )
77 : {
78 0 : if ( xStatusIndicator.is() )
79 0 : xStatusIndicator->setValue( (sal_uInt16)( ( 100 * nYPos ) / mnHeight ) );
80 0 : }
81 :
82 :
83 :
84 0 : sal_Bool RASWriter::WriteRAS( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem)
85 : {
86 0 : Bitmap aBmp;
87 :
88 0 : if ( pFilterConfigItem )
89 : {
90 0 : xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
91 0 : if ( xStatusIndicator.is() )
92 : {
93 0 : OUString aMsg;
94 0 : xStatusIndicator->start( aMsg, 100 );
95 : }
96 : }
97 :
98 0 : BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
99 0 : aBmp = aBmpEx.GetBitmap();
100 :
101 0 : if ( aBmp.GetBitCount() == 4 )
102 0 : aBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
103 :
104 0 : mnDepth = aBmp.GetBitCount();
105 :
106 : // export code below only handles three discrete cases
107 0 : mnDepth = mnDepth <= 1 ? 1 : mnDepth <= 8 ? 8 : 24;
108 :
109 0 : mpAcc = aBmp.AcquireReadAccess();
110 0 : if ( mpAcc )
111 : {
112 0 : sal_uInt16 nOStmOldModus = m_rOStm.GetNumberFormatInt();
113 0 : m_rOStm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
114 :
115 0 : if ( ImplWriteHeader() )
116 : {
117 0 : if ( mnDepth <= 8 )
118 0 : ImplWritePalette();
119 0 : ImplWriteBody();
120 : }
121 :
122 0 : m_rOStm.SetNumberFormatInt( nOStmOldModus );
123 :
124 0 : aBmp.ReleaseAccess( mpAcc );
125 : }
126 : else
127 0 : mbStatus = sal_False;
128 :
129 0 : if ( xStatusIndicator.is() )
130 0 : xStatusIndicator->end();
131 :
132 0 : return mbStatus;
133 : }
134 :
135 :
136 :
137 0 : sal_Bool RASWriter::ImplWriteHeader()
138 : {
139 0 : mnWidth = mpAcc->Width();
140 0 : mnHeight = mpAcc->Height();
141 0 : if ( mnDepth <= 8 )
142 : {
143 0 : mnColors = mpAcc->GetPaletteEntryCount();
144 0 : if (mnColors == 0)
145 0 : mbStatus = sal_False;
146 : }
147 0 : if ( mbStatus && mnWidth && mnHeight && mnDepth )
148 : {
149 0 : m_rOStm.WriteUInt32( (sal_uInt32)0x59a66a95 ).WriteUInt32( (sal_uInt32)mnWidth ).WriteUInt32( (sal_uInt32)mnHeight )
150 0 : .WriteUInt32( (sal_uInt32)mnDepth )
151 0 : .WriteUInt32( (sal_uInt32)(( ( ( ( mnWidth * mnDepth ) + 15 ) >> 4 ) << 1 ) * mnHeight) )
152 0 : .WriteUInt32( (sal_uInt32)2 );
153 :
154 0 : if ( mnDepth > 8 )
155 0 : m_rOStm.WriteUInt32( (sal_uInt32)0 ).WriteUInt32( (sal_uInt32)0 );
156 : else
157 : {
158 :
159 0 : m_rOStm.WriteUInt32( (sal_uInt32)1 ).WriteUInt32( (sal_uInt32)( mnColors * 3 ) );
160 : }
161 : }
162 0 : else mbStatus = sal_False;
163 :
164 0 : return mbStatus;
165 : }
166 :
167 :
168 :
169 0 : void RASWriter::ImplWritePalette()
170 : {
171 : sal_uInt16 i;
172 :
173 0 : for ( i = 0; i < mnColors; m_rOStm.WriteUChar( mpAcc->GetPaletteColor( i++ ).GetRed() ) ) ;
174 0 : for ( i = 0; i < mnColors; m_rOStm.WriteUChar( mpAcc->GetPaletteColor( i++ ).GetGreen() ) ) ;
175 0 : for ( i = 0; i < mnColors; m_rOStm.WriteUChar( mpAcc->GetPaletteColor( i++ ).GetBlue() ) ) ;
176 0 : }
177 :
178 :
179 :
180 0 : void RASWriter::ImplWriteBody()
181 : {
182 : sal_uLong x, y;
183 :
184 0 : if ( mnDepth == 24 )
185 : {
186 0 : for ( y = 0; y < mnHeight; y++ )
187 : {
188 0 : ImplCallback( y ); // processing output
189 0 : for ( x = 0; x < mnWidth; x++ )
190 : {
191 0 : BitmapColor aColor( mpAcc->GetPixel( y, x ) );
192 0 : ImplPutByte( aColor.GetBlue() ); // Format ist BGR
193 0 : ImplPutByte( aColor.GetGreen() );
194 0 : ImplPutByte( aColor.GetRed() );
195 0 : }
196 0 : if ( x & 1 ) ImplPutByte( 0 ); // WORD ALIGNMENT ???
197 : }
198 : }
199 0 : else if ( mnDepth == 8 )
200 : {
201 0 : for ( y = 0; y < mnHeight; y++ )
202 : {
203 0 : ImplCallback( y ); // processing output
204 0 : for ( x = 0; x < mnWidth; x++ )
205 : {
206 0 : ImplPutByte ( mpAcc->GetPixelIndex( y, x ) );
207 : }
208 0 : if ( x & 1 ) ImplPutByte( 0 ); // WORD ALIGNMENT ???
209 : }
210 : }
211 0 : else if ( mnDepth == 1 )
212 : {
213 0 : sal_uInt8 nDat = 0;
214 :
215 0 : for ( y = 0; y < mnHeight; y++ )
216 : {
217 0 : ImplCallback( y ); // processing output
218 0 : for ( x = 0; x < mnWidth; x++ )
219 : {
220 0 : nDat = ( ( nDat << 1 ) | ( mpAcc->GetPixelIndex( y, x ) & 1 ) );
221 0 : if ( ( x & 7 ) == 7 )
222 0 : ImplPutByte( nDat );
223 : }
224 0 : if ( x & 7 )
225 0 : ImplPutByte( sal::static_int_cast< sal_uInt8 >(nDat << ( ( ( x & 7 ) ^ 7 ) + 1)) );// write remaining bits
226 0 : if (!( ( x - 1 ) & 0x8 ) )
227 0 : ImplPutByte( 0 ); // WORD ALIGNMENT ???
228 : }
229 : }
230 0 : ImplPutByte( mnRepVal + 1 ); // end of RLE decoding
231 0 : }
232 :
233 :
234 :
235 0 : void RASWriter::ImplPutByte( sal_uInt8 nPutThis )
236 : {
237 0 : if ( mnRepCount == 0xffffffff )
238 : {
239 0 : mnRepCount = 0;
240 0 : mnRepVal = nPutThis;
241 : }
242 : else
243 : {
244 0 : if ( ( nPutThis == mnRepVal ) && ( mnRepCount != 0xff ) )
245 0 : mnRepCount++;
246 : else
247 : {
248 0 : if ( mnRepCount == 0 )
249 : {
250 0 : m_rOStm.WriteUChar( (sal_uInt8)mnRepVal );
251 0 : if ( mnRepVal == 0x80 )
252 0 : m_rOStm.WriteUChar( (sal_uInt8)0 );
253 : }
254 : else
255 : {
256 0 : m_rOStm.WriteUChar( (sal_uInt8)0x80 );
257 0 : m_rOStm.WriteUChar( (sal_uInt8)mnRepCount );
258 0 : m_rOStm.WriteUChar( (sal_uInt8)mnRepVal );
259 : }
260 0 : mnRepVal = nPutThis;
261 0 : mnRepCount = 0;
262 : }
263 : }
264 0 : }
265 :
266 :
267 :
268 :
269 : // - exported function -
270 :
271 :
272 : // this needs to be kept in sync with
273 : // ImpFilterLibCacheEntry::GetImportFunction() from
274 : // vcl/source/filter/graphicfilter.cxx
275 : #if defined(DISABLE_DYNLOADING)
276 : #define GraphicExport eraGraphicExport
277 : #endif
278 :
279 : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
280 0 : GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
281 : {
282 0 : RASWriter aRASWriter(rStream);
283 :
284 0 : return aRASWriter.WriteRAS( rGraphic, pFilterConfigItem );
285 : }
286 :
287 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|