-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageSetter.java
More file actions
140 lines (111 loc) · 4.09 KB
/
ImageSetter.java
File metadata and controls
140 lines (111 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
ImageSetter.java
Adjustable image that can be set to new images (useful for filters, drawing)
*/
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.awt.Rectangle;
public class ImageSetter {
private BufferedImage myImage;
private int width;
private int height;
/**
Construct a PixelImage from a Java BufferedImage
@param bi The image
*/
public ImageSetter( BufferedImage bi ) {
this.myImage = bi;
this.width = bi.getWidth();
this.height = bi.getHeight();
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
/**
Return the image's pixel data as an array of Pixels.
@return The array of pixels
*/
public Pixel[][] getData() {
/* The Raster has origin 0,0 , so the size of the
array is [width][height], where width and height are the
dimensions of the Raster
*/
Raster raster = this.myImage.getRaster();
Pixel[][] data = new Pixel[ raster.getHeight() ][ raster.getWidth() ];
int[] samples = new int[4];
// Translates from java image data to Pixel data
for ( int row = 0; row < raster.getHeight(); row++ ) {
for ( int col = 0; col < raster.getWidth(); col++ ) {
raster.getPixel( col, row, samples );
Pixel ourPixel;
if( isTransparent( row, col ) )
ourPixel = new Pixel( samples[0], samples[1], samples[2], samples[3] );
else
ourPixel = new Pixel( samples[0], samples[1], samples[2] );
data[ row ][ col ] = ourPixel;
}
}
return data;
}
/**
Determines whether a pixel at a given location is an RGBA value (has an alpha channel (has transparency) ) or is an RGB value
@param x The x location of the pixel
@param y The y location of the pixel
@return boolean True if the pixel is RGBA, false otherwise
@see BufferedImage.getRGB( int x, int y )
@see getData()
*/
public boolean isTransparent( int x, int y ) {
int pixel = myImage.getRGB( x, y );
if( ( pixel>>24 ) == 0x00 ) {
return true;
}
return false;
}
/**
Set the image's pixel data from an array. This array must match
that returned by getData() in terms of dimensions.
@param data The array to pull from
@throws IllegalArgumentException if the passed in array does not match
the dimensions of the PixelImage or has pixels with invalid values
*/
public void setData( Pixel[][] data ) throws IllegalArgumentException {
WritableRaster wr = this.myImage.getRaster();
if ( data.length != wr.getHeight() ) {
throw new IllegalArgumentException( "Array size does not match" );
} else if ( data[0].length != wr.getWidth() ) {
throw new IllegalArgumentException( "Array size does not match" );
}
// Translates from an array of Pixel data to java image data
for ( int row = 0; row < wr.getHeight(); row++ ) {
for ( int col = 0; col < wr.getWidth(); col++ ) {
if( isTransparent( row, col ) )
wr.setPixel( col, row, data[ row ][ col ].rgba );
else
wr.setPixel( col, row, data[ row ][ col ].rgb );
}
}
}
/**
Sets the image to a new size based on the dimensions of the rectangle
@param box The Rectangle object containing the x and y coordinate of the upper left corner, and the width and height of the rectangle
@see GraphicsBaseFrame.crop(..)
*/
public void setSize( Rectangle box ) {
myImage = new BufferedImage( box.width, box.height, myImage.getType() );
}
/**
Get the BufferedImage of this image
@return BufferedImage The BufferedImage of this ImageSetter image
*/
public BufferedImage getImage() {
return this.myImage;
}
public void setImage( BufferedImage newImage ) {
this.myImage = newImage;
}
}