Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / megamek / client / ui / AWT / util / RotateFilter.java @ 21606

History | View | Annotate | Download (6.62 KB)

1
/*
2
 * MegaMek - Copyright (C) 2000-2003 Ben Mazur (bmazur@sev.org)
3
 * 
4
 *  This program is free software; you can redistribute it and/or modify it 
5
 *  under the terms of the GNU General Public License as published by the Free 
6
 *  Software Foundation; either version 2 of the License, or (at your option) 
7
 *  any later version.
8
 * 
9
 *  This program is distributed in the hope that it will be useful, but 
10
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
11
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
12
 *  for more details.
13
 */
14

    
15
/*
16
 * RotateFilter.java
17
 *
18
 * Created on April 17, 2002, 5:13 PM
19
 * 
20
 * Modified by Javier Carrasco, PRODEVELOP on 19/09/2007
21
 * Monochrome and forced transparency
22
 *//************************************************
23
 *                                                                                                *
24
 *   Modfied By:                                                                *
25
 *   Prodevelop Integraci?n de Tecnolog?as SL        *
26
 *   Conde Salvatierra de ?lava , 34-10                        *
27
 *   46004 Valencia                                                                *
28
 *   Spain                                                                                *
29
 *                                                                                                *
30
 *   +34 963 510 612                                                        *
31
 *   +34 963 510 968                                                        *
32
 *   gis@prodevelop.es                                                        *
33
 *   http://www.prodevelop.es                                        *
34
 *                                                                                                *
35
 *   gvSIG Mobile Team 2006                                         *
36
 *                                                                                          *         
37
 ************************************************/
38

    
39
package megamek.client.ui.AWT.util;
40

    
41
import java.awt.Color;
42
import java.awt.image.ColorModel;
43
import java.awt.image.RGBImageFilter;
44

    
45
/**
46
 * Filters an image by rotating it.  The image is rotated around its center.
47
 *
48
 * TODO: This could be optimized... oh, um... everywhere.  It was pretty late
49
 *  at night when I programmed most of this.
50
 *
51
 * @author  Ben
52
 * @version 
53
 */
