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