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 :
25 : //============================ XPMWriter ==================================
26 :
27 : class XPMWriter {
28 :
29 : private:
30 :
31 : SvStream& m_rOStm; // the output XPM file
32 :
33 : bool mbStatus;
34 : bool mbTrans;
35 : BitmapReadAccess* mpAcc;
36 : sal_uLong mnWidth, mnHeight; // size in Pixel
37 : sal_uInt16 mnColors;
38 :
39 : com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
40 :
41 : void ImplCallback( sal_uInt16 nPercent );
42 : bool ImplWriteHeader();
43 : void ImplWritePalette();
44 : void ImplWriteColor( sal_uInt16 );
45 : void ImplWriteBody();
46 : void ImplWriteNumber( sal_Int32 );
47 : void ImplWritePixel( sal_uLong ) const;
48 :
49 : public:
50 : XPMWriter(SvStream& rOStm);
51 : ~XPMWriter();
52 :
53 : bool WriteXPM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
54 : };
55 :
56 : //=================== Methoden von XPMWriter ==============================
57 :
58 0 : XPMWriter::XPMWriter(SvStream& rOStm)
59 : : m_rOStm(rOStm)
60 : , mbStatus(true)
61 : , mbTrans(false)
62 : , mpAcc(NULL)
63 : , mnWidth(0)
64 : , mnHeight(0)
65 0 : , mnColors(0)
66 : {
67 0 : }
68 :
69 :
70 :
71 0 : XPMWriter::~XPMWriter()
72 : {
73 0 : }
74 :
75 :
76 :
77 0 : void XPMWriter::ImplCallback( sal_uInt16 nPercent )
78 : {
79 0 : if ( xStatusIndicator.is() )
80 : {
81 0 : if ( nPercent <= 100 )
82 0 : xStatusIndicator->setValue( nPercent );
83 : }
84 0 : }
85 :
86 :
87 :
88 0 : bool XPMWriter::WriteXPM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem)
89 : {
90 0 : Bitmap aBmp;
91 :
92 0 : if ( pFilterConfigItem )
93 : {
94 0 : xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
95 0 : if ( xStatusIndicator.is() )
96 : {
97 0 : OUString aMsg;
98 0 : xStatusIndicator->start( aMsg, 100 );
99 : }
100 : }
101 :
102 0 : BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
103 0 : aBmp = aBmpEx.GetBitmap();
104 :
105 0 : if ( rGraphic.IsTransparent() ) // possibly create transparent color
106 : {
107 0 : mbTrans = true;
108 0 : if ( aBmp.GetBitCount() >= 8 ) // if necessary convert image to 8 bit
109 0 : aBmp.Convert( BMP_CONVERSION_8BIT_TRANS );
110 : else
111 0 : aBmp.Convert( BMP_CONVERSION_4BIT_TRANS );
112 0 : aBmp.Replace( aBmpEx.GetMask(), BMP_COL_TRANS );
113 : }
114 : else
115 : {
116 0 : if ( aBmp.GetBitCount() > 8 ) // if necessary convert image to 8 bit
117 0 : aBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
118 : }
119 0 : mpAcc = aBmp.AcquireReadAccess();
120 0 : if ( mpAcc )
121 : {
122 0 : SvStreamEndian nOStmOldModus = m_rOStm.GetEndian();
123 0 : m_rOStm.SetEndian( SvStreamEndian::BIG );
124 :
125 0 : mnColors = mpAcc->GetPaletteEntryCount();
126 0 : if ( ImplWriteHeader() )
127 : {
128 0 : ImplWritePalette();
129 0 : ImplWriteBody();
130 0 : m_rOStm.WriteCharPtr( "\x22XPMENDEXT\x22\x0a};" );
131 : }
132 :
133 0 : m_rOStm.SetEndian(nOStmOldModus);
134 :
135 0 : Bitmap::ReleaseAccess( mpAcc );
136 : }
137 : else
138 0 : mbStatus = false;
139 :
140 :
141 0 : if ( xStatusIndicator.is() )
142 0 : xStatusIndicator->end();
143 :
144 0 : return mbStatus;
145 : }
146 :
147 :
148 :
149 0 : bool XPMWriter::ImplWriteHeader()
150 : {
151 0 : mnWidth = mpAcc->Width();
152 0 : mnHeight = mpAcc->Height();
153 0 : if ( mnWidth && mnHeight && mnColors )
154 : {
155 0 : m_rOStm.WriteCharPtr( "/* XPM */\x0astatic char * image[] = \x0a{\x0a\x22" );
156 0 : ImplWriteNumber( mnWidth );
157 0 : m_rOStm.WriteUChar( 32 );
158 0 : ImplWriteNumber( mnHeight );
159 0 : m_rOStm.WriteUChar( 32 );
160 0 : ImplWriteNumber( mnColors );
161 0 : m_rOStm.WriteUChar( 32 );
162 0 : ImplWriteNumber( ( mnColors > 26 ) ? 2 : 1 );
163 0 : m_rOStm.WriteCharPtr( "\x22,\x0a" );
164 : }
165 0 : else mbStatus = false;
166 0 : return mbStatus;
167 : }
168 :
169 :
170 :
171 0 : void XPMWriter::ImplWritePalette()
172 : {
173 0 : sal_uInt16 nTransIndex = 0xffff;
174 :
175 0 : if ( mbTrans )
176 0 : nTransIndex = mpAcc->GetBestPaletteIndex( BMP_COL_TRANS );
177 0 : for ( sal_uInt16 i = 0; i < mnColors; i++ )
178 : {
179 0 : m_rOStm.WriteCharPtr( "\x22" );
180 0 : ImplWritePixel( i );
181 0 : m_rOStm.WriteUChar( 32 );
182 0 : if ( nTransIndex != i )
183 : {
184 0 : ImplWriteColor( i );
185 0 : m_rOStm.WriteCharPtr( "\x22,\x0a" );
186 : }
187 : else
188 0 : m_rOStm.WriteCharPtr( "c none\x22,\x0a" );
189 : }
190 0 : }
191 :
192 :
193 :
194 0 : void XPMWriter::ImplWriteBody()
195 : {
196 0 : for ( sal_uLong y = 0; y < mnHeight; y++ )
197 : {
198 0 : ImplCallback( (sal_uInt16)( ( 100 * y ) / mnHeight ) ); // processing output in percent
199 0 : m_rOStm.WriteUChar( 0x22 );
200 0 : for ( sal_uLong x = 0; x < mnWidth; x++ )
201 : {
202 0 : ImplWritePixel( mpAcc->GetPixelIndex( y, x ) );
203 : }
204 0 : m_rOStm.WriteCharPtr( "\x22,\x0a" );
205 : }
206 0 : }
207 :
208 :
209 : // write a decimal number in ascii format into the stream
210 :
211 0 : void XPMWriter::ImplWriteNumber(sal_Int32 nNumber)
212 : {
213 0 : const OString aNum(OString::number(nNumber));
214 0 : m_rOStm.WriteCharPtr( aNum.getStr() );
215 0 : }
216 :
217 :
218 :
219 0 : void XPMWriter::ImplWritePixel( sal_uLong nCol ) const
220 : {
221 0 : if ( mnColors > 26 )
222 : {
223 0 : sal_uInt8 nDiff = (sal_uInt8) ( nCol / 26 );
224 0 : m_rOStm.WriteUChar( nDiff + 'A' );
225 0 : m_rOStm.WriteUChar( nCol - ( nDiff*26 ) + 'A' );
226 : }
227 : else
228 0 : m_rOStm.WriteUChar( nCol + 'A' );
229 0 : }
230 :
231 :
232 : // write a color value in hex format into the stream
233 0 : void XPMWriter::ImplWriteColor( sal_uInt16 nNumber )
234 : {
235 : sal_uLong nTmp;
236 : sal_uInt8 j;
237 :
238 0 : m_rOStm.WriteCharPtr( "c #" ); // # indicates a following hex value
239 0 : const BitmapColor& rColor = mpAcc->GetPaletteColor( nNumber );
240 0 : nTmp = ( rColor.GetRed() << 16 ) | ( rColor.GetGreen() << 8 ) | rColor.GetBlue();
241 0 : for ( signed char i = 20; i >= 0 ; i-=4 )
242 : {
243 0 : if ( ( j = (sal_uInt8)( nTmp >> i ) & 0xf ) > 9 )
244 0 : j += 'A' - 10;
245 : else
246 0 : j += '0';
247 0 : m_rOStm.WriteUChar( j );
248 : }
249 0 : }
250 :
251 :
252 :
253 : // this needs to be kept in sync with
254 : // ImpFilterLibCacheEntry::GetImportFunction() from
255 : // vcl/source/filter/graphicfilter.cxx
256 : #if defined(DISABLE_DYNLOADING)
257 : #define GraphicExport expGraphicExport
258 : #endif
259 :
260 : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
261 0 : GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
262 : {
263 0 : XPMWriter aXPMWriter(rStream);
264 :
265 0 : return aXPMWriter.WriteXPM( rGraphic, pFilterConfigItem );
266 0 : }
267 :
268 :
269 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|