Bump version to 5.0-14
[LibreOffice.git] / filter / source / graphicfilter / epbm / epbm.cxx
blob1e1118a76c61c44a91a9ce0ce6446486830120e9
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 //============================ PBMWriter ==================================
31 class PBMWriter {
33 private:
35 SvStream& m_rOStm; // the output PBM file
37 bool mbStatus;
38 sal_Int32 mnMode; // 0 -> raw, 1-> ascii
39 BitmapReadAccess* mpAcc;
40 sal_uLong mnWidth, mnHeight; // size in pixel
42 bool ImplWriteHeader();
43 void ImplWriteBody();
44 void ImplWriteNumber( sal_Int32 );
46 com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
48 public:
49 PBMWriter(SvStream &rPBM);
50 ~PBMWriter();
52 bool WritePBM( const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem );
55 //=================== Methods of PBMWriter ==============================
57 PBMWriter::PBMWriter(SvStream &rPBM)
58 : m_rOStm(rPBM)
59 , mbStatus(true)
60 , mnMode(0)
61 , mpAcc(NULL)
62 , mnWidth(0)
63 , mnHeight(0)
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() )
84 OUString aMsg;
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();
97 if( mpAcc )
99 if ( ImplWriteHeader() )
100 ImplWriteBody();
102 Bitmap::ReleaseAccess( mpAcc );
104 else
105 mbStatus = false;
107 m_rOStm.SetEndian( aOStmOldModus );
109 if ( xStatusIndicator.is() )
110 xStatusIndicator->end();
112 return mbStatus;
117 bool PBMWriter::ImplWriteHeader()
119 mnWidth = mpAcc->Width();
120 mnHeight = mpAcc->Height();
121 if ( mnWidth && mnHeight )
123 if ( mnMode == 0 )
124 m_rOStm.WriteCharPtr( "P4\x0a" );
125 else
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;
134 return mbStatus;
139 void PBMWriter::ImplWriteBody()
141 if ( mnMode == 0 )
143 sal_uInt8 nBYTE = 0;
144 for ( sal_uLong y = 0; y < mnHeight; y++ )
146 sal_uLong x;
147 for ( x = 0; x < mnWidth; x++ )
149 nBYTE <<= 1;
150 if (!(mpAcc->GetPixelIndex( y, x ) & 1 ) )
151 nBYTE++;
152 if ( ( x & 7 ) == 7 )
153 m_rOStm.WriteUChar( nBYTE );
155 if ( ( x & 7 ) != 0 )
156 m_rOStm.WriteUChar( nBYTE << ( ( x ^ 7 ) + 1 ) );
159 else
161 for ( sal_uLong y = 0; y < mnHeight; y++ )
163 int nxCount = 70;
164 for ( sal_uLong x = 0; x < mnWidth; x++ )
166 if (!( --nxCount ) )
168 nxCount = 69;
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
198 #endif
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: */