Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/ImageBuffer.java
1456 views
1
package org.newdawn.slick;
2
3
import java.nio.ByteBuffer;
4
import java.nio.ByteOrder;
5
6
import org.lwjgl.BufferUtils;
7
import org.newdawn.slick.opengl.ImageData;
8
9
/**
10
* A utility for creating images from pixel operations
11
*
12
* Expected usage is:
13
* <code>
14
* ImageBuffer buffer = new ImageBuffer(320,200);
15
* buffer.setRGBA(100,100,50,50,20,255);
16
* ..
17
* Image image = buffer.getImage();
18
* </code>
19
*
20
* @author kevin
21
*/
22
public class ImageBuffer implements ImageData {
23
/** The width of the image */
24
private int width;
25
/** The height of the image */
26
private int height;
27
/** The width of the texture */
28
private int texWidth;
29
/** The height of the texture */
30
private int texHeight;
31
/** The raw data generated for the image */
32
private byte[] rawData;
33
34
/**
35
*
36
* @param width
37
* @param height
38
*/
39
public ImageBuffer(int width, int height) {
40
this.width = width;
41
this.height = height;
42
43
texWidth = get2Fold(width);
44
texHeight = get2Fold(height);
45
46
rawData = new byte[texWidth * texHeight * 4];
47
}
48
49
/**
50
* Retrieve the raw data stored within the image buffer
51
*
52
* @return The raw data in RGBA packed format from within the image buffer
53
*/
54
public byte[] getRGBA() {
55
return rawData;
56
}
57
58
/**
59
* @see org.newdawn.slick.opengl.ImageData#getDepth()
60
*/
61
public int getDepth() {
62
return 32;
63
}
64
65
/**
66
* @see org.newdawn.slick.opengl.ImageData#getHeight()
67
*/
68
public int getHeight() {
69
return height;
70
}
71
72
/**
73
* @see org.newdawn.slick.opengl.ImageData#getTexHeight()
74
*/
75
public int getTexHeight() {
76
return texHeight;
77
}
78
79
/**
80
* @see org.newdawn.slick.opengl.ImageData#getTexWidth()
81
*/
82
public int getTexWidth() {
83
return texWidth;
84
}
85
86
/**
87
* @see org.newdawn.slick.opengl.ImageData#getWidth()
88
*/
89
public int getWidth() {
90
return width;
91
}
92
93
/**
94
* @see org.newdawn.slick.opengl.ImageData#getImageBufferData()
95
*/
96
public ByteBuffer getImageBufferData() {
97
ByteBuffer scratch = BufferUtils.createByteBuffer(rawData.length);
98
scratch.put(rawData);
99
scratch.flip();
100
101
return scratch;
102
}
103
104
/**
105
* Set a pixel in the image buffer
106
*
107
* @param x The x position of the pixel to set
108
* @param y The y position of the pixel to set
109
* @param r The red component to set (0->255)
110
* @param g The green component to set (0->255)
111
* @param b The blue component to set (0->255)
112
* @param a The alpha component to set (0->255)
113
*/
114
public void setRGBA(int x, int y, int r, int g, int b, int a) {
115
if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) {
116
throw new RuntimeException("Specified location: "+x+","+y+" outside of image");
117
}
118
119
int ofs = ((x + (y * texWidth)) * 4);
120
121
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
122
rawData[ofs] = (byte) r;
123
rawData[ofs + 1] = (byte) g;
124
rawData[ofs + 2] = (byte) b;
125
rawData[ofs + 3] = (byte) a;
126
} else {
127
rawData[ofs] = (byte) r;
128
rawData[ofs + 1] = (byte) g;
129
rawData[ofs + 2] = (byte) b;
130
rawData[ofs + 3] = (byte) a;
131
}
132
}
133
134
/**
135
* Get an image generated based on this buffer
136
*
137
* @return The image generated from this buffer
138
*/
139
public Image getImage() {
140
return new Image(this);
141
}
142
143
/**
144
* Get an image generated based on this buffer
145
*
146
* @param filter The filtering method to use when scaling this image
147
* @return The image generated from this buffer
148
*/
149
public Image getImage(int filter) {
150
return new Image(this, filter);
151
}
152
153
/**
154
* Get the closest greater power of 2 to the fold number
155
*
156
* @param fold The target number
157
* @return The power of 2
158
*/
159
private int get2Fold(int fold) {
160
int ret = 2;
161
while (ret < fold) {
162
ret *= 2;
163
}
164
return ret;
165
}
166
167
}
168
169