1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <vcl/graph.hxx>
22 #include <vcl/bmpacc.hxx>
23 #include <vcl/fltcall.hxx>
25 //============================ XPMWriter ==================================
31 SvStream
& m_rOStm
; // the output XPM file
35 BitmapReadAccess
* mpAcc
;
36 sal_uLong mnWidth
, mnHeight
; // size in Pixel
39 com::sun::star::uno::Reference
< com::sun::star::task::XStatusIndicator
> xStatusIndicator
;
41 void ImplCallback( sal_uInt16 nPercent
);
42 sal_Bool
ImplWriteHeader();
43 void ImplWritePalette();
44 void ImplWriteColor( sal_uInt16
);
46 void ImplWriteNumber( sal_Int32
);
47 void ImplWritePixel( sal_uLong
) const;
50 XPMWriter(SvStream
& rOStm
);
53 sal_Bool
WriteXPM( const Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
);
56 //=================== Methoden von XPMWriter ==============================
58 XPMWriter::XPMWriter(SvStream
& rOStm
)
66 // ------------------------------------------------------------------------
68 XPMWriter::~XPMWriter()
72 // ------------------------------------------------------------------------
74 void XPMWriter::ImplCallback( sal_uInt16 nPercent
)
76 if ( xStatusIndicator
.is() )
78 if ( nPercent
<= 100 )
79 xStatusIndicator
->setValue( nPercent
);
83 // ------------------------------------------------------------------------
85 sal_Bool
XPMWriter::WriteXPM( const Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
)
89 if ( pFilterConfigItem
)
91 xStatusIndicator
= pFilterConfigItem
->GetStatusIndicator();
92 if ( xStatusIndicator
.is() )
95 xStatusIndicator
->start( aMsg
, 100 );
99 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
100 aBmp
= aBmpEx
.GetBitmap();
102 if ( rGraphic
.IsTransparent() ) // possibly create transparent color
105 if ( aBmp
.GetBitCount() >= 8 ) // if necessary convert image to 8 bit
106 aBmp
.Convert( BMP_CONVERSION_8BIT_TRANS
);
108 aBmp
.Convert( BMP_CONVERSION_4BIT_TRANS
);
109 aBmp
.Replace( aBmpEx
.GetMask(), BMP_COL_TRANS
);
113 if ( aBmp
.GetBitCount() > 8 ) // if necessary convert image to 8 bit
114 aBmp
.Convert( BMP_CONVERSION_8BIT_COLORS
);
116 mpAcc
= aBmp
.AcquireReadAccess();
119 sal_uInt16 nOStmOldModus
= m_rOStm
.GetNumberFormatInt();
120 m_rOStm
.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN
);
122 mnColors
= mpAcc
->GetPaletteEntryCount();
123 if ( ImplWriteHeader() )
127 m_rOStm
<< "\x22XPMENDEXT\x22\x0a};";
130 m_rOStm
.SetNumberFormatInt(nOStmOldModus
);
132 aBmp
.ReleaseAccess( mpAcc
);
135 mbStatus
= sal_False
;
138 if ( xStatusIndicator
.is() )
139 xStatusIndicator
->end();
144 // ------------------------------------------------------------------------
146 sal_Bool
XPMWriter::ImplWriteHeader()
148 mnWidth
= mpAcc
->Width();
149 mnHeight
= mpAcc
->Height();
150 if ( mnWidth
&& mnHeight
&& mnColors
)
152 m_rOStm
<< "/* XPM */\x0astatic char * image[] = \x0a{\x0a\x22";
153 ImplWriteNumber( mnWidth
);
154 m_rOStm
<< (sal_uInt8
)32;
155 ImplWriteNumber( mnHeight
);
156 m_rOStm
<< (sal_uInt8
)32;
157 ImplWriteNumber( mnColors
);
158 m_rOStm
<< (sal_uInt8
)32;
159 ImplWriteNumber( ( mnColors
> 26 ) ? 2 : 1 );
160 m_rOStm
<< "\x22,\x0a";
162 else mbStatus
= sal_False
;
166 // ------------------------------------------------------------------------
168 void XPMWriter::ImplWritePalette()
170 sal_uInt16 nTransIndex
= 0xffff;
173 nTransIndex
= mpAcc
->GetBestPaletteIndex( BMP_COL_TRANS
);
174 for ( sal_uInt16 i
= 0; i
< mnColors
; i
++ )
178 m_rOStm
<< (sal_uInt8
)32;
179 if ( nTransIndex
!= i
)
182 m_rOStm
<< "\x22,\x0a";
185 m_rOStm
<< "c none\x22,\x0a";
189 // ------------------------------------------------------------------------
191 void XPMWriter::ImplWriteBody()
193 for ( sal_uLong y
= 0; y
< mnHeight
; y
++ )
195 ImplCallback( (sal_uInt16
)( ( 100 * y
) / mnHeight
) ); // processing output in percent
196 m_rOStm
<< (sal_uInt8
)0x22;
197 for ( sal_uLong x
= 0; x
< mnWidth
; x
++ )
199 ImplWritePixel( mpAcc
->GetPixelIndex( y
, x
) );
201 m_rOStm
<< "\x22,\x0a";
205 // ------------------------------------------------------------------------
206 // write a decimal number in ascii format into the stream
208 void XPMWriter::ImplWriteNumber(sal_Int32 nNumber
)
210 const OString
aNum(OString::valueOf(nNumber
));
211 m_rOStm
<< aNum
.getStr();
214 // ------------------------------------------------------------------------
216 void XPMWriter::ImplWritePixel( sal_uLong nCol
) const
220 sal_uInt8 nDiff
= (sal_uInt8
) ( nCol
/ 26 );
221 m_rOStm
<< (sal_uInt8
)( nDiff
+ 'A' );
222 m_rOStm
<< (sal_uInt8
)( nCol
- ( nDiff
*26 ) + 'A' );
225 m_rOStm
<< (sal_uInt8
)( nCol
+ 'A' );
228 // ------------------------------------------------------------------------
229 // write a color value in hex format into the stream
230 void XPMWriter::ImplWriteColor( sal_uInt16 nNumber
)
235 m_rOStm
<< "c #"; // # indicates a following hex value
236 const BitmapColor
& rColor
= mpAcc
->GetPaletteColor( nNumber
);
237 nTmp
= ( rColor
.GetRed() << 16 ) | ( rColor
.GetGreen() << 8 ) | rColor
.GetBlue();
238 for ( signed char i
= 20; i
>= 0 ; i
-=4 )
240 if ( ( j
= (sal_uInt8
)( nTmp
>> i
) & 0xf ) > 9 )
248 // ------------------------------------------------------------------------
250 // this needs to be kept in sync with
251 // ImpFilterLibCacheEntry::GetImportFunction() from
252 // vcl/source/filter/graphicfilter.cxx
253 #if defined(DISABLE_DYNLOADING)
254 #define GraphicExport expGraphicExport
257 extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
258 GraphicExport(SvStream
& rStream
, Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
, sal_Bool
)
260 XPMWriter
aXPMWriter(rStream
);
262 return aXPMWriter
.WriteXPM( rGraphic
, pFilterConfigItem
);
266 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */