1/*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.netbeans.jemmy.image;
24
25import java.awt.image.BufferedImage;
26import java.io.IOException;
27
28import org.netbeans.jemmy.JemmyException;
29
30/**
31 * Allowes compares images in memory to ones stored in files and compare such
32 * images one with another.
33 *
34 * @author Alexandre Iline (alexandre.iline@oracle.com)
35 */
36public class FileImageComparator {
37
38    ImageLoader loader;
39    ImageComparator comparator;
40
41    /**
42     * Constructs a FileImageComparator object.
43     *
44     * @param comparator - ImageComparator to be used for image comparision.
45     * @param loader - ImageLoader to be used for image loading.
46     */
47    public FileImageComparator(ImageComparator comparator, ImageLoader loader) {
48        this.loader = loader;
49        this.comparator = comparator;
50    }
51
52    /**
53     * Compares an image with one stored in file. Comparision is performed by
54     * ImageComparator passed into constructor. Image is loaded by ImageLoader
55     * passed into constructor.
56     *
57     * @param image an image to compare.
58     * @param fileName a file containing an image to compare.
59     * @return true if images match each other.
60     */
61    public boolean compare(BufferedImage image, String fileName) {
62        try {
63            return comparator.compare(image, loader.load(fileName));
64        } catch (IOException e) {
65            throw (new JemmyException("IOException during image loading", e));
66        }
67    }
68
69    /**
70     * Compares two image stored in files.. Comparision is performed by
71     * ImageComparator passed into constructor. Images are loaded by ImageLoader
72     * passed into constructor.
73     *
74     * @param fileName1 a file containing an image to compare.
75     * @param fileName2 a file containing an image to compare.
76     * @return true if images match each other.
77     */
78    public boolean compare(String fileName1, String fileName2) {
79        try {
80            return (comparator.compare(loader.load(fileName1),
81                    loader.load(fileName2)));
82        } catch (IOException e) {
83            throw (new JemmyException("IOException during image loading", e));
84        }
85    }
86}
87