001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.svggen;
020:
021: import java.awt.Button;
022: import java.awt.Color;
023: import java.awt.Graphics2D;
024: import java.awt.Image;
025: import java.awt.MediaTracker;
026: import java.awt.RenderingHints;
027: import java.awt.Toolkit;
028: import java.awt.image.BufferedImage;
029:
030: /**
031: * This test validates the convertion of Java 2D RescaleOp
032: * into an SVG filer.
033: *
034: * @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
035: * @author <a href="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
036: * @version $Id: Rescale.java 504106 2007-02-06 12:31:06Z dvholten $
037: */
038: public class Rescale implements Painter {
039: public void paint(Graphics2D g) {
040: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
041: RenderingHints.VALUE_ANTIALIAS_ON);
042: //
043: // Load Image
044: //
045: Image image = Toolkit
046: .getDefaultToolkit()
047: .createImage(
048: "test-resources/org/apache/batik/svggen/resources/vangogh.jpg");
049: MediaTracker tracker = new MediaTracker(new Button(""));
050: tracker.addImage(image, 0);
051: try {
052: tracker.waitForAll();
053: } catch (InterruptedException e) {
054: tracker.removeImage(image);
055: image = null;
056: } finally {
057: if (image != null)
058: tracker.removeImage(image);
059: if (tracker.isErrorAny())
060: image = null;
061: if (image != null) {
062: if (image.getWidth(null) < 0
063: || image.getHeight(null) < 0)
064: image = null;
065: }
066: }
067:
068: if (image == null) {
069: throw new Error("Could not load image");
070: }
071:
072: BufferedImage bi = new BufferedImage(image.getWidth(null),
073: image.getHeight(null), BufferedImage.TYPE_INT_RGB);
074: Graphics2D ig = bi.createGraphics();
075: ig.drawImage(image, 0, 0, null);
076:
077: java.awt.image.RescaleOp brighten = new java.awt.image.RescaleOp(
078: 1.5f, 0, null);
079: java.awt.image.RescaleOp darken = new java.awt.image.RescaleOp(
080: .6f, 0, null);
081:
082: // Simply paint the image without and with rescale filters
083: g.setPaint(Color.black);
084: g.drawString("Brighter / Normal / Darker", 10, 20);
085: g.drawImage(bi, brighten, 10, 30);
086: g.drawImage(image, 10 + bi.getWidth() + 10, 30, null);
087: g.drawImage(bi, darken, 10 + 2 * (bi.getWidth() + 10), 30);
088:
089: g.translate(0, bi.getHeight() + 30 + 20);
090: g.drawString("Rescale Red / Green / Blue", 10, 20);
091: java.awt.image.RescaleOp redStress = new java.awt.image.RescaleOp(
092: new float[] { 2.0f, 1.0f, 1.0f },
093: new float[] { 0, 0, 0 }, null);
094: java.awt.image.RescaleOp greenStress = new java.awt.image.RescaleOp(
095: new float[] { 1.0f, 2.0f, 1.0f },
096: new float[] { 0, 0, 0 }, null);
097: java.awt.image.RescaleOp blueStress = new java.awt.image.RescaleOp(
098: new float[] { 1.0f, 1.0f, 2.0f },
099: new float[] { 0, 0, 0 }, null);
100:
101: g.drawImage(bi, redStress, 10, 30);
102: g.drawImage(bi, greenStress, 10 + bi.getWidth() + 10, 30);
103: g.drawImage(bi, blueStress, 10 + 2 * (bi.getWidth() + 10), 30);
104: }
105: }
|