| Start/ | End/ | |||
| True | False | - | Line | Source |
| 1 | /* | |||
| 2 | * [ JIGA ] | |||
| 3 | * | |||
| 4 | * Copyright (c) 2003 Glenn Sanson <glenn.sanson at free.fr> | |||
| 5 | * | |||
| 6 | * This code is distributed under the GNU Library General Public License | |||
| 7 | * | |||
| 8 | * This library is free software; you can redistribute it and/or | |||
| 9 | * modify it under the terms of the GNU Library General Public License | |||
| 10 | * as published by the Free Software Foundation; either version 2 of the | |||
| 11 | * License, or (at your option) any later version. | |||
| 12 | * | |||
| 13 | * This library is distributed in the hope that it will be useful, but | |||
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
| 16 | * Library General Public License for more details. | |||
| 17 | * | |||
| 18 | * You should have received a copy of the GNU Library General Public License | |||
| 19 | * along with this program; if not, write to the Free Software Foundation, Inc., | |||
| 20 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |||
| 21 | * | |||
| 22 | * | |||
| 23 | * [http://glenn.sanson.free.fr/jiga/] | |||
| 24 | */ | |||
| 25 | ||||
| 26 | package net.library.jiga; | |||
| 27 | ||||
| 28 | import java.awt.Image; | |||
| 29 | import java.awt.MediaTracker; | |||
| 30 | import java.awt.image.PixelGrabber; | |||
| 31 | import java.awt.image.MemoryImageSource; | |||
| 32 | ||||
| 33 | /** | |||
| 34 | * An extention of <code>java.awt.Image</code> that allow features implemented in java 1.2+ (AffineTransform). | |||
| 35 | * This class mainly include image rotation for the moment. | |||
| 36 | * It can also be useful to retrieve pixels data associated with an image | |||
| 37 | * @author Glenn Sanson | |||
| 38 | */ | |||
| 39 | public class EffectImage { | |||
| 40 | ||||
| 41 | public final static int RENDERING_FAST = 0; | |||
| 42 | public final static int RENDERING_SMOOTH = 1; | |||
| 43 | ||||
| 44 | GameApplet applet; | |||
| 45 | ||||
| 46 | int[] imageData; | |||
| 47 | int imgWidth, imgHeight; | |||
| 48 | ||||
| 0 | 0 | - | 49 | private EffectImage() {} |
| 50 | ||||
| 51 | /** | |||
| 52 | * Defines an Image with no Alpha | |||
| 53 | * @param applet The <code>GameApplet</code> of the current process | |||
| 54 | * @param sourceImage The input image | |||
| 55 | */ | |||
| 0 | 0 | - | 56 | public EffectImage(GameApplet applet, String sourceImage) { |
| 57 | this(applet, sourceImage, null); | |||
| 58 | } | |||
| 59 | ||||
| 60 | /** | |||
| 61 | * Defines an image with alpha component. | |||
| 62 | * This constructor was defined to allow use of JPEG images as source for non-alpha data. | |||
| 63 | * The images used for the alpha component must have the same size as the normal image | |||
| 64 | * @param applet The <code>GameApplet</code> of the current process | |||
| 65 | * @param sourceImage The input image | |||
| 66 | * @param alphaImage An image used to retrieve alpha component (Red componant is used). | |||
| 67 | */ | |||
| 95 | 0 | 68 | public EffectImage(GameApplet applet, String sourceImage, String alphaImage) { | |
| 69 | ||||
| 70 | this.applet = applet; | |||
| 71 | ||||
| 72 | Image img = applet.getGameMedia().loadImage(sourceImage); | |||
| 73 | ||||
| 74 | imgWidth = img.getWidth(applet); | |||
| 75 | imgHeight = img.getHeight(applet); | |||
| 76 | ||||
| 77 | imageData = new int[imgWidth * imgHeight]; | |||
| 78 | ||||
| 79 | PixelGrabber pg = new PixelGrabber(img, 0, 0, imgWidth, imgHeight, imageData, 0, imgWidth); | |||
| 95 | 0 | 80 | try { | |
| 81 | pg.grabPixels(); | |||
| 82 | } | |||
| 0 | - | 83 | catch(Exception e) { | |
| 84 | e.printStackTrace(); | |||
| 85 | } | |||
| 86 | ||||
| 95 | 0 | - | 87 | if (alphaImage != null) { |
| 88 | ||||
| 89 | Image alpha = applet.getGameMedia().loadImage(alphaImage); | |||
| 90 | int[] alphaData = new int[imgWidth * imgHeight]; | |||
| 91 | ||||
| 92 | // Alpha is supposed to have same width & height | |||
| 93 | pg = new PixelGrabber(alpha, 0, 0, imgWidth, imgHeight, alphaData, 0, imgWidth); | |||
| 95 | 0 | 94 | try { | |
| 95 | pg.grabPixels(); | |||
| 96 | } | |||
| 0 | - | 97 | catch(Exception e) { | |
| 98 | e.printStackTrace(); | |||
| 99 | } | |||
| 100 | ||||
| 950000 | 95 | 101 | for (int i=0 ; i<imageData.length ; i++) { | |
| 102 | imageData[i] = (imageData[i] & 0xFFFFFF) | ((alphaData[i] & 0xFF0000) << 8); | |||
| 103 | } | |||
| 104 | } | |||
| 105 | } | |||
| 106 | ||||
| 107 | /** | |||
| 108 | * Calculates the output of a rotation of the original image | |||
| 109 | * @param theta The rotation angle (RAD) | |||
| 110 | * @param x The X coordinate of the center | |||
| 111 | * @param y The Y coordinate of the center | |||
| 112 | * @return an <code>Image</code> of the rotation | |||
| 113 | */ | |||
| 3895 | 0 | 114 | public Image getRotate(double theta, double x, double y) { | |
| 3895 | 115 | return getFastRotate(theta, x, y); | ||
| 116 | } | |||
| 117 | ||||
| 118 | /** | |||
| 119 | * Calculates the output of a rotation of the original image | |||
| 120 | * @param theta The rotation angle (RAD) | |||
| 121 | * @param x The X coordinate of the center | |||
| 122 | * @param y The Y coordinate of the center | |||
| 123 | * @param rendering The rendering method to use | |||
| 124 | * @return an <code>Image</code> of the rotation | |||
| 125 | */ | |||
| 0 | 0 | - | 126 | public Image getRotate(double theta, double x, double y, int rendering) { |
| 127 | /* | |||
| 128 | if (rendering == EffectImage.RENDERING_SMOOTH) { | |||
| 129 | } | |||
| 130 | */ | |||
| 131 | ||||
| 0 | - | 132 | return getFastRotate(theta, x, y); | |
| 133 | } | |||
| 134 | ||||
| 135 | /** | |||
| 136 | * Calculates the output of a rotation of the original image, using a fast algorithme. | |||
| 137 | * This algorithm does not interpolate pixel values | |||
| 138 | * @param theta The rotation angle (RAD) | |||
| 139 | * @param x The X coordinate of the center | |||
| 140 | * @param y The Y coordinate of the center | |||
| 141 | * @return an <code>Image</code> of the rotation | |||
| 142 | */ | |||
| 3895 | 0 | 143 | private Image getFastRotate(double theta, double x, double y) { | |
| 144 | ||||
| 145 | int[] output = new int[imageData.length]; | |||
| 146 | ||||
| 147 | // Reusable values | |||
| 148 | double cosTheta = Math.cos(-theta); | |||
| 149 | double sinTheta = Math.sin(-theta); | |||
| 150 | ||||
| 151 | double deltaX = x * (1. - cosTheta) + y * sinTheta; | |||
| 152 | double deltaY = y * (1. - sinTheta) - y * cosTheta; | |||
| 153 | ||||
| 154 | // Calculate | |||
| 389500 | 3895 | 155 | for (int j=0 ; j<imgHeight ; j++) { | |
| 3895E4 | 389500 | 156 | for (int i=0 ; i<imgWidth ; i++) { | |
| 157 | // For each output point | |||
| 158 | // Find its origin | |||
| 159 | int orX = (int)Math.round(i * cosTheta - j * sinTheta + deltaX); | |||
| 160 | int orY = (int)Math.round(i * sinTheta + j * cosTheta + deltaY); | |||
| 161 | ||||
| 4465E3 | 3448E4 | 162 | if (orX < 0 || orX >= imgWidth || orY < 0 || orY >= imgHeight) { | |
| 1084E3 | 162 | T || _ || _ || _ | ||
| 1148E3 | 162 | F || T || _ || _ | ||
| 1084E3 | 162 | F || F || T || _ | ||
| 1148E3 | 162 | F || F || F || T | ||
| 3448E4 | 162 | F || F || F || F | ||
| 163 | // out of scope | |||
| 164 | output[i + j * imgWidth] = 0; | |||
| 165 | } | |||
| 166 | else { | |||
| 167 | // Compute value | |||
| 168 | output[i + j * imgWidth] = imageData[orX + orY * imgWidth]; | |||
| 169 | } | |||
| 170 | } | |||
| 171 | } | |||
| 172 | ||||
| 173 | Image image = applet.createImage(new MemoryImageSource(imgWidth, imgHeight, output, 0, imgWidth)); | |||
| 174 | ||||
| 175 | MediaTracker mediaTracker = new MediaTracker(applet); | |||
| 176 | ||||
| 177 | mediaTracker.addImage(image, 0); | |||
| 3895 | 0 | 178 | try { | |
| 179 | mediaTracker.waitForID(0); | |||
| 180 | } | |||
| 0 | - | 181 | catch(Exception e) { | |
| 182 | System.err.println("Rotation error"); | |||
| 183 | } | |||
| 184 | ||||
| 3895 | 185 | return image; | ||
| 186 | } | |||
| 187 | ||||
| 188 | /** | |||
| 189 | * Retrieve pixels | |||
| 190 | * @return a 1D representation of pixels data | |||
| 191 | */ | |||
| 0 | 0 | - | 192 | public int[] getData() { |
| 0 | - | 193 | return imageData; | |
| 194 | } | |||
| 195 | } | |||
| ***TER 69% (22/32) of SOURCE FILE EffectImage.java | ||||