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 package graphical;
24 
25 import helper.OSHelper;
26 import helper.ProcessHandler;
27 import java.io.File;
28 import java.io.IOException;
29 
30 /**
31  * Helper class to interpret a jpg filename
32  */
33 class NameDPIPage
34 {
35 
36     String Name;
37     int DPI;
38     int Page;
39 
NameDPIPage(String _sName, int _nDPI, int _nPage)40     private NameDPIPage(String _sName, int _nDPI, int _nPage)
41     {
42         Name = _sName;
43         DPI = _nDPI;
44         Page = _nPage;
45     }
46 
interpret(String _sFilename)47     public static NameDPIPage interpret(String _sFilename)
48     {
49         String sBasename = FileHelper.getBasename(_sFilename);         // if exist a path, remove it
50         String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);  // remove extension (.jpg)
51 
52         // check if there exist a 'DPI_' at specific position
53         String sDPICheck = sNameNoSuffix.substring(sNameNoSuffix.length() - 8, sNameNoSuffix.length() - 4);
54         String sName;
55         int nDPI = -1;
56         int nPage = -1;
57         if (sDPICheck.equals("DPI_"))
58         {
59             // seems to be a generated filename by us.
60             int nDPIStart = sNameNoSuffix.lastIndexOf("_", sNameNoSuffix.length() - 8);
61             sName = sNameNoSuffix.substring(0, nDPIStart);
62             if (nDPIStart > 0)
63             {
64                 String sDPI = sNameNoSuffix.substring(nDPIStart + 1, sNameNoSuffix.length() - 8);
65                 try
66                 {
67                     nDPI = Integer.valueOf(sDPI).intValue();
68                 }
69                 catch (java.lang.NumberFormatException e)
70                 {
71                     GlobalLogWriter.println("DPI: Number format exception");
72                 }
73                 String sPage = sNameNoSuffix.substring(sNameNoSuffix.length() - 4);
74                 try
75                 {
76                     nPage = Integer.valueOf(sPage).intValue();
77                 }
78                 catch (java.lang.NumberFormatException e)
79                 {
80                     GlobalLogWriter.println("Page: Number format exception");
81                 }
82             }
83         }
84         else
85         {
86             sName = sNameNoSuffix;
87         }
88 
89         return new NameDPIPage(sName, nDPI, nPage);
90     }
91 }
92 
93 class CountNotXXXPixelsFromImage extends Thread
94 {
95 
96     private String m_sFilename;
97     protected int m_nValue;
98 
CountNotXXXPixelsFromImage(String _sFilename)99     CountNotXXXPixelsFromImage(String _sFilename)
100     {
101         m_sFilename = _sFilename;
102     }
103 
getValue()104     public int getValue()
105     {
106         return m_nValue;
107     }
108 
setValue(int _nValue)109     protected void setValue(int _nValue)
110     {
111         m_nValue = _nValue;
112     }
113 
getFilename()114     protected String getFilename()
115     {
116         return m_sFilename;
117     }
118 }
119 
120 class CountNotWhitePixelsFromImage extends CountNotXXXPixelsFromImage
121 {
122 
CountNotWhitePixelsFromImage(String _sFilename)123     CountNotWhitePixelsFromImage(String _sFilename)
124     {
125         super(_sFilename);
126     }
127 
run()128     public void run()
129     {
130         try
131         {
132             final int nNotWhiteCount = PixelCounter.countNotWhitePixelsFromImage(getFilename());
133             setValue(nNotWhiteCount);
134         }
135         catch (java.io.IOException e)
136         {
137             m_nValue = -1;
138         }
139     }
140 }
141 
142 class CountNotBlackPixelsFromImage extends CountNotXXXPixelsFromImage
143 {
144 
CountNotBlackPixelsFromImage(String _sFilename)145     CountNotBlackPixelsFromImage(String _sFilename)
146     {
147         super(_sFilename);
148     }
149 
run()150     public void run()
151     {
152         try
153         {
154             final int nNotBlackCount = PixelCounter.countNotBlackPixelsFromImage(getFilename());
155             setValue(nNotBlackCount);
156         }
157         catch (java.io.IOException e)
158         {
159             m_nValue = -1;
160         }
161     }
162 }
163 
164 /**
165  *
166  * @author ll93751
167  */
168 public class JPEGComparator extends EnhancedComplexTestCase
169 {
170     // @Override
171 
getTestMethodNames()172     public String[] getTestMethodNames()
173     {
174         return new String[]{"CompareJPEGvsJPEG"};
175     }
176     private Tolerance m_aTolerance;
177 
178     /**
179      * test function.
180      */
CompareJPEGvsJPEG()181     public void CompareJPEGvsJPEG()
182     {
183         GlobalLogWriter.set(log);
184         ParameterHelper aParam = new ParameterHelper(param);
185 
186         // run through all documents found in Inputpath
187         foreachJPEGcompareWithJPEG(aParam);
188     }
189 
checkOneFile(String _sDocumentName, String _sResult, ParameterHelper _aParams)190     public void checkOneFile(String _sDocumentName, String _sResult, ParameterHelper _aParams) throws OfficeException
191     {
192         // private void callEveryPictureInIniFile(IniFile _aIniFile, String _sSectionName, ParameterHelper _aParam)
193         // {
194         String sPath = FileHelper.getPath(_sDocumentName);
195         String sSectionName = FileHelper.getBasename(_sDocumentName);
196 
197         // take the build id out of the ini file in the reference file and put it into the current parameter helper
198         String sIniFileForRefBuildID = FileHelper.appendPath(sPath, sSectionName + ".ini");
199         IniFile aIniFileForRefBuildID = new IniFile(sIniFileForRefBuildID);
200         String sRefBuildID = aIniFileForRefBuildID.getValue("global", "buildid");
201         aIniFileForRefBuildID.close();
202 
203         _aParams.getTestParameters().put("RefBuildId", sRefBuildID);
204 
205         String sIniFile = FileHelper.appendPath(sPath, "index.ini");
206         IniFile aIniFile = new IniFile(sIniFile);
207         if (aIniFile.hasValue(sSectionName, "pages"))
208         {
209             // only which has 'pages' has also pictures
210             int nPages = aIniFile.getIntValue(sSectionName, "pages", 0);
211             String sJPEGSchema = aIniFile.getValue(sSectionName, "jpegschema");
212             int nTolerance = aIniFile.getIntValue(sSectionName, "tolerance", 0);
213             m_aTolerance = new Tolerance(nTolerance);
214             for (int i = 1; i <= nPages; i++)
215             {
216                 String sJPEGFilename = JPEGCreator.getFilenameForJPEGSchema(sJPEGSchema, i);
217                 // String sPath = FileHelper.getPath(_aParam.getInputPath());
218                 String sJPEGPath = FileHelper.getPath(sJPEGFilename);
219                 if (!sPath.equals(sJPEGPath))
220                 {
221                     GlobalLogWriter.println("Path where to find the index and where to file the JPEG pictures are not the same.");
222 
223                 }
224                 // String sEntry = FileHelper.appendPath(sPath, sSection);
225                 File aFile = new File(sJPEGFilename);
226                 assure("File '" + sJPEGFilename + "' doesn't exists.", aFile.exists(), true);
227                 if (aFile.exists())
228                 {
229                     GlobalLogWriter.println("Page: " + i);
230                     checkOnePicture(sJPEGFilename, _sResult, _aParams);
231                 }
232             }
233         }
234         else
235         {
236             GlobalLogWriter.println("The document '" + sSectionName + "' seems to have no picture representation.");
237         }
238 
239         String sResultIniFile = FileHelper.appendPath(_sResult, sSectionName);
240         evaluateResult(sResultIniFile, _aParams);
241     }
242 
evaluateResult(String _sDocument, ParameterHelper _aParams)243     private void evaluateResult(String _sDocument, ParameterHelper _aParams)
244     {
245         String sResultIniFile = _sDocument + ".ini";
246         File aFile = new File(sResultIniFile);
247         assure("Result file doesn't exists " + sResultIniFile, aFile.exists());
248 
249         int good = 0;
250         int bad = 0;
251         int ugly = 0;
252         int ok_status = 1; // 1=ok 2=bad 3=ugly
253 
254         IniFile aResultIniFile = new IniFile(sResultIniFile);
255         int nPages = aResultIniFile.getIntValue("global", "pages", 0);
256         for (int i = 0; i < nPages; i++)
257         {
258             String sCurrentPage = "page" + String.valueOf(i + 1);
259             int nPercent = aResultIniFile.getIntValue(sCurrentPage, "percent", -1);
260             if (nPercent == 0)
261             {
262                 good++;
263             }
264             else if (nPercent <= 5)
265             {
266                 bad++;
267                 ok_status = 2;
268             }
269             else
270             {
271                 ugly++;
272                 ok_status = 3;
273             }
274         }
275 
276         assure("Error: document doesn't contains pages", nPages > 0);
277 
278 // TODO: this information has to come out of the ini files
279         String sStatusRunThrough = "PASSED, ";
280         String sPassed = "OK";
281 
282         String sStatusMessage = "From " + nPages + " page(s) are: ";
283         String sGood = "";
284         String sBad = "";
285         String sUgly = "";
286 
287         if (good > 0)
288         {
289             sGood = " good:=" + good;
290             sStatusMessage += sGood;
291         }
292         if (bad > 0)
293         {
294             sBad = " bad:=" + bad;
295             sStatusMessage += sBad;
296         }
297         if (ugly > 0)
298         {
299             sUgly = " ugly:=" + ugly;
300             sStatusMessage += sUgly;
301         }
302 
303         // Failure matrix
304         //         0     1
305         // ugly    OK    FAILED
306         // bad     OK
307         // good    OK
308 
309         if (ugly > 0)
310         {
311             sPassed = "FAILED";
312         }
313         else
314         {
315             if (bad > 0)
316             {
317                 sPassed = "NEED A LOOK";
318             }
319             else
320             {
321                 sPassed = "OK";
322             }
323         }
324         sStatusRunThrough += sPassed;
325         aResultIniFile.insertValue("global", "state", sStatusRunThrough);
326         aResultIniFile.insertValue("global", "info", sStatusMessage);
327         aResultIniFile.close();
328 
329         _aParams.getTestParameters().put("current_state", sStatusRunThrough);
330         _aParams.getTestParameters().put("current_info", sStatusMessage);
331         _aParams.getTestParameters().put("current_ok_status", ok_status);
332 
333         // if we have a ugly page, we must return this as a FAILED STATUS in Log file!
334         assure("There exist pages marked as ugly.", ugly == 0);
335     }
336 
checkOnePicture(String _sDocumentName, String _sResult, ParameterHelper _aParams)337     private void checkOnePicture(String _sDocumentName, String _sResult, ParameterHelper _aParams)
338     {
339         GlobalLogWriter.println("JPEG: Compare difference between '" + _sDocumentName + "'  and '" + _sResult + "'");
340         File aResultFile = new File(_sResult);
341         if (aResultFile.isDirectory())
342         {
343             // result is just a directory, so we search for the basename of the source and take this.
344             String sBasename = FileHelper.getBasename(_sDocumentName);
345             String sResultFilename = FileHelper.appendPath(_sResult, sBasename);
346             aResultFile = new File(sResultFilename);
347             if (aResultFile.exists())
348             {
349                 // Original and Result exists
350                 String sInputPath = _aParams.getInputPath();
351                 if (sInputPath.toLowerCase().endsWith("index.ini"))
352                 {
353                     // special case
354                     // we want to get the buildid from the info file.
355                 }
356 
357                 compareJPEG(_sDocumentName, sResultFilename, _aParams);
358 
359             }
360             else
361             {
362                 String sResultFilenamePDF = util.utils.replaceAll13(sResultFilename, ".ps_", ".pdf_");
363                 File aResultPDFFile = new File(sResultFilenamePDF);
364                 if (aResultPDFFile.exists())
365                 {
366                     // Original and Result exists
367                     String sInputPath = _aParams.getInputPath();
368                     if (sInputPath.toLowerCase().endsWith("index.ini"))
369                     {
370                         // special case
371                         // we want to get the buildid from the info file.
372                     }
373 
374                     compareJPEG(_sDocumentName, sResultFilenamePDF, _aParams);
375                 }
376                 else
377                 {
378                     GlobalLogWriter.println("Warning: Result JPEG doesn't exists '" + sResultFilename + "'");
379                 }
380             }
381         }
382         else
383         {
384             // result is also a file
385             if (aResultFile.exists())
386             {
387                 compareJPEG(_sDocumentName, _sResult, _aParams);
388             }
389             else
390             {
391                 GlobalLogWriter.println("Warning: Result JPEG doesn't exists '" + _sResult + "'");
392             }
393         }
394     }
395 
396     /**
397      * compare 2 JPEGs, it is a need, that both _sDocumentName and _sResultFilename exist.
398      * @param _sDocumentName
399      * @param _sResult
400      * @param _aParams
401      */
compareJPEG(String _sDocumentName, String _sResult, ParameterHelper _aParams)402     private void compareJPEG(String _sDocumentName, String _sResult, ParameterHelper _aParams)
403     {
404         NameDPIPage aNameDPIPage = NameDPIPage.interpret(_sDocumentName);
405 
406         String sSourceBasename = FileHelper.getBasename(_sDocumentName);
407         String sSourcePath = FileHelper.getPath(_sDocumentName);
408         String sDestinationBasename = FileHelper.getBasename(_sResult);
409         String sDestinationPath = FileHelper.getPath(_sResult);
410 
411         if (!sSourcePath.equals(sDestinationPath))
412         {
413             // we want to have all in one Directory, Original, Reference and the Difference result.
414             // copy the original file to the reference path
415             String sNewSourceBasename = "Original_" + sSourceBasename;
416             // String sSource = FileHelper.appendPath(sSourcePath, sSourceBasename);
417             String sSource = _sDocumentName;
418             String sDestination = FileHelper.appendPath(sDestinationPath, sNewSourceBasename);
419             FileHelper.copy(sSource, sDestination);
420             sSourceBasename = sNewSourceBasename;
421             //
422             JPEGCreator.convertToNearSameFileWithWidth340(sDestination);
423         }
424         String sDifferenceBasename = "Difference_between_" + FileHelper.getNameNoSuffix(sSourceBasename) + "_and_" + FileHelper.getNameNoSuffix(sDestinationBasename) + ".jpg";
425         // String sDifferencePath = sDestinationPath;
426 
427         String sSource = FileHelper.appendPath(sDestinationPath, sSourceBasename);
428         String sDestination = FileHelper.appendPath(sDestinationPath, sDestinationBasename);
429         String sDifference = FileHelper.appendPath(sDestinationPath, sDifferenceBasename);
430         int nErr = compareJPEG(sSource, sDestination, sDifference);
431         if (nErr == 0 && FileHelper.exists(sDifference))
432         {
433             // check the difference, returns the count of different colors
434             // this means, 1=only one color, no differences found.
435             int nResult = identify(sDifference);
436             int nPercentColorDiffer = 0;
437 
438             String sResult = "YES";
439 
440             if (m_aTolerance != null)
441             {
442                 final int nAcceptedTolerance = m_aTolerance.getAccept();
443                 if (nResult <= nAcceptedTolerance)
444                 {
445                     nResult = 1;
446                     sResult = "IN TOLERANCE";
447                     GlobalLogWriter.println("The differences are in tolerance.");
448 
449                 }
450             }
451             if (nResult != 1)
452             {
453                 sResult = "NO";
454                 try
455                 {
456                     nPercentColorDiffer = estimateGfx(sSource, sDestination, sDifference);
457                 }
458                 catch (java.io.IOException e)
459                 {
460                     GlobalLogWriter.println("Can't estimate the different colors. " + e.getMessage());
461                 }
462             }
463 
464             // store the result in a result.ini file
465             String sResultFile = FileHelper.appendPath(sDestinationPath, aNameDPIPage.Name + ".ini");
466             int nPage = aNameDPIPage.Page;
467             if (nPage < 0)
468             {
469                 nPage = 0;
470             }
471             IniFile aResultIni = new IniFile(sResultFile);
472 
473             String[] aComment =
474             {
475                 "; This file is automatically created by a graphical.JPEGComparator run",
476                 "; ",
477                 "; If you see this file in a browser you may have forgotten to set the follows in the property file",
478                 "; " + PropertyName.DOC_COMPARATOR_HTML_OUTPUT_PREFIX + "=http://<computer>/gfxcmp_ui/cw.php?inifile=",
479                 "; Please check the documentation if you got confused.",
480                 "; ",
481                 "; "
482             };
483             aResultIni.insertFirstComment(aComment);
484 
485             // write down the global flags
486             int nMaxPage = Math.max(nPage, aResultIni.getIntValue("global", "pages", 0));
487             aResultIni.insertValue("global", "pages", nMaxPage);
488 
489             // INIoutput.writeValue("buildid", _sBuildID);
490             // INIoutput.writeValue("refbuildid", _sRefBuildID);
491             String sRefBuildId = (String) _aParams.getTestParameters().get("RefBuildId");
492             if (sRefBuildId == null)
493             {
494                 sRefBuildId = "";
495             }
496             aResultIni.insertValue("global", "refbuildid", sRefBuildId);
497 
498             aResultIni.insertValue("global", "diffdiff", "no");
499             aResultIni.insertValue("global", "basename", aNameDPIPage.Name);
500             aResultIni.insertValue("global", "dpi", aNameDPIPage.DPI);
501 
502             // write down flags for each page
503             String sSection = "page" + String.valueOf(nPage);
504 
505             aResultIni.insertValue(sSection, "oldgfx", sSource);
506             aResultIni.insertValue(sSection, "newgfx", sDestination);
507             aResultIni.insertValue(sSection, "diffgfx", sDifference);
508             aResultIni.insertValue(sSection, "percent", nPercentColorDiffer);
509             aResultIni.insertValue(sSection, "BM", "false");
510             aResultIni.insertValue(sSection, "result", sResult);
511 
512             aResultIni.close();
513         }
514     }
515 
516 //    // This creates a status for exact on document
517 //    static boolean createINIStatus(StatusHelper[] aList, String _sFilenamePrefix, String _sOutputPath, String _sAbsoluteInputFile, String _sBuildID, String _sRefBuildID)
518 //        {
519 //            // Status
520 //            String fs = System.getProperty("file.separator");
521 //            String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
522 //            String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
523 ////            String sHTMLFile = _sFilenamePrefix + sNameNoSuffix + ".html";
524 ////            HTMLOutputter HTMLoutput = HTMLOutputter.create(_sOutputPath, sHTMLFile, "", "");
525 ////            HTMLoutput.header(sNameNoSuffix);
526 ////  TODO: version info was fine
527 ////            HTMLoutput.checkSection(sBasename);
528 //            // Status end
529 //
530 //            String sINIFile = _sFilenamePrefix + sNameNoSuffix + ".ini";
531 //            INIOutputter INIoutput = INIOutputter.create(_sOutputPath, sINIFile, "", "");
532 //            INIoutput.createHeader();
533 ////  TODO: version info was fine
534 //
535 //            INIoutput.writeSection("global");
536 //            INIoutput.writeValue("pages", String.valueOf(aList.length));
537 //            INIoutput.writeValue("buildid", _sBuildID);
538 //            INIoutput.writeValue("refbuildid", _sRefBuildID);
539 //            INIoutput.writeValue("diffdiff", "no");
540 //            INIoutput.writeValue("basename", sBasename);
541 //
542 //            boolean bResultIsOk = true;          // result over all pages
543 //            for (int i=0;i<aList.length; i++)
544 //            {
545 //                INIoutput.writeSection("page" + String.valueOf(i + 1));   // list start at point 0, but this is page 1 and so on... current_page = (i + 1)
546 //                aList[i].printStatus();
547 //
548 //                boolean bCurrentResult = true;   // result over exact one page
549 //
550 //                int nCurrentDiffStatus = aList[i].nDiffStatus;
551 //
552 //                // check if the status is in a defined range
553 //                if (nCurrentDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES)
554 //                {
555 //                    // ok.
556 //                }
557 //                else if (nCurrentDiffStatus == StatusHelper.DIFF_DIFFERENCES_FOUND && aList[i].nPercent < 5)
558 //                {
559 //                    // ok.
560 //                }
561 //                else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_NO_PROBLEMS)
562 //                {
563 //                    // ok.
564 //                }
565 //                else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_DIFFERENCES_FOUND && aList[i].nPercent2 < 5)
566 //                {
567 //                    // ok.
568 //                }
569 //                else
570 //                {
571 //                    // failed.
572 //                    bCurrentResult = false; // logic: nDiff==0 = true if there is no difference
573 //                }
574 //
575 //                // Status
576 ////                HTMLoutput.checkLine(aList[i], bCurrentResult);
577 //                INIoutput.checkLine(aList[i], bCurrentResult);
578 //                bResultIsOk &= bCurrentResult;
579 //            }
580 //            // Status
581 ////            HTMLoutput.close();
582 //            INIoutput.close();
583 //            return bResultIsOk;
584 //        }
585     /**
586      * count how much pixel differ and between Old or New and the Difference graphics
587      *
588      * First, count the old graphics, then the new graphics due to the fact both should be equal
589      * it should be legal to take result from old or new. We take the graphics with less values.
590      *
591      * Second, count the difference graphics, now take the percent algorithm and
592      * build a percent value, which contain the number of different pixels as a percent value
593      *
594      * Interpretation:
595      * 0%    there is no difference
596      *
597      * &lt;100% Take a look into the difference graphics, maybe the difference graphics shows
598      *       text like outlined or the text is little bit move left, right up or down.
599      *
600      * &gt;>100% Yes it's possible that there is a difference more then 100%, maybe a font problem
601      *       between old and new graphics. The font of the new graphics is little bit bigger,
602      *       so the pixel count between old graphics and new graphics is twice the more.
603      *
604      * @param _sOldGfx path & name to the jpeg file (1)
605      * @param _sNewGfx path & name to the other jpeg file (2)
606      * @param _sDiffGfx path & name to the new difference file which shows the difference between (1) and (2)
607      * @return the count of different pixels
608      * @throws java.io.IOException if file access is not possible
609      */
estimateGfx(String _sOldGfx, String _sNewGfx, String _sDiffGfx)610     public static int estimateGfx(String _sOldGfx, String _sNewGfx, String _sDiffGfx)
611             throws java.io.IOException
612     {
613         TimeHelper a = new TimeHelper();
614         a.start();
615         // Count Pixels
616         final int nNotWhiteCount_OldGraphic = PixelCounter.countNotWhitePixelsFromImage(_sOldGfx);
617         final int nNotWhiteCount_NewGraphic = PixelCounter.countNotWhitePixelsFromImage(_sNewGfx);
618         final int nNotBlackCount_DiffGraphic = PixelCounter.countNotBlackPixelsFromImage(_sDiffGfx);
619 
620         // Count Pixels in different threads
621 //        CountNotWhitePixelsFromImage t1 = new CountNotWhitePixelsFromImage(_sOldGfx);
622 //        CountNotWhitePixelsFromImage t2 = new CountNotWhitePixelsFromImage(_sNewGfx);
623 //        CountNotBlackPixelsFromImage t3 = new CountNotBlackPixelsFromImage(_sDiffGfx);
624 //        t1.start();
625 //        t2.start();
626 //        t3.start();
627 //        try
628 //        {
629 //            t1.join();
630 //        }
631 //        catch (InterruptedException ex)
632 //        {
633 //            GlobalLogWriter.get().println("Thread 1 failed: " + ex.getMessage());
634 //        }
635 //        try
636 //        {
637 //            t2.join();
638 //        }
639 //        catch (InterruptedException ex)
640 //        {
641 //            GlobalLogWriter.get().println("Thread 2 failed: " + ex.getMessage());
642 //        }
643 //        try
644 //        {
645 //            t3.join();
646 //        }
647 //        catch (InterruptedException ex)
648 //        {
649 //            GlobalLogWriter.get().println("Thread 3 failed: " + ex.getMessage());
650 //        }
651 //        final int nNotWhiteCount_OldGraphic = t1.getValue();
652 //        final int nNotWhiteCount_NewGraphic = t2.getValue();
653 //        final int nNotBlackCount_DiffGraphic = t3.getValue();
654 
655         a.stop();
656         GlobalLogWriter.println("Thread Time is: " + a.getTime());
657 
658         int nMinNotWhiteCount = Math.min(nNotWhiteCount_NewGraphic, nNotWhiteCount_OldGraphic);
659 
660         // check if not zero
661         if (nMinNotWhiteCount == 0)
662         {
663             nMinNotWhiteCount = Math.max(nNotWhiteCount_NewGraphic, nNotWhiteCount_OldGraphic);
664             if (nMinNotWhiteCount == 0)
665             {
666                 nMinNotWhiteCount = 1;
667             }
668         }
669 
670         int nPercent = Math.abs(nNotBlackCount_DiffGraphic * 100 / nMinNotWhiteCount);
671         GlobalLogWriter.println("Graphics check, pixel based:" + String.valueOf(nPercent) + "% pixel differ ");
672         return nPercent;
673     }
674 
compareJPEG(String _sOldGfx, String _sNewGfx, String _sDiffGfx)675     private static int compareJPEG(String _sOldGfx, String _sNewGfx, String _sDiffGfx)
676     {
677         String sComposite = "composite";
678         if (OSHelper.isWindows())
679         {
680             sComposite = "composite.exe";
681             String sIMPath = (String) param.get("imagemagick.path");
682             if (sIMPath != null)
683             {
684                 sComposite = FileHelper.appendPath(sIMPath, sComposite);
685             }
686         }
687 
688         // String sCommand = sComposite + " -compose difference " +
689         //     StringHelper.doubleQuoteIfNeed(_sOldGfx) + " " +
690         //     StringHelper.doubleQuoteIfNeed(_sNewGfx) + " " +
691         //     StringHelper.doubleQuoteIfNeed(_sDiffGfx);
692 
693         String[] sCommandArray =
694         {
695             sComposite,
696             "-compose",
697             "difference",
698             _sOldGfx,
699             _sNewGfx,
700             _sDiffGfx
701         };
702 
703         ProcessHandler aHandler = new ProcessHandler(sCommandArray);
704         boolean bBackValue = aHandler.executeSynchronously();
705         int nExitCode = aHandler.getExitCode();
706         if (nExitCode != 0)
707         {
708             GlobalLogWriter.println("'" + sComposite + "' return with ");
709             String sBack = aHandler.getOutputText();
710             GlobalLogWriter.println("'" + sBack + "'");
711         }
712         else
713         {
714             // creates an extra smaller difference picture
715             File aDiffFile = new File(_sDiffGfx);
716             if (aDiffFile.exists())
717             {
718                 JPEGCreator.convertToNearSameFileWithWidth340(_sDiffGfx);
719             }
720         }
721         return nExitCode;
722     }
723 
724     /**
725      * wrapper for ImageMagick identify,
726      * function checks how many different colors a picture contains.
727      * if it's only one color (nResult==1), like background color, there is no difference.
728      */
identify(String _sDiffGfx)729     int identify(String _sDiffGfx)
730     {
731         int nResult = 0;
732         // would like to know what the meaning of %k is for ImageMagick's 'identify'
733         String sIM_Format = "%k";
734         // if (OSHelper.isWindows())
735         // {
736         //     sIM_Format = "%%k";
737         // }
738 
739         String sIdentify = "identify";
740         if (OSHelper.isWindows())
741         {
742             sIdentify = "identify.exe";
743             String sIMPath = (String) param.get("imagemagick.path");
744             if (sIMPath != null)
745             {
746                 sIdentify = FileHelper.appendPath(sIMPath, sIdentify);
747             }
748         }
749 
750         // String sCommand = sIdentify + " " + sIM_Format + " " + StringHelper.doubleQuoteIfNeed(_sDiffGfx);
751 
752         String[] sCommandArray =
753         {
754             sIdentify,
755             "-format",
756             sIM_Format,
757             _sDiffGfx
758         };
759         ProcessHandler aHandler = new ProcessHandler(sCommandArray);
760         boolean bBackValue = aHandler.executeSynchronously();
761         int nExitCode = aHandler.getExitCode();
762 
763         String sBack = aHandler.getOutputText();
764         GlobalLogWriter.println("'" + sBack + "'");
765 
766         // try to interpret the result, which we get as a String
767         try
768         {
769             int nIdx = sBack.indexOf("\n");
770             if (nIdx > 0)
771             {
772                 sBack = sBack.substring(0, nIdx);
773             }
774 
775             nResult = Integer.valueOf(sBack).intValue();
776         }
777         catch (java.lang.NumberFormatException e)
778         {
779             GlobalLogWriter.println("identify(): Number format exception");
780             nResult = 0;
781         }
782         return nResult;
783     }
784 //    public static void main(String [] _args)
785 //    {
786 //// give an index.ini file, ok
787 //// give a directory, where exist jpeg files ok
788 //// inputpath (given file) doesn't exists
789 //// give a jpeg file.
790 //
791 //        String args[] = {
792 //            "-TimeOut", "3600000",
793 //            "-tb", "java_complex",
794 //            "-o", "graphical.JPEGComparator",
795 //            "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\index.ini",
796 //            "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output2",
797 ////            "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\GroupReport.odt.pdf_180DPI_0001.jpg",
798 ////            "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output2\\Report1.odt.pdf_180DPI_0001.jpg",
799 //            "-DOC_COMPARATOR_HTML_OUTPUT_PREFIX", "http://so-gfxcmp-lin.germany.sun.com/gfxcmp_ui/cw.php?inifile=",
800 ////            "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "PDF",      /* default: "OOo" */
801 ////            "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "msoffice", /* default: "OOo" */
802 ////            "-OFFICE_VIEWABLE", "false",
803 ////            "-AppExecutionCommand", "\"C:/Programme/sun/staroffice 9/program/soffice.exe\"  -norestore -nocrashreport -accept=pipe,name=ll93751;urp;",
804 //            "-NoOffice"
805 //        };
806 //
807 //        org.openoffice.Runner.main(args);
808 //    }
809 }
810