summaryrefslogtreecommitdiff
path: root/bean/qa/complex/bean/ScreenComparer.java
blob: c415c8404345059afb92f157a18f248e0d5025e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
package complex.bean;


// import complexlib.ComplexTestCase;
import java.io.File;
import java.awt.Rectangle;
// import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
// import java.awt.event.*;
// import java.awt.Frame;
import javax.imageio.ImageIO;
// import javax.imageio.stream.FileImageOutputStream;



class ScreenComparer
{
    Rectangle m_rect;
    BufferedImage m_img1;
    BufferedImage m_img2;
    BufferedImage m_imgDiff;

    int m_diffColor;
    public ScreenComparer(int x, int y, int width, int height)
    {
        this(new Rectangle(x, y, width, height));
    }

    public ScreenComparer(Rectangle location)
    {
        m_rect = location;
        int red = 0xff;
        int alpha = 0xff;
        m_diffColor = (alpha << 24);
        m_diffColor = m_diffColor | (red << 16);
    }

    public ScreenComparer()
    {
        this(new Rectangle(0, 0, 0, 0));
    }

    public void reset()
    {
        m_rect = null;
        m_img1 = null;
        m_img2 = null;
        m_imgDiff = null;
    }

    public Rectangle getLocation()
    {
        return m_rect;
    }
    public void grabOne() throws Exception
    {
        grabOne(m_rect);
    }

    public void grabOne(Rectangle r) throws Exception
    {
        java.awt.Robot robot = new java.awt.Robot();
        m_img1 = robot.createScreenCapture(r);
    }

    public void grabTwo() throws Exception
    {
        grabTwo(m_rect);
    }

    public void grabTwo(Rectangle r) throws Exception
    {
        java.awt.Robot robot = new java.awt.Robot();
        m_img2 = robot.createScreenCapture(r);
    }

    public boolean compare() throws Exception
    {
        if (m_img1 == null || m_img2 == null)
        {
            throw new Exception("Only one image captured!");
        }
        boolean ret = true;
        int w1 = m_img1.getWidth();
        int h1 = m_img1.getHeight();
        int w2 = m_img2.getWidth();
        int h2 = m_img2.getHeight();

        if (w1 != w2 || h1 != h2)
        {
            System.out.println("### 1\n");
            //Different size. Create an image that holds both images.
            int w = w1 > w2 ? w1 : w2;
            int h = h1 > h2 ? h1 : h2;
            m_imgDiff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            for (int y = 0; y < h; y ++)
            {
                for (int x = 0; x < w; x++)
                {
                    boolean bOutOfRange = false;
                    int pixel1 = 0;
                    int pixel2 = 0;
                    //get the pixel for m_img1
                    if (x < w1 && y < h1)
                    {
                        pixel1 = m_img1.getRGB(x, y);
                    }
                    else
                    {
                        bOutOfRange = true;
                    }

                    if (x < w2 && y < h2)
                    {
                        pixel2 = m_img2.getRGB(x, y);
                    }
                    else
                    {
                        bOutOfRange = true;
                    }

                    if (bOutOfRange || pixel1 != pixel2)
                    {
                        m_imgDiff.setRGB(x, y, m_diffColor);
                    }
                    else
                    {
                        m_imgDiff.setRGB(x, y, pixel1);
                    }

                }
            }
            return false;
        }

        //Images have same dimension
        int[] pixels1 = new int[w1 * h1];
        PixelGrabber pg = new PixelGrabber(
            m_img1.getSource(), 0, 0, w1, h1, pixels1, 0, w1);
        pg.grabPixels();

        int[] pixels2 = new int[w2 * h2];
        PixelGrabber pg2 = new PixelGrabber(
            m_img2.getSource(), 0, 0, w2, h2, pixels2, 0, w2);
        pg2.grabPixels();

        m_imgDiff = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_ARGB);

        //First check if the images differ.
        int lenAr = pixels1.length;
        int index = 0;
        for (index = 0; index < lenAr; index++)
        {
            if (pixels1[index] != pixels2[index])
            {
                break;
            }
        }

        //If the images are different, then create the diff image
        if (index < lenAr)
        {
            for (int y = 0; y < h1; y++)
            {
                for (int x = 0; x < w1; x++)
                {
                    int offset = y * w1 + x;
                    if (pixels1[offset] != pixels2[offset])
                    {
                        ret = ret && false;
                        m_imgDiff.setRGB(x, y, m_diffColor);
                    }
                    else
                    {
                        m_imgDiff.setRGB(x, y, pixels1[offset]);
                    }
                }
            }
        }
        return ret;
    }

    /** Writes Images to a location. The
     *  directory is determined by the java property OOoBean.Images
     *
     */
    public void writeImages() throws Exception
    {
        String imgLocation = System.getProperty("OOoBean.Images", "");
        File file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation));
        File file1 = new File(file_tmp.getPath()+".png");
        file_tmp.delete();
        if (m_img1 != null)
        {
            ImageIO.write(m_img1, "png", file1);
            System.out.println("\nCompared images:");
            System.out.println("1. " + file1.getPath());
        }
        file1= null;
        file_tmp= null;
        file_tmp = File.createTempFile("OOoBean", "", new File(imgLocation));
        file1 = new File(file_tmp.getPath()+".png");
        file_tmp.delete();
        if (m_img2 != null)
        {
            ImageIO.write(m_img2, "png", file1);
            System.out.println("2. " + file1.getPath());
        }
        file1= null;
        file_tmp= null;
        file_tmp = File.createTempFile("OOoBean", "_diff", new File(imgLocation));
        file1 = new File(file_tmp.getPath()+".png");
        file_tmp.delete();
        if (m_imgDiff != null)
        {
            ImageIO.write(m_imgDiff, "png", file1);
            System.out.println("Diff image: " + file1.getPath() + "\n");
        }
    }

}