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 //============================ PPMWriter ==================================
35 SvStream
& m_rOStm
; // Die auszugebende PPM-Datei
36 sal_uInt16 mpOStmOldModus
;
40 BitmapReadAccess
* mpAcc
;
41 sal_uLong mnWidth
, mnHeight
; // Bildausmass in Pixeln
43 sal_Bool
ImplWriteHeader();
45 void ImplWriteNumber( sal_Int32
);
47 com::sun::star::uno::Reference
< com::sun::star::task::XStatusIndicator
> xStatusIndicator
;
50 PPMWriter(SvStream
&rStrm
);
53 sal_Bool
WritePPM( const Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
);
56 //=================== Methods of PPMWriter ==============================
58 PPMWriter::PPMWriter(SvStream
&rStrm
)
60 , mbStatus ( sal_True
)
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() )
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();
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
PPMWriter::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. col.
132 m_rOStm
<< (sal_uInt8
)10;
135 mbStatus
= sal_False
;
140 // ------------------------------------------------------------------------
142 void PPMWriter::ImplWriteBody()
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();
159 for ( sal_uLong y
= 0; y
< mnHeight
; y
++ )
162 for ( sal_uLong x
= 0; x
< mnWidth
; x
++ )
164 sal_uInt8 i
, nDat
[3], nNumb
;
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;
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' );
188 nNumb
= nDat
[ i
] / 10;
191 m_rOStm
<< (sal_uInt8
)( nNumb
+ '0' );
192 nDat
[ i
] -= ( nNumb
* 10 );
193 m_rOStm
<< (sal_uInt8
)( nDat
[ i
] + '0' );
198 m_rOStm
<< (sal_uInt8
)( nDat
[ i
] + '0' );
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
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: */