1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
37 #include <tools/fsys.hxx>
38 #include <tools/stream.hxx>
39 #include <vcl/svapp.hxx>
40 #include "svtools/filter.hxx"
42 #define EXIT_NOERROR 0x00000000
43 #define EXIT_COMMONERROR 0x00000001
44 #define EXIT_INVALID_FILE 0x00000002
45 #define EXIT_INVALID_GRAPHICFILTER 0x00000004
46 #define EXIT_INVALID_INPUTGRAPHIC 0x00000008
47 #define EXIT_OUTPUTERROR 0x00000010
49 #define LOWERHEXTONUM( _def_Char ) (((_def_Char)<='9') ? ((_def_Char)-'0') : ((_def_Char)-'a'+10))
62 BOOL
GetCommandOption( const ::std::vector
< String
>& rArgs
, const String
& rSwitch
, String
& rParam
);
63 void SetExitCode( BYTE cExit
) { if( ( EXIT_NOERROR
== cExitCode
) || ( cExit
!= EXIT_NOERROR
) ) cExitCode
= cExit
; }
65 virtual void Message( const String
& rText
, BYTE cExitCode
= EXIT_NOERROR
);
72 int Start( const ::std::vector
< String
>& rArgs
);
75 // -----------------------------------------------------------------------
81 // -----------------------------------------------------------------------
87 // -----------------------------------------------------------------------
89 BOOL
G2GApp::GetCommandOption( const ::std::vector
< String
>& rArgs
, const String
& rSwitch
, String
& rParam
)
93 for( int i
= 0, nCount
= rArgs
.size(); ( i
< nCount
) && !bRet
; i
++ )
95 String
aTestStr( '-' );
97 for( int n
= 0; ( n
< 2 ) && !bRet
; n
++ )
101 if( aTestStr
.CompareIgnoreCaseToAscii( rArgs
[ i
] ) == COMPARE_EQUAL
)
105 if( i
< ( nCount
- 1 ) )
106 rParam
= rArgs
[ i
+ 1 ];
119 // -----------------------------------------------------------------------
121 void G2GApp::Message( const String
& rText
, BYTE nExitCode
)
123 if( EXIT_NOERROR
!= nExitCode
)
124 SetExitCode( nExitCode
);
126 ByteString
aText( rText
, RTL_TEXTENCODING_UTF8
);
127 aText
.Append( "\r\n" );
128 fprintf( stderr
, aText
.GetBuffer() );
131 // -----------------------------------------------------------------------------
133 void G2GApp::ShowUsage()
135 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Usage:" ) ) );
136 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g inputfile outputfile -format exportformat -filterpath path [ -# RRGGBB ]" ) ) );
137 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Options:" ) ) );
138 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -format short name of export filter to use ( e.g. gif, png, jpg, ... )" ) ) );
139 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -filterpath path to externally loaded filter libraries" ) ) );
140 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -# hex value of color to be set transparent in export file (optional)" ) ) );
141 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Examples:" ) ) );
142 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.jpg -format jpg -filterpath /home/filter" ) ) );
143 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.gif -format gif -filterpath /home/filter -# C0C0C0" ) ) );
146 // -----------------------------------------------------------------------------
148 int G2GApp::Start( const ::std::vector
< String
>& rArgs
)
150 int nCmdCount
= rArgs
.size();
153 cExitCode
= EXIT_NOERROR
;
157 GraphicFilter
aFilter( sal_False
);
158 String aInFile
, aOutFile
, aFilterStr
, aFilterPath
, aTransColStr
;
160 aInFile
= rArgs
[ nCurCmd
++ ];
161 aOutFile
= rArgs
[ nCurCmd
++ ];
162 GetCommandOption( rArgs
, String( RTL_CONSTASCII_USTRINGPARAM( "format" ) ), aFilterStr
);
163 GetCommandOption( rArgs
, String( RTL_CONSTASCII_USTRINGPARAM( "filterpath" ) ), aFilterPath
);
164 GetCommandOption( rArgs
, '#', aTransColStr
);
166 aFilter
.SetFilterPath( aFilterPath
);
168 if( aInFile
.Len() && aOutFile
.Len() && aFilterStr
.Len() )
170 const USHORT nExportFilter
= aFilter
.GetExportFormatNumberForShortName( aFilterStr
);
172 if( GRFILTER_FORMAT_NOTFOUND
== nExportFilter
)
173 Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid graphic filter" ) ), EXIT_INVALID_GRAPHICFILTER
);
176 if( DirEntry( aInFile
).Exists() )
178 SvFileStream
aInStm( aInFile
, STREAM_READ
);
180 const GfxLink aGfxLink
;
182 aGraphic
.SetLink( aGfxLink
);
184 if( aFilter
.ImportGraphic( aGraphic
, aInFile
, aInStm
) == GRFILTER_OK
)
186 SvFileStream
aOutStm( aOutFile
, STREAM_WRITE
| STREAM_TRUNC
);
188 if( ( aTransColStr
.Len() == 6 ) && aFilter
.IsExportPixelFormat( nExportFilter
) )
190 ByteString
aHexStr( aTransColStr
, RTL_TEXTENCODING_ASCII_US
);
191 sal_Bool bHex
= sal_True
;
193 aHexStr
.ToLowerAscii();
195 for( sal_uInt16 i
= 0; ( i
< 6 ) && bHex
; i
++ )
196 if( !isxdigit( aHexStr
.GetChar( i
) ) )
201 const BYTE cTransR
= ( LOWERHEXTONUM( aHexStr
.GetChar( 0 ) ) << 4 ) | LOWERHEXTONUM( aHexStr
.GetChar( 1 ) );
202 const BYTE cTransG
= ( LOWERHEXTONUM( aHexStr
.GetChar( 2 ) ) << 4 ) | LOWERHEXTONUM( aHexStr
.GetChar( 3 ) );
203 const BYTE cTransB
= ( LOWERHEXTONUM( aHexStr
.GetChar( 4 ) ) << 4 ) | LOWERHEXTONUM( aHexStr
.GetChar( 5 ) );
205 BitmapEx
aBmpEx( aGraphic
.GetBitmapEx() );
206 Bitmap
aOldBmp( aBmpEx
.GetBitmap() );
207 Bitmap
aOldMask( aBmpEx
.GetMask() );
208 Bitmap
aNewMask( aOldBmp
.CreateMask( Color( cTransR
, cTransG
, cTransB
) ) );
210 if( !aOldMask
.IsEmpty() )
211 aNewMask
.CombineSimple( aOldMask
, BMP_COMBINE_OR
);
213 aGraphic
= BitmapEx( aOldBmp
, aNewMask
);
217 aFilter
.ExportGraphic( aGraphic
, aOutFile
, aOutStm
, nExportFilter
);
219 if( aOutStm
.GetError() )
220 Message( String( RTL_CONSTASCII_USTRINGPARAM( "could not write output file" ) ), EXIT_OUTPUTERROR
);
223 Message( String( RTL_CONSTASCII_USTRINGPARAM( "could import graphic" ) ), EXIT_INVALID_INPUTGRAPHIC
);
226 Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid file(s)" ) ), EXIT_INVALID_FILE
);
240 int main( int nArgCount
, char* ppArgs
[] )
242 ::std::vector
< String
> aArgs
;
245 InitVCL( com::sun::star::uno::Reference
< com::sun::star::lang::XMultiServiceFactory
>() );
247 for( int i
= 1; i
< nArgCount
; i
++ )
248 aArgs
.push_back( String( ppArgs
[ i
], RTL_TEXTENCODING_ASCII_US
) );
250 return aG2GApp
.Start( aArgs
);