2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import helper
.OSHelper
;
22 import helper
.ProcessHandler
;
23 import helper
.StringHelper
;
26 public class JPEGCreator
extends EnhancedComplexTestCase
28 public String
[] getTestMethodNames()
30 return new String
[]{"PostscriptOrPDFToJPEG"};
36 public void PostscriptOrPDFToJPEG()
38 GlobalLogWriter
.set(log
);
39 ParameterHelper aParam
= new ParameterHelper(param
);
41 // run through all documents found in Inputpath
42 foreachPSorPDFinInputPath(aParam
);
46 public void checkOneFile(String _sDocumentName
, String _sResult
, ParameterHelper _aParams
) throws OfficeException
48 GlobalLogWriter
.println(" Document: " + _sDocumentName
);
49 GlobalLogWriter
.println(" results: " + _sResult
);
50 String sJPEGNameSchema
= createJPEG(_sDocumentName
, "", _aParams
);
52 // store information only if jpeg files exists
53 int nPages
= countPages(sJPEGNameSchema
);
56 createSmallPictures(sJPEGNameSchema
);
58 // read out tolerance file
59 String sFileDir
= FileHelper
.getPath(_sDocumentName
);
60 String sBasename
= FileHelper
.getBasename(_sDocumentName
);
62 String sToleranceFile
= FileHelper
.appendPath(sFileDir
, "tolerance.ini");
63 File aToleranceFile
= new File(sToleranceFile
);
64 if (aToleranceFile
.exists())
66 IniFile aIniFile
= new IniFile(sToleranceFile
);
67 nTolerance
= aIniFile
.getIntValue(sBasename
, "accept", 0); // default for all pages
71 String sIndexFile
= FileHelper
.appendPath(_sResult
, "index.ini");
72 File aIndexFile
= new File(sIndexFile
);
73 if (aIndexFile
.exists())
75 // store only if an index file exists
76 IniFile aIniFile
= new IniFile(sIndexFile
);
77 aIniFile
.insertValue(sBasename
, "jpegschema", sJPEGNameSchema
);
78 aIniFile
.insertValue(sBasename
, "pages", nPages
);
79 aIniFile
.insertValue(sBasename
, "tolerance", nTolerance
);
85 assure("There are no pages in document:'" + _sDocumentName
+ "', maybe document currupt?", false, true);
90 * Create a lot of smaller and nicer Pictures of the big fat pages.
94 public void createSmallPictures(String _sJPEGSchema
)
96 ParameterHelper aParam
= new ParameterHelper(param
);
97 if (! aParam
.createSmallPictures())
102 if (_sJPEGSchema
.length() > 0)
104 // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
105 for (int i
=1;i
<10000;i
++)
107 String sJPEGFilename
= getFilenameForJPEGSchema(_sJPEGSchema
, i
);
108 if (FileHelper
.exists(sJPEGFilename
))
110 convertToNearSameFileWithWidth340(sJPEGFilename
);
114 break; // stop file check
122 * convert a picture to a new picture with 340 pixel width.
123 * @param _sJPEGFilename
125 public static void convertToNearSameFileWithWidth340(String _sJPEGFilename
)
127 ParameterHelper aParam
= new ParameterHelper(param
);
128 if (! aParam
.createSmallPictures())
132 String sJPEGFilename
= _sJPEGFilename
.replaceAll("\\\\", "/");
133 String sNewJPEGFilename
;
134 sNewJPEGFilename
= util
.utils
.replaceAll13(sJPEGFilename
, ".jpg", "_w340.jpg");
135 convertToWidth340(sJPEGFilename
, sNewJPEGFilename
);
139 * convert chart2_Regression.ods.ps_180DPI_0001.jpg -filter Catrom -resize
140 340x chart2_Regression.ods.ps_180DPI_0001_w340.jpg
143 Cubic schlecht, weil unscharf
145 Triangle ganz brauchbar (default?)
151 private static void convertToWidth340(String _sFrom
, String _To
)
153 String sConvertEXE
= "convert";
154 if (OSHelper
.isLinuxIntel())
156 sConvertEXE
= "convert";
158 if (OSHelper
.isWindows())
162 // sConvertEXE = "C:\\Programme\\ImageMagick-6.0.3-q8\\convert.exe";
163 sConvertEXE
= "convert.exe";
164 String sConvertPath
= (String
)param
.get("imagemagick.path");
165 if (sConvertPath
!= null)
167 sConvertEXE
= FileHelper
.appendPath(sConvertPath
, sConvertEXE
);
171 String
[] sCommandArray
=
179 ProcessHandler aHandler
= new ProcessHandler(sCommandArray
);
180 aHandler
.executeSynchronously();
181 aHandler
.getExitCode();
183 String sBack
= aHandler
.getOutputText();
184 if (sBack
.length() > 0)
186 GlobalLogWriter
.println("'" + sBack
+ "'");
191 * create out of a given Postscript/PDF _sFile a list of JPEGs, one for every page
193 * @param _sAdditional
195 * @return the schema of the first jpeg name
197 String
createJPEG(String _sFile
, String _sAdditional
, ParameterHelper _aParam
)
199 if (_sFile
.startsWith("file:///"))
201 _sFile
= FileHelper
.getSystemPathFromFileURL(_sFile
);
203 File aFile
= new File(_sFile
);
206 String sAbsFile
= aFile
.getAbsolutePath();
207 if (!sAbsFile
.equals(_sFile
))
214 GlobalLogWriter
.println("File: '" + _sFile
+ "' doesn't exist.");
217 String sFileDir
= FileHelper
.getPath(_sFile
);
218 String sBasename
= FileHelper
.getBasename(_sFile
);
220 String sTmpDir
= util
.utils
.getUsersTempDir();
221 if (_aParam
.getOutputPath() != null)
223 sTmpDir
= _aParam
.getOutputPath();
226 String sJPEGNameSchema
= "";
227 if (_sFile
.toLowerCase().endsWith("ps") ||
228 _sFile
.toLowerCase().endsWith("prn") ||
229 _sFile
.toLowerCase().endsWith("pdf"))
231 // seems to be a Postscript of PDF file
233 sJPEGNameSchema
= createJPEGsFromPostscript(sTmpDir
, sFileDir
, sBasename
, _aParam
.getResolutionInDPI());
235 else if (_sFile
.toLowerCase().endsWith("jpg") ||
236 _sFile
.toLowerCase().endsWith("jpeg"))
238 // do nothing, it's already a picture.
243 // we assume it's an office document.
244 // Office files will not handled here, we need a PS or PDF file
246 return sJPEGNameSchema
;
249 private String
getJPEGName(String _sOutputPath
, String _sBasename
, int _nResolutionInDPI
, String _sGS_PageOutput
)
251 String sName
= _sBasename
+ "_" + String
.valueOf(_nResolutionInDPI
) + "DPI_" + _sGS_PageOutput
+ ".jpg";
252 String sJPEGName
= FileHelper
.appendPath(_sOutputPath
, sName
);
257 * Create via ghostscript (gs) from the reference file for every page a JPEG file
260 * m_sOutputPath, m_sReferenceFile, m_sReferencePath
262 * return exit code from gs command
265 private static final String m_sGS_PageOutput
= "%04d";
267 private String
createJPEGsFromPostscript(String _sOutputPath
, String _sSourcePath
, String _sSourceFile
, int _nResolutionInDPI
)
269 FileHelper
.makeDirectories("", _sOutputPath
);
271 String sJPEGNameSchema
= getJPEGName(_sOutputPath
, _sSourceFile
, _nResolutionInDPI
, m_sGS_PageOutput
);
272 String sPostscriptOrPDFFile
= FileHelper
.appendPath(_sSourcePath
, _sSourceFile
);
273 String sGhostscriptEXE
= "gs";
274 if (OSHelper
.isWindows())
276 sGhostscriptEXE
= "gswin32c.exe";
277 String sGhostscriptEXE2
= (String
)param
.get("gs.exe");
278 if (sGhostscriptEXE2
!= null)
280 sGhostscriptEXE
= sGhostscriptEXE2
;
282 String sGhostscriptPath
= (String
)param
.get("gs.path");
283 if (sGhostscriptPath
!= null)
285 sGhostscriptEXE
= FileHelper
.appendPath(sGhostscriptPath
, sGhostscriptEXE
);
289 String
[] sCommandArray
=
295 "-r" + String
.valueOf(_nResolutionInDPI
),
297 "-sOutputFile=" + sJPEGNameSchema
,
304 // TODO: gives ghostscript an error we can handle?
305 ProcessHandler aHandler
= new ProcessHandler(sCommandArray
);
306 boolean bBackValue
= aHandler
.executeSynchronously();
307 assure("There seems to be a problem with ghostscript", bBackValue
, true);
308 int nExitCode
= aHandler
.getExitCode();
312 // TODO: return a real filename, due to the fact we don't know how much files are created, maybe better to return a list
313 int nPages
= countPages(sJPEGNameSchema
);
316 // return only a valid schema name if there at least one page.
317 sJPEGNameSchema
= "";
318 assure("Document '" + sPostscriptOrPDFFile
+ "' doesn't create pages.", false, true);
323 assure("There seems to be a problem with ghostscript and '" + sPostscriptOrPDFFile
+ "' exit code: " + nExitCode
, false, true);
324 GlobalLogWriter
.println("Warning: There seems to be a problem with '" + sGhostscriptEXE
+ "'...");
327 return sJPEGNameSchema
; // sNewJPEGFilename;
330 public static String
getFilenameForJPEGSchema(String _sJPEGSchema
, int _nPage
)
332 if (_nPage
< 1 || _nPage
> 9999)
336 final String sFilename
= util
.utils
.replaceAll13(_sJPEGSchema
, m_sGS_PageOutput
, StringHelper
.createValueString(_nPage
, 4));
341 * Return the number of really exists JPEG files
342 * Give a JPEG Schema which contains something like '%04d' which will be converted by a number between 0001 and 9999 always
343 * with leading '0' (zero)
344 * @param _sJPEGSchema
345 * @return number of really existing pages of a given JPEG schema
347 public int countPages(String _sJPEGSchema
)
350 if (_sJPEGSchema
.length() > 0)
352 // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
353 for (int i
=1;i
<10000;i
++)
355 String sNewJPEGFilename
= getFilenameForJPEGSchema(_sJPEGSchema
, i
);
356 if (FileHelper
.exists(sNewJPEGFilename
))
362 break; // stop file check