merge the formfield patch from ooo-build
[ooovba.git] / goodies / source / filter.vcl / epbm / epbm.cxx
blob076ed6d07cb6c93d4b08cfebc7bd23cecd8eae4f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: epbm.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_goodies.hxx"
34 #include <vcl/svapp.hxx>
35 #include <vcl/graph.hxx>
36 #include <vcl/bmpacc.hxx>
37 #include <vcl/msgbox.hxx>
38 #include <svtools/solar.hrc>
39 #include <svtools/fltcall.hxx>
40 #include <svtools/FilterConfigItem.hxx>
41 #include "strings.hrc"
42 #include "dlgepbm.hrc"
43 #include "dlgepbm.hxx"
45 //============================ PBMWriter ==================================
47 class PBMWriter {
49 private:
51 SvStream* mpOStm; // Die auszugebende PBM-Datei
52 USHORT mpOStmOldModus;
54 BOOL mbStatus;
55 sal_Int32 mnMode; // 0 -> raw, 1-> ascii
56 BitmapReadAccess* mpAcc;
57 ULONG mnWidth, mnHeight; // Bildausmass in Pixeln
59 BOOL ImplWriteHeader();
60 void ImplWriteBody();
61 void ImplWriteNumber( sal_Int32 );
63 com::sun::star::uno::Reference< com::sun::star::task::XStatusIndicator > xStatusIndicator;
65 public:
66 PBMWriter();
67 ~PBMWriter();
69 BOOL WritePBM( const Graphic& rGraphic, SvStream& rPBM, FilterConfigItem* pFilterConfigItem );
72 //=================== Methoden von PBMWriter ==============================
74 PBMWriter::PBMWriter() :
75 mbStatus ( TRUE ),
76 mpAcc ( NULL )
80 // ------------------------------------------------------------------------
82 PBMWriter::~PBMWriter()
86 // ------------------------------------------------------------------------
88 BOOL PBMWriter::WritePBM( const Graphic& rGraphic, SvStream& rPBM, FilterConfigItem* pFilterConfigItem )
91 mpOStm = &rPBM;
93 if ( pFilterConfigItem )
95 mnMode = pFilterConfigItem->ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "FileFormat" ) ), 0 );
97 xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
98 if ( xStatusIndicator.is() )
100 rtl::OUString aMsg;
101 xStatusIndicator->start( aMsg, 100 );
105 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
106 Bitmap aBmp = aBmpEx.GetBitmap();
107 aBmp.Convert( BMP_CONVERSION_1BIT_THRESHOLD );
109 mpOStmOldModus = mpOStm->GetNumberFormatInt();
110 mpOStm->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
112 mpAcc = aBmp.AcquireReadAccess();
113 if( mpAcc )
115 if ( ImplWriteHeader() )
116 ImplWriteBody();
118 aBmp.ReleaseAccess( mpAcc );
120 else
121 mbStatus = FALSE;
123 mpOStm->SetNumberFormatInt( mpOStmOldModus );
125 if ( xStatusIndicator.is() )
126 xStatusIndicator->end();
128 return mbStatus;
131 // ------------------------------------------------------------------------
133 BOOL PBMWriter::ImplWriteHeader()
135 mnWidth = mpAcc->Width();
136 mnHeight = mpAcc->Height();
137 if ( mnWidth && mnHeight )
139 if ( mnMode == 0 )
140 *mpOStm << "P4\x0a";
141 else
142 *mpOStm << "P1\x0a";
144 ImplWriteNumber( mnWidth );
145 *mpOStm << (BYTE)32;
146 ImplWriteNumber( mnHeight );
147 *mpOStm << (BYTE)10;
149 else mbStatus = FALSE;
150 return mbStatus;
153 // ------------------------------------------------------------------------
155 void PBMWriter::ImplWriteBody()
157 if ( mnMode == 0 )
159 BYTE nBYTE = 0;
160 for ( ULONG y = 0; y < mnHeight; y++ )
162 ULONG x;
163 for ( x = 0; x < mnWidth; x++ )
165 nBYTE <<= 1;
166 if (!(mpAcc->GetPixel( y, x ) & 1 ) )
167 nBYTE++;
168 if ( ( x & 7 ) == 7 )
169 *mpOStm << nBYTE;
171 if ( ( x & 7 ) != 0 )
172 *mpOStm << (BYTE)( nBYTE << ( ( x ^ 7 ) + 1 ) );
175 else
177 int nxCount;
178 for ( ULONG y = 0; y < mnHeight; y++ )
180 nxCount = 70;
181 for ( ULONG x = 0; x < mnWidth; x++ )
183 if (!( --nxCount ) )
185 nxCount = 69;
186 *mpOStm << (BYTE)10;
188 *mpOStm << (BYTE)( ( mpAcc->GetPixel( y, x ) ^ 1 ) + '0' ) ;
190 *mpOStm << (BYTE)10;
195 // ------------------------------------------------------------------------
196 // eine Dezimalzahl im ASCII format wird in den Stream geschrieben
198 void PBMWriter::ImplWriteNumber( sal_Int32 nNumber )
200 const ByteString aNum( ByteString::CreateFromInt32( nNumber ) );
202 for( sal_Int16 n = 0, nLen = aNum.Len(); n < nLen; n++ )
203 *mpOStm << aNum.GetChar( n );
207 // ------------------------------------------------------------------------
209 // ---------------------
210 // - exported function -
211 // ---------------------
213 extern "C" BOOL __LOADONCALLAPI GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, BOOL )
215 PBMWriter aPBMWriter;
217 return aPBMWriter.WritePBM( rGraphic, rStream, pFilterConfigItem );
220 // ------------------------------------------------------------------------
222 extern "C" BOOL __LOADONCALLAPI DoExportDialog( FltCallDialogParameter& rPara )
224 BOOL bRet = FALSE;
226 if ( rPara.pWindow )
228 ByteString aResMgrName( "epb" );
229 ResMgr* pResMgr;
231 pResMgr = ResMgr::CreateResMgr( aResMgrName.GetBuffer(), Application::GetSettings().GetUILocale() );
233 if( pResMgr )
235 rPara.pResMgr = pResMgr;
236 bRet = ( DlgExportEPBM( rPara ).Execute() == RET_OK );
237 delete pResMgr;
239 else
240 bRet = TRUE;
243 return bRet;
246 // ------------------------------------------------------------------------
247 #ifndef GCC
248 #endif
250 // ---------------
251 // - Win16 trash -
252 // ---------------
254 #ifdef WIN
256 static HINSTANCE hDLLInst = 0;
258 extern "C" int CALLBACK LibMain( HINSTANCE hDLL, WORD, WORD nHeap, LPSTR )
260 if ( nHeap )
261 UnlockData( 0 );
263 hDLLInst = hDLL;
265 return TRUE;
268 // ------------------------------------------------------------------------
270 extern "C" int CALLBACK WEP( int )
272 return 1;
275 #endif