bump product version to 4.1.6.2
[LibreOffice.git] / filter / source / graphicfilter / epgm / epgm.cxx
blob025e5157ff823a02f4d88902822483ec4cb92404
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 //============================ PGMWriter ==================================
31 class PGMWriter {
33 private:
35 SvStream& m_rOStm; // the output PGM file
36 sal_uInt16 mpOStmOldModus;
38 sal_Bool mbStatus;
39 sal_uInt32 mnMode;
40 BitmapReadAccess* mpAcc;
41 sal_uLong mnWidth, mnHeight; // image size 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 PGMWriter(SvStream &rStream);
51 ~PGMWriter();
53 sal_Bool WritePGM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
56 //=================== Methoden von PGMWriter ==============================
58 PGMWriter::PGMWriter(SvStream &rStream)
59 : m_rOStm(rStream)
60 , mbStatus(sal_True)
61 , mpAcc(NULL)
65 // ------------------------------------------------------------------------
67 PGMWriter::~PGMWriter()
71 // ------------------------------------------------------------------------
73 sal_Bool PGMWriter::WritePGM( 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_8BIT_GREYS );
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 PGMWriter::ImplWriteHeader()
118 mnWidth = mpAcc->Width();
119 mnHeight = mpAcc->Height();
120 if ( mnWidth && mnHeight )
122 if ( mnMode == 0 )
123 m_rOStm << "P5\x0a";
124 else
125 m_rOStm << "P2\x0a";
127 ImplWriteNumber( mnWidth );
128 m_rOStm << (sal_uInt8)32;
129 ImplWriteNumber( mnHeight );
130 m_rOStm << (sal_uInt8)32;
131 ImplWriteNumber( 255 ); // max. gray value
132 m_rOStm << (sal_uInt8)10;
134 else
135 mbStatus = sal_False;
137 return mbStatus;
140 // ------------------------------------------------------------------------
142 void PGMWriter::ImplWriteBody()
144 if ( mnMode == 0 )
146 for ( sal_uLong y = 0; y < mnHeight; y++ )
148 for ( sal_uLong x = 0; x < mnWidth; x++ )
150 m_rOStm << mpAcc->GetPixelIndex( y, x );
154 else
156 for ( sal_uLong y = 0; y < mnHeight; y++ )
158 int nCount = 70;
159 for ( sal_uLong x = 0; x < mnWidth; x++ )
161 sal_uInt8 nDat, nNumb;
162 if ( nCount < 0 )
164 nCount = 69;
165 m_rOStm << (sal_uInt8)10;
167 nDat = mpAcc->GetPixelIndex( y, x );
168 nNumb = nDat / 100;
169 if ( nNumb )
171 m_rOStm << (sal_uInt8)( nNumb + '0' );
172 nDat -= ( nNumb * 100 );
173 nNumb = nDat / 10;
174 m_rOStm << (sal_uInt8)( nNumb + '0' );
175 nDat -= ( nNumb * 10 );
176 m_rOStm << (sal_uInt8)( nDat + '0' );
177 nCount -= 4;
179 else
181 nNumb = nDat / 10;
182 if ( nNumb )
184 m_rOStm << (sal_uInt8)( nNumb + '0' );
185 nDat -= ( nNumb * 10 );
186 m_rOStm << (sal_uInt8)( nDat + '0' );
187 nCount -= 3;
189 else
191 m_rOStm << (sal_uInt8)( nDat + '0' );
192 nCount -= 2;
195 m_rOStm << (sal_uInt8)' ';
197 m_rOStm << (sal_uInt8)10;
202 // ------------------------------------------------------------------------
203 // write a decimal number in ascii format into the stream
204 void PGMWriter::ImplWriteNumber(sal_Int32 nNumber)
206 const OString aNum(OString::valueOf(nNumber));
207 m_rOStm << aNum.getStr();
210 // ------------------------------------------------------------------------
212 // ---------------------
213 // - exported function -
214 // ---------------------
216 // this needs to be kept in sync with
217 // ImpFilterLibCacheEntry::GetImportFunction() from
218 // vcl/source/filter/graphicfilter.cxx
219 #if defined(DISABLE_DYNLOADING)
220 #define GraphicExport epgGraphicExport
221 #endif
223 extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
224 GraphicExport(SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, sal_Bool)
226 PGMWriter aPGMWriter(rStream);
228 return aPGMWriter.WritePGM( rGraphic, pFilterConfigItem );
231 // ------------------------------------------------------------------------
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */