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 convwatch;
25 
26 
27 import helper.ProcessHandler;
28 import java.io.File;
29 import convwatch.PixelCounter;
30 import convwatch.StatusHelper;
31 import helper.OSHelper;
32 import helper.StringHelper;
33 import java.util.ArrayList;
34 import util.utils;
35 
36 // -----------------------------------------------------------------------------
37 // --------------------------------- PRNCompare ---------------------------------
38 
39 // class DifferenceType
40 // {
41 //     final static int NO_DIFFERENCE = 1;
42 //     final static int DIFFERENCE = 2;
43 //
44 //     public int nValue = NO_DIFFERENCE;
45 //     public boolean equals(int _n)
46 //         {
47 //             if ( _n == nValue ) return true;
48 //             return false;
49 //         }
50 // }
51 
52 // -----------------------------------------------------------------------------
53 public class PRNCompare
54 {
55     // OSHelper m_aHelper;
56     String fs;
57 
PRNCompare()58     public PRNCompare()
59         {
60             // m_aHelper = new OSHelper();
61             fs = System.getProperty("file.separator");
62         }
63 
executeSynchronously(String _sCommand)64     String executeSynchronously(String _sCommand)
65         {
66             // System.out.println(_sCommand);
67 
68             ProcessHandler aHandler = new ProcessHandler(_sCommand);
69             boolean bBackValue = aHandler.executeSynchronously();
70 
71             String sText = aHandler.getOutputText();
72 
73             GlobalLogWriter.get().println("Exitcode: " + String.valueOf(aHandler.getExitCode()));
74             return sText;
75         }
76 
77 
getMaxNumOfFileEntry(String _sDirectory, String _sBasename)78     int getMaxNumOfFileEntry(String _sDirectory, String _sBasename)
79         {
80 //  TODO: check if directory exist should be done earlier
81             File aDirectory = new File(_sDirectory);
82             File[] aDirList = aDirectory.listFiles(
83                 /*
84                   new FileFilter() {
85                   boolean accept(File filename)
86                   {
87                   if (filename.getName().endsWith("jpg"))
88                   return true;
89                   return false;
90                   }
91                   } */
92                 );
93 
94             int nMaxNumber = 0;
95             for(int n = 0; n<aDirList.length ; n++)
96             {
97                 String sDirEntry = aDirList[n].getName();
98                 if (sDirEntry.endsWith(".jpg"))
99                 {
100                     int nNum = nMaxNumber;
101 
102                     if (sDirEntry.startsWith(_sBasename))
103                     {
104                         // System.out.println(sDirEntry);
105                         int nJpgIdx = sDirEntry.lastIndexOf(".jpg");
106                         String sValue = sDirEntry.substring(_sBasename.length(), nJpgIdx);
107                         int nValue = 0;
108                         try
109                         {
110                             nValue = Integer.valueOf(sValue).intValue();
111                         }
112                         catch(java.lang.NumberFormatException e)
113                         {
114                         }
115 
116                         // System.out.println(nValue);
117                         nNum = nValue;
118                     }
119 
120                     if (nNum > nMaxNumber)
121                     {
122                         nMaxNumber = nNum;
123                     }
124                 }
125             }
126             return nMaxNumber;
127         }
128 
129     String m_sInputPath;
130     String m_sReferencePath;
131     String m_sOutputPath;
132     String m_sDocFile;
133     String m_sReferenceFile;
134     String m_sPostScriptFile;
135     // String m_sOldDiff;
136     int m_nMaxPages = 0;
137     int m_nResolutionInDPI = 0;
138     TriState m_tUseBorderMove;
139     String m_sDocumentType;
140 
setInputPath(String _sInputPath)141     public void setInputPath(String _sInputPath) { m_sInputPath = _sInputPath; }
142 
setReferencePath(String _sReferencePath)143     public void setReferencePath(String _sReferencePath) { m_sReferencePath = _sReferencePath; }
144 
setOutputPath(String _sOutPath)145     public void setOutputPath(String _sOutPath) { m_sOutputPath = _sOutPath; }
146 
setDocFile(String _sDocFile)147     public void setDocFile(String _sDocFile) { m_sDocFile = _sDocFile;}
148 
setReferenceFile(String _sPRNFile)149     public void setReferenceFile(String _sPRNFile){ m_sReferenceFile = _sPRNFile;}
150 
setPostScriptFile(String _sPSFile)151     public void setPostScriptFile(String _sPSFile){ m_sPostScriptFile = _sPSFile;}
152 
setBorderMove(TriState _b)153     public void setBorderMove(TriState _b) {m_tUseBorderMove = _b;}
getBorderMove()154     public TriState getBorderMove() {return m_tUseBorderMove;}
155     // public void setOldDiffPath(String _sOldDiff)
156     //     {
157     //         m_sOldDiff = _sOldDiff;
158     //     }
setMaxPages(int _n)159     public void setMaxPages(int _n) {m_nMaxPages = _n;}
getMaxPages()160     int getMaxPages() {return m_nMaxPages;}
161 
setResolutionInDPI(int _n)162     public void setResolutionInDPI(int _n) {m_nResolutionInDPI = _n;}
getResolutionInDPI()163     int getResolutionInDPI() {return m_nResolutionInDPI;}
164 
setDocumentType(String _sTypeName)165     public void setDocumentType(String _sTypeName)
166         {
167             m_sDocumentType = _sTypeName;
168         }
169 
170     // -----------------------------------------------------------------------------
171 
compare()172     public StatusHelper[] compare()
173         {
174             createJPEGFromPostscript(m_sOutputPath, m_sReferencePath, m_sReferenceFile, getResolutionInDPI());
175 //  TODO: Assume, that Postscript is already in the OutputPath, this may change.
176             createJPEGFromPostscript(m_sOutputPath, m_sOutputPath, m_sPostScriptFile, getResolutionInDPI());
177             StatusHelper[] aList = createDiffs(m_sOutputPath,
178                                                m_sOutputPath, m_sReferenceFile,
179                                                m_sOutputPath, m_sPostScriptFile,
180                                                getMaxPages(), m_tUseBorderMove);
181 
182 //  TODO: Rename?
183 
184             return aList;
185         }
186 
compare_new()187     public StatusHelper[] compare_new()
188         {
189             String[] aRefList = createJPEGFromPostscript(m_sOutputPath, m_sReferencePath, m_sReferenceFile, getResolutionInDPI());
190 //  TODO: Assume, that Postscript is already in the OutputPath, this may change.
191             String[] aPSList = createJPEGFromPostscript(m_sOutputPath, m_sOutputPath, m_sPostScriptFile, getResolutionInDPI());
192             StatusHelper[] aList = createDiffs(m_sOutputPath,
193                                                aRefList,
194                                                aPSList,
195                                                getMaxPages(), m_tUseBorderMove);
196 
197             return aList;
198         }
199 
getJPEGName(String _sOutputPath, String _sBasename, String _sGS_PageOutput)200     static String getJPEGName(String _sOutputPath, String _sBasename, String _sGS_PageOutput)
201         {
202             String fs = System.getProperty("file.separator");
203 
204             String sJPEGName = _sOutputPath + fs + _sBasename + _sGS_PageOutput + ".jpg";
205             return sJPEGName;
206         }
207 
208     /**
209      * Create via ghostscript (gs) from the reference file for every page a JPEG file
210      *
211      * MUST set:
212      * m_sOutputPath, m_sReferenceFile, m_sReferencePath
213      *
214      * return exit code from gs command
215      */
createJPEGFromPostscript(String _sOutputPath, String _sSourcePath, String _sSourceFile, int _nResolutionInDPI)216     public static String[] createJPEGFromPostscript(String _sOutputPath, String _sSourcePath, String _sSourceFile, int _nResolutionInDPI)
217         {
218             String sGS_PageOutput = "%04d";
219             // if ( OSHelper.isWindows() )
220             // {
221             //     sGS_PageOutput = "%%d";
222             // }
223 
224             FileHelper.makeDirectories("", _sOutputPath);
225 
226             // create a jpeg from original prn
227             String fs = System.getProperty("file.separator");
228 
229             String sJPGFilename = getJPEGName(_sOutputPath, _sSourceFile, sGS_PageOutput);
230             String sOriginalFile = _sSourcePath + fs + _sSourceFile;
231             String sCommand;
232             String sGS = "gs";
233             if (OSHelper.isWindows())
234             {
235                 sGS = "gswin32c.exe";
236             }
237 
238             sCommand = sGS + " -dNOPROMPT -dBATCH -sDEVICE=jpeg -r" + String.valueOf(_nResolutionInDPI) + " -dNOPAUSE -sOutputFile=" + StringHelper.doubleQuoteIfNeed(sJPGFilename) + " " + StringHelper.doubleQuoteIfNeed(sOriginalFile);
239             String[] sCommandArray =
240                 {
241                     sGS,
242                     "-dNOPROMPT",
243                     "-dBATCH",
244                     "-sDEVICE=jpeg",
245                     "-r" + String.valueOf(_nResolutionInDPI),
246                     "-dNOPAUSE",
247                     "-sOutputFile=" + sJPGFilename,
248                     sOriginalFile
249                 };
250             // System.out.println("Start Command array");
251             // try
252             // {
253             //     Runtime.getRuntime().exec(sCommandArray);
254             // } catch (Exception e) {
255             //     System.out.println("FAILED");
256             // }
257             // System.out.println("done");
258 
259             ProcessHandler aHandler = new ProcessHandler(sCommandArray);
260             boolean bBackValue = aHandler.executeSynchronously();
261 
262             // TODO: return a real filename, due to the fact we don't know how much files are created, maybe better to return a list
263 
264             ArrayList m_aFileList = new ArrayList();
265             for (int i=1;i<9999;i++)
266             {
267                 String sNewJPEGFilename = utils.replaceAll13(sJPGFilename, sGS_PageOutput, StringHelper.createValueString(i, 4));
268                 if (FileHelper.exists(sNewJPEGFilename))
269                 {
270                     m_aFileList.add(sNewJPEGFilename); // as long as the files exist, fill the array
271                 }
272                 else
273                 {
274                     break;                             // stop file check
275                 }
276             }
277             String[] aList = new String[m_aFileList.size()];
278             aList = (String[])m_aFileList.toArray(aList);
279             return aList; // sNewJPEGFilename;
280         }
281 
282     /**
283      * Create Difference Files from the JPEGs
284      * parameter, how much difference files should create
285      *
286      * m_sPostScriptFile
287      * m_sReferenceFile
288      * m_sOutputPath
289      */
createDiffs(String _sOutputPath, String _sSourcePath1, String _sSourceFile1, String _sSourcePath2, String _sSourceFile2, int _nMaxDiffs, TriState _tUseBorderMove)290     public StatusHelper[] createDiffs(String _sOutputPath, String _sSourcePath1, String _sSourceFile1, String _sSourcePath2, String _sSourceFile2, int _nMaxDiffs, TriState _tUseBorderMove)
291         {
292             if (_nMaxDiffs < 1)
293             {
294                 _nMaxDiffs = 1;
295             }
296 
297             String sS1Basename = FileHelper.getBasename(_sSourceFile1);
298             String sS2Basename = FileHelper.getBasename(_sSourceFile2);
299 
300             // count, from which file (jpegs) exist more, take the less one
301             // more are not compareable
302             int nS1_Files = getMaxNumOfFileEntry(_sSourcePath1, sS1Basename);
303             int nS2_Files = getMaxNumOfFileEntry(_sSourcePath2, sS2Basename);
304 
305             // System.out.println("count of s1 files " + String.valueOf(nS1_Files));
306             // System.out.println("count of s2 files " + String.valueOf(nS2_Files));
307 
308             // take the min of both
309             int nMin = Math.min(nS1_Files, nS2_Files);
310             nMin = Math.min(nMin, _nMaxDiffs);
311 
312             StatusHelper[] aList = new StatusHelper[nMin];
313 
314 //  TODO: if both document do not have same page count, produce an error
315             // System.out.println("min of both: " + String.valueOf(nMin));
316 
317             int nStatusIndex = 0;
318             for (int i=1;i<=nMin;i++)
319             {
320                 String sOldGfx =  getJPEGName(_sSourcePath1, sS1Basename, StringHelper.createValueString(i, 4));
321                 String sNewGfx =  getJPEGName(_sSourcePath2, sS2Basename, StringHelper.createValueString(i, 4));
322                 String sDiffGfx_ = getJPEGName(_sOutputPath, sS1Basename + ".diff", StringHelper.createValueString(i, 4));
323 
324 
325                 String sDiffGfx = compareJPEGs(sOldGfx, sNewGfx, sDiffGfx_);
326                 StatusHelper aStatus = new StatusHelper(sOldGfx, sNewGfx, sDiffGfx);
327 
328                 // if (FileHelper.exists(sDiffGfx))
329                 if (sDiffGfx.length() > 0)
330                 {
331                     int nResult = identify(sDiffGfx);
332                     if (nResult == 1)
333                     {
334                         aStatus.nDiffStatus = StatusHelper.DIFF_NO_DIFFERENCES;
335                     }
336                     else
337                     {
338                         try
339                         {
340                             int nPercent = estimateGfx(sOldGfx, sNewGfx, sDiffGfx);
341                             aStatus.nDiffStatus = StatusHelper.DIFF_DIFFERENCES_FOUND;
342                             aStatus.nPercent = nPercent;
343 
344                             // GlobalLogWriter.get().println("Hello World:  Percent:= " + nPercent);
345                             // GlobalLogWriter.get().println("Hello World: TriState:= " + _tUseBorderMove.intValue());
346                             // GlobalLogWriter.get().println("Hello World:  DocType:= " + m_sDocumentType);
347 
348 // TODO: insert here the new BorderRemover if the percentage value is creater than 75%
349                             if (nPercent > 75 &&
350                                 ((_tUseBorderMove == TriState.TRUE ) ||
351                                  ((_tUseBorderMove == TriState.UNSET) &&
352                                   m_sDocumentType.indexOf("MS PowerPoint") > 0)))
353                             {
354                                 setBorderMove(TriState.TRUE);
355 
356                                 String sOld_BM_Gfx =  getJPEGName(_sSourcePath1, sS1Basename + ".BM", StringHelper.createValueString(i, 4));
357                                 String sNew_BM_Gfx =  getJPEGName(_sSourcePath2, sS2Basename + ".BM", StringHelper.createValueString(i, 4));
358                                 String sDiff_BM_Gfx_ = getJPEGName(_sOutputPath, sS1Basename + ".diff.BM", StringHelper.createValueString(i, 4));
359                                 aStatus.setFilesForBorderMove(sOld_BM_Gfx, sNew_BM_Gfx, sDiff_BM_Gfx_);
360                                 try
361                                 {
362                                     BorderRemover a = new BorderRemover();
363                                     a.createNewImageWithoutBorder(sOldGfx, sOld_BM_Gfx);
364                                     a.createNewImageWithoutBorder(sNewGfx, sNew_BM_Gfx);
365 
366                                     String sDiff_BM_Gfx = compareJPEGs( sOld_BM_Gfx, sNew_BM_Gfx, sDiff_BM_Gfx_);
367 
368                                     // if (FileHelper.exists(sDiff_BM_Gfx))
369                                     if (sDiff_BM_Gfx.length() > 0)
370                                     {
371                                         nResult = identify(sDiff_BM_Gfx);
372                                         if (nResult == 1)
373                                         {
374                                             aStatus.nDiffStatus = StatusHelper.DIFF_AFTER_MOVE_DONE_NO_PROBLEMS;
375                                             aStatus.nPercent2 = 0;
376                                         }
377                                         else
378                                         {
379                                             nPercent = estimateGfx(sOld_BM_Gfx, sNew_BM_Gfx, sDiff_BM_Gfx);
380                                             aStatus.nDiffStatus = StatusHelper.DIFF_AFTER_MOVE_DONE_DIFFERENCES_FOUND;
381                                             aStatus.nPercent2 = nPercent;
382                                         }
383                                     }
384                                     else
385                                     {
386                                     }
387                                 }
388                                 catch(java.io.IOException e)
389                                 {
390                                     GlobalLogWriter.get().println("Exception caught. At border remove: " + e.getMessage());
391                                 }
392                             }
393 
394 
395                         }
396                         catch (java.io.IOException e)
397                         {
398                             GlobalLogWriter.get().println(e.getMessage());
399                         }
400                     }
401 
402                     // checkDiff(sOldGfx, sNewGfx, sDiffGfx);
403                     // if (i >= _nMaxDiffs)
404                     // {
405                     //     break;
406                     // }
407                 }
408                 aList[nStatusIndex ++] = aStatus;
409             }
410             return aList;
411         }
412 
413 
createDiffs(String _sOutputPath, String[] _aRefList, String[] _aPSList, int _nMaxDiffs, TriState _tUseBorderMove)414     public StatusHelper[] createDiffs(String _sOutputPath, String[] _aRefList, String[] _aPSList, int _nMaxDiffs, TriState _tUseBorderMove)
415         {
416             if (_nMaxDiffs < 1)
417             {
418                 _nMaxDiffs = 1;
419             }
420 
421             // count, from which file (jpegs) exist more, take the less one
422             // more are not compareable
423 
424             // take the min of both
425             int nMin = Math.min(_aRefList.length, _aPSList.length);
426             nMin = Math.min(nMin, _nMaxDiffs);
427 
428             StatusHelper[] aList = new StatusHelper[nMin];
429 
430 //  TODO: if both document do not have same page count, produce an error
431             // System.out.println("min of both: " + String.valueOf(nMin));
432 
433             int nStatusIndex = 0;
434             for (int i=1;i<=nMin;i++)
435             {
436                 String sOldGfx =  _aRefList[i];
437                 String sNewGfx =  _aPSList[i];
438                 // String sDiffGfx_ = getJPEGName(_sOutputPath, sS1Basename + ".diff", StringHelper.createValueString(i, 4));
439 
440 
441                 String sDiffGfx = compareJPEGs(sOldGfx, sNewGfx );
442                 StatusHelper aStatus = new StatusHelper(sOldGfx, sNewGfx, sDiffGfx);
443 
444                 // if (FileHelper.exists(sDiffGfx))
445                 if (sDiffGfx.length() > 0)
446                 {
447                     int nResult = identify(sDiffGfx);
448                     if (nResult == 1)
449                     {
450                         aStatus.nDiffStatus = StatusHelper.DIFF_NO_DIFFERENCES;
451                     }
452                     else
453                     {
454                         try
455                         {
456                             int nPercent = estimateGfx(sOldGfx, sNewGfx, sDiffGfx);
457                             // GlobalLogWriter.get().println("Hello World:  Percent:= " + nPercent);
458                             // GlobalLogWriter.get().println("Hello World: TriState:= " + _tUseBorderMove.intValue());
459                             // GlobalLogWriter.get().println("Hello World:  DocType:= " + m_sDocumentType);
460 
461                             aStatus.nDiffStatus = StatusHelper.DIFF_DIFFERENCES_FOUND;
462                             aStatus.nPercent = nPercent;
463 
464                             if (nPercent > 75 &&
465                                 ((_tUseBorderMove == TriState.TRUE ) ||
466                                  ((_tUseBorderMove == TriState.UNSET) &&
467                                   m_sDocumentType.indexOf("MS PowerPoint") > 0)))
468                             {
469                                 _tUseBorderMove = TriState.TRUE;
470 //  TODO: problem is here, that we have to create some new names.
471 
472                                 String sBasename1 = FileHelper.getBasename(sOldGfx);
473                                 String sNameNoSuffix1 = FileHelper.getNameNoSuffix(sBasename1);
474                                 String sBasename2 = FileHelper.getBasename(sNewGfx);
475                                 String sNameNoSuffix2 = FileHelper.getNameNoSuffix(sBasename2);
476 
477                                 String sTmpDir = util.utils.getUsersTempDir();
478                                 String fs = System.getProperty("file.separator");
479 
480                                 String sOld_BM_Gfx =  sTmpDir + fs + sNameNoSuffix1 + "-BM-" + StringHelper.createValueString(i, 4) + ".jpg";
481                                 String sNew_BM_Gfx =  sTmpDir + fs + sNameNoSuffix2 + "-BM-" + StringHelper.createValueString(i, 4) + ".jpg";
482                                 try
483                                 {
484                                     BorderRemover a = new BorderRemover();
485                                     a.createNewImageWithoutBorder(sOldGfx, sOld_BM_Gfx);
486                                     a.createNewImageWithoutBorder(sNewGfx, sNew_BM_Gfx);
487 
488                                     String sDiff_BM_Gfx = compareJPEGs( sOld_BM_Gfx, sNew_BM_Gfx );
489 
490                                     aStatus.setFilesForBorderMove(sOld_BM_Gfx, sNew_BM_Gfx, sDiff_BM_Gfx);
491 
492                                     // if (FileHelper.exists(sDiff_BM_Gfx))
493                                     if (sDiff_BM_Gfx.length() > 0)
494                                     {
495                                         nResult = identify(sDiff_BM_Gfx);
496                                         if (nResult == 1)
497                                         {
498                                             aStatus.nDiffStatus = StatusHelper.DIFF_AFTER_MOVE_DONE_NO_PROBLEMS;
499                                             aStatus.nPercent2 = 0;
500                                         }
501                                         else
502                                         {
503                                             nPercent = estimateGfx(sOld_BM_Gfx, sNew_BM_Gfx, sDiff_BM_Gfx);
504                                             aStatus.nDiffStatus = StatusHelper.DIFF_AFTER_MOVE_DONE_DIFFERENCES_FOUND;
505                                             aStatus.nPercent2 = nPercent;
506                                         }
507                                     }
508                                     else
509                                     {
510                                     }
511                                 }
512                                 catch(java.io.IOException e)
513                                 {
514                                     GlobalLogWriter.get().println("Exception caught. At border remove: " + e.getMessage());
515                                 }
516                             }
517                         }
518                         catch (java.io.IOException e)
519                         {
520                             GlobalLogWriter.get().println(e.getMessage());
521                         }
522                     }
523 
524                     // checkDiff(sOldGfx, sNewGfx, sDiffGfx);
525                     // if (i >= _nMaxDiffs)
526                     // {
527                     //     break;
528                     // }
529                 }
530                 aList[nStatusIndex ++] = aStatus;
531             }
532             return aList;
533         }
534 
compareJPEGs(String _sOldGfx, String _sNewGfx)535     public static String compareJPEGs(String _sOldGfx, String _sNewGfx)
536         {
537             String sBasename1 = FileHelper.getBasename(_sOldGfx);
538             String sNameNoSuffix1 = FileHelper.getNameNoSuffix(sBasename1);
539             String sBasename2 = FileHelper.getBasename(_sNewGfx);
540             String sNameNoSuffix2 = FileHelper.getNameNoSuffix(sBasename2);
541 
542             String sTmpDir = util.utils.getUsersTempDir();
543             String fs = System.getProperty("file.separator");
544 
545             String sDiffName = sTmpDir + fs + sNameNoSuffix1 + "-" + sNameNoSuffix2 + "-diff.jpg";
546 
547             return compareJPEGs(_sOldGfx, _sNewGfx, sDiffName);
548         }
549 
compareJPEGs(String _sOldGfx, String _sNewGfx, String _sDiffGfx)550     public static String compareJPEGs(String _sOldGfx, String _sNewGfx, String _sDiffGfx)
551         {
552             String sComposite = "composite";
553             if (OSHelper.isWindows())
554             {
555                 sComposite = "composite.exe";
556             }
557 
558             // String sCommand = sComposite + " -compose difference " +
559             //     StringHelper.doubleQuoteIfNeed(_sOldGfx) + " " +
560             //     StringHelper.doubleQuoteIfNeed(_sNewGfx) + " " +
561             //     StringHelper.doubleQuoteIfNeed(_sDiffGfx);
562 
563             String[] sCommandArray =
564                 {
565                     sComposite,
566                     "-compose",
567                     "difference",
568                     _sOldGfx,
569                     _sNewGfx,
570                     _sDiffGfx
571                 };
572 
573             ProcessHandler aHandler = new ProcessHandler(sCommandArray);
574             boolean bBackValue = aHandler.executeSynchronously();
575 
576             String sBack = aHandler.getOutputText();
577             GlobalLogWriter.get().println("'" + sBack + "'");
578 
579             // return aHandler.getExitCode();
580             if (FileHelper.exists(_sDiffGfx))
581             {
582                 return _sDiffGfx;
583             }
584             return "";
585         }
586 
587     /**
588      * wrapper for ImageMagick identify,
589      * function checks how many different colors a picture contains.
590      * if it's only one color (nResult==1), like background color, there is no difference.
591      */
identify(String _sDiffGfx)592     int identify(String _sDiffGfx)
593         {
594             int nResult = 0;
595             // would like to know what the meaning of %k is for ImageMagick's 'identify'
596             String sIM_Format = "%k";
597             // if (OSHelper.isWindows())
598             // {
599             //     sIM_Format = "%%k";
600             // }
601 
602             String sIdentify = "identify";
603             if (OSHelper.isWindows())
604             {
605                 sIdentify = "identify.exe";
606             }
607 
608             // String sCommand = sIdentify + " " + sIM_Format + " " + StringHelper.doubleQuoteIfNeed(_sDiffGfx);
609 
610             String[] sCommandArray =
611                 {
612                     sIdentify,
613                     "-format",
614                     sIM_Format,
615                     _sDiffGfx
616                 };
617             ProcessHandler aHandler = new ProcessHandler(sCommandArray);
618             boolean bBackValue = aHandler.executeSynchronously();
619             String sBack = aHandler.getOutputText();
620             GlobalLogWriter.get().println("'" + sBack + "'");
621 
622             // try to interpret the result, which we get as a String
623             try
624             {
625                 int nIdx = sBack.indexOf("\n");
626                 if (nIdx > 0)
627                 {
628                     sBack = sBack.substring(0, nIdx);
629                 }
630 
631                 nResult = Integer.valueOf(sBack).intValue();
632             }
633             catch(java.lang.NumberFormatException e)
634             {
635                 GlobalLogWriter.get().println("Number format exception");
636                 nResult = 0;
637             }
638             return nResult;
639         }
640 
641     /*
642      * Check 2 different differ files
643      * return 1 if there is no difference between both diff files.
644      */
645 
646 //  TODO: Maybe a StatusHelper is a better return value
checkDiffDiff(String _sOutputPath, String _sSourcePath1, String _sSourceFile1, String _sSourcePath2, String _sSourceFile2)647     public StatusHelper checkDiffDiff(String _sOutputPath, String _sSourcePath1, String _sSourceFile1, String _sSourcePath2, String _sSourceFile2)
648         {
649             String sNewGfx =  _sSourcePath1 + fs + _sSourceFile1;
650             String sOldGfx =  _sSourcePath2 + fs + _sSourceFile2;
651 
652             int nNumber = 1;
653             String sDiffGfx;
654             sDiffGfx = getJPEGName(_sOutputPath, _sSourceFile1 + ".diff", StringHelper.createValueString(nNumber, 4));
655 
656             StatusHelper aCurrentStatus = new StatusHelper(sOldGfx, sNewGfx, sDiffGfx);
657 
658             // String sComposite = "composite";
659             // if (OSHelper.isWindows())
660             // {
661             //     sComposite = "composite.exe";
662             // }
663             //
664             // String sCommand = sComposite  +" -compose difference " +
665             //     StringHelper.doubleQuoteIfNeed(sOldGfx) + " " +
666             //     StringHelper.doubleQuoteIfNeed(sNewGfx) + " " +
667             //     StringHelper.doubleQuoteIfNeed(sDiffGfx);
668             //
669             //
670             // // System.out.println(sCommand);
671             // // executeSynchronously(sCommand);
672             // ProcessHandler aHandler = new ProcessHandler(sCommand);
673             // boolean bBackValue = aHandler.executeSynchronously();
674 
675             compareJPEGs(sOldGfx, sNewGfx, sDiffGfx);
676 
677             if (FileHelper.exists(sDiffGfx))
678             {
679                 int nResult = identify(sDiffGfx);
680                 if (nResult == 1)
681                 {
682                     aCurrentStatus.nDiffStatus = StatusHelper.DIFF_NO_DIFFERENCES;
683                 }
684                 else
685                 {
686                     aCurrentStatus.nDiffStatus = StatusHelper.DIFF_DIFFERENCES_FOUND;
687                     try
688                     {
689                         aCurrentStatus.nPercent = estimateGfx(sOldGfx, sNewGfx, sDiffGfx);
690                     }
691                     catch (java.io.IOException e)
692                     {
693                         GlobalLogWriter.get().println(e.getMessage());
694                         aCurrentStatus.nPercent = -1;
695                     }
696                 }
697                 // LLA: should diffdiff file delete?
698                 // File aFile = new File(sDiffGfx);
699                 // aFile.delete();
700             }
701             else
702             {
703                 GlobalLogWriter.get().println("composite can't create the diffdiff file.");
704             }
705 
706             return aCurrentStatus;
707         }
708 
709     // -----------------------------------------------------------------------------
710     /**
711      * count how much pixel differ and between Old or New and the Difference graphics
712      *
713      * First, count the old graphics, then the new graphics due to the fact both should be equal
714      * it should be legal to take result from old or new. We take the graphics with less values.
715      *
716      * Second, count the difference graphics, now take the percent algorithm and
717      * build a percent value, which contain the number of different pixels as a percent value
718      *
719      * Interpretation:
720      * 0%    there is no difference
721      *
722      * <100% Take a look into the difference graphics, maybe the difference graphics shows
723      *       text like outlined or the text is little bit move left, right up or down.
724      *
725      * >100% Yes it's possible that there is a difference more then 100%, maybe a font problem
726      *       between old and new graphics. The font of the new graphics is little bit bigger,
727      *       so the pixel count between old graphics and new graphics is twice the more.
728      *
729      */
estimateGfx(String _sOldGfx, String _sNewGfx, String _sDiffGfx)730     public int estimateGfx(String _sOldGfx, String _sNewGfx, String _sDiffGfx)
731         throws java.io.IOException
732         {
733             // new count pixels
734             int nNotWhiteCount_OldGraphic = PixelCounter.countNotWhitePixelsFromImage(_sOldGfx);
735             int nNotWhiteCount_NewGraphic = PixelCounter.countNotWhitePixelsFromImage(_sNewGfx);
736             int nNotBlackCount_DiffGraphic = PixelCounter.countNotBlackPixelsFromImage(_sDiffGfx);
737 
738             int nMinNotWhiteCount = Math.min(nNotWhiteCount_NewGraphic, nNotWhiteCount_OldGraphic);
739 
740             // check if not zero
741             if (nMinNotWhiteCount == 0)
742             {
743                 nMinNotWhiteCount = Math.max(nNotWhiteCount_NewGraphic, nNotWhiteCount_OldGraphic);
744                 if (nMinNotWhiteCount == 0)
745                 {
746                     nMinNotWhiteCount = 1;
747                 }
748             }
749 
750             int nPercent = Math.abs(nNotBlackCount_DiffGraphic * 100 / nMinNotWhiteCount);
751             GlobalLogWriter.get().println( "Graphics check, pixel based:" + String.valueOf(nPercent) + "% pixel differ ");
752             return nPercent;
753         }
754 
755 
756 
757 /*
758  * Some selftest functionallity
759  */
760 //    public static void main(String[] args)
761 //        {
762             // System.out.println(FileHelper.getNameNoSuffix("doc.sxw"));
763             // System.out.println(FileHelper.getSuffix("doc.sxw"));
764             // System.out.println(FileHelper.getBasename("doc.sxw"));
765             // System.out.println(FileHelper.getBasename("/tmp/doc.sxw"));
766 
767 //            PRNCompare a = new PRNCompare();
768 //             a.setInputPath(     "/cws/so-cwsserv06/qadev18/SRC680/src.m47/convwatch.keep/input/msoffice/xp/PowerPoint");
769 //             a.setReferencePath( "/cws/so-cwsserv06/qadev18/SRC680/src.m47/convwatch.keep/input/msoffice/xp/PowerPoint");
770 //             a.setOutputPath(    "/tmp/convwatch_java");
771 //             a.setDocFile(       "1_Gov.ppt");
772 //             a.setReferenceFile( "1_Gov.prn" );
773 //             a.setPostScriptFile("1_Gov.ps" );
774             // a.compare();
775 
776 
777 // LLA: 20040804 sample how to build jpegs from reference files
778 //             a.createJPEGFromPostscript("/tmp/convwatch_java",
779 //                                        "/home/apitest/WorkFromHome/20040804/reference", "worddoc.prn" );
780 //
781 //             a.createJPEGFromPostscript("/tmp/convwatch_java",
782 //                                        "/home/apitest/WorkFromHome/20040804/reference", "worddoc.ps" );
783 
784 //             Status[] aList = a.createDiffs("/tmp/convwatch_java",
785 //                                            "/tmp/convwatch_java", "worddoc.prn",
786 //                                            "/tmp/convwatch_java", "worddoc.ps",
787 //                                            2);
788 
789 // LLA: 20040805 sample how to check 2 gfx files
790 // this function return DifferenceType.NO_DIFFERENCE if the pictures contain no graphically difference
791 //             DifferenceType aReturnValue = a.checkDiffDiff("/tmp/convwatch_java",
792 //                                                           "/tmp/convwatch_java", "worddoc.prn.diff1.jpg",
793 //                                                           "/tmp/convwatch_java/old", "worddoc.prn.diff1.jpg");
794 //             if (aReturnValue.equals( DifferenceType.NO_DIFFERENCE ))
795 //             {
796 //                 System.out.println("There is no difference between both diff files.");
797 //             }
798 
799             // a.setOldDiff(       "/olddiffs");
800 //        }
801 }
802