1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svtools.hxx"
26
27 #include <stdio.h>
28 #include <signal.h>
29 #include <ctype.h>
30
31 #include <sal/main.h>
32
33 #include <tools/fsys.hxx>
34 #include <tools/stream.hxx>
35 #include <vcl/svapp.hxx>
36 #include "svtools/filter.hxx"
37
38 #define EXIT_NOERROR 0x00000000
39 #define EXIT_COMMONERROR 0x00000001
40 #define EXIT_INVALID_FILE 0x00000002
41 #define EXIT_INVALID_GRAPHICFILTER 0x00000004
42 #define EXIT_INVALID_INPUTGRAPHIC 0x00000008
43 #define EXIT_OUTPUTERROR 0x00000010
44
45 #define LOWERHEXTONUM( _def_Char ) (((_def_Char)<='9') ? ((_def_Char)-'0') : ((_def_Char)-'a'+10))
46
47 // ----------
48 // - G2GApp -
49 // ----------
50
51 class G2GApp
52 {
53 private:
54
55 sal_uInt8 cExitCode;
56
57 void ShowUsage();
58 sal_Bool GetCommandOption( const ::std::vector< String >& rArgs, const String& rSwitch, String& rParam );
SetExitCode(sal_uInt8 cExit)59 void SetExitCode( sal_uInt8 cExit ) { if( ( EXIT_NOERROR == cExitCode ) || ( cExit != EXIT_NOERROR ) ) cExitCode = cExit; }
60
61 virtual void Message( const String& rText, sal_uInt8 cExitCode = EXIT_NOERROR );
62
63 public:
64
65 G2GApp();
66 virtual ~G2GApp();
67
68 int Start( const ::std::vector< String >& rArgs );
69 };
70
71 // -----------------------------------------------------------------------
72
G2GApp()73 G2GApp::G2GApp()
74 {
75 }
76
77 // -----------------------------------------------------------------------
78
~G2GApp()79 G2GApp::~G2GApp()
80 {
81 }
82
83 // -----------------------------------------------------------------------
84
GetCommandOption(const::std::vector<String> & rArgs,const String & rSwitch,String & rParam)85 sal_Bool G2GApp::GetCommandOption( const ::std::vector< String >& rArgs, const String& rSwitch, String& rParam )
86 {
87 sal_Bool bRet = sal_False;
88
89 for( int i = 0, nCount = rArgs.size(); ( i < nCount ) && !bRet; i++ )
90 {
91 String aTestStr( '-' );
92
93 for( int n = 0; ( n < 2 ) && !bRet; n++ )
94 {
95 aTestStr += rSwitch;
96
97 if( aTestStr.CompareIgnoreCaseToAscii( rArgs[ i ] ) == COMPARE_EQUAL )
98 {
99 bRet = sal_True;
100
101 if( i < ( nCount - 1 ) )
102 rParam = rArgs[ i + 1 ];
103 else
104 rParam = String();
105 }
106
107 if( 0 == n )
108 aTestStr = '/';
109 }
110 }
111
112 return bRet;
113 }
114
115 // -----------------------------------------------------------------------
116
Message(const String & rText,sal_uInt8 nExitCode)117 void G2GApp::Message( const String& rText, sal_uInt8 nExitCode )
118 {
119 if( EXIT_NOERROR != nExitCode )
120 SetExitCode( nExitCode );
121
122 ByteString aText( rText, RTL_TEXTENCODING_UTF8 );
123 aText.Append( "\r\n" );
124 fprintf( stderr, aText.GetBuffer() );
125 }
126
127 // -----------------------------------------------------------------------------
128
ShowUsage()129 void G2GApp::ShowUsage()
130 {
131 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Usage:" ) ) );
132 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g inputfile outputfile -format exportformat -filterpath path [ -# RRGGBB ]" ) ) );
133 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Options:" ) ) );
134 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -format short name of export filter to use ( e.g. gif, png, jpg, ... )" ) ) );
135 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -filterpath path to externally loaded filter libraries" ) ) );
136 Message( String( RTL_CONSTASCII_USTRINGPARAM( " -# hex value of color to be set transparent in export file (optional)" ) ) );
137 Message( String( RTL_CONSTASCII_USTRINGPARAM( "Examples:" ) ) );
138 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.jpg -format jpg -filterpath /home/filter" ) ) );
139 Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.gif -format gif -filterpath /home/filter -# C0C0C0" ) ) );
140 }
141
142 // -----------------------------------------------------------------------------
143
Start(const::std::vector<String> & rArgs)144 int G2GApp::Start( const ::std::vector< String >& rArgs )
145 {
146 int nCmdCount = rArgs.size();
147 sal_uInt16 nCurCmd = 0;
148
149 cExitCode = EXIT_NOERROR;
150
151 if( nCmdCount >= 6 )
152 {
153 GraphicFilter aFilter( sal_False );
154 String aInFile, aOutFile, aFilterStr, aFilterPath, aTransColStr;
155
156 aInFile = rArgs[ nCurCmd++ ];
157 aOutFile = rArgs[ nCurCmd++ ];
158 GetCommandOption( rArgs, String( RTL_CONSTASCII_USTRINGPARAM( "format" ) ), aFilterStr );
159 GetCommandOption( rArgs, String( RTL_CONSTASCII_USTRINGPARAM( "filterpath" ) ), aFilterPath );
160 GetCommandOption( rArgs, '#', aTransColStr );
161
162 aFilter.SetFilterPath( aFilterPath );
163
164 if( aInFile.Len() && aOutFile.Len() && aFilterStr.Len() )
165 {
166 const sal_uInt16 nExportFilter = aFilter.GetExportFormatNumberForShortName( aFilterStr );
167
168 if( GRFILTER_FORMAT_NOTFOUND == nExportFilter )
169 Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid graphic filter" ) ), EXIT_INVALID_GRAPHICFILTER );
170 else
171 {
172 if( DirEntry( aInFile ).Exists() )
173 {
174 SvFileStream aInStm( aInFile, STREAM_READ );
175 Graphic aGraphic;
176 const GfxLink aGfxLink;
177
178 aGraphic.SetLink( aGfxLink );
179
180 if( aFilter.ImportGraphic( aGraphic, aInFile, aInStm ) == GRFILTER_OK )
181 {
182 SvFileStream aOutStm( aOutFile, STREAM_WRITE | STREAM_TRUNC );
183
184 if( ( aTransColStr.Len() == 6 ) && aFilter.IsExportPixelFormat( nExportFilter ) )
185 {
186 ByteString aHexStr( aTransColStr, RTL_TEXTENCODING_ASCII_US );
187 sal_Bool bHex = sal_True;
188
189 aHexStr.ToLowerAscii();
190
191 for( sal_uInt16 i = 0; ( i < 6 ) && bHex; i++ )
192 if( !isxdigit( aHexStr.GetChar( i ) ) )
193 bHex = sal_False;
194
195 if( bHex )
196 {
197 const sal_uInt8 cTransR = ( LOWERHEXTONUM( aHexStr.GetChar( 0 ) ) << 4 ) | LOWERHEXTONUM( aHexStr.GetChar( 1 ) );
198 const sal_uInt8 cTransG = ( LOWERHEXTONUM( aHexStr.GetChar( 2 ) ) << 4 ) | LOWERHEXTONUM( aHexStr.GetChar( 3 ) );
199 const sal_uInt8 cTransB = ( LOWERHEXTONUM( aHexStr.GetChar( 4 ) ) << 4 ) | LOWERHEXTONUM( aHexStr.GetChar( 5 ) );
200
201 BitmapEx aBmpEx( aGraphic.GetBitmapEx() );
202 Bitmap aOldBmp( aBmpEx.GetBitmap() );
203 Bitmap aOldMask( aBmpEx.GetMask() );
204 Bitmap aNewMask( aOldBmp.CreateMask( Color( cTransR, cTransG, cTransB ) ) );
205
206 if( !aOldMask.IsEmpty() )
207 aNewMask.CombineSimple( aOldMask, BMP_COMBINE_OR );
208
209 aGraphic = BitmapEx( aOldBmp, aNewMask );
210 }
211 }
212
213 aFilter.ExportGraphic( aGraphic, aOutFile, aOutStm, nExportFilter );
214
215 if( aOutStm.GetError() )
216 Message( String( RTL_CONSTASCII_USTRINGPARAM( "could not write output file" ) ), EXIT_OUTPUTERROR );
217 }
218 else
219 Message( String( RTL_CONSTASCII_USTRINGPARAM( "could import graphic" ) ), EXIT_INVALID_INPUTGRAPHIC );
220 }
221 else
222 Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid file(s)" ) ), EXIT_INVALID_FILE );
223 }
224 }
225 }
226 else
227 ShowUsage();
228
229 return cExitCode;
230 }
231
232 // --------
233 // - Main -
234 // --------
235
main(int nArgCount,char * ppArgs[])236 int main( int nArgCount, char* ppArgs[] )
237 {
238 ::std::vector< String > aArgs;
239 G2GApp aG2GApp;
240
241 InitVCL( com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >() );
242
243 for( int i = 1; i < nArgCount; i++ )
244 aArgs.push_back( String( ppArgs[ i ], RTL_TEXTENCODING_ASCII_US ) );
245
246 return aG2GApp.Start( aArgs );
247 }
248