54
public class RotateFilter extends RGBImageFilter {
55
    
56
    private static final int  ALPHA_CLIP = 144;
57
    
58
    private double sin;
59
    private double cos;
60
    
61
    private int width;
62
    private int height;
63
    private double cx;
64
    private double cy;
65
    private int[] raster;
66
    private Color col;
67
    
68
    // private int textColorRGB = 0;
69
    
70
    /** Creates new RotateFilter1 */
71
    public RotateFilter(double angle, Color col) {
72
        this.sin = Math.sin(angle);
73
        this.cos = Math.cos(angle);
74
        this.col=col;
75
    }
76
    
77
    /**
78
     * Store the dimensions, when set.
79
     */
80
    public void setDimensions(int width, int height) {
81
        this.width = width;
82
        this.height = height;
83
        cx = width / 2.0;
84
        cy = height / 2.0;
85
        raster = new int[width * height];
86
  consumer.setDimensions(width, height);
87
    }
88
    
89
    /**
90
     * Don't filter, just store.
91
     */
92
    public int filterRGB(int x, int y, int rgb) {
93
        raster[y * width + x] = rgb;
94
        return rgb;
95
        //return col.getRGB() | (col.getRGB() & 0xff000000);
96
    }
97
    
98
    /**
99
     * Here's where we do the work.
100
     */
101
    public void imageComplete(int status) {
102
  if (status == IMAGEERROR || status == IMAGEABORTED) {
103
      consumer.imageComplete(status);
104
      return;
105
  }
106
        // filter everything
107
        rotate();
108
        // done!
109
        consumer.setPixels(0, 0, width, height, ColorModel.getRGBdefault(), raster, 0, width);
110
  consumer.imageComplete(status);
111
    }
112
    
113
    /**
114
     * Rotate all pixels.
115
     */
116
    private void rotate() {
117
        int[] newpixels = new int[width * height];
118
        for (int y = 0; y < height; y++) {
119
            for (int x = 0; x < width; x++) {
120
                newpixels[y * width + x] = rotatedPixel(x, y);
121
            }
122
        }
123
        raster = newpixels;
124
    }
125
    
126
    /**
127
     * Returns the "destination image" pixel
128
     */
129
    private final int rotatedPixel(int x, int y) {
130
        double tx = -(cx - x);
131
        double ty = -(cy - y);
132

    
133
        double rx = cos * tx - sin * ty;
134
        double ry = cos * ty + sin * tx;
135
        
136
        return pixelBilinear(rx + cx, ry + cy);
137
    }
138
    
139
    /**
140
     * Returns a pixel from the source image
141
     */
142
    private final int pixel(int x, int y) {
143
        if (x < 0 || y < 0 || x >= width || y >= height) {
144
            return 0;
145
        }
146
                return raster[y * width + x];
147
    }
148
    
149
    private final int alpha(int pix) {
150
        return (pix >> 24) & 0xff;
151
    }
152
    
153
    private final int blue(int pix) {
154
        return pix & 0xff;
155
    }
156
    
157
    private final int red(int pix) {
158
        return (pix >> 16) & 0xff;
159
    }
160
    
161
    private final int green(int pix) {
162
        return (pix >> 8) & 0xff;
163
    }
164
    
165
    private final int combine(int alpha, int red, int green, int blue) {
166
        //return (alpha > ALPHA_CLIP ? 0xFF000000 : 0) | (red << 16) | (green << 8) | (blue);
167
            return (alpha << 24) | (red << 16) | (green << 8) | (blue);
168
    }
169
    
170
    /**
171
     * Get the bilinearly calculated pixel at the coordinates.  
172
     * Lazy black & white mode.
173
     */
174
    private int pixelBilinear(double x, double y) {
175
        int fx = (int)Math.floor(x);
176
        int fy = (int)Math.floor(y);
177
        /*
178
        int alpha0 = alpha(pixel(fx,   fy));
179
        int alpha1 = alpha(pixel(fx+1, fy));
180
        int alpha2 = alpha(pixel(fx,   fy+1));
181
        int alpha3 = alpha(pixel(fx+1, fy+1));
182
        
183
        if (alpha0!=0)
184
                alpha0 = red(pixel(fx,   fy));
185
        if (alpha1!=0)
186
                alpha1 = red(pixel(fx+1,   fy));
187
        if (alpha2!=0)
188
                alpha2 = red(pixel(fx,   fy+1));
189
        if (alpha3!=0)
190
                alpha3 = red(pixel(fx+1,   fy+1));
191
        
192
        // don't bother calculating transparent pixels
193
        if (alpha0 == 0 && alpha1 == 0 && alpha2 == 0 && alpha3 == 0) {
194
            return 0;
195
        }
196
*/
197
        int red0 = red(pixel(fx,   fy));
198
        int red1 = red(pixel(fx+1, fy));
199
        int red2 = red(pixel(fx,   fy+1));
200
        int red3 = red(pixel(fx+1, fy+1));
201
/*
202
        int green0 = green(pixel(fx,   fy));
203
        int green1 = green(pixel(fx+1, fy));
204
        int green2 = green(pixel(fx,   fy+1));
205
        int green3 = green(pixel(fx+1, fy+1));
206

207
        int blue0 = blue(pixel(fx,   fy));
208
        int blue1 = blue(pixel(fx+1, fy));
209
        int blue2 = blue(pixel(fx,   fy+1));
210
        int blue3 = blue(pixel(fx+1, fy+1));
211
*/
212
        double xv = x - fx;
213
        double yv = y - fy;
214

    
215
        double mul0 = (1.0 - xv) * (1.0 - yv);
216
        double mul1 = xv * (1.0 - yv);
217
        double mul2 = (1.0 - xv) * yv;
218
        double mul3 = xv * yv;        
219
                
220
        //int alpha = (int)Math.round(mul0*alpha0 + mul1*alpha1 + mul2*alpha2 + mul3*alpha3);
221
        //int blue = (int)Math.round(mul0*blue0 + mul1*blue1 + mul2*blue2 + mul3*blue3);
222
        int red = (int)Math.round(mul0*red0 + mul1*red1 + mul2*red2 + mul3*red3);
223
        //int green = (int)Math.round(mul0*green0 + mul1*green1 + mul2*green2 + mul3*green3);
224
        /*int blue=red;
225
        int green=red;
226
        int alpha=red;*/
227
        
228
        return combine(red, col.getRed(), col.getGreen(), col.getBlue());
229
    }
230
    
231
}