1 16 17 package org.apache.batik.ext.awt.image.codec; 18 19 import org.apache.batik.test.*; 20 import java.awt.*; 21 import java.awt.geom.*; 22 import java.awt.image.*; 23 import java.io.*; 24 25 33 public class PNGEncoderTest extends AbstractTest { 34 38 public static final String ERROR_CANNOT_ENCODE_IMAGE 39 = "PNGEncoderTest.error.cannot.encode.image"; 40 41 45 public static final String ERROR_CANNOT_DECODE_IMAGE 46 = "PNGEncoderTest.error.cannot.decode.image"; 47 48 51 public static final String ERROR_DECODED_DOES_NOT_MATCH_ENCODED 52 = "PNGEncoderTest.error.decoded.does.not.match.encoded"; 53 54 public TestReport runImpl() throws Exception { 55 BufferedImage image = new BufferedImage(100, 75, BufferedImage.TYPE_INT_ARGB); 57 Graphics2D ig = image.createGraphics(); 58 ig.scale(.5, .5); 59 ig.setPaint(new Color(128,0,0)); 60 ig.fillRect(0, 0, 100, 50); 61 ig.setPaint(Color.orange); 62 ig.fillRect(100, 0, 100, 50); 63 ig.setPaint(Color.yellow); 64 ig.fillRect(0, 50, 100, 50); 65 ig.setPaint(Color.red); 66 ig.fillRect(100, 50, 100, 50); 67 ig.setPaint(new Color(255, 127, 127)); 68 ig.fillRect(0, 100, 100, 50); 69 ig.setPaint(Color.black); 70 ig.draw(new Rectangle2D.Double(0.5, 0.5, 199, 149)); 71 ig.dispose(); 72 73 image = image.getSubimage(50, 0, 50, 25); 74 75 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 78 OutputStream os = buildOutputStream(bos); 79 80 PNGEncodeParam params = 82 PNGEncodeParam.getDefaultEncodeParam(image); 83 PNGImageEncoder pngImageEncoder = new PNGImageEncoder(os, params); 84 85 try{ 86 pngImageEncoder.encode(image); 87 os.close(); 88 }catch(Exception e){ 89 return reportException(ERROR_CANNOT_ENCODE_IMAGE, e); 90 } 91 92 InputStream is 94 = buildInputStream(bos); 95 96 PNGImageDecoder pngImageDecoder 97 = new PNGImageDecoder(is, new PNGDecodeParam()); 98 99 RenderedImage decodedRenderedImage = null; 100 try{ 101 decodedRenderedImage = pngImageDecoder.decodeAsRenderedImage(0); 102 }catch(Exception e){ 103 return reportException(ERROR_CANNOT_DECODE_IMAGE, 104 e); 105 } 106 107 BufferedImage decodedImage = null; 108 if(decodedRenderedImage instanceof BufferedImage){ 109 decodedImage = (BufferedImage)decodedRenderedImage; 110 } 111 else{ 112 decodedImage = new BufferedImage(decodedRenderedImage.getWidth(), 113 decodedRenderedImage.getHeight(), 114 BufferedImage.TYPE_INT_ARGB); 115 ig = decodedImage.createGraphics(); 116 ig.drawRenderedImage(decodedRenderedImage, 117 new AffineTransform()); 118 ig.dispose(); 119 } 120 121 if(checkIdentical(image, decodedImage) != true){ 123 return reportError(ERROR_DECODED_DOES_NOT_MATCH_ENCODED); 124 } 125 126 return reportSuccess(); 127 } 128 129 134 public OutputStream buildOutputStream(ByteArrayOutputStream bos){ 135 return bos; 136 } 137 138 143 public InputStream buildInputStream(ByteArrayOutputStream bos){ 144 return new ByteArrayInputStream(bos.toByteArray()); 145 } 146 147 150 public static boolean checkIdentical(BufferedImage imgA, 151 BufferedImage imgB){ 152 boolean identical = true; 153 if(imgA.getWidth() == imgB.getWidth() 154 && 155 imgA.getHeight() == imgB.getHeight()){ 156 int w = imgA.getWidth(); 157 int h = imgA.getHeight(); 158 for(int i=0; i<h; i++){ 159 for(int j=0; j<w; j++){ 160 if(imgA.getRGB(j,i) != imgB.getRGB(j,i)){ 161 identical = false; 162 break; 163 } 164 } 165 if( !identical ){ 166 break; 167 } 168 } 169 } 170 171 return identical; 172 } 173 174 } 175 | Popular Tags |