bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / graphical / JPEGCreator.java
blobcb3ee7f34cc7b3d9af5fa85a1b53bad7446d29a5
1 /*
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 .
19 package graphical;
21 import helper.OSHelper;
22 import helper.ProcessHandler;
23 import helper.StringHelper;
24 import java.io.File;
26 public class JPEGCreator extends EnhancedComplexTestCase
28 public String[] getTestMethodNames()
30 return new String[]{"PostscriptOrPDFToJPEG"};
33 /**
34 * test function.
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);
54 if (nPages > 0)
56 createSmallPictures(sJPEGNameSchema);
58 // read out tolerance file
59 String sFileDir = FileHelper.getPath(_sDocumentName);
60 String sBasename = FileHelper.getBasename(_sDocumentName);
61 int nTolerance = 0;
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
68 aIniFile.close();
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);
80 aIniFile.close();
83 else
85 assure("There are no pages in document:'" + _sDocumentName + "', maybe document currupt?", false, true);
89 /**
90 * Create a lot of smaller and nicer Pictures of the big fat pages.
91 * Looks better
92 * @param _sJPEGSchema
94 public void createSmallPictures(String _sJPEGSchema)
96 ParameterHelper aParam = new ParameterHelper(param);
97 if (! aParam.createSmallPictures())
99 return;
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);
112 else
114 break; // stop file check
118 // return nPages;
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())
130 return;
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
142 Point wie bisher
143 Cubic schlecht, weil unscharf
145 Triangle ganz brauchbar (default?)
146 Catrom am besten
148 * @param _sFrom
149 * @param _To
151 private static void convertToWidth340(String _sFrom, String _To)
153 String sConvertEXE = "convert";
154 if (OSHelper.isLinuxIntel())
156 sConvertEXE = "convert";
158 if (OSHelper.isWindows())
160 // TODO!
161 // HACK Hard coded!
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 =
173 sConvertEXE,
174 _sFrom,
175 "-filter", "Catrom",
176 "-resize", "340x",
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
192 * @param _sFile
193 * @param _sAdditional
194 * @param _aParam
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);
204 if (aFile.exists())
206 String sAbsFile = aFile.getAbsolutePath();
207 if (!sAbsFile.equals(_sFile))
209 _sFile = sAbsFile;
212 else
214 GlobalLogWriter.println("File: '" + _sFile + "' doesn't exist.");
215 return "";
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.
239 return _sFile;
241 else
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);
253 return sJPEGName;
257 * Create via ghostscript (gs) from the reference file for every page a JPEG file
259 * MUST set:
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 =
291 sGhostscriptEXE,
292 "-dNOPROMPT",
293 "-dBATCH",
294 "-sDEVICE=jpeg",
295 "-r" + String.valueOf(_nResolutionInDPI),
296 "-dNOPAUSE",
297 "-sOutputFile=" + sJPEGNameSchema,
298 sPostscriptOrPDFFile
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();
310 if (nExitCode == 0)
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);
314 if (nPages == 0)
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);
321 else
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)
334 return "";
336 final String sFilename = util.utils.replaceAll13(_sJPEGSchema, m_sGS_PageOutput, StringHelper.createValueString(_nPage, 4));
337 return sFilename;
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)
349 int nPages = 0;
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))
358 nPages ++;
360 else
362 break; // stop file check
366 return nPages;