1 17 18 19 20 package org.apache.fop.visual; 21 22 import java.awt.Color ; 23 import java.awt.Graphics2D ; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.ColorModel ; 26 import java.awt.image.RenderedImage ; 27 import java.awt.image.WritableRaster ; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.net.URL ; 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 48 public class BitmapComparator { 49 50 56 public static BufferedImage buildDiffImage(BufferedImage ref, 57 BufferedImage gen) { 58 BufferedImage diff = new BufferedImage (ref.getWidth(), 59 ref.getHeight(), 60 BufferedImage.TYPE_INT_ARGB); 61 WritableRaster refWR = ref.getRaster(); 62 WritableRaster genWR = gen.getRaster(); 63 WritableRaster dstWR = diff.getRaster(); 64 65 boolean refPre = ref.isAlphaPremultiplied(); 66 if (!refPre) { 67 ColorModel cm = ref.getColorModel(); 68 cm = GraphicsUtil.coerceData(refWR, cm, true); 69 ref = new BufferedImage (cm, refWR, true, null); 70 } 71 boolean genPre = gen.isAlphaPremultiplied(); 72 if (!genPre) { 73 ColorModel cm = gen.getColorModel(); 74 cm = GraphicsUtil.coerceData(genWR, cm, true); 75 gen = new BufferedImage (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 = ((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 cm = gen.getColorModel(); 105 cm = GraphicsUtil.coerceData(genWR, cm, false); 106 } 107 108 if (!refPre) { 109 ColorModel cm = ref.getColorModel(); 110 cm = GraphicsUtil.coerceData(refWR, cm, false); 111 } 112 113 return diff; 114 } 115 116 122 public static BufferedImage buildCompareImage(BufferedImage [] images) { 123 BufferedImage cmp = new BufferedImage ( 124 images[0].getWidth() * images.length, 125 images[0].getHeight(), BufferedImage.TYPE_INT_ARGB); 126 127 Graphics2D 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; } 141 } 142 g.dispose(); 143 144 return cmp; 145 } 146 147 154 public static BufferedImage buildCompareImage(BufferedImage ref, 155 BufferedImage gen) { 156 return buildCompareImage(new BufferedImage [] {ref, gen}); 157 } 158 159 165 public static BufferedImage getImage(URL 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 red = filt.createDefaultRendering(); 173 if (red == null) { 174 return null; 175 } 176 177 BufferedImage img = new BufferedImage (red.getWidth(), 178 red.getHeight(), 179 BufferedImage.TYPE_INT_ARGB); 180 red.copyData(img.getRaster()); 181 182 return img; 183 } 184 185 190 public static BufferedImage getImage(File bitmapFile) { 191 try { 192 InputStream in = new java.io.FileInputStream (bitmapFile); 193 try { 194 in = new java.io.BufferedInputStream (in); 195 196 ImageTagRegistry reg = ImageTagRegistry.getRegistry(); 197 Filter filt = reg.readStream(in); 198 if (filt == null) { 199 return null; 200 } 201 202 RenderedImage red = filt.createDefaultRendering(); 203 if (red == null) { 204 return null; 205 } 206 207 BufferedImage img = new BufferedImage (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 e) { 216 return null; 217 } 218 } 219 220 } 221 | Popular Tags |