1 18 package org.apache.batik.transcoder.image; 19 20 import java.awt.image.BufferedImage ; 21 import java.awt.image.RenderedImage ; 22 23 import java.io.FileOutputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.PrintWriter ; 30 import java.io.StringWriter ; 31 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 35 import java.util.Map ; 36 37 import org.apache.batik.ext.awt.image.spi.ImageTagRegistry; 38 import org.apache.batik.ext.awt.image.renderable.Filter; 39 40 import org.apache.batik.ext.awt.image.codec.PNGImageEncoder; 41 import org.apache.batik.ext.awt.image.codec.PNGEncodeParam; 42 43 import org.apache.batik.transcoder.TranscoderException; 44 import org.apache.batik.transcoder.TranscoderInput; 45 import org.apache.batik.transcoder.TranscoderOutput; 46 47 import org.apache.batik.transcoder.image.ImageTranscoder; 48 import org.apache.batik.transcoder.image.PNGTranscoder; 49 50 import org.apache.batik.test.AbstractTest; 51 import org.apache.batik.test.DefaultTestReport; 52 import org.apache.batik.test.TestReport; 53 import org.apache.batik.test.svg.SVGRenderingAccuracyTest; 54 55 61 public abstract class AbstractImageTranscoderTest extends AbstractTest { 62 63 66 public static final String ERROR_IMAGE_DIFFER = 67 "AbstractImageTranscoderTest.error.image.differ"; 68 69 72 public static final String DIFFERENCE_IMAGE = 73 "AbstractImageTranscoderTest.error.difference.image"; 74 75 78 public static final String ERROR_TRANSCODING = 79 "AbstractImageTranscoderTest.error.transcoder.exception"; 80 81 84 public AbstractImageTranscoderTest() { 85 } 86 87 95 protected URL resolveURL(String url){ 96 File f = (new File (url)).getAbsoluteFile(); 98 if(f.getParentFile().exists()){ 99 try{ 100 return f.toURL(); 101 }catch(MalformedURLException e){ 102 throw new IllegalArgumentException (); 103 } 104 } 105 106 try{ 108 return new URL (url); 109 }catch(MalformedURLException e){ 110 throw new IllegalArgumentException (url); 111 } 112 } 113 114 DefaultTestReport report; 115 119 public TestReport runImpl() throws Exception { 120 report = new DefaultTestReport(this); 121 122 try { 123 DiffImageTranscoder transcoder = 124 new DiffImageTranscoder(getReferenceImageData()); 125 126 Map hints = createTranscodingHints(); 127 if (hints != null) { 128 transcoder.setTranscodingHints(hints); 129 } 130 131 TranscoderInput input = createTranscoderInput(); 132 transcoder.transcode(input, null); 133 } catch (Exception ex) { 134 report.setErrorCode(ERROR_TRANSCODING); 135 report.addDescriptionEntry(ERROR_TRANSCODING, toString(ex)); 136 ex.printStackTrace(); 137 report.setPassed(false); 138 } 139 140 return report; 141 } 142 143 146 protected abstract TranscoderInput createTranscoderInput(); 147 148 151 protected Map createTranscodingHints() { 152 return null; 153 } 154 155 158 protected abstract byte [] getReferenceImageData(); 159 160 164 167 public static String toString(Exception ex) { 168 StringWriter trace = new StringWriter (); 169 ex.printStackTrace(new PrintWriter (trace)); 170 return trace.toString(); 171 } 172 173 static String filename; 174 175 178 public static byte [] createBufferedImageData(URL url) { 179 try { 180 filename = url.toString(); 181 InputStream istream = url.openStream(); 183 byte [] imgData = null; 184 byte [] buf = new byte[1024]; 185 int length; 186 while ((length = istream.read(buf, 0, buf.length)) == buf.length) { 187 if (imgData != null) { 188 byte [] imgDataTmp = new byte[imgData.length + length]; 189 System.arraycopy 190 (imgData, 0, imgDataTmp, 0, imgData.length); 191 System.arraycopy 192 (buf, 0, imgDataTmp, imgData.length, length); 193 imgData = imgDataTmp; 194 } else { 195 imgData = new byte[length]; 196 System.arraycopy(buf, 0, imgData, 0, length); 197 } 198 } 199 if (imgData != null) { 200 byte [] imgDataTmp = new byte[imgData.length + length]; 201 System.arraycopy 202 (imgData, 0, imgDataTmp, 0, imgData.length); 203 System.arraycopy 204 (buf, 0, imgDataTmp, imgData.length, length); 205 imgData = imgDataTmp; 206 } else { 207 imgData = new byte[length]; 208 System.arraycopy(buf, 0, imgData, 0, length); 209 } 210 istream.close(); 211 return imgData; 212 } catch (IOException ex) { 213 return null; 214 } 215 } 216 217 220 protected class DiffImageTranscoder extends ImageTranscoder { 221 222 223 protected boolean state; 224 225 226 protected byte [] refImgData; 227 228 234 public DiffImageTranscoder(byte [] refImgData) { 235 this.refImgData = refImgData; 236 } 237 238 243 public BufferedImage createImage(int w, int h) { 244 return new BufferedImage (w, h, BufferedImage.TYPE_INT_ARGB); 245 } 246 247 256 public void writeImage(BufferedImage img, TranscoderOutput output) 257 throws TranscoderException { 258 259 compareImage(img); 260 } 261 262 protected void writeCandidateReference(byte [] imgData) { 263 try { 264 String s = new File (filename).getName(); 265 s = "test-references/org/apache/batik/transcoder/image/candidate-reference/"+s; 266 System.out.println(s); 267 FileOutputStream ostream = new FileOutputStream (s); 268 ostream.write(imgData, 0, imgData.length); 269 ostream.flush(); 270 ostream.close(); 271 } catch (Exception ex) { } 272 return; 273 } 274 275 protected void writeCandidateVariation(byte [] imgData, byte [] refData) 276 { 277 writeCandidateReference(imgData); 278 try { 279 BufferedImage ref = getImage(new ByteArrayInputStream (refData)); 280 BufferedImage img = getImage(new ByteArrayInputStream (imgData)); 281 BufferedImage diff = 282 SVGRenderingAccuracyTest.buildDiffImage(ref, img); 283 String s = new File (filename).getName(); 284 s = ("test-references/org/apache/batik/transcoder/image/"+ 285 "candidate-variation/"+s); 286 PNGImageEncoder encoder 287 = new PNGImageEncoder 288 (new FileOutputStream (s), 289 PNGEncodeParam.getDefaultEncodeParam(diff)); 290 encoder.encode(diff); 291 report.addDescriptionEntry(DIFFERENCE_IMAGE,new File (s)); 292 } catch (Exception e) { } 293 } 294 295 298 protected void compareImage(BufferedImage img) 299 throws TranscoderException { 300 303 ByteArrayOutputStream out = new ByteArrayOutputStream (); 304 TranscoderOutput output = new TranscoderOutput(out); 305 PNGTranscoder t = new PNGTranscoder(); 306 t.writeImage(img, output); 307 byte [] imgData = out.toByteArray(); 308 309 if (refImgData == null) { 310 report.setErrorCode(ERROR_IMAGE_DIFFER); 311 report.addDescriptionEntry(ERROR_IMAGE_DIFFER, ""); 312 report.setPassed(false); 313 writeCandidateReference(imgData); 314 state = false; 315 return; 316 } 317 318 if (refImgData.length != imgData.length) { 319 report.setErrorCode(ERROR_IMAGE_DIFFER); 320 report.addDescriptionEntry(ERROR_IMAGE_DIFFER, ""); 321 report.setPassed(false); 322 writeCandidateVariation(imgData, refImgData); 323 return; 324 } 325 326 for (int i = 0; i < refImgData.length; ++i) { 327 if (refImgData[i] != imgData[i]) { 328 report.setErrorCode(ERROR_IMAGE_DIFFER); 329 report.addDescriptionEntry(ERROR_IMAGE_DIFFER, ""); 330 report.setPassed(false); 331 writeCandidateVariation(imgData, refImgData); 332 return; 333 } 334 } 335 336 state = true; 337 } 338 339 343 public boolean isIdentical() { 344 return state; 345 } 346 } 347 348 protected BufferedImage getImage(InputStream is) 349 throws IOException { 350 ImageTagRegistry reg = ImageTagRegistry.getRegistry(); 351 Filter filt = reg.readStream(is); 352 if(filt == null) 353 throw new IOException ("Couldn't read Stream"); 354 355 RenderedImage red = filt.createDefaultRendering(); 356 if(red == null) 357 throw new IOException ("Couldn't render Stream"); 358 359 BufferedImage img = new BufferedImage (red.getWidth(), 360 red.getHeight(), 361 BufferedImage.TYPE_INT_ARGB); 362 red.copyData(img.getRaster()); 363 return img; 364 } 365 } 366 | Popular Tags |