1 18 package org.apache.batik.svggen; 19 20 import java.awt.Button ; 21 import java.awt.Color ; 22 import java.awt.Graphics2D ; 23 import java.awt.Image ; 24 import java.awt.MediaTracker ; 25 import java.awt.RenderingHints ; 26 import java.awt.Toolkit ; 27 import java.awt.image.BufferedImage ; 28 29 37 public class Rescale implements Painter { 38 public void paint(Graphics2D g) { 39 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 40 RenderingHints.VALUE_ANTIALIAS_ON); 41 Image image = Toolkit.getDefaultToolkit().createImage("test-resources/org/apache/batik/svggen/resources/vangogh.jpg"); 45 MediaTracker tracker = new MediaTracker (new Button ("")); 46 tracker.addImage(image, 0); 47 try{ 48 tracker.waitForAll(); 49 }catch(InterruptedException e){ 50 tracker.removeImage(image); 51 image = null; 52 }finally { 53 if(image != null) 54 tracker.removeImage(image); 55 if(tracker.isErrorAny()) 56 image = null; 57 if(image != null){ 58 if(image.getWidth(null)<0 || 59 image.getHeight(null)<0) 60 image = null; 61 } 62 } 63 64 if(image == null){ 65 throw new Error ("Could not load image"); 66 } 67 68 BufferedImage bi = new BufferedImage (image.getWidth(null), 69 image.getHeight(null), BufferedImage.TYPE_INT_RGB); 70 Graphics2D ig = bi.createGraphics(); 71 ig.drawImage(image, 0, 0, null); 72 73 java.awt.image.RescaleOp brighten = new java.awt.image.RescaleOp (1.5f, 0, null); 74 java.awt.image.RescaleOp darken = new java.awt.image.RescaleOp (.6f, 0, null); 75 76 g.setPaint(Color.black); 78 g.drawString("Brighter / Normal / Darker", 10, 20); 79 g.drawImage(bi, brighten, 10, 30); 80 g.drawImage(image, 10 + bi.getWidth() + 10, 30, null); 81 g.drawImage(bi, darken, 10 + 2*(bi.getWidth() + 10), 30); 82 83 g.translate(0, bi.getHeight() + 30 + 20); 84 g.drawString("Rescale Red / Green / Blue", 10, 20); 85 java.awt.image.RescaleOp redStress = new java.awt.image.RescaleOp (new float[]{ 2f, 1f, 1f }, 86 new float[]{ 0, 0, 0 }, null); 87 java.awt.image.RescaleOp greenStress = new java.awt.image.RescaleOp (new float[]{ 1f, 2f, 1f }, 88 new float[]{ 0, 0, 0 }, null); 89 java.awt.image.RescaleOp blueStress = new java.awt.image.RescaleOp (new float[]{ 1f, 1f, 2f }, 90 new float[]{ 0, 0, 0 }, null); 91 92 g.drawImage(bi, redStress, 10, 30); 93 g.drawImage(bi, greenStress, 10 + bi.getWidth() + 10, 30); 94 g.drawImage(bi, blueStress, 10 + 2*(bi.getWidth() + 10), 30); 95 } 96 } 97 | Popular Tags |