bump product version to 4.1.6.2
[LibreOffice.git] / filter / source / graphicfilter / eppm / eppm.cxx
blob84388f76070cb2eb12411e0312c07d9951aa58fa
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/svapp.hxx>
22 #include <vcl/graph.hxx>
23 #include <vcl/bmpacc.hxx>
24 #include <vcl/msgbox.hxx>
25 #include <svl/solar.hrc>
26 #include <vcl/fltcall.hxx>
27 #include <vcl/FilterConfigItem.hxx>
29 //============================ PPMWriter ==================================
31 class PPMWriter {
33 private:
35 SvStream& m_rOStm; // Die auszugebende PPM-Datei
36 sal_uInt16 mpOStmOldModus;
38 sal_Bool mbStatus;
39 sal_Int32 mnMode;
40 BitmapReadAccess* mpAcc;
41 sal_uLong mnWidth, mnHeight; // Bildausmass in Pixeln
43 sal_Bool ImplWriteHeader();
44 void ImplWriteBody();
45 void ImplWriteNumber( sal_Int32 );
47 com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
49 public:
50 PPMWriter(SvStream &rStrm);
51 ~PPMWriter();
53 sal_Bool WritePPM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
56 //=================== Methods of PPMWriter ==============================
58 PPMWriter::PPMWriter(SvStream &rStrm)
59 : m_rOStm(rStrm)
60 , mbStatus ( sal_True )
61 , mpAcc ( NULL )
65 // ------------------------------------------------------------------------
67 PPMWriter::~PPMWriter()
71 // ------------------------------------------------------------------------
73 sal_Bool PPMWriter::WritePPM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem )
75 if ( pFilterConfigItem )
77 mnMode = pFilterConfigItem->ReadInt32( "FileFormat", 0 );
79 xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
80 if ( xStatusIndicator.is() )
82 OUString aMsg;
83 xStatusIndicator->start( aMsg, 100 );
87 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
88 Bitmap aBmp = aBmpEx.GetBitmap();
89 aBmp.Convert( BMP_CONVERSION_24BIT );
91 mpOStmOldModus = m_rOStm.GetNumberFormatInt();
92 m_rOStm.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
94 mpAcc = aBmp.AcquireReadAccess();
95 if( mpAcc )
97 if ( ImplWriteHeader() )
99 ImplWriteBody();
101 aBmp.ReleaseAccess( mpAcc );
103 else
104 mbStatus = sal_False;
106 m_rOStm.SetNumberFormatInt( mpOStmOldModus );
108 if ( xStatusIndicator.is() )
109 xStatusIndicator->end();
111 return mbStatus;
114 // ------------------------------------------------------------------------
116 sal_Bool PPMWriter::ImplWriteHeader()
118 mnWidth = mpAcc->Width();
119 mnHeight = mpAcc->Height();
120 if ( mnWidth && mnHeight )
122 if ( mnMode == 0 )
123 m_rOStm << "P6\x0a";
124 else
125 m_rOStm << "P3\x0a";
127 ImplWriteNumber( mnWidth );
128 m_rOStm << (sal_uInt8)32;
129 ImplWriteNumber( mnHeight );
130 m_rOStm << (sal_uInt8)32;
131 ImplWriteNumber( 255 ); // max. col.
132 m_rOStm << (sal_uInt8)10;
134 else
135 mbStatus = sal_False;
137 return mbStatus;
140 // ------------------------------------------------------------------------
142 void PPMWriter::ImplWriteBody()
144 if ( mnMode == 0 )
146 for ( sal_uLong y = 0; y < mnHeight; y++ )
148 for ( sal_uLong x = 0; x < mnWidth; x++ )
150 const BitmapColor& rColor = mpAcc->GetPixel( y, x );
151 m_rOStm << rColor.GetRed();
152 m_rOStm << rColor.GetGreen();
153 m_rOStm << rColor.GetBlue();
157 else
159 for ( sal_uLong y = 0; y < mnHeight; y++ )
161 int nCount = 70;
162 for ( sal_uLong x = 0; x < mnWidth; x++ )
164 sal_uInt8 i, nDat[3], nNumb;
165 if ( nCount < 0 )
167 nCount = 69;
168 m_rOStm << (sal_uInt8)10;
170 nDat[0] = mpAcc->GetPixel( y, x ).GetRed();
171 nDat[1] = mpAcc->GetPixel( y, x ).GetGreen();
172 nDat[2] = mpAcc->GetPixel( y, x ).GetBlue();
173 for ( i = 0; i < 3; i++ )
175 nNumb = nDat[ i ] / 100;
176 if ( nNumb )
178 m_rOStm << (sal_uInt8)( nNumb + '0' );
179 nDat[ i ] -= ( nNumb * 100 );
180 nNumb = nDat[ i ] / 10;
181 m_rOStm << (sal_uInt8)( nNumb + '0' );
182 nDat[ i ] -= ( nNumb * 10 );
183 m_rOStm << (sal_uInt8)( nDat[ i ] + '0' );
184 nCount -= 4;
186 else
188 nNumb = nDat[ i ] / 10;
189 if ( nNumb )
191 m_rOStm << (sal_uInt8)( nNumb + '0' );
192 nDat[ i ] -= ( nNumb * 10 );
193 m_rOStm << (sal_uInt8)( nDat[ i ] + '0' );
194 nCount -= 3;
196 else
198 m_rOStm << (sal_uInt8)( nDat[ i ] + '0' );
199 nCount -= 2;
202 m_rOStm << (sal_uInt8)' ';
205 m_rOStm << (sal_uInt8)10;
210 // ------------------------------------------------------------------------
211 // a decimal number in ASCII format is being written into the stream
213 void PPMWriter::ImplWriteNumber(sal_Int32 nNumber)
215 const OString aNum(OString::valueOf(nNumber));
216 m_rOStm << aNum.getStr();
219 // ------------------------------------------------------------------------
221 // ---------------------
222 // - exported function -
223 // ---------------------
225 // this needs to be kept in sync with
226 // ImpFilterLibCacheEntry::GetImportFunction() from
227 // vcl/source/filter/graphicfilter.cxx
228 #if defined(DISABLE_DYNLOADING)
229 #define GraphicExport eppGraphicExport
230 #endif
232 extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
233 GraphicExport(SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, sal_Bool)
235 PPMWriter aPPMWriter(rStream);
236 return aPPMWriter.WritePPM( rGraphic, pFilterConfigItem );
239 // ------------------------------------------------------------------------
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */