1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScreenComparer.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import complexlib
.ComplexTestCase
;
35 import java
.awt
.Rectangle
;
36 import java
.awt
.BorderLayout
;
37 import java
.awt
.image
.BufferedImage
;
38 import java
.awt
.image
.PixelGrabber
;
39 import java
.awt
.event
.*;
40 import java
.awt
.Frame
;
41 import javax
.imageio
.ImageIO
;
42 import javax
.imageio
.stream
.FileImageOutputStream
;
51 BufferedImage m_imgDiff
;
54 public ScreenComparer(int x
, int y
, int width
, int height
)
56 this(new Rectangle(x
, y
, width
, height
));
59 public ScreenComparer(Rectangle location
)
64 m_diffColor
= (alpha
<< 24);
65 m_diffColor
= m_diffColor
| (red
<< 16);
68 public ScreenComparer()
70 this(new Rectangle(0, 0, 0, 0));
81 public Rectangle
getLocation()
85 public void grabOne() throws Exception
90 public void grabOne(Rectangle r
) throws Exception
92 java
.awt
.Robot robot
= new java
.awt
.Robot();
93 m_img1
= robot
.createScreenCapture(r
);
96 public void grabTwo() throws Exception
101 public void grabTwo(Rectangle r
) throws Exception
103 java
.awt
.Robot robot
= new java
.awt
.Robot();
104 m_img2
= robot
.createScreenCapture(r
);
107 public boolean compare() throws Exception
109 if (m_img1
== null || m_img2
== null)
110 throw new Exception("Only one image captured!");
112 int w1
= m_img1
.getWidth();
113 int h1
= m_img1
.getHeight();
114 int w2
= m_img2
.getWidth();
115 int h2
= m_img2
.getHeight();
117 if (w1
!= w2
|| h1
!= h2
)
119 System
.out
.println("### 1\n");
120 //Different size. Create an image that holds both images.
121 int w
= w1
> w2 ? w1
: w2
;
122 int h
= h1
> h2 ? h1
: h2
;
123 m_imgDiff
= new BufferedImage(w
, h
, BufferedImage
.TYPE_INT_ARGB
);
124 for (int y
= 0; y
< h
; y
++)
126 for (int x
= 0; x
< w
; x
++)
128 boolean bOutOfRange
= false;
131 //get the pixel for m_img1
132 if (x
< w1
&& y
< h1
)
133 pixel1
= m_img1
.getRGB(x
, y
);
137 if (x
< w2
&& y
< h2
)
138 pixel2
= m_img2
.getRGB(x
, y
);
142 if (bOutOfRange
|| pixel1
!= pixel2
)
143 m_imgDiff
.setRGB(x
, y
, m_diffColor
);
145 m_imgDiff
.setRGB(x
, y
, pixel1
);
152 //Images have same dimension
153 int[] pixels1
= new int[w1
* h1
];
154 PixelGrabber pg
= new PixelGrabber(
155 m_img1
.getSource(), 0, 0, w1
, h1
, pixels1
, 0, w1
);
158 int[] pixels2
= new int[w2
* h2
];
159 PixelGrabber pg2
= new PixelGrabber(
160 m_img2
.getSource(), 0, 0, w2
, h2
, pixels2
, 0, w2
);
163 m_imgDiff
= new BufferedImage(w1
, h1
, BufferedImage
.TYPE_INT_ARGB
);
165 //First check if the the images differ.
166 int lenAr
= pixels1
.length
;
168 for (index
= 0; index
< lenAr
; index
++)
170 if (pixels1
[index
] != pixels2
[index
])
174 //If the images are different, then create the diff image
177 for (int y
= 0; y
< h1
; y
++)
179 for (int x
= 0; x
< w1
; x
++)
181 int offset
= y
* w1
+ x
;
182 if (pixels1
[offset
] != pixels2
[offset
])
185 m_imgDiff
.setRGB(x
, y
, m_diffColor
);
189 m_imgDiff
.setRGB(x
, y
, pixels1
[offset
]);
197 /** Writes Images to a location. The
198 * directory is determined by the java property OOoBean.Images
201 public void writeImages() throws Exception
203 String imgLocation
= System
.getProperty("OOoBean.Images", "");
204 File file_tmp
= File
.createTempFile("OOoBean", "", new File(imgLocation
));
205 File file1
= new File(file_tmp
.getPath()+".png");
209 ImageIO
.write(m_img1
, "png", file1
);
210 System
.out
.println("\nCompared images:");
211 System
.out
.println("1. " + file1
.getPath());
215 file_tmp
= File
.createTempFile("OOoBean", "", new File(imgLocation
));
216 file1
= new File(file_tmp
.getPath()+".png");
220 ImageIO
.write(m_img2
, "png", file1
);
221 System
.out
.println("2. " + file1
.getPath());
225 file_tmp
= File
.createTempFile("OOoBean", "_diff", new File(imgLocation
));
226 file1
= new File(file_tmp
.getPath()+".png");
228 if (m_imgDiff
!= null)
230 ImageIO
.write(m_imgDiff
, "png", file1
);
231 System
.out
.println("Diff image: " + file1
.getPath() + "\n");