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 complex.bean;
24 
25 
26 // import complexlib.ComplexTestCase;
27 import java.io.File;
28 import java.awt.Rectangle;
29 // import java.awt.BorderLayout;
30 import java.awt.image.BufferedImage;
31 import java.awt.image.PixelGrabber;
32 // import java.awt.event.*;
33 // import java.awt.Frame;
34 import javax.imageio.ImageIO;
35 // import javax.imageio.stream.FileImageOutputStream;
36 
37 
38 
39 class ScreenComparer
40 {
41     Rectangle m_rect;
42     BufferedImage m_img1;
43     BufferedImage m_img2;
44     BufferedImage m_imgDiff;
45 
46     int m_diffColor;
ScreenComparer(int x, int y, int width, int height)47     public ScreenComparer(int x, int y, int width, int height)
48     {
49         this(new Rectangle(x, y, width, height));
50     }
51 
ScreenComparer(Rectangle location)52     public ScreenComparer(Rectangle location)
53     {
54         m_rect = location;
55         int red = 0xff;
56         int alpha = 0xff;
57         m_diffColor = (alpha << 24);
58         m_diffColor = m_diffColor | (red << 16);
59     }
60 
ScreenComparer()61     public ScreenComparer()
62     {
63         this(new Rectangle(0, 0, 0, 0));
64     }
65 
reset()66     public void reset()
67     {
68         m_rect = null;
69         m_img1 = null;
70         m_img2 = null;
71         m_imgDiff = null;
72     }
73 
getLocation()74     public Rectangle getLocation()
75     {
76         return m_rect;
77     }
grabOne()78     public void grabOne() throws Exception
79     {
80         grabOne(m_rect);
81     }
82 
grabOne(Rectangle r)83     public void grabOne(Rectangle r) throws Exception
84     {
85         java.awt.Robot robot = new java.awt.Robot();
86         m_img1 = robot.createScreenCapture(r);
87     }
88 
grabTwo()89     public void grabTwo() throws Exception
90     {
91         grabTwo(m_rect);
92     }
93 
grabTwo(Rectangle r)94     public void grabTwo(Rectangle r) throws Exception
95     {
96         java.awt.Robot robot = new java.awt.Robot();
97         m_img2 = robot.createScreenCapture(r);
98     }
99 
compare()100     public boolean compare() throws Exception
101     {
102         if (m_img1 == null || m_img2 == null)
103         {
104             throw new Exception("Only one image captured!");
105         }
106         boolean ret = true;
107         int w1 = m_img1.getWidth();
108         int h1 = m_img1.getHeight();
109         int w2 = m_img2.getWidth();
110         int h2 = m_img2.getHeight();
111 
112         if (w1 != w2 || h1 != h2)
113         {
114             System.out.println("### 1\n");
115             //Different size. Create an image that holds both images.
116             int w = w1 > w2 ? w1 : w2;
117             int h = h1 > h2 ? h1 : h2;
118             m_imgDiff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
119             for (int y = 0; y < h; y ++)
120             {
121                 for (int x = 0; x < w; x++)
122                 {
123                     boolean bOutOfRange = false;
124                     int pixel1 = 0;
125                     int pixel2 = 0;
126                     //get the pixel for m_img1
127                     if (x < w1 && y < h1)
128                     {
129                         pixel1 = m_img1.getRGB(x, y);
130                     }
131                     else
132                     {
133                         bOutOfRange = true;
134                     }
135 
136                     if (x < w2 && y < h2)
137                     {
138                         pixel2 = m_img2.getRGB(x, y);
139                     }
140                     else
141                     {
142                         bOutOfRange = true;
143                     }
144 
145                     if (bOutOfRange || pixel1 != pixel2)
146                     {
147                         m_imgDiff.setRGB(x, y, m_diffColor);
148                     }
149                     else
150                     {
151                         m_imgDiff.setRGB(x, y, pixel1);
152                     }
153 
154                 }
155             }
156             return false;
157         }
158 
159         //Images have same dimension
160         int[] pixels1 = new int[w1 * h1];
161         PixelGrabber pg = new PixelGrabber(
162             m_img1.getSource(), 0, 0, w1, h1, pixels1, 0, w1);
163         pg.grabPixels();
164 
165         int[] pixels2 = new int[w2 * h2];
166         PixelGrabber pg2 = new PixelGrabber(
167             m_img2.getSource(), 0, 0, w2, h2, pixels2, 0, w2);
168         pg2.grabPixels();
169 
170         m_imgDiff = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_ARGB);
171 
172         //First check if the images differ.
173         int lenAr = pixels1.length;
174         int index = 0;
175         for (index = 0; index < lenAr; index++)
176         {
177             if (pixels1[index] != pixels2[index])
178             {
179                 break;
180             }
181         }
182 
183         //If the images are different, then create the diff image
184         if (index < lenAr)
185         {
186             for (int y = 0; y < h1; y++)
187             {
188                 for (int x = 0; x < w1; x++)
189                 {
190                     int offset = y * w1 + x;
191                     if (pixels1[offset] != pixels2[offset])
192                     {
193                         ret = ret && false;
194                         m_imgDiff.setRGB(x, y, m_diffColor);
195                     }
196                     else
197                     {
198                         m_imgDiff.setRGB(x, y, pixels1[offset]);
199                     }
200                 }
201             }
202         }
203         return ret;
204     }
205 
206     /** Writes Images to a location. The
207      *  directory is determined by the java property OOoBean.Images
208      *
209      */
writeImages()210     public void writeImages() throws Exception
211     {
212         String imgLocation = System.getProperty("OOoBean.Images", "");
213         File file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation));
214         File file1 = new File(file_tmp.getPath()+".png");
215         file_tmp.delete();
216         if (m_img1 != null)
217         {
218             ImageIO.write(m_img1, "png", file1);
219             System.out.println("\nCompared images:");
220             System.out.println("1. " + file1.getPath());
221         }
222         file1= null;
223         file_tmp= null;
224         file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation));
225         file1 = new File(file_tmp.getPath()+".png");
226         file_tmp.delete();
227         if (m_img2 != null)
228         {
229             ImageIO.write(m_img2, "png", file1);
230             System.out.println("2. " + file1.getPath());
231         }
232         file1= null;
233         file_tmp= null;
234         file_tmp = File.createTempFile("OOoBean", "_diff", new File(imgLocation));
235         file1 = new File(file_tmp.getPath()+".png");
236         file_tmp.delete();
237         if (m_imgDiff != null)
238         {
239             ImageIO.write(m_imgDiff, "png", file1);
240             System.out.println("Diff image: " + file1.getPath() + "\n");
241         }
242     }
243 
244 }
245 
246