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