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