1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: expm.cxx,v $
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 ==================================
44 SvStream
* mpOStm
; // Die auszugebende XPM-Datei
45 USHORT mpOStmOldModus
;
49 BitmapReadAccess
* mpAcc
;
50 ULONG mnWidth
, mnHeight
; // Bildausmass in Pixeln
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
);
60 void ImplWriteNumber( sal_Int32
);
61 void ImplWritePixel( ULONG
);
67 BOOL
WriteXPM( const Graphic
& rGraphic
, SvStream
& rXPM
, FilterConfigItem
* pFilterConfigItem
);
70 //=================== Methoden von XPMWriter ==============================
72 XPMWriter::XPMWriter() :
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
)
104 if ( pFilterConfigItem
)
106 xStatusIndicator
= pFilterConfigItem
->GetStatusIndicator();
107 if ( xStatusIndicator
.is() )
110 xStatusIndicator
->start( aMsg
, 100 );
114 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
115 aBmp
= aBmpEx
.GetBitmap();
117 if ( rGraphic
.IsTransparent() ) // event. transparente Farbe erzeugen
120 if ( aBmp
.GetBitCount() >= 8 ) // wenn noetig Bild auf 8 bit konvertieren
121 aBmp
.Convert( BMP_CONVERSION_8BIT_TRANS
);
123 aBmp
.Convert( BMP_CONVERSION_4BIT_TRANS
);
124 aBmp
.Replace( aBmpEx
.GetMask(), BMP_COL_TRANS
);
128 if ( aBmp
.GetBitCount() > 8 ) // wenn noetig Bild auf 8 bit konvertieren
129 aBmp
.Convert( BMP_CONVERSION_8BIT_COLORS
);
131 mpAcc
= aBmp
.AcquireReadAccess();
134 mnColors
= mpAcc
->GetPaletteEntryCount();
135 mpOStmOldModus
= mpOStm
->GetNumberFormatInt();
136 mpOStm
->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN
);
138 if ( ImplWriteHeader() )
142 *mpOStm
<< "\x22XPMENDEXT\x22\x0a};";
144 aBmp
.ReleaseAccess( mpAcc
);
149 mpOStm
->SetNumberFormatInt( mpOStmOldModus
);
151 if ( xStatusIndicator
.is() )
152 xStatusIndicator
->end();
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
);
168 ImplWriteNumber( mnHeight
);
170 ImplWriteNumber( mnColors
);
172 ImplWriteNumber( ( mnColors
> 26 ) ? 2 : 1 );
173 *mpOStm
<< "\x22,\x0a";
175 else mbStatus
= FALSE
;
179 // ------------------------------------------------------------------------
181 void XPMWriter::ImplWritePalette()
183 USHORT nTransIndex
= 0xffff;
186 nTransIndex
= mpAcc
->GetBestMatchingColor( BMP_COL_TRANS
);
187 for ( USHORT i
= 0; i
< mnColors
; i
++ )
192 if ( nTransIndex
!= i
)
195 *mpOStm
<< "\x22,\x0a";
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
)
236 BYTE nDiff
= (BYTE
) ( nCol
/ 26 );
237 *mpOStm
<< (BYTE
)( nDiff
+ 'A' );
238 *mpOStm
<< (BYTE
)( nCol
- ( nDiff
*26 ) + 'A' );
241 *mpOStm
<< (BYTE
)( nCol
+ 'A' );
244 // ------------------------------------------------------------------------
245 // ein Farbwert wird im Hexadezimalzahlformat in den Stream geschrieben
246 void XPMWriter::ImplWriteColor( USHORT nNumber
)
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 )
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
);
286 static HINSTANCE hDLLInst
= 0;
288 extern "C" int CALLBACK
LibMain( HINSTANCE hDLL
, WORD
, WORD nHeap
, LPSTR
)
298 // ------------------------------------------------------------------------
300 extern "C" int CALLBACK
WEP( int )