1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 ==================================
35 SvStream
& m_rOStm
; // the output PGM file
36 sal_uInt16 mpOStmOldModus
;
40 BitmapReadAccess
* mpAcc
;
41 sal_uLong mnWidth
, mnHeight
; // image size in pixeln
43 sal_Bool
ImplWriteHeader();
45 void ImplWriteNumber( sal_Int32
);
47 com::sun::star::uno::Reference
< com::sun::star::task::XStatusIndicator
> xStatusIndicator
;
50 PGMWriter(SvStream
&rStream
);
53 sal_Bool
WritePGM( const Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
);
56 //=================== Methoden von PGMWriter ==============================
58 PGMWriter::PGMWriter(SvStream
&rStream
)
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() )
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();
97 if ( ImplWriteHeader() )
101 aBmp
.ReleaseAccess( mpAcc
);
104 mbStatus
= sal_False
;
106 m_rOStm
.SetNumberFormatInt( mpOStmOldModus
);
108 if ( xStatusIndicator
.is() )
109 xStatusIndicator
->end();
114 // ------------------------------------------------------------------------
116 sal_Bool
PGMWriter::ImplWriteHeader()
118 mnWidth
= mpAcc
->Width();
119 mnHeight
= mpAcc
->Height();
120 if ( mnWidth
&& mnHeight
)
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;
135 mbStatus
= sal_False
;
140 // ------------------------------------------------------------------------
142 void PGMWriter::ImplWriteBody()
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
);
156 for ( sal_uLong y
= 0; y
< mnHeight
; y
++ )
159 for ( sal_uLong x
= 0; x
< mnWidth
; x
++ )
161 sal_uInt8 nDat
, nNumb
;
165 m_rOStm
<< (sal_uInt8
)10;
167 nDat
= mpAcc
->GetPixelIndex( y
, x
);
171 m_rOStm
<< (sal_uInt8
)( nNumb
+ '0' );
172 nDat
-= ( nNumb
* 100 );
174 m_rOStm
<< (sal_uInt8
)( nNumb
+ '0' );
175 nDat
-= ( nNumb
* 10 );
176 m_rOStm
<< (sal_uInt8
)( nDat
+ '0' );
184 m_rOStm
<< (sal_uInt8
)( nNumb
+ '0' );
185 nDat
-= ( nNumb
* 10 );
186 m_rOStm
<< (sal_uInt8
)( nDat
+ '0' );
191 m_rOStm
<< (sal_uInt8
)( nDat
+ '0' );
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
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: */