merge the formfield patch from ooo-build
[ooovba.git] / goodies / source / filter.vcl / eras / eras.cxx
blob384283b16378c6d585929f6f113d537eeeafd34d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: eras.cxx,v $
10 * $Revision: 1.11.38.1 $
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_goodies.hxx"
34 #include <vcl/graph.hxx>
35 #include <vcl/bmpacc.hxx>
36 #include <svtools/fltcall.hxx>
37 #include <svtools/FilterConfigItem.hxx>
39 //============================ RASWriter ==================================
41 class RASWriter {
43 private:
45 SvStream* mpOStm;
46 USHORT mpOStmOldModus;
48 BOOL mbStatus;
49 BitmapReadAccess* mpAcc;
51 ULONG mnWidth, mnHeight;
52 USHORT mnColors, mnDepth;
54 ULONG mnRepCount;
55 BYTE mnRepVal;
57 com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
59 void ImplCallback( ULONG nCurrentYPos );
60 BOOL ImplWriteHeader();
61 void ImplWritePalette();
62 void ImplWriteBody();
63 void ImplPutByte( BYTE ); // RLE decoding
65 public:
66 RASWriter();
67 ~RASWriter();
69 BOOL WriteRAS( const Graphic& rGraphic, SvStream& rRAS, FilterConfigItem* pFilterConfigItem );
72 //=================== Methoden von RASWriter ==============================
74 RASWriter::RASWriter() :
75 mbStatus ( TRUE ),
76 mpAcc ( NULL ),
77 mnRepCount ( 0xffffffff )
81 // ------------------------------------------------------------------------
83 RASWriter::~RASWriter()
87 // ------------------------------------------------------------------------
89 void RASWriter::ImplCallback( ULONG nYPos )
91 if ( xStatusIndicator.is() )
92 xStatusIndicator->setValue( (USHORT)( ( 100 * nYPos ) / mnHeight ) );
95 // ------------------------------------------------------------------------
97 BOOL RASWriter::WriteRAS( const Graphic& rGraphic, SvStream& rRAS, FilterConfigItem* pFilterConfigItem)
99 Bitmap aBmp;
101 mpOStm = &rRAS;
103 if ( pFilterConfigItem )
105 xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
106 if ( xStatusIndicator.is() )
108 rtl::OUString aMsg;
109 xStatusIndicator->start( aMsg, 100 );
113 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
114 aBmp = aBmpEx.GetBitmap();
116 if ( aBmp.GetBitCount() == 4 )
117 aBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
119 mnDepth = aBmp.GetBitCount();
121 // export code below only handles three discrete cases
122 mnDepth = mnDepth <= 1 ? 1 : mnDepth <= 8 ? 8 : 24;
124 mpAcc = aBmp.AcquireReadAccess();
125 if ( mpAcc )
127 mpOStmOldModus = mpOStm->GetNumberFormatInt();
128 mpOStm->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
130 if ( ImplWriteHeader() )
132 if ( mnDepth <= 8 )
133 ImplWritePalette();
134 ImplWriteBody();
136 aBmp.ReleaseAccess( mpAcc );
138 else
139 mbStatus = FALSE;
141 mpOStm->SetNumberFormatInt( mpOStmOldModus );
143 if ( xStatusIndicator.is() )
144 xStatusIndicator->end();
146 return mbStatus;
149 // ------------------------------------------------------------------------
151 BOOL RASWriter::ImplWriteHeader()
153 mnWidth = mpAcc->Width();
154 mnHeight = mpAcc->Height();
155 if ( mnDepth <= 8 )
157 mnColors = mpAcc->GetPaletteEntryCount();
158 if (mnColors == 0)
159 mbStatus = FALSE;
161 if ( mbStatus && mnWidth && mnHeight && mnDepth )
163 *mpOStm << (UINT32)0x59a66a95 << (UINT32)mnWidth << (UINT32)mnHeight
164 << (UINT32)mnDepth
165 << (UINT32)(( ( ( ( mnWidth * mnDepth ) + 15 ) >> 4 ) << 1 ) * mnHeight)
166 << (UINT32)2;
168 if ( mnDepth > 8 )
169 *mpOStm << (UINT32)0 << (UINT32)0;
170 else
173 *mpOStm << (UINT32)1 << (UINT32)( mnColors * 3 );
176 else mbStatus = FALSE;
178 return mbStatus;
181 // ------------------------------------------------------------------------
183 void RASWriter::ImplWritePalette()
185 USHORT i;
187 for ( i = 0; i < mnColors; *mpOStm << mpAcc->GetPaletteColor( i++ ).GetRed() ) ;
188 for ( i = 0; i < mnColors; *mpOStm << mpAcc->GetPaletteColor( i++ ).GetGreen() ) ;
189 for ( i = 0; i < mnColors; *mpOStm << mpAcc->GetPaletteColor( i++ ).GetBlue() ) ;
192 // ------------------------------------------------------------------------
194 void RASWriter::ImplWriteBody()
196 ULONG x, y;
198 if ( mnDepth == 24 )
200 for ( y = 0; y < mnHeight; y++ )
202 ImplCallback( y ); // processing output
203 for ( x = 0; x < mnWidth; x++ )
205 BitmapColor aColor( mpAcc->GetPixel( y, x ) );
206 ImplPutByte( aColor.GetBlue() ); // Format ist BGR
207 ImplPutByte( aColor.GetGreen() );
208 ImplPutByte( aColor.GetRed() );
210 if ( x & 1 ) ImplPutByte( 0 ); // WORD ALIGNMENT ???
213 else if ( mnDepth == 8 )
215 for ( y = 0; y < mnHeight; y++ )
217 ImplCallback( y ); // processing output
218 for ( x = 0; x < mnWidth; x++ )
220 ImplPutByte ( mpAcc->GetPixel( y, x ) );
222 if ( x & 1 ) ImplPutByte( 0 ); // WORD ALIGNMENT ???
225 else if ( mnDepth == 1 )
227 BYTE nDat = 0;
229 for ( y = 0; y < mnHeight; y++ )
231 ImplCallback( y ); // processing output
232 for ( x = 0; x < mnWidth; x++ )
234 nDat = ( ( nDat << 1 ) | ( mpAcc->GetPixel ( y, x ) & 1 ) );
235 if ( ( x & 7 ) == 7 )
236 ImplPutByte( nDat );
238 if ( x & 7 )
239 ImplPutByte( sal::static_int_cast< BYTE >(nDat << ( ( ( x & 7 ) ^ 7 ) + 1)) );// write remaining bits
240 if (!( ( x - 1 ) & 0x8 ) )
241 ImplPutByte( 0 ); // WORD ALIGNMENT ???
244 ImplPutByte( mnRepVal + 1 ); // end of RLE decoding
247 // ------------------------------------------------------------------------
249 void RASWriter::ImplPutByte( BYTE nPutThis )
251 if ( mnRepCount == 0xffffffff )
253 mnRepCount = 0;
254 mnRepVal = nPutThis;
256 else
258 if ( ( nPutThis == mnRepVal ) && ( mnRepCount != 0xff ) )
259 mnRepCount++;
260 else
262 if ( mnRepCount == 0 )
264 *mpOStm << (BYTE)mnRepVal;
265 if ( mnRepVal == 0x80 )
266 *mpOStm << (BYTE)0;
268 else
270 *mpOStm << (BYTE)0x80;
271 *mpOStm << (BYTE)mnRepCount;
272 *mpOStm << (BYTE)mnRepVal;
274 mnRepVal = nPutThis;
275 mnRepCount = 0;
280 // ------------------------------------------------------------------------
282 // ---------------------
283 // - exported function -
284 // ---------------------
286 extern "C" BOOL __LOADONCALLAPI GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, BOOL )
288 RASWriter aRASWriter;
290 return aRASWriter.WriteRAS( rGraphic, rStream, pFilterConfigItem );
292 #ifndef GCC
293 #endif
295 // ---------------
296 // - Win16 trash -
297 // ---------------
299 #ifdef WIN
301 static HINSTANCE hDLLInst = 0;
303 extern "C" int CALLBACK LibMain( HINSTANCE hDLL, WORD, WORD nHeap, LPSTR )
305 if ( nHeap )
306 UnlockData( 0 );
308 hDLLInst = hDLL;
310 return TRUE;
313 // ------------------------------------------------------------------------
315 extern "C" int CALLBACK WEP( int )
317 return 1;
320 #endif