merge the formfield patch from ooo-build
[ooovba.git] / svtools / bmpmaker / g2g.cxx
blob1d0694f310667105408e348644acf47a6827b019
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: g2g.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_svtools.hxx"
34 #include <stdio.h>
35 #include <signal.h>
36 #include <ctype.h>
38 #include <sal/main.h>
40 #include <tools/fsys.hxx>
41 #include <tools/stream.hxx>
42 #include <vcl/svapp.hxx>
43 #include "svtools/filter.hxx"
45 #define EXIT_NOERROR 0x00000000
46 #define EXIT_COMMONERROR 0x00000001
47 #define EXIT_INVALID_FILE 0x00000002
48 #define EXIT_INVALID_GRAPHICFILTER 0x00000004
49 #define EXIT_INVALID_INPUTGRAPHIC 0x00000008
50 #define EXIT_OUTPUTERROR 0x00000010
52 #define LOWERHEXTONUM( _def_Char ) (((_def_Char)<='9') ? ((_def_Char)-'0') : ((_def_Char)-'a'+10))
54 // ----------
55 // - G2GApp -
56 // ----------
58 class G2GApp
60 private:
62 BYTE cExitCode;
64 void ShowUsage();
65 BOOL GetCommandOption( const ::std::vector< String >& rArgs, const String& rSwitch, String& rParam );
66 void SetExitCode( BYTE cExit ) { if( ( EXIT_NOERROR == cExitCode ) || ( cExit != EXIT_NOERROR ) ) cExitCode = cExit; }
68 virtual void Message( const String& rText, BYTE cExitCode = EXIT_NOERROR );
70 public:
72 G2GApp();
73 virtual ~G2GApp();
75 int Start( const ::std::vector< String >& rArgs );
78 // -----------------------------------------------------------------------
80 G2GApp::G2GApp()
84 // -----------------------------------------------------------------------
86 G2GApp::~G2GApp()
90 // -----------------------------------------------------------------------
92 BOOL G2GApp::GetCommandOption( const ::std::vector< String >& rArgs, const String& rSwitch, String& rParam )
94 BOOL bRet = FALSE;
96 for( int i = 0, nCount = rArgs.size(); ( i < nCount ) && !bRet; i++ )
98 String aTestStr( '-' );
100 for( int n = 0; ( n < 2 ) && !bRet; n++ )
102 aTestStr += rSwitch;
104 if( aTestStr.CompareIgnoreCaseToAscii( rArgs[ i ] ) == COMPARE_EQUAL )
106 bRet = TRUE;
108 if( i < ( nCount - 1 ) )
109 rParam = rArgs[ i + 1 ];
110 else
111 rParam = String();
114 if( 0 == n )
115 aTestStr = '/';
119 return bRet;
122 // -----------------------------------------------------------------------
124 void G2GApp::Message( const String& rText, BYTE nExitCode )
126 if( EXIT_NOERROR != nExitCode )
127 SetExitCode( nExitCode );
129 ByteString aText( rText, RTL_TEXTENCODING_UTF8 );
130 aText.Append( "\r\n" );
131 fprintf( stderr, "%s", aText.GetBuffer() );
134 // -----------------------------------------------------------------------------
136 void G2GApp::ShowUsage()
138 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Usage:" ) ) );
139 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g inputfile outputfile -format exportformat -filterpath path [ -# RRGGBB ]" ) ) );
140 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Options:" ) ) );
141 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -format short name of export filter to use ( e.g. gif, png, jpg, ... )" ) ) );
142 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -filterpath path to externally loaded filter libraries" ) ) );
143 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -# hex value of color to be set transparent in export file (optional)" ) ) );
144 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Examples:" ) ) );
145 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.jpg -format jpg -filterpath /home/filter" ) ) );
146 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.gif -format gif -filterpath /home/filter -# C0C0C0" ) ) );
149 // -----------------------------------------------------------------------------
151 int G2GApp::Start( const ::std::vector< String >& rArgs )
153 int nCmdCount = rArgs.size();
154 USHORT nCurCmd = 0;
156 cExitCode = EXIT_NOERROR;
158 if( nCmdCount >= 6 )
160 GraphicFilter aFilter( sal_False );
161 String aInFile, aOutFile, aFilterStr, aFilterPath, aTransColStr;
163 aInFile = rArgs[ nCurCmd++ ];
164 aOutFile = rArgs[ nCurCmd++ ];
165 GetCommandOption( rArgs, String( RTL_CONSTASCII_USTRINGPARAM( "format" ) ), aFilterStr );
166 GetCommandOption( rArgs, String( RTL_CONSTASCII_USTRINGPARAM( "filterpath" ) ), aFilterPath );
167 GetCommandOption( rArgs, '#', aTransColStr );
169 aFilter.SetFilterPath( aFilterPath );
171 if( aInFile.Len() && aOutFile.Len() && aFilterStr.Len() )
173 const USHORT nExportFilter = aFilter.GetExportFormatNumberForShortName( aFilterStr );
175 if( GRFILTER_FORMAT_NOTFOUND == nExportFilter )
176 Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid graphic filter" ) ), EXIT_INVALID_GRAPHICFILTER );
177 else
179 if( DirEntry( aInFile ).Exists() )
181 SvFileStream aInStm( aInFile, STREAM_READ );
182 Graphic aGraphic;
183 const GfxLink aGfxLink;
185 aGraphic.SetLink( aGfxLink );
187 if( aFilter.ImportGraphic( aGraphic, aInFile, aInStm ) == GRFILTER_OK )
189 SvFileStream aOutStm( aOutFile, STREAM_WRITE | STREAM_TRUNC );
191 if( ( aTransColStr.Len() == 6 ) && aFilter.IsExportPixelFormat( nExportFilter ) )
193 ByteString aHexStr( aTransColStr, RTL_TEXTENCODING_ASCII_US );
194 sal_Bool bHex = sal_True;
196 aHexStr.ToLowerAscii();
198 for( sal_uInt16 i = 0; ( i < 6 ) && bHex; i++ )
199 if( !isxdigit( aHexStr.GetChar( i ) ) )
200 bHex = sal_False;
202 if( bHex )
204 const BYTE cTransR = ( LOWERHEXTONUM( aHexStr.GetChar( 0 ) ) << 4 ) | LOWERHEXTONUM( aHexStr.GetChar( 1 ) );
205 const BYTE cTransG = ( LOWERHEXTONUM( aHexStr.GetChar( 2 ) ) << 4 ) | LOWERHEXTONUM( aHexStr.GetChar( 3 ) );
206 const BYTE cTransB = ( LOWERHEXTONUM( aHexStr.GetChar( 4 ) ) << 4 ) | LOWERHEXTONUM( aHexStr.GetChar( 5 ) );
208 BitmapEx aBmpEx( aGraphic.GetBitmapEx() );
209 Bitmap aOldBmp( aBmpEx.GetBitmap() );
210 Bitmap aOldMask( aBmpEx.GetMask() );
211 Bitmap aNewMask( aOldBmp.CreateMask( Color( cTransR, cTransG, cTransB ) ) );
213 if( !aOldMask.IsEmpty() )
214 aNewMask.CombineSimple( aOldMask, BMP_COMBINE_OR );
216 aGraphic = BitmapEx( aOldBmp, aNewMask );
220 aFilter.ExportGraphic( aGraphic, aOutFile, aOutStm, nExportFilter );
222 if( aOutStm.GetError() )
223 Message( String( RTL_CONSTASCII_USTRINGPARAM( "could not write output file" ) ), EXIT_OUTPUTERROR );
225 else
226 Message( String( RTL_CONSTASCII_USTRINGPARAM( "could import graphic" ) ), EXIT_INVALID_INPUTGRAPHIC );
228 else
229 Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid file(s)" ) ), EXIT_INVALID_FILE );
233 else
234 ShowUsage();
236 return cExitCode;
239 // --------
240 // - Main -
241 // --------
243 int main( int nArgCount, char* ppArgs[] )
245 ::std::vector< String > aArgs;
246 G2GApp aG2GApp;
248 InitVCL( com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >() );
250 for( int i = 1; i < nArgCount; i++ )
251 aArgs.push_back( String( ppArgs[ i ], RTL_TEXTENCODING_ASCII_US ) );
253 return aG2GApp.Start( aArgs );