/Users/lyon/j4p/src/ip/vs/ImageUtils.java

1    /** 
2     * Victor Silva - University of Bridgeport 26 April 1997 
3     * 
4     * Various image translation utilities. 
5     */ 
6    package ip.vs; 
7     
8    public class ImageUtils { 
9        int centerX; 
10       int centerY; 
11       int[] pixArray; 
12       int bgColor = 0; 
13    
14       public int[] rotateImage(int inputImage[], int width, 
15                                int height, double angle) { 
16           if (angle % 360 != 0) { 
17               centerX = (int) Math.round(width / 2); 
18               centerY = (int) Math.round(height / 2); 
19               pixArray = new int[width * height]; 
20    
21               //Convert from degrees to radians and calculate cos and sin of angle 
22               //Negate the angle to make sure the rotation is clockwise 
23               double angleRadians = -angle / (180.0 / Math.PI); 
24               double ca = Math.cos(angleRadians); 
25               double sa = Math.sin(angleRadians); 
26    
27               int index = 0; 
28               for (int y = -centerY; y < centerY; y++) { 
29                   for (int x = -centerX; x < centerX; x++) { 
30                       int xs = (int) (x * ca - y * sa) + centerX; 
31                       int ys = (int) (y * ca + x * sa) + centerY; 
32                       if ((xs >= 0) && (xs < width) && (ys >= 0) && (ys < height)) 
33                           pixArray[index++] = inputImage[width * ys + xs]; 
34                       else 
35                           pixArray[index++] = bgColor; 
36                   } 
37               } 
38           } 
39           return (pixArray); 
40       } 
41    
42       public int[] zoomImage( 
43               int inputImage[], 
44               int width, int height, int percent) { 
45           //Duplicate image 
46           centerX = (int) Math.round(width / 2); 
47           centerY = (int) Math.round(height / 2); 
48           pixArray = new int[width * height]; 
49    
50           int index = 0; 
51           for (int y = -centerY; y < centerY; y++) { 
52               for (int x = -centerX; x < centerX; x++) { 
53                   int xs = (x * percent) / 100 + centerX; 
54                   int ys = (y * percent) / 100 + centerY; 
55                   if (xs >= 0 && xs < width && ys >= 0 && ys < height) 
56                       pixArray[index++] = inputImage[width * ys + xs]; 
57                   else 
58                       pixArray[index++] = bgColor; 
59               } 
60           } 
61           return (pixArray); 
62       } 
63   }