KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > visual > BitmapComparator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: BitmapComparator.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.visual;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Graphics2D JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.awt.image.ColorModel JavaDoc;
26 import java.awt.image.RenderedImage JavaDoc;
27 import java.awt.image.WritableRaster JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import org.apache.batik.ext.awt.image.GraphicsUtil;
34 import org.apache.batik.ext.awt.image.renderable.Filter;
35 import org.apache.batik.ext.awt.image.spi.ImageTagRegistry;
36 import org.apache.batik.util.ParsedURL;
37 import org.apache.commons.io.IOUtils;
38
39 /**
40  * Helper class to visually compare two bitmap images.
41  * <p>
42  * This class was created by extracting reusable code from
43  * org.apache.batik.test.util.ImageCompareText (Apache Batik)
44  * into this separate class.
45  * <p>
46  * TODO Move as utility class to XML Graphics Commons when possible
47  */

48 public class BitmapComparator {
49
50     /**
51      * Builds a new BufferedImage that is the difference between the two input images
52      * @param ref the reference bitmap
53      * @param gen the newly generated bitmap
54      * @return the diff bitmap
55      */

56     public static BufferedImage JavaDoc buildDiffImage(BufferedImage JavaDoc ref,
57                                                BufferedImage JavaDoc gen) {
58         BufferedImage JavaDoc diff = new BufferedImage JavaDoc(ref.getWidth(),
59                                                ref.getHeight(),
60                                                BufferedImage.TYPE_INT_ARGB);
61         WritableRaster JavaDoc refWR = ref.getRaster();
62         WritableRaster JavaDoc genWR = gen.getRaster();
63         WritableRaster JavaDoc dstWR = diff.getRaster();
64
65         boolean refPre = ref.isAlphaPremultiplied();
66         if (!refPre) {
67             ColorModel JavaDoc cm = ref.getColorModel();
68             cm = GraphicsUtil.coerceData(refWR, cm, true);
69             ref = new BufferedImage JavaDoc(cm, refWR, true, null);
70         }
71         boolean genPre = gen.isAlphaPremultiplied();
72         if (!genPre) {
73             ColorModel JavaDoc cm = gen.getColorModel();
74             cm = GraphicsUtil.coerceData(genWR, cm, true);
75             gen = new BufferedImage JavaDoc(cm, genWR, true, null);
76         }
77
78
79         int w = ref.getWidth();
80         int h = ref.getHeight();
81
82         int y, i, val;
83         int [] refPix = null;
84         int [] genPix = null;
85         for (y = 0; y < h; y++) {
86             refPix = refWR.getPixels (0, y, w, 1, refPix);
87             genPix = genWR.getPixels (0, y, w, 1, genPix);
88             for (i = 0; i < refPix.length; i++) {
89                 // val = ((genPix[i] - refPix[i]) * 5) + 128;
90
val = ((refPix[i] - genPix[i]) * 10) + 128;
91                 if ((val & 0xFFFFFF00) != 0) {
92                     if ((val & 0x80000000) != 0) {
93                         val = 0;
94                     } else {
95                         val = 255;
96                     }
97                 }
98                 genPix[i] = val;
99             }
100             dstWR.setPixels(0, y, w, 1, genPix);
101         }
102
103         if (!genPre) {
104             ColorModel JavaDoc cm = gen.getColorModel();
105             cm = GraphicsUtil.coerceData(genWR, cm, false);
106         }
107         
108         if (!refPre) {
109             ColorModel JavaDoc cm = ref.getColorModel();
110             cm = GraphicsUtil.coerceData(refWR, cm, false);
111         }
112
113         return diff;
114     }
115
116     /**
117      * Builds a combined image that places a number of images next to each other for
118      * manual, visual comparison.
119      * @param images the array of bitmaps
120      * @return the combined image
121      */

122     public static BufferedImage JavaDoc buildCompareImage(BufferedImage JavaDoc[] images) {
123         BufferedImage JavaDoc cmp = new BufferedImage JavaDoc(
124                 images[0].getWidth() * images.length,
125                 images[0].getHeight(), BufferedImage.TYPE_INT_ARGB);
126
127         Graphics2D JavaDoc g = cmp.createGraphics();
128         g.setPaint(Color.white);
129         g.fillRect(0, 0, cmp.getWidth(), cmp.getHeight());
130         int lastWidth = 0;
131         for (int i = 0; i < images.length; i++) {
132             if (lastWidth > 0) {
133                 g.translate(lastWidth, 0);
134             }
135             if (images[i] != null) {
136                 g.drawImage(images[i], 0, 0, null);
137                 lastWidth = images[i].getWidth();
138             } else {
139                 lastWidth = 20; //Maybe add a special placeholder image instead later
140
}
141         }
142         g.dispose();
143
144         return cmp;
145     }
146
147     /**
148      * Builds a combined image that places two images next to each other for
149      * manual, visual comparison.
150      * @param ref the reference image
151      * @param gen the actual image
152      * @return the combined image
153      */

154     public static BufferedImage JavaDoc buildCompareImage(BufferedImage JavaDoc ref,
155             BufferedImage JavaDoc gen) {
156         return buildCompareImage(new BufferedImage JavaDoc[] {ref, gen});
157     }
158     
159     /**
160      * Loads an image from a URL
161      * @param url the URL to the image
162      * @return the bitmap as BufferedImage
163      * TODO This method doesn't close the InputStream opened by the URL.
164      */

165     public static BufferedImage JavaDoc getImage(URL JavaDoc url) {
166         ImageTagRegistry reg = ImageTagRegistry.getRegistry();
167         Filter filt = reg.readURL(new ParsedURL(url));
168         if (filt == null) {
169             return null;
170         }
171
172         RenderedImage JavaDoc red = filt.createDefaultRendering();
173         if (red == null) {
174             return null;
175         }
176         
177         BufferedImage JavaDoc img = new BufferedImage JavaDoc(red.getWidth(),
178                                               red.getHeight(),
179                                               BufferedImage.TYPE_INT_ARGB);
180         red.copyData(img.getRaster());
181
182         return img;
183     }
184
185     /**
186      * Loads an image from a URL
187      * @param bitmapFile the bitmap file
188      * @return the bitmap as BufferedImage
189      */

190     public static BufferedImage JavaDoc getImage(File JavaDoc bitmapFile) {
191         try {
192             InputStream JavaDoc in = new java.io.FileInputStream JavaDoc(bitmapFile);
193             try {
194                 in = new java.io.BufferedInputStream JavaDoc(in);
195                 
196                 ImageTagRegistry reg = ImageTagRegistry.getRegistry();
197                 Filter filt = reg.readStream(in);
198                 if (filt == null) {
199                     return null;
200                 }
201
202                 RenderedImage JavaDoc red = filt.createDefaultRendering();
203                 if (red == null) {
204                     return null;
205                 }
206                 
207                 BufferedImage JavaDoc img = new BufferedImage JavaDoc(red.getWidth(),
208                                                       red.getHeight(),
209                                                       BufferedImage.TYPE_INT_ARGB);
210                 red.copyData(img.getRaster());
211                 return img;
212             } finally {
213                 IOUtils.closeQuietly(in);
214             }
215         } catch (IOException JavaDoc e) {
216             return null;
217         }
218     }
219
220 }
221
Popular Tags