Update ooo320-m1
[ooovba.git] / goodies / source / filter.vcl / eppm / eppm.cxx
blob9617353688355073045bb375d36eca9bca711a89
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: eppm.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 "dlgeppm.hrc"
43 #include "dlgeppm.hxx"
45 //============================ PPMWriter ==================================
47 class PPMWriter {
49 private:
51 SvStream* mpOStm; // Die auszugebende PPM-Datei
52 USHORT mpOStmOldModus;
54 BOOL mbStatus;
55 sal_Int32 mnMode;
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 PPMWriter();
67 ~PPMWriter();
69 BOOL WritePPM( const Graphic& rGraphic, SvStream& rPPM, FilterConfigItem* pFilterConfigItem );
72 //=================== Methoden von PPMWriter ==============================
74 PPMWriter::PPMWriter() :
75 mbStatus ( TRUE ),
76 mpAcc ( NULL )
80 // ------------------------------------------------------------------------
82 PPMWriter::~PPMWriter()
86 // ------------------------------------------------------------------------
88 BOOL PPMWriter::WritePPM( const Graphic& rGraphic, SvStream& rPPM, FilterConfigItem* pFilterConfigItem )
91 mpOStm = &rPPM;
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_24BIT );
109 mpOStmOldModus = mpOStm->GetNumberFormatInt();
110 mpOStm->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
112 mpAcc = aBmp.AcquireReadAccess();
113 if( mpAcc )
115 if ( ImplWriteHeader() )
117 ImplWriteBody();
119 aBmp.ReleaseAccess( mpAcc );
121 else
122 mbStatus = FALSE;
124 mpOStm->SetNumberFormatInt( mpOStmOldModus );
126 if ( xStatusIndicator.is() )
127 xStatusIndicator->end();
129 return mbStatus;
132 // ------------------------------------------------------------------------
134 BOOL PPMWriter::ImplWriteHeader()
136 mnWidth = mpAcc->Width();
137 mnHeight = mpAcc->Height();
138 if ( mnWidth && mnHeight )
140 if ( mnMode == 0 )
141 *mpOStm << "P6\x0a";
142 else
143 *mpOStm << "P3\x0a";
145 ImplWriteNumber( mnWidth );
146 *mpOStm << (BYTE)32;
147 ImplWriteNumber( mnHeight );
148 *mpOStm << (BYTE)32;
149 ImplWriteNumber( 255 ); // max. col.
150 *mpOStm << (BYTE)10;
152 else
153 mbStatus = FALSE;
155 return mbStatus;
158 // ------------------------------------------------------------------------
160 void PPMWriter::ImplWriteBody()
162 if ( mnMode == 0 )
164 for ( ULONG y = 0; y < mnHeight; y++ )
166 for ( ULONG x = 0; x < mnWidth; x++ )
168 const BitmapColor& rColor = mpAcc->GetPixel( y, x );
169 *mpOStm << rColor.GetRed();
170 *mpOStm << rColor.GetGreen();
171 *mpOStm << rColor.GetBlue();
175 else
177 for ( ULONG y = 0; y < mnHeight; y++ )
179 int nCount = 70;
180 for ( ULONG x = 0; x < mnWidth; x++ )
182 BYTE i, nDat[3], nNumb;
183 if ( nCount < 0 )
185 nCount = 69;
186 *mpOStm << (BYTE)10;
188 nDat[0] = mpAcc->GetPixel( y, x ).GetRed();
189 nDat[1] = mpAcc->GetPixel( y, x ).GetGreen();
190 nDat[2] = mpAcc->GetPixel( y, x ).GetBlue();
191 for ( i = 0; i < 3; i++ )
193 nNumb = nDat[ i ] / 100;
194 if ( nNumb )
196 *mpOStm << (BYTE)( nNumb + '0' );
197 nDat[ i ] -= ( nNumb * 100 );
198 nNumb = nDat[ i ] / 10;
199 *mpOStm << (BYTE)( nNumb + '0' );
200 nDat[ i ] -= ( nNumb * 10 );
201 *mpOStm << (BYTE)( nDat[ i ] + '0' );
202 nCount -= 4;
204 else
206 nNumb = nDat[ i ] / 10;
207 if ( nNumb )
209 *mpOStm << (BYTE)( nNumb + '0' );
210 nDat[ i ] -= ( nNumb * 10 );
211 *mpOStm << (BYTE)( nDat[ i ] + '0' );
212 nCount -= 3;
214 else
216 *mpOStm << (BYTE)( nDat[ i ] + '0' );
217 nCount -= 2;
220 *mpOStm << (BYTE)' ';
223 *mpOStm << (BYTE)10;
228 // ------------------------------------------------------------------------
229 // eine Dezimalzahl im ASCII format wird in den Stream geschrieben
231 void PPMWriter::ImplWriteNumber( sal_Int32 nNumber )
233 const ByteString aNum( ByteString::CreateFromInt32( nNumber ) );
235 for( sal_Int16 n = 0, nLen = aNum.Len(); n < nLen; n++ )
236 *mpOStm << aNum.GetChar( n );
240 // ------------------------------------------------------------------------
242 // ---------------------
243 // - exported function -
244 // ---------------------
246 extern "C" BOOL __LOADONCALLAPI GraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pFilterConfigItem, BOOL )
248 PPMWriter aPPMWriter;
249 return aPPMWriter.WritePPM( rGraphic, rStream, pFilterConfigItem );
252 // ------------------------------------------------------------------------
254 extern "C" BOOL __LOADONCALLAPI DoExportDialog( FltCallDialogParameter& rPara )
256 BOOL bRet = FALSE;
258 if ( rPara.pWindow )
260 ByteString aResMgrName( "epp" );
261 ResMgr* pResMgr;
263 pResMgr = ResMgr::CreateResMgr( aResMgrName.GetBuffer(), Application::GetSettings().GetUILocale() );
265 if( pResMgr )
267 rPara.pResMgr = pResMgr;
268 bRet = ( DlgExportEPPM( rPara ).Execute() == RET_OK );
269 delete pResMgr;
271 else
272 bRet = TRUE;
275 return bRet;
278 #ifndef GCC
279 #endif
281 // ------------------------------------------------------------------------
283 // ---------------
284 // - Win16 trash -
285 // ---------------
287 #ifdef WIN
289 static HINSTANCE hDLLInst = 0;
291 extern "C" int CALLBACK LibMain( HINSTANCE hDLL, WORD, WORD nHeap, LPSTR )
293 if ( nHeap )
294 UnlockData( 0 );
296 hDLLInst = hDLL;
298 return TRUE;
301 // ------------------------------------------------------------------------
303 extern "C" int CALLBACK WEP( int )
305 return 1;
308 #endif