merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / graphical / JPEGCreator.java
blob5b78976d65f6726cc1c21504e68ba7731a4d9464
1 /*
2 * ************************************************************************
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * Copyright 2008 by Sun Microsystems, Inc.
7 *
8 * OpenOffice.org - a multi-platform office productivity suite
9 *
10 * $RCSfile: JPEGCreator.java,v $
11 * $Revision: 1.1.2.2 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 * ***********************************************************************
33 package graphical;
35 import helper.OSHelper;
36 import helper.ProcessHandler;
37 import helper.StringHelper;
38 import java.io.File;
40 /**
42 * @author ll93751
44 public class JPEGCreator extends EnhancedComplexTestCase
46 // @Override
47 public String[] getTestMethodNames()
49 return new String[]{"PostscriptOrPDFToJPEG"};
52 /**
53 * test function.
55 public void PostscriptOrPDFToJPEG()
57 GlobalLogWriter.set(log);
58 ParameterHelper aParam = new ParameterHelper(param);
60 // run through all documents found in Inputpath
61 foreachPSorPDFinInputPath(aParam);
65 public void checkOneFile(String _sDocumentName, String _sResult, ParameterHelper _aParams) throws OfficeException
67 GlobalLogWriter.println("Document: " + _sDocumentName + " results: " + _sResult);
68 // IOffice aOffice = new Office(_aParams, _sResult);
69 // aOffice.start();
70 // aOffice.load(_sDocumentName);
71 // aOffice.storeAsPostscript();
72 // aOffice.close();
73 String sJPEGNameSchema = createJPEG(_sDocumentName, "", _aParams);
75 // store information only if jpeg files exists
76 int nPages = countPages(sJPEGNameSchema);
77 if (nPages > 0)
79 createSmallPictures(sJPEGNameSchema);
81 String sIndexFile = FileHelper.appendPath(_sResult, "index.ini");
82 File aIndexFile = new File(sIndexFile);
83 if (aIndexFile.exists())
85 // store only if an index file exists
86 IniFile aIniFile = new IniFile(sIndexFile);
87 String sBasename = FileHelper.getBasename(_sDocumentName);
88 aIniFile.insertValue(sBasename, "jpegschema", sJPEGNameSchema);
89 aIniFile.insertValue(sBasename, "pages", nPages);
90 aIniFile.close();
95 /**
96 * Create a lot of smaller and nicer Pictures of the big fat pages.
97 * Looks better
98 * @param _sJPEGSchema
100 public void createSmallPictures(String _sJPEGSchema)
102 int nPages = 0;
103 if (_sJPEGSchema.length() > 0)
105 // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
106 for (int i=1;i<10000;i++)
108 String sJPEGFilename = getFilenameForJPEGSchema(_sJPEGSchema, i);
109 if (FileHelper.exists(sJPEGFilename))
111 convertToNearSameFileWithWidth340(sJPEGFilename);
112 // m_aFileList.add(sNewJPEGFilename); // as long as the files exist, fill the array
113 nPages ++;
115 else
117 break; // stop file check
121 // return nPages;
125 * convert a picture to a new picture with 340 pixel width.
126 * @param _sJPEGFilename
128 public static void convertToNearSameFileWithWidth340(String _sJPEGFilename)
130 String sNewJPEGFilename;
131 sNewJPEGFilename = util.utils.replaceAll13(_sJPEGFilename, ".jpg", "_w340.jpg");
132 convertToWidth340(_sJPEGFilename, sNewJPEGFilename);
136 * convert chart2_Regression.ods.ps_180DPI_0001.jpg -filter Catrom -resize
137 340x chart2_Regression.ods.ps_180DPI_0001_w340.jpg
139 Point wie bisher
140 Cubic schlecht, weil unscharf
142 Triangle ganz brauchbar (default?)
143 Catrom am besten
145 * @param _sFrom
146 * @param _To
148 private static void convertToWidth340(String _sFrom, String _To)
150 // int nResult = 0;
152 String sConvertEXE = "convert";
153 if (OSHelper.isLinuxIntel())
155 sConvertEXE = "convert";
157 if (OSHelper.isWindows())
159 sConvertEXE = "convert.exe";
162 String[] sCommandArray =
164 sConvertEXE,
165 _sFrom,
166 "-filter", "Catrom",
167 "-resize", "340x",
170 ProcessHandler aHandler = new ProcessHandler(sCommandArray);
171 boolean bBackValue = aHandler.executeSynchronously();
172 int nExitCode = aHandler.getExitCode();
174 String sBack = aHandler.getOutputText();
175 if (sBack.length() > 0)
177 GlobalLogWriter.get().println("'" + sBack + "'");
179 // try to interpret the result, which we get as a String
180 // try
181 // {
182 // int nIdx = sBack.indexOf("\n");
183 // if (nIdx > 0)
184 // {
185 // sBack = sBack.substring(0, nIdx);
186 // }
188 // nResult = Integer.valueOf(sBack).intValue();
189 // }
190 // catch(java.lang.NumberFormatException e)
191 // {
192 // GlobalLogWriter.get().println("Number format exception");
193 // nResult = 0;
194 // }
195 // return nResult;
199 * create out of a given Postscript/PDF _sFile a list of JPEGs, one for every page
200 * @param _sFile
201 * @param _sAdditional
202 * @param _aParam
203 * @return the schema of the first jpeg name
205 String createJPEG(String _sFile, String _sAdditional, ParameterHelper _aParam)
207 if (_sFile.startsWith("file:///"))
209 _sFile = FileHelper.getSystemPathFromFileURL(_sFile);
211 File aFile = new File(_sFile);
212 if (aFile.exists())
214 String sAbsFile = aFile.getAbsolutePath();
215 if (!sAbsFile.equals(_sFile))
217 _sFile = sAbsFile;
220 else
222 GlobalLogWriter.get().println("File: '" + _sFile + "' doesn't exist.");
223 return "";
225 String sFileDir = FileHelper.getPath(_sFile);
226 String sBasename = FileHelper.getBasename(_sFile);
227 // String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
229 String sTmpDir = util.utils.getUsersTempDir();
230 if (_aParam.getOutputPath() != null)
232 sTmpDir = _aParam.getOutputPath();
235 String sJPEGNameSchema = "";
236 if (_sFile.toLowerCase().endsWith("ps") ||
237 _sFile.toLowerCase().endsWith("prn") ||
238 _sFile.toLowerCase().endsWith("pdf"))
240 // seems to be a Postscript of PDF file
242 sJPEGNameSchema = createJPEGsFromPostscript(sTmpDir, sFileDir, sBasename, _aParam.getResolutionInDPI());
244 else if (_sFile.toLowerCase().endsWith("jpg") ||
245 _sFile.toLowerCase().endsWith("jpeg"))
247 // do nothing, it's already a picture.
248 return _sFile;
250 else
252 // we assume it's an office document.
253 // Office files will not handled here, we need a PS or PDF file
255 return sJPEGNameSchema;
258 private String getJPEGName(String _sOutputPath, String _sBasename, int _nResolutionInDPI, String _sGS_PageOutput)
260 // String fs = System.getProperty("file.separator");
261 String sName = _sBasename + "_" + String.valueOf(_nResolutionInDPI) + "DPI_" + _sGS_PageOutput + ".jpg";
262 String sJPEGName = FileHelper.appendPath(_sOutputPath, sName);
263 return sJPEGName;
267 * Create via ghostscript (gs) from the reference file for every page a JPEG file
269 * MUST set:
270 * m_sOutputPath, m_sReferenceFile, m_sReferencePath
272 * return exit code from gs command
275 private static final String m_sGS_PageOutput = "%04d";
277 private String createJPEGsFromPostscript(String _sOutputPath, String _sSourcePath, String _sSourceFile, int _nResolutionInDPI)
279 FileHelper.makeDirectories("", _sOutputPath);
281 // create a jpeg from original prn
282 // String fs = System.getProperty("file.separator");
284 String sJPEGNameSchema = getJPEGName(_sOutputPath, _sSourceFile, _nResolutionInDPI, m_sGS_PageOutput);
285 String sPostscriptOrPDFFile = FileHelper.appendPath(_sSourcePath, _sSourceFile);
286 String sGhostscriptEXE = "gs";
287 if (OSHelper.isWindows())
289 sGhostscriptEXE = "gswin32c.exe";
292 // String sCommand = sGhostscriptEXE + " -dNOPROMPT -dBATCH -sDEVICE=jpeg -r" + String.valueOf(_nResolutionInDPI) + " -dNOPAUSE -sOutputFile=" + StringHelper.doubleQuoteIfNeed(sJPGFilename) + " " + StringHelper.doubleQuoteIfNeed(sOriginalFile);
293 String[] sCommandArray =
295 sGhostscriptEXE,
296 "-dNOPROMPT",
297 "-dBATCH",
298 "-sDEVICE=jpeg",
299 "-r" + String.valueOf(_nResolutionInDPI),
300 "-dNOPAUSE",
301 "-sOutputFile=" + sJPEGNameSchema,
302 sPostscriptOrPDFFile
307 // System.out.println("Start Command array");
308 // try
309 // {
310 // Runtime.getRuntime().exec(sCommandArray);
311 // } catch (Exception e) {
312 // System.out.println("FAILED");
313 // }
314 // System.out.println("done");
316 // TODO: gives ghostscript an error we can handle?
317 ProcessHandler aHandler = new ProcessHandler(sCommandArray);
318 boolean bBackValue = aHandler.executeSynchronously();
319 assure("There seems to be a problem with ghostscript", bBackValue, true);
320 int nExitCode = aHandler.getExitCode();
321 // ArrayList m_aFileList = new ArrayList();
323 // String sJPEGNameSchema = "";
324 if (nExitCode == 0)
326 // TODO: return a real filename, due to the fact we don't know how much files are created, maybe better to return a list
327 int nPages = countPages(sJPEGNameSchema);
328 if (nPages == 0)
330 // return only a valid schema name if there at least one page.
331 sJPEGNameSchema = "";
334 else
336 assure("There seems to be a problem with ghostscript and '" + sPostscriptOrPDFFile + "' exit code: " + nExitCode, false, true);
337 GlobalLogWriter.println("Warning: There seems to be a problem with '" + sGhostscriptEXE + "'...");
339 // String[] aList = new String[m_aFileList.size()];
340 // aList = (String[])m_aFileList.toArray(aList);
341 return sJPEGNameSchema; // sNewJPEGFilename;
344 public static String getFilenameForJPEGSchema(String _sJPEGSchema, int _nPage)
346 if (_nPage < 1 || _nPage > 9999)
348 return "";
350 final String sFilename = util.utils.replaceAll13(_sJPEGSchema, m_sGS_PageOutput, StringHelper.createValueString(_nPage, 4));
351 return sFilename;
355 * Return the number of really exists JPEG files
356 * Give a JPEG Schema which contains something like '%04d' which will be converted by a number between 0001 and 9999 always
357 * with leading '0' (zero)
358 * @param _sJPEGSchema
359 * @return number of really existing pages of a given JPEG schema
361 public int countPages(String _sJPEGSchema)
363 int nPages = 0;
364 if (_sJPEGSchema.length() > 0)
366 // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
367 for (int i=1;i<10000;i++)
369 String sNewJPEGFilename = getFilenameForJPEGSchema(_sJPEGSchema, i);
370 if (FileHelper.exists(sNewJPEGFilename))
372 // m_aFileList.add(sNewJPEGFilename); // as long as the files exist, fill the array
373 nPages ++;
375 else
377 break; // stop file check
381 return nPages;
384 public static void main(String [] _args)
386 // DONE: give an index.ini file ok
387 // String args[] = {
388 // "-TimeOut", "3600000",
389 // "-tb", "java_complex",
390 // "-o", "graphical.JPEGCreator",
391 // "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\index.ini",
392 // "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output",
393 // "-DOC_COMPARATOR_PRINT_MAX_PAGE", "9999",
394 // "-DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION", "180",
395 // "-DOC_COMPARATOR_HTML_OUTPUT_PREFIX", "http://so-gfxcmp-lin.germany.sun.com/gfxcmp_ui/cw.php?inifile=",
396 //// "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "PDF", /* default: "OOo" */
397 //// "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "msoffice", /* default: "OOo" */
398 //// "-OFFICE_VIEWABLE", "false",
399 // "-AppExecutionCommand", "\"C:/Programme/sun/staroffice 9/program/soffice.exe\" -norestore -nocrashreport -accept=pipe,name=ll93751;urp;",
400 // "-NoOffice"
401 // };
403 // Done: give a directory, where exist pdf/ps files ok.
404 // Done: inputpath (given file) doesn't exists, ok.
405 // Done: give a ps/pdf file. ok.
407 String args[] = {
408 "-TimeOut", "3600000",
409 "-tb", "java_complex",
410 "-o", "graphical.JPEGCreator",
411 "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\Names7.odt.pdf",
412 "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output",
413 "-DOC_COMPARATOR_PRINT_MAX_PAGE", "9999",
414 "-DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION", "180",
415 "-DOC_COMPARATOR_HTML_OUTPUT_PREFIX", "http://so-gfxcmp-lin.germany.sun.com/gfxcmp_ui/cw.php?inifile=",
416 // "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "PDF", /* default: "OOo" */
417 // "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "msoffice", /* default: "OOo" */
418 // "-OFFICE_VIEWABLE", "false",
419 "-AppExecutionCommand", "\"C:/Programme/sun/staroffice 9/program/soffice.exe\" -norestore -nocrashreport -accept=pipe,name=ll93751;urp;",
420 "-NoOffice"
423 org.openoffice.Runner.main(args);