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 package graphical;
25
26 import helper.OSHelper;
27 import helper.ProcessHandler;
28 import helper.StringHelper;
29 import java.io.File;
30
31 /**
32 *
33 * @author ll93751
34 */
35 public class JPEGCreator extends EnhancedComplexTestCase
36 {
37 // @Override
getTestMethodNames()38 public String[] getTestMethodNames()
39 {
40 return new String[]{"PostscriptOrPDFToJPEG"};
41 }
42
43 /**
44 * test function.
45 */
PostscriptOrPDFToJPEG()46 public void PostscriptOrPDFToJPEG()
47 {
48 GlobalLogWriter.set(log);
49 ParameterHelper aParam = new ParameterHelper(param);
50
51 // run through all documents found in Inputpath
52 foreachPSorPDFinInputPath(aParam);
53 }
54
55
checkOneFile(String _sDocumentName, String _sResult, ParameterHelper _aParams)56 public void checkOneFile(String _sDocumentName, String _sResult, ParameterHelper _aParams) throws OfficeException
57 {
58 GlobalLogWriter.println(" Document: " + _sDocumentName);
59 GlobalLogWriter.println(" results: " + _sResult);
60 // IOffice aOffice = new Office(_aParams, _sResult);
61 // aOffice.start();
62 // aOffice.load(_sDocumentName);
63 // aOffice.storeAsPostscript();
64 // aOffice.close();
65 String sJPEGNameSchema = createJPEG(_sDocumentName, "", _aParams);
66
67 // store information only if jpeg files exists
68 int nPages = countPages(sJPEGNameSchema);
69 if (nPages > 0)
70 {
71 createSmallPictures(sJPEGNameSchema);
72
73 // read out tolerance file
74 String sFileDir = FileHelper.getPath(_sDocumentName);
75 String sBasename = FileHelper.getBasename(_sDocumentName);
76 int nTolerance = 0;
77 String sToleranceFile = FileHelper.appendPath(sFileDir, "tolerance.ini");
78 File aToleranceFile = new File(sToleranceFile);
79 if (aToleranceFile.exists())
80 {
81 IniFile aIniFile = new IniFile(sToleranceFile);
82 nTolerance = aIniFile.getIntValue(sBasename, "accept", 0); // default for all pages
83 aIniFile.close();
84 }
85
86 String sIndexFile = FileHelper.appendPath(_sResult, "index.ini");
87 File aIndexFile = new File(sIndexFile);
88 if (aIndexFile.exists())
89 {
90 // store only if an index file exists
91 IniFile aIniFile = new IniFile(sIndexFile);
92 aIniFile.insertValue(sBasename, "jpegschema", sJPEGNameSchema);
93 aIniFile.insertValue(sBasename, "pages", nPages);
94 aIniFile.insertValue(sBasename, "tolerance", nTolerance);
95 aIniFile.close();
96 }
97 }
98 else
99 {
100 assure("There are no pages in document:'" + _sDocumentName + "', maybe document currupt?", false, true);
101 }
102 }
103
104 /**
105 * Create a lot of smaller and nicer Pictures of the big fat pages.
106 * Looks better
107 * @param _sJPEGSchema
108 */
createSmallPictures(String _sJPEGSchema)109 public void createSmallPictures(String _sJPEGSchema)
110 {
111 ParameterHelper aParam = new ParameterHelper(param);
112 if (! aParam.createSmallPictures())
113 {
114 return;
115 }
116
117 int nPages = 0;
118 if (_sJPEGSchema.length() > 0)
119 {
120 // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
121 for (int i=1;i<10000;i++)
122 {
123 String sJPEGFilename = getFilenameForJPEGSchema(_sJPEGSchema, i);
124 if (FileHelper.exists(sJPEGFilename))
125 {
126 convertToNearSameFileWithWidth340(sJPEGFilename);
127 // m_aFileList.add(sNewJPEGFilename); // as long as the files exist, fill the array
128 nPages ++;
129 }
130 else
131 {
132 break; // stop file check
133 }
134 }
135 }
136 // return nPages;
137 }
138
139 /**
140 * convert a picture to a new picture with 340 pixel width.
141 * @param _sJPEGFilename
142 */
convertToNearSameFileWithWidth340(String _sJPEGFilename)143 public static void convertToNearSameFileWithWidth340(String _sJPEGFilename)
144 {
145 ParameterHelper aParam = new ParameterHelper(param);
146 if (! aParam.createSmallPictures())
147 {
148 return;
149 }
150 String sJPEGFilename = _sJPEGFilename.replaceAll("\\\\", "/");
151 // if (OSHelper.isWindows())
152 // {
153 // sJPEGFilename = sJPEGFilename.replaceAll("/", "\\\\");
154 // }
155 String sNewJPEGFilename;
156 sNewJPEGFilename = util.utils.replaceAll13(sJPEGFilename, ".jpg", "_w340.jpg");
157 convertToWidth340(sJPEGFilename, sNewJPEGFilename);
158 }
159
160 /**
161 * convert chart2_Regression.ods.ps_180DPI_0001.jpg -filter Catrom -resize
162 340x chart2_Regression.ods.ps_180DPI_0001_w340.jpg
163
164 Point wie bisher
165 Cubic schlecht, weil unscharf
166 ...
167 Triangle ganz brauchbar (default?)
168 Catrom am besten
169
170 * @param _sFrom
171 * @param _To
172 */
convertToWidth340(String _sFrom, String _To)173 private static void convertToWidth340(String _sFrom, String _To)
174 {
175 // int nResult = 0;
176
177 String sConvertEXE = "convert";
178 if (OSHelper.isLinuxIntel())
179 {
180 sConvertEXE = "convert";
181 }
182 if (OSHelper.isWindows())
183 {
184 // TODO!
185 // HACK Hard coded!
186 // sConvertEXE = "C:\\Programme\\ImageMagick-6.0.3-q8\\convert.exe";
187 sConvertEXE = "convert.exe";
188 String sConvertPath = (String)param.get("imagemagick.path");
189 if (sConvertPath != null)
190 {
191 sConvertEXE = FileHelper.appendPath(sConvertPath, sConvertEXE);
192 }
193 }
194
195 String[] sCommandArray =
196 {
197 sConvertEXE,
198 _sFrom,
199 "-filter", "Catrom",
200 "-resize", "340x",
201 _To
202 };
203 ProcessHandler aHandler = new ProcessHandler(sCommandArray);
204 boolean bBackValue = aHandler.executeSynchronously();
205 int nExitCode = aHandler.getExitCode();
206
207 String sBack = aHandler.getOutputText();
208 if (sBack.length() > 0)
209 {
210 GlobalLogWriter.println("'" + sBack + "'");
211 }
212 // try to interpret the result, which we get as a String
213 // try
214 // {
215 // int nIdx = sBack.indexOf("\n");
216 // if (nIdx > 0)
217 // {
218 // sBack = sBack.substring(0, nIdx);
219 // }
220 //
221 // nResult = Integer.valueOf(sBack).intValue();
222 // }
223 // catch(java.lang.NumberFormatException e)
224 // {
225 // GlobalLogWriter.get().println("Number format exception");
226 // nResult = 0;
227 // }
228 // return nResult;
229 }
230
231 /**
232 * create out of a given Postscript/PDF _sFile a list of JPEGs, one for every page
233 * @param _sFile
234 * @param _sAdditional
235 * @param _aParam
236 * @return the schema of the first jpeg name
237 */
createJPEG(String _sFile, String _sAdditional, ParameterHelper _aParam)238 String createJPEG(String _sFile, String _sAdditional, ParameterHelper _aParam)
239 {
240 if (_sFile.startsWith("file:///"))
241 {
242 _sFile = FileHelper.getSystemPathFromFileURL(_sFile);
243 }
244 File aFile = new File(_sFile);
245 if (aFile.exists())
246 {
247 String sAbsFile = aFile.getAbsolutePath();
248 if (!sAbsFile.equals(_sFile))
249 {
250 _sFile = sAbsFile;
251 }
252 }
253 else
254 {
255 GlobalLogWriter.println("File: '" + _sFile + "' doesn't exist.");
256 return "";
257 }
258 String sFileDir = FileHelper.getPath(_sFile);
259 String sBasename = FileHelper.getBasename(_sFile);
260 // String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
261
262 String sTmpDir = util.utils.getUsersTempDir();
263 if (_aParam.getOutputPath() != null)
264 {
265 sTmpDir = _aParam.getOutputPath();
266 }
267
268 String sJPEGNameSchema = "";
269 if (_sFile.toLowerCase().endsWith("ps") ||
270 _sFile.toLowerCase().endsWith("prn") ||
271 _sFile.toLowerCase().endsWith("pdf"))
272 {
273 // seems to be a Postscript of PDF file
274
275 sJPEGNameSchema = createJPEGsFromPostscript(sTmpDir, sFileDir, sBasename, _aParam.getResolutionInDPI());
276 }
277 else if (_sFile.toLowerCase().endsWith("jpg") ||
278 _sFile.toLowerCase().endsWith("jpeg"))
279 {
280 // do nothing, it's already a picture.
281 return _sFile;
282 }
283 else
284 {
285 // we assume it's an office document.
286 // Office files will not handled here, we need a PS or PDF file
287 }
288 return sJPEGNameSchema;
289 }
290
getJPEGName(String _sOutputPath, String _sBasename, int _nResolutionInDPI, String _sGS_PageOutput)291 private String getJPEGName(String _sOutputPath, String _sBasename, int _nResolutionInDPI, String _sGS_PageOutput)
292 {
293 // String fs = System.getProperty("file.separator");
294 String sName = _sBasename + "_" + String.valueOf(_nResolutionInDPI) + "DPI_" + _sGS_PageOutput + ".jpg";
295 String sJPEGName = FileHelper.appendPath(_sOutputPath, sName);
296 return sJPEGName;
297 }
298
299 /**
300 * Create via ghostscript (gs) from the reference file for every page a JPEG file
301 *
302 * MUST set:
303 * m_sOutputPath, m_sReferenceFile, m_sReferencePath
304 *
305 * return exit code from gs command
306 */
307
308 private static final String m_sGS_PageOutput = "%04d";
309
createJPEGsFromPostscript(String _sOutputPath, String _sSourcePath, String _sSourceFile, int _nResolutionInDPI)310 private String createJPEGsFromPostscript(String _sOutputPath, String _sSourcePath, String _sSourceFile, int _nResolutionInDPI)
311 {
312 FileHelper.makeDirectories("", _sOutputPath);
313
314 // create a jpeg from original prn
315 // String fs = System.getProperty("file.separator");
316
317 String sJPEGNameSchema = getJPEGName(_sOutputPath, _sSourceFile, _nResolutionInDPI, m_sGS_PageOutput);
318 String sPostscriptOrPDFFile = FileHelper.appendPath(_sSourcePath, _sSourceFile);
319 String sGhostscriptEXE = "gs";
320 if (OSHelper.isWindows())
321 {
322 sGhostscriptEXE = "gswin32c.exe";
323 String sGhostscriptEXE2 = (String)param.get("gs.exe");
324 if (sGhostscriptEXE2 != null)
325 {
326 sGhostscriptEXE = sGhostscriptEXE2;
327 }
328 String sGhostscriptPath = (String)param.get("gs.path");
329 if (sGhostscriptPath != null)
330 {
331 sGhostscriptEXE = FileHelper.appendPath(sGhostscriptPath, sGhostscriptEXE);
332 }
333 }
334
335 // String sCommand = sGhostscriptEXE + " -dNOPROMPT -dBATCH -sDEVICE=jpeg -r" + String.valueOf(_nResolutionInDPI) + " -dNOPAUSE -sOutputFile=" + StringHelper.doubleQuoteIfNeed(sJPGFilename) + " " + StringHelper.doubleQuoteIfNeed(sOriginalFile);
336 String[] sCommandArray =
337 {
338 sGhostscriptEXE,
339 "-dNOPROMPT",
340 "-dBATCH",
341 "-sDEVICE=jpeg",
342 "-r" + String.valueOf(_nResolutionInDPI),
343 "-dNOPAUSE",
344 "-sOutputFile=" + sJPEGNameSchema,
345 sPostscriptOrPDFFile
346
347
348
349 };
350 // System.out.println("Start Command array");
351 // try
352 // {
353 // Runtime.getRuntime().exec(sCommandArray);
354 // } catch (Exception e) {
355 // System.out.println("FAILED");
356 // }
357 // System.out.println("done");
358
359 // TODO: gives ghostscript an error we can handle?
360 ProcessHandler aHandler = new ProcessHandler(sCommandArray);
361 boolean bBackValue = aHandler.executeSynchronously();
362 assure("There seems to be a problem with ghostscript", bBackValue, true);
363 int nExitCode = aHandler.getExitCode();
364 // ArrayList m_aFileList = new ArrayList();
365
366 // String sJPEGNameSchema = "";
367 if (nExitCode == 0)
368 {
369 // TODO: return a real filename, due to the fact we don't know how much files are created, maybe better to return a list
370 int nPages = countPages(sJPEGNameSchema);
371 if (nPages == 0)
372 {
373 // return only a valid schema name if there at least one page.
374 sJPEGNameSchema = "";
375 assure("Document '" + sPostscriptOrPDFFile + "' doesn't create pages.", false, true);
376 }
377 }
378 else
379 {
380 assure("There seems to be a problem with ghostscript and '" + sPostscriptOrPDFFile + "' exit code: " + nExitCode, false, true);
381 GlobalLogWriter.println("Warning: There seems to be a problem with '" + sGhostscriptEXE + "'...");
382 }
383 // String[] aList = new String[m_aFileList.size()];
384 // aList = (String[])m_aFileList.toArray(aList);
385 return sJPEGNameSchema; // sNewJPEGFilename;
386 }
387
getFilenameForJPEGSchema(String _sJPEGSchema, int _nPage)388 public static String getFilenameForJPEGSchema(String _sJPEGSchema, int _nPage)
389 {
390 if (_nPage < 1 || _nPage > 9999)
391 {
392 return "";
393 }
394 final String sFilename = util.utils.replaceAll13(_sJPEGSchema, m_sGS_PageOutput, StringHelper.createValueString(_nPage, 4));
395 return sFilename;
396 }
397
398 /**
399 * Return the number of really exists JPEG files
400 * Give a JPEG Schema which contains something like '%04d' which will be converted by a number between 0001 and 9999 always
401 * with leading '0' (zero)
402 * @param _sJPEGSchema
403 * @return number of really existing pages of a given JPEG schema
404 */
countPages(String _sJPEGSchema)405 public int countPages(String _sJPEGSchema)
406 {
407 int nPages = 0;
408 if (_sJPEGSchema.length() > 0)
409 {
410 // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
411 for (int i=1;i<10000;i++)
412 {
413 String sNewJPEGFilename = getFilenameForJPEGSchema(_sJPEGSchema, i);
414 if (FileHelper.exists(sNewJPEGFilename))
415 {
416 // m_aFileList.add(sNewJPEGFilename); // as long as the files exist, fill the array
417 nPages ++;
418 }
419 else
420 {
421 break; // stop file check
422 }
423 }
424 }
425 return nPages;
426 }
427
428 // public static void main(String [] _args)
429 // {
430 //// DONE: give an index.ini file ok
431 //// String args[] = {
432 //// "-TimeOut", "3600000",
433 //// "-tb", "java_complex",
434 //// "-o", "graphical.JPEGCreator",
435 //// "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\index.ini",
436 //// "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output",
437 //// "-DOC_COMPARATOR_PRINT_MAX_PAGE", "9999",
438 //// "-DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION", "180",
439 //// "-DOC_COMPARATOR_HTML_OUTPUT_PREFIX", "http://so-gfxcmp-lin.germany.sun.com/gfxcmp_ui/cw.php?inifile=",
440 ////// "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "PDF", /* default: "OOo" */
441 ////// "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "msoffice", /* default: "OOo" */
442 ////// "-OFFICE_VIEWABLE", "false",
443 //// "-AppExecutionCommand", "\"C:/Programme/sun/staroffice 9/program/soffice.exe\" -norestore -nocrashreport -accept=pipe,name=ll93751;urp;",
444 //// "-NoOffice"
445 //// };
446 //
447 //// Done: give a directory, where exist pdf/ps files ok.
448 //// Done: inputpath (given file) doesn't exists, ok.
449 //// Done: give a ps/pdf file. ok.
450 //
451 // String args[] = {
452 // "-TimeOut", "3600000",
453 // "-tb", "java_complex",
454 // "-o", "graphical.JPEGCreator",
455 // "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\Names7.odt.pdf",
456 // "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output",
457 // "-DOC_COMPARATOR_PRINT_MAX_PAGE", "9999",
458 // "-DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION", "180",
459 // "-DOC_COMPARATOR_HTML_OUTPUT_PREFIX", "http://so-gfxcmp-lin.germany.sun.com/gfxcmp_ui/cw.php?inifile=",
460 //// "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "PDF", /* default: "OOo" */
461 //// "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "msoffice", /* default: "OOo" */
462 //// "-OFFICE_VIEWABLE", "false",
463 // "-AppExecutionCommand", "\"C:/Programme/sun/staroffice 9/program/soffice.exe\" -norestore -nocrashreport -accept=pipe,name=ll93751;urp;",
464 // "-NoOffice"
465 // };
466 //
467 // org.openoffice.Runner.main(args);
468 // }
469
470 }
471