001/* Static methods that Transform java.awt.Images 002 003 @Copyright (c) 2001-2013 The Regents of the University of California. 004 All rights reserved. 005 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the 009 above copyright notice and the following two paragraphs appear in all 010 copies of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION 2 026 COPYRIGHTENDKEY 027 */ 028package ptolemy.actor.lib.image; 029 030import java.awt.Graphics2D; 031import java.awt.Image; 032import java.awt.geom.AffineTransform; 033import java.awt.image.BufferedImage; 034 035////////////////////////////////////////////////////////////////////////// 036//// Transform 037 038/** 039 Transform an image by either rotating or scaling it. 040 041 <p>Some of the code in this file is based on code from 042 <a href="http://download.oracle.com/javase/tutorial/extra/fullscreen/example.html#in_browser">http://download.oracle.com/javase/tutorial/extra/fullscreen/example.html</a>. 043 044 @author Christopher Hylands 045 @version $Id$ 046 @since Ptolemy II 3.0 047 @Pt.ProposedRating Red (cxh) 048 @Pt.AcceptedRating Red (cxh) 049 */ 050public class Transform { 051 /** Rotate an Image. 052 * @param originalImage The java.awt.Image to rotate. 053 * @param rotate The number of degrees to rotate the originalImage 054 * @return The rotated Image. 055 */ 056 public static Image rotate(Image originalImage, int rotate) { 057 int width = originalImage.getWidth(null); 058 int height = originalImage.getHeight(null); 059 int newWidth = width; 060 int newHeight = height; 061 062 if (rotate == 90 || rotate == 270) { 063 newWidth = height; 064 newHeight = width; 065 } 066 067 // Create an image buffer in which to paint on. 068 BufferedImage outputImage = new BufferedImage(newWidth, newHeight, 069 BufferedImage.TYPE_INT_RGB); 070 071 // Set the rotation 072 AffineTransform rotateAffineTransform = new AffineTransform(); 073 074 // Convert rotate to radians. 075 rotateAffineTransform.rotate(rotate * (Math.PI / 180.0F), width / 2.0, 076 height / 2.0); 077 078 if (!(rotate == 180 || rotate == 360)) { 079 rotateAffineTransform.translate(width / 2 - height / 2, 080 width / 2 - height / 2); 081 } 082 083 // Paint image. 084 Graphics2D graphics2d = outputImage.createGraphics(); 085 graphics2d.drawImage(originalImage, rotateAffineTransform, null); 086 graphics2d.dispose(); 087 088 return outputImage; 089 } 090 091 /** Scale an image so that its maximum dimension is no larger than 092 * the maximumDimension parameter. 093 * This method is useful for creating thumbnail images. 094 * @param originalImage The java.awt.Image to rotate. 095 * @param maximumDimension The maximum x or y dimension 096 * @return The scaled Image. 097 */ 098 public static Image scale(Image originalImage, int maximumDimension) { 099 // Determine the scale. 100 double scale = (double) maximumDimension 101 / (double) originalImage.getHeight(null); 102 103 if (originalImage.getWidth(null) > originalImage.getHeight(null)) { 104 scale = (double) maximumDimension 105 / (double) originalImage.getWidth(null); 106 } 107 108 // Determine size of new image. 109 // One of them should equal maximumDimension. 110 int scaledWidth = (int) (scale * originalImage.getWidth(null)); 111 int scaledHeight = (int) (scale * originalImage.getHeight(null)); 112 113 // Create an image buffer in which to paint on. 114 BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, 115 BufferedImage.TYPE_INT_RGB); 116 117 // Set the scale. 118 AffineTransform scaleAffineTransform = new AffineTransform(); 119 120 // If the image is smaller than the desired image size, 121 // don't bother scaling. 122 //if (scale < 1.0d) { 123 scaleAffineTransform.scale(scale, scale); 124 125 //} 126 // Paint image. 127 Graphics2D graphics2d = outputImage.createGraphics(); 128 graphics2d.drawImage(originalImage, scaleAffineTransform, null); 129 graphics2d.dispose(); 130 return outputImage; 131 } 132}