Update ooo320-m1
[ooovba.git] / goodies / source / filter.vcl / expm / expm.cxx
blob4e149e67b3c88e24e60240056244e0c8fcc41019
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: expm.cxx,v $
10 * $Revision: 1.10 $
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>
38 //============================ XPMWriter ==================================
40 class XPMWriter {
42 private:
44 SvStream* mpOStm; // Die auszugebende XPM-Datei
45 USHORT mpOStmOldModus;
47 BOOL mbStatus;
48 BOOL mbTrans;
49 BitmapReadAccess* mpAcc;
50 ULONG mnWidth, mnHeight; // Bildausmass in Pixeln
51 USHORT mnColors;
53 com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
55 void ImplCallback( USHORT nPercent );
56 BOOL ImplWriteHeader();
57 void ImplWritePalette();
58 void ImplWriteColor( USHORT );
59 void ImplWriteBody();
60 void ImplWriteNumber( sal_Int32 );
61 void ImplWritePixel( ULONG );
63 public:
64 XPMWriter();
65 ~XPMWriter();
67 BOOL WriteXPM( const Graphic& rGraphic, SvStream& rXPM, FilterConfigItem* pFilterConfigItem );
70 //=================== Methoden von XPMWriter ==============================
72 XPMWriter::XPMWriter() :
73 mbStatus ( TRUE ),
74 mbTrans ( FALSE ),
75 mpAcc ( NULL )
79 // ------------------------------------------------------------------------
81 XPMWriter::~XPMWriter()
85 // ------------------------------------------------------------------------
87 void XPMWriter::ImplCallback( USHORT nPercent )
89 if ( xStatusIndicator.is() )
91 if ( nPercent <= 100 )
92 xStatusIndicator->setValue( nPercent );
96 // ------------------------------------------------------------------------
98 BOOL XPMWriter::WriteXPM( const Graphic& rGraphic, SvStream& rXPM, FilterConfigItem* pFilterConfigItem)
100 Bitmap aBmp;
102 mpOStm = &rXPM;
104 if ( pFilterConfigItem )
106 xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
107 if ( xStatusIndicator.is() )
109 rtl::OUString aMsg;
110 xStatusIndicator->start( aMsg, 100 );
114 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
115 aBmp = aBmpEx.GetBitmap();
117 if ( rGraphic.IsTransparent() ) // event. transparente Farbe erzeugen
119 mbTrans = TRUE;
120 if ( aBmp.GetBitCount() >= 8 ) // wenn noetig Bild auf 8 bit konvertieren
121 aBmp.Convert( BMP_CONVERSION_8BIT_TRANS );
122 else
123 aBmp.Convert( BMP_CONVERSION_4BIT_TRANS );
124 aBmp.Replace( aBmpEx.GetMask(), BMP_COL_TRANS );
126 else
128 if ( aBmp.GetBitCount() > 8 ) // wenn noetig Bild auf 8 bit konvertieren
129 aBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
131 mpAcc = aBmp.AcquireReadAccess();
132 if ( mpAcc )
134 mnColors = mpAcc->GetPaletteEntryCount();
135 mpOStmOldModus = mpOStm->GetNumberFormatInt();
136 mpOStm->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
138 if ( ImplWriteHeader() )
140 ImplWritePalette();
141 ImplWriteBody();
142 *mpOStm << "\x22XPMENDEXT\x22\x0a};";
144 aBmp.ReleaseAccess( mpAcc );
146 else
147 mbStatus = FALSE;
149 mpOStm->SetNumberFormatInt( mpOStmOldModus );
151 if ( xStatusIndicator.is() )
152 xStatusIndicator->end();
154 return mbStatus;
157 // ------------------------------------------------------------------------
159 BOOL XPMWriter::ImplWriteHeader()
161 mnWidth = mpAcc->Width();
162 mnHeight = mpAcc->Height();
163 if ( mnWidth && mnHeight && mnColors )
165 *mpOStm << "/* XPM */\x0astatic char * image[] = \x0a{\x0a\x22";
166 ImplWriteNumber( mnWidth );
167 *mpOStm << (BYTE)32;
168 ImplWriteNumber( mnHeight );
169 *mpOStm << (BYTE)32;
170 ImplWriteNumber( mnColors );
171 *mpOStm << (BYTE)32;
172 ImplWriteNumber( ( mnColors > 26 ) ? 2 : 1 );
173 *mpOStm << "\x22,\x0a";
175 else mbStatus = FALSE;
176 return mbStatus;
179 // ------------------------------------------------------------------------
181 void XPMWriter::ImplWritePalette()
183 USHORT nTransIndex = 0xffff;
185 if ( mbTrans )
186 nTransIndex = mpAcc->GetBestMatchingColor( BMP_COL_TRANS );
187 for ( USHORT i = 0; i < mnColors; i++ )
189 *mpOStm << "\x22";
190 ImplWritePixel( i );
191 *mpOStm << (BYTE)32;
192 if ( nTransIndex != i )
194 ImplWriteColor( i );
195 *mpOStm << "\x22,\x0a";
197 else
198 *mpOStm << "c none\x22,\x0a";
202 // ------------------------------------------------------------------------
204 void XPMWriter::ImplWriteBody()
206 for ( ULONG y = 0; y < mnHeight; y++ )
208 ImplCallback( (USHORT)( ( 100 * y ) / mnHeight ) ); // processing output in percent
209 *mpOStm << (BYTE)0x22;
210 for ( ULONG x = 0; x < mnWidth; x++ )
212 ImplWritePixel( (BYTE)(mpAcc->GetPixel( y, x ) ) );
214 *mpOStm << "\x22,\x0a";
218 // ------------------------------------------------------------------------
219 // eine Dezimalzahl im ASCII format wird in den Stream geschrieben
221 void XPMWriter::ImplWriteNumber( sal_Int32 nNumber )
223 const ByteString aNum( ByteString::CreateFromInt32( nNumber ) );
225 for( sal_Int16 n = 0UL, nLen = aNum.Len(); n < nLen; n++ )
226 *mpOStm << aNum.GetChar( n );
230 // ------------------------------------------------------------------------
232 void XPMWriter::ImplWritePixel( ULONG nCol )
234 if ( mnColors > 26 )
236 BYTE nDiff = (BYTE) ( nCol / 26 );
237 *mpOStm << (BYTE)( nDiff + 'A' );
238 *mpOStm << (BYTE)( nCol - ( nDiff*26 ) + 'A' );
240 else
241 *mpOStm << (BYTE)( nCol + 'A' );
244 // ------------------------------------------------------------------------
245 // ein Farbwert wird im Hexadezimalzahlformat in den Stream geschrieben
246 void XPMWriter::ImplWriteColor( USHORT nNumber )
248 ULONG nTmp;
249 BYTE j;
251 *mpOStm << "c #"; // # zeigt einen folgenden Hexwert an
252 const BitmapColor& rColor = mpAcc->GetPaletteColor( nNumber );
253 nTmp = ( rColor.GetRed() << 16 ) | ( rColor.GetGreen() << 8 ) | rColor.GetBlue();
254 for ( signed char i = 20; i >= 0 ; i-=4 )
256 if ( ( j = (BYTE)( nTmp >> i ) & 0xf ) > 9 )
257 j += 'A' - 10;
258 else
259 j += '0';
260 *mpOStm << j;
264 // ------------------------------------------------------------------------
266 // ---------------------
267 // - exported function -
268 // ---------------------
270 extern "C" BOOL __LOADONCALLAPI GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, BOOL )
272 XPMWriter aXPMWriter;
274 return aXPMWriter.WriteXPM( rGraphic, rStream, pFilterConfigItem );
277 #ifndef GCC
278 #endif
280 // ---------------
281 // - Win16 trash -
282 // ---------------
284 #ifdef WIN
286 static HINSTANCE hDLLInst = 0;
288 extern "C" int CALLBACK LibMain( HINSTANCE hDLL, WORD, WORD nHeap, LPSTR )
290 if ( nHeap )
291 UnlockData( 0 );
293 hDLLInst = hDLL;
295 return TRUE;
298 // ------------------------------------------------------------------------
300 extern "C" int CALLBACK WEP( int )
302 return 1;
305 #endif