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 //============================ PBMWriter ==================================
35 SvStream
& m_rOStm
; // the output PBM file
38 sal_Int32 mnMode
; // 0 -> raw, 1-> ascii
39 BitmapReadAccess
* mpAcc
;
40 sal_uLong mnWidth
, mnHeight
; // size in pixel
42 bool ImplWriteHeader();
44 void ImplWriteNumber( sal_Int32
);
46 com::sun::star::uno::Reference
< com::sun::star::task::XStatusIndicator
> xStatusIndicator
;
49 PBMWriter(SvStream
&rPBM
);
52 bool WritePBM( const Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
);
55 //=================== Methods of PBMWriter ==============================
57 PBMWriter::PBMWriter(SvStream
&rPBM
)
69 PBMWriter::~PBMWriter()
75 bool PBMWriter::WritePBM( const Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
)
77 if ( pFilterConfigItem
)
79 mnMode
= pFilterConfigItem
->ReadInt32( "FileFormat", 0 );
81 xStatusIndicator
= pFilterConfigItem
->GetStatusIndicator();
82 if ( xStatusIndicator
.is() )
85 xStatusIndicator
->start( aMsg
, 100 );
89 BitmapEx
aBmpEx( rGraphic
.GetBitmapEx() );
90 Bitmap aBmp
= aBmpEx
.GetBitmap();
91 aBmp
.Convert( BMP_CONVERSION_1BIT_THRESHOLD
);
93 SvStreamEndian aOStmOldModus
= m_rOStm
.GetEndian();
94 m_rOStm
.SetEndian( SvStreamEndian::BIG
);
96 mpAcc
= aBmp
.AcquireReadAccess();
99 if ( ImplWriteHeader() )
102 Bitmap::ReleaseAccess( mpAcc
);
107 m_rOStm
.SetEndian( aOStmOldModus
);
109 if ( xStatusIndicator
.is() )
110 xStatusIndicator
->end();
117 bool PBMWriter::ImplWriteHeader()
119 mnWidth
= mpAcc
->Width();
120 mnHeight
= mpAcc
->Height();
121 if ( mnWidth
&& mnHeight
)
124 m_rOStm
.WriteCharPtr( "P4\x0a" );
126 m_rOStm
.WriteCharPtr( "P1\x0a" );
128 ImplWriteNumber( mnWidth
);
129 m_rOStm
.WriteUChar( 32 );
130 ImplWriteNumber( mnHeight
);
131 m_rOStm
.WriteUChar( 10 );
133 else mbStatus
= false;
139 void PBMWriter::ImplWriteBody()
144 for ( sal_uLong y
= 0; y
< mnHeight
; y
++ )
147 for ( x
= 0; x
< mnWidth
; x
++ )
150 if (!(mpAcc
->GetPixelIndex( y
, x
) & 1 ) )
152 if ( ( x
& 7 ) == 7 )
153 m_rOStm
.WriteUChar( nBYTE
);
155 if ( ( x
& 7 ) != 0 )
156 m_rOStm
.WriteUChar( nBYTE
<< ( ( x
^ 7 ) + 1 ) );
161 for ( sal_uLong y
= 0; y
< mnHeight
; y
++ )
164 for ( sal_uLong x
= 0; x
< mnWidth
; x
++ )
169 m_rOStm
.WriteUChar( 10 );
171 m_rOStm
.WriteUChar( ( mpAcc
->GetPixelIndex( y
, x
) ^ 1 ) + '0' ) ;
173 m_rOStm
.WriteUChar( 10 );
179 // A decimal number in ascii format is written in the stream.
181 void PBMWriter::ImplWriteNumber(sal_Int32 nNumber
)
183 const OString
aNum(OString::number(nNumber
));
184 m_rOStm
.WriteCharPtr( aNum
.getStr() );
190 // - exported function -
193 // this needs to be kept in sync with
194 // ImpFilterLibCacheEntry::GetImportFunction() from
195 // vcl/source/filter/graphicfilter.cxx
196 #if defined(DISABLE_DYNLOADING)
197 #define GraphicExport epbGraphicExport
200 extern "C" SAL_DLLPUBLIC_EXPORT
bool SAL_CALL
201 GraphicExport( SvStream
& rStream
, Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
)
203 PBMWriter
aPBMWriter(rStream
);
205 return aPBMWriter
.WritePBM( rGraphic
, pFilterConfigItem
);
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */