bump product version to 4.1.6.2
[LibreOffice.git] / filter / source / graphicfilter / expm / expm.cxx
blobad0e704ebf50b68fdf3e89807f19e786b994b28d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 ==================================
27 class XPMWriter {
29 private:
31 SvStream& m_rOStm; // the output XPM file
33 sal_Bool mbStatus;
34 sal_Bool mbTrans;
35 BitmapReadAccess* mpAcc;
36 sal_uLong mnWidth, mnHeight; // size in Pixel
37 sal_uInt16 mnColors;
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 );
45 void ImplWriteBody();
46 void ImplWriteNumber( sal_Int32 );
47 void ImplWritePixel( sal_uLong ) const;
49 public:
50 XPMWriter(SvStream& rOStm);
51 ~XPMWriter();
53 sal_Bool WriteXPM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
56 //=================== Methoden von XPMWriter ==============================
58 XPMWriter::XPMWriter(SvStream& rOStm)
59 : m_rOStm(rOStm)
60 , mbStatus(sal_True)
61 , mbTrans(sal_False)
62 , mpAcc(NULL)
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)
87 Bitmap aBmp;
89 if ( pFilterConfigItem )
91 xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
92 if ( xStatusIndicator.is() )
94 OUString aMsg;
95 xStatusIndicator->start( aMsg, 100 );
99 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
100 aBmp = aBmpEx.GetBitmap();
102 if ( rGraphic.IsTransparent() ) // possibly create transparent color
104 mbTrans = sal_True;
105 if ( aBmp.GetBitCount() >= 8 ) // if necessary convert image to 8 bit
106 aBmp.Convert( BMP_CONVERSION_8BIT_TRANS );
107 else
108 aBmp.Convert( BMP_CONVERSION_4BIT_TRANS );
109 aBmp.Replace( aBmpEx.GetMask(), BMP_COL_TRANS );
111 else
113 if ( aBmp.GetBitCount() > 8 ) // if necessary convert image to 8 bit
114 aBmp.Convert( BMP_CONVERSION_8BIT_COLORS );
116 mpAcc = aBmp.AcquireReadAccess();
117 if ( mpAcc )
119 sal_uInt16 nOStmOldModus = m_rOStm.GetNumberFormatInt();
120 m_rOStm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
122 mnColors = mpAcc->GetPaletteEntryCount();
123 if ( ImplWriteHeader() )
125 ImplWritePalette();
126 ImplWriteBody();
127 m_rOStm << "\x22XPMENDEXT\x22\x0a};";
130 m_rOStm.SetNumberFormatInt(nOStmOldModus);
132 aBmp.ReleaseAccess( mpAcc );
134 else
135 mbStatus = sal_False;
138 if ( xStatusIndicator.is() )
139 xStatusIndicator->end();
141 return mbStatus;
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;
163 return mbStatus;
166 // ------------------------------------------------------------------------
168 void XPMWriter::ImplWritePalette()
170 sal_uInt16 nTransIndex = 0xffff;
172 if ( mbTrans )
173 nTransIndex = mpAcc->GetBestPaletteIndex( BMP_COL_TRANS );
174 for ( sal_uInt16 i = 0; i < mnColors; i++ )
176 m_rOStm << "\x22";
177 ImplWritePixel( i );
178 m_rOStm << (sal_uInt8)32;
179 if ( nTransIndex != i )
181 ImplWriteColor( i );
182 m_rOStm << "\x22,\x0a";
184 else
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
218 if ( mnColors > 26 )
220 sal_uInt8 nDiff = (sal_uInt8) ( nCol / 26 );
221 m_rOStm << (sal_uInt8)( nDiff + 'A' );
222 m_rOStm << (sal_uInt8)( nCol - ( nDiff*26 ) + 'A' );
224 else
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 )
232 sal_uLong nTmp;
233 sal_uInt8 j;
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 )
241 j += 'A' - 10;
242 else
243 j += '0';
244 m_rOStm << j;
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
255 #endif
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: */