Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <stdio.h>
31 : : #include <signal.h>
32 : : #include <ctype.h>
33 : :
34 : : #include <rtl/strbuf.hxx>
35 : : #include <tools/fsys.hxx>
36 : : #include <tools/stream.hxx>
37 : : #include <vcl/svapp.hxx>
38 : : #include "svtools/filter.hxx"
39 : :
40 : : #define EXIT_NOERROR 0x00000000
41 : : #define EXIT_COMMONERROR 0x00000001
42 : : #define EXIT_INVALID_FILE 0x00000002
43 : : #define EXIT_INVALID_GRAPHICFILTER 0x00000004
44 : : #define EXIT_INVALID_INPUTGRAPHIC 0x00000008
45 : : #define EXIT_OUTPUTERROR 0x00000010
46 : :
47 : : #define LOWERHEXTONUM( _def_Char ) (((_def_Char)<='9') ? ((_def_Char)-'0') : ((_def_Char)-'a'+10))
48 : :
49 : : // ----------
50 : : // - G2GApp -
51 : : // ----------
52 : :
53 : : class G2GApp
54 : : {
55 : : private:
56 : :
57 : : sal_uInt8 cExitCode;
58 : :
59 : : void ShowUsage();
60 : : sal_Bool GetCommandOption( const ::std::vector< String >& rArgs, const String& rSwitch, String& rParam );
61 : 0 : void SetExitCode( sal_uInt8 cExit ) { if( ( EXIT_NOERROR == cExitCode ) || ( cExit != EXIT_NOERROR ) ) cExitCode = cExit; }
62 : :
63 : : virtual void Message( const String& rText, sal_uInt8 cExitCode = EXIT_NOERROR );
64 : :
65 : : public:
66 : :
67 : : G2GApp();
68 : : virtual ~G2GApp();
69 : :
70 : : int Start( const ::std::vector< String >& rArgs );
71 : : };
72 : :
73 : : // -----------------------------------------------------------------------
74 : :
75 : 0 : G2GApp::G2GApp()
76 : : {
77 : 0 : }
78 : :
79 : : // -----------------------------------------------------------------------
80 : :
81 : 0 : G2GApp::~G2GApp()
82 : : {
83 : 0 : }
84 : :
85 : : // -----------------------------------------------------------------------
86 : :
87 : 0 : sal_Bool G2GApp::GetCommandOption( const ::std::vector< String >& rArgs, const String& rSwitch, String& rParam )
88 : : {
89 : 0 : sal_Bool bRet = sal_False;
90 : :
91 : 0 : for( int i = 0, nCount = rArgs.size(); ( i < nCount ) && !bRet; i++ )
92 : : {
93 : 0 : rtl::OUString aTestStr( '-' );
94 : :
95 : 0 : for( int n = 0; ( n < 2 ) && !bRet; n++ )
96 : : {
97 : 0 : aTestStr += rSwitch;
98 : :
99 : 0 : if( aTestStr.equalsIgnoreAsciiCase( rArgs[ i ] ) )
100 : : {
101 : 0 : bRet = sal_True;
102 : :
103 : 0 : if( i < ( nCount - 1 ) )
104 : 0 : rParam = rArgs[ i + 1 ];
105 : : else
106 : 0 : rParam = String();
107 : : }
108 : :
109 : 0 : if( 0 == n )
110 : 0 : aTestStr = rtl::OUString('/');
111 : : }
112 : 0 : }
113 : :
114 : 0 : return bRet;
115 : : }
116 : :
117 : : // -----------------------------------------------------------------------
118 : :
119 : 0 : void G2GApp::Message( const String& rText, sal_uInt8 nExitCode )
120 : : {
121 : 0 : if( EXIT_NOERROR != nExitCode )
122 : 0 : SetExitCode( nExitCode );
123 : :
124 : 0 : rtl::OStringBuffer aText(rtl::OUStringToOString(rText, RTL_TEXTENCODING_UTF8));
125 : 0 : aText.append(RTL_CONSTASCII_STRINGPARAM("\r\n"));
126 : 0 : fprintf(stderr, "%s", aText.getStr());
127 : 0 : }
128 : :
129 : : // -----------------------------------------------------------------------------
130 : :
131 : 0 : void G2GApp::ShowUsage()
132 : : {
133 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( "Usage:" ) ) );
134 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g inputfile outputfile -format exportformat -filterpath path [ -# RRGGBB ]" ) ) );
135 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( "Options:" ) ) );
136 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( " -format short name of export filter to use ( e.g. gif, png, jpg, ... )" ) ) );
137 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( " -filterpath path to externally loaded filter libraries" ) ) );
138 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( " -# hex value of color to be set transparent in export file (optional)" ) ) );
139 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( "Examples:" ) ) );
140 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.jpg -format jpg -filterpath /home/filter" ) ) );
141 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( " g2g /home/test.bmp /home/test.gif -format gif -filterpath /home/filter -# C0C0C0" ) ) );
142 : 0 : }
143 : :
144 : : // -----------------------------------------------------------------------------
145 : :
146 : 0 : int G2GApp::Start( const ::std::vector< String >& rArgs )
147 : : {
148 : 0 : size_t nCmdCount = rArgs.size();
149 : :
150 : 0 : cExitCode = EXIT_NOERROR;
151 : :
152 : 0 : if( nCmdCount >= 6 )
153 : : {
154 : 0 : GraphicFilter aFilter( sal_False );
155 : 0 : String aInFile, aOutFile, aFilterStr, aFilterPath, aTransColStr;
156 : 0 : size_t nCurCmd = 0;
157 : :
158 : 0 : aInFile = rArgs[ nCurCmd++ ];
159 : 0 : aOutFile = rArgs[ nCurCmd++ ];
160 : 0 : GetCommandOption( rArgs, rtl::OUString("format"), aFilterStr );
161 : 0 : GetCommandOption( rArgs, rtl::OUString("filterpath"), aFilterPath );
162 : 0 : GetCommandOption( rArgs, rtl::OUString('#'), aTransColStr );
163 : :
164 : 0 : aFilter.SetFilterPath( aFilterPath );
165 : :
166 : 0 : if( aInFile.Len() && aOutFile.Len() && aFilterStr.Len() )
167 : : {
168 : 0 : const sal_uInt16 nExportFilter = aFilter.GetExportFormatNumberForShortName( aFilterStr );
169 : :
170 : 0 : if( GRFILTER_FORMAT_NOTFOUND == nExportFilter )
171 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid graphic filter" ) ), EXIT_INVALID_GRAPHICFILTER );
172 : : else
173 : : {
174 : 0 : if( DirEntry( aInFile ).Exists() )
175 : : {
176 : 0 : SvFileStream aInStm( aInFile, STREAM_READ );
177 : 0 : Graphic aGraphic;
178 : 0 : const GfxLink aGfxLink;
179 : :
180 : 0 : aGraphic.SetLink( aGfxLink );
181 : :
182 : 0 : if( aFilter.ImportGraphic( aGraphic, aInFile, aInStm ) == GRFILTER_OK )
183 : : {
184 : 0 : SvFileStream aOutStm( aOutFile, STREAM_WRITE | STREAM_TRUNC );
185 : :
186 : 0 : if( ( aTransColStr.Len() == 6 ) && aFilter.IsExportPixelFormat( nExportFilter ) )
187 : : {
188 : : rtl::OString aHexStr(rtl::OUStringToOString(aTransColStr, RTL_TEXTENCODING_ASCII_US).
189 : 0 : toAsciiLowerCase());
190 : 0 : sal_Bool bHex = sal_True;
191 : :
192 : 0 : for( sal_uInt16 i = 0; ( i < 6 ) && bHex; i++ )
193 : 0 : if( !isxdigit( aHexStr[i] ) )
194 : 0 : bHex = sal_False;
195 : :
196 : 0 : if( bHex )
197 : : {
198 : 0 : const sal_uInt8 cTransR = ( LOWERHEXTONUM( aHexStr[0] ) << 4 ) | LOWERHEXTONUM( aHexStr[1] );
199 : 0 : const sal_uInt8 cTransG = ( LOWERHEXTONUM( aHexStr[2] ) << 4 ) | LOWERHEXTONUM( aHexStr[3] );
200 : 0 : const sal_uInt8 cTransB = ( LOWERHEXTONUM( aHexStr[4] ) << 4 ) | LOWERHEXTONUM( aHexStr[5] );
201 : :
202 : 0 : BitmapEx aBmpEx( aGraphic.GetBitmapEx() );
203 : 0 : Bitmap aOldBmp( aBmpEx.GetBitmap() );
204 : 0 : Bitmap aOldMask( aBmpEx.GetMask() );
205 : 0 : Bitmap aNewMask( aOldBmp.CreateMask( Color( cTransR, cTransG, cTransB ) ) );
206 : :
207 : 0 : if( !aOldMask.IsEmpty() )
208 : 0 : aNewMask.CombineSimple( aOldMask, BMP_COMBINE_OR );
209 : :
210 : 0 : aGraphic = BitmapEx( aOldBmp, aNewMask );
211 : 0 : }
212 : : }
213 : :
214 : 0 : aFilter.ExportGraphic( aGraphic, aOutFile, aOutStm, nExportFilter );
215 : :
216 : 0 : if( aOutStm.GetError() )
217 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( "could not write output file" ) ), EXIT_OUTPUTERROR );
218 : : }
219 : : else
220 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( "could import graphic" ) ), EXIT_INVALID_INPUTGRAPHIC );
221 : : }
222 : : else
223 : 0 : Message( String( RTL_CONSTASCII_USTRINGPARAM( "invalid file(s)" ) ), EXIT_INVALID_FILE );
224 : : }
225 : 0 : }
226 : : }
227 : : else
228 : 0 : ShowUsage();
229 : :
230 : 0 : return cExitCode;
231 : : }
232 : :
233 : : // --------
234 : : // - Main -
235 : : // --------
236 : :
237 : 0 : int main( int nArgCount, char* ppArgs[] )
238 : : {
239 : 0 : ::std::vector< String > aArgs;
240 : 0 : G2GApp aG2GApp;
241 : :
242 : 0 : InitVCL( com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >() );
243 : :
244 : 0 : for( int i = 1; i < nArgCount; i++ )
245 : 0 : aArgs.push_back( String( ppArgs[ i ], RTL_TEXTENCODING_ASCII_US ) );
246 : :
247 : 0 : return aG2GApp.Start( aArgs );
248 : : }
249 : :
250 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|