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 import javax.annotation.Nonnull;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class ImageBuffer implements ImageData {
25
26 private final int width;
27
28 private final int height;
29
30 private final int texWidth;
31
32 private final int texHeight;
33
34 private final byte[] rawData;
35
36
37
38
39
40
41 public ImageBuffer(int width, int height) {
42 this.width = width;
43 this.height = height;
44
45 texWidth = get2Fold(width);
46 texHeight = get2Fold(height);
47
48 rawData = new byte[texWidth * texHeight * 4];
49 }
50
51
52
53
54
55
56 public byte[] getRGBA() {
57 return rawData;
58 }
59
60
61
62
63 @Nonnull
64 public Format getFormat() {
65 return Format.RGBA;
66 }
67
68
69
70
71 public int getHeight() {
72 return height;
73 }
74
75
76
77
78 public int getTexHeight() {
79 return texHeight;
80 }
81
82
83
84
85 public int getTexWidth() {
86 return texWidth;
87 }
88
89
90
91
92 public int getWidth() {
93 return width;
94 }
95
96
97
98
99 public ByteBuffer getImageBufferData() {
100 ByteBuffer scratch = BufferUtils.createByteBuffer(rawData.length);
101 scratch.put(rawData);
102 scratch.flip();
103
104 return scratch;
105 }
106
107
108
109
110
111
112
113
114
115
116
117 public void setRGBA(int x, int y, int r, int g, int b, int a) {
118 if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) {
119 throw new RuntimeException("Specified location: "+x+","+y+" outside of image");
120 }
121
122 int ofs = ((x + (y * texWidth)) * 4);
123
124 if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
125 rawData[ofs] = (byte) b;
126 rawData[ofs + 1] = (byte) g;
127 rawData[ofs + 2] = (byte) r;
128 rawData[ofs + 3] = (byte) a;
129 } else {
130 rawData[ofs] = (byte) r;
131 rawData[ofs + 1] = (byte) g;
132 rawData[ofs + 2] = (byte) b;
133 rawData[ofs + 3] = (byte) a;
134 }
135 }
136
137
138
139
140
141
142 @Nonnull
143 public Image getImage() {
144 return new Image(this);
145 }
146
147
148
149
150
151
152
153 @Nonnull
154 public Image getImage(int filter) {
155 return new Image(this, filter);
156 }
157
158
159
160
161
162
163
164 private int get2Fold(int fold) {
165 int ret = 2;
166 while (ret < fold) {
167 ret *= 2;
168 }
169 return ret;
170 }
171
172 }