My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.graphics;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.GdxRuntimeException;

/** <p>
* A Pixmap represents an image in memory. It has a width and height expressed in pixels as well as a {@link Format} specifying
* the number and order of color components per pixel. Coordinates of pixels are specified with respect to the top left corner of
* the image, with the x-axis pointing to the right and the y-axis pointing downwards.
* </p>
*
* <p>
* By default all methods use blending. You can disable blending with {@link Pixmap#setBlending(Blending)}. The
* {@link Pixmap#drawPixmap(Pixmap, int, int, int, int, int, int, int, int)} method will scale and stretch the source image to a
* target image. There either nearest neighbour or bilinear filtering can be used.
* </p>
*
* <p>
* A Pixmap stores its data in native heap memory. It is mandatory to call {@link Pixmap#dispose()} when the pixmap is no longer
* needed, otherwise memory leaks will result
* </p>
*
* @author badlogicgames@gmail.com */
public class Pixmap implements Disposable {
/** Different pixel formats.
*
* @author mzechner */
public enum Format {
Alpha, Intensity, LuminanceAlpha, RGB565, RGBA4444, RGB888, RGBA8888;

public static int toGdx2DPixmapFormat (Format format) {
if (format == Alpha) return Gdx2DPixmap.GDX2D_FORMAT_ALPHA;
if (format == Intensity) return Gdx2DPixmap.GDX2D_FORMAT_ALPHA;
if (format == LuminanceAlpha) return Gdx2DPixmap.GDX2D_FORMAT_LUMINANCE_ALPHA;
if (format == RGB565) return Gdx2DPixmap.GDX2D_FORMAT_RGB565;
if (format == RGBA4444) return Gdx2DPixmap.GDX2D_FORMAT_RGBA4444;
if (format == RGB888) return Gdx2DPixmap.GDX2D_FORMAT_RGB888;
if (format == RGBA8888) return Gdx2DPixmap.GDX2D_FORMAT_RGBA8888;
throw new GdxRuntimeException("Unknown Format: " + format);
}

public static Format fromGdx2DPixmapFormat (int format) {
if (format == Gdx2DPixmap.GDX2D_FORMAT_ALPHA) return Alpha;
if (format == Gdx2DPixmap.GDX2D_FORMAT_LUMINANCE_ALPHA) return LuminanceAlpha;
if (format == Gdx2DPixmap.GDX2D_FORMAT_RGB565) return RGB565;
if (format == Gdx2DPixmap.GDX2D_FORMAT_RGBA4444) return RGBA4444;
if (format == Gdx2DPixmap.GDX2D_FORMAT_RGB888) return RGB888;
if (format == Gdx2DPixmap.GDX2D_FORMAT_RGBA8888) return RGBA8888;
throw new GdxRuntimeException("Unknown Gdx2DPixmap Format: " + format);
}
}

/** Blending functions to be set with {@link Pixmap#setBlending}.
* @author mzechner */
public enum Blending {
None, SourceOver
}

/** Filters to be used with {@link Pixmap#drawPixmap(Pixmap, int, int, int, int, int, int, int, int)}.
*
* @author mzechner */
public enum Filter {
NearestNeighbour, BiLinear
}

/** global blending state **/
private static Blending blending = Blending.SourceOver;

final Gdx2DPixmap pixmap;
int color = 0;

private boolean disposed;

/** Sets the type of {@link Blending} to be used for all operations. Default is {@link Blending#SourceOver}.
* @param blending the blending type */
public static void setBlending (Blending blending) {
Pixmap.blending = blending;
Gdx2DPixmap.setBlend(blending == Blending.None ? 0 : 1);
}

/** Sets the type of interpolation {@link Filter} to be used in conjunction with
* {@link Pixmap#drawPixmap(Pixmap, int, int, int, int, int, int, int, int)}.
* @param filter the filter. */
public static void setFilter (Filter filter) {
Gdx2DPixmap.setScale(filter == Filter.NearestNeighbour ? Gdx2DPixmap.GDX2D_SCALE_NEAREST : Gdx2DPixmap.GDX2D_SCALE_LINEAR);
}

/** Creates a new Pixmap instance with the given width, height and format.
* @param width the width in pixels
* @param height the height in pixels
* @param format the {@link Format} */
public Pixmap (int width, int height, Format format) {
pixmap = new Gdx2DPixmap(width, height, Format.toGdx2DPixmapFormat(format));
setColor(0, 0, 0, 0);
fill();
}

/** Creates a new Pixmap instance from the given encoded image data. The image can be encoded as JPEG, PNG or BMP.
* @param encodedData the encoded image data
* @param offset the offset
* @param len the length */
public Pixmap (byte[] encodedData, int offset, int len) {
try {
pixmap = new Gdx2DPixmap(encodedData, offset, len, 0);
} catch (IOException e) {
throw new GdxRuntimeException("Couldn't load pixmap from image data", e);
}
}

/** Creates a new Pixmap instance from the given file. The file must be a Png, Jpeg or Bitmap. Paletted formats are not
* supported.
*
* @param file the {@link FileHandle} */
public Pixmap (FileHandle file) {
try {
byte[] bytes = file.readBytes();
pixmap = new Gdx2DPixmap(bytes, 0, bytes.length, 0);
} catch (Exception e) {
throw new GdxRuntimeException("Couldn't load file: " + file, e);
}
}

/** Constructs a new Pixmap from a {@link Gdx2DPixmap}.
* @param pixmap */
public Pixmap (Gdx2DPixmap pixmap) {
this.pixmap = pixmap;
}

/** Sets the color for the following drawing operations
* @param color the color, encoded as RGBA8888 */
public void setColor (int color) {
this.color = color;
}

/** Sets the color for the following drawing operations.
*
* @param r The red component.
* @param g The green component.
* @param b The blue component.
* @param a The alpha component. */
public void setColor (float r, float g, float b, float a) {
color = Color.rgba8888(r, g, b, a);
}

/** Sets the color for the following drawing operations.
* @param color The color. */
public void setColor (Color color) {
this.color = Color.rgba8888(color.r, color.g, color.b, color.a);
}

/** Fills the complete bitmap with the currently set color. */
public void fill () {
pixmap.clear(color);
}

// /**
// * Sets the width in pixels of strokes.
// *
// * @param width The stroke width in pixels.
// */
// public void setStrokeWidth (int width);

/** Draws a line between the given coordinates using the currently set color.
*
* @param x The x-coodinate of the first point
* @param y The y-coordinate of the first point
* @param x2 The x-coordinate of the first point
* @param y2 The y-coordinate of the first point */
public void drawLine (int x, int y, int x2, int y2) {
pixmap.drawLine(x, y, x2, y2, color);
}

/** Draws a rectangle outline starting at x, y extending by width to the right and by height downwards (y-axis points downwards)
* using the current color.
*
* @param x The x coordinate
* @param y The y coordinate
* @param width The width in pixels
* @param height The height in pixels */
public void drawRectangle (int x, int y, int width, int height) {
pixmap.drawRect(x, y, width, height, color);
}

/** Draws an area form another Pixmap to this Pixmap.
*
* @param pixmap The other Pixmap
* @param x The target x-coordinate (top left corner)
* @param y The target y-coordinate (top left corner)
*/
public void drawPixmap(Pixmap pixmap, int x, int y) {
drawPixmap(pixmap, x, y, 0, 0, pixmap.getWidth(), pixmap.getHeight());
}

/** Draws an area form another Pixmap to this Pixmap.
*
* @param pixmap The other Pixmap
* @param x The target x-coordinate (top left corner)
* @param y The target y-coordinate (top left corner)
* @param srcx The source x-coordinate (top left corner)
* @param srcy The source y-coordinate (top left corner);
* @param srcWidth The width of the area form the other Pixmap in pixels
* @param srcHeight The height of the area form the other Pixmap in pixles */
public void drawPixmap (Pixmap pixmap, int x, int y, int srcx, int srcy, int srcWidth, int srcHeight) {
this.pixmap.drawPixmap(pixmap.pixmap, srcx, srcy, x, y, srcWidth, srcHeight);
}

/** Draws an area form another Pixmap to this Pixmap. This will automatically scale and stretch the source image to the
* specified target rectangle. Use {@link Pixmap#setFilter(Filter)} to specify the type of filtering to be used (nearest
* neighbour or bilinear).
*
* @param pixmap The other Pixmap
* @param srcx The source x-coordinate (top left corner)
* @param srcy The source y-coordinate (top left corner);
* @param srcWidth The width of the area form the other Pixmap in pixels
* @param srcHeight The height of the area form the other Pixmap in pixles
* @param dstx The target x-coordinate (top left corner)
* @param dsty The target y-coordinate (top left corner)
* @param dstWidth The target width
* @param dstHeight the target height */
public void drawPixmap (Pixmap pixmap, int srcx, int srcy, int srcWidth, int srcHeight, int dstx, int dsty, int dstWidth,
int dstHeight) {
this.pixmap.drawPixmap(pixmap.pixmap, srcx, srcy, srcWidth, srcHeight, dstx, dsty, dstWidth, dstHeight);
}

/** Fills a rectangle starting at x, y extending by width to the right and by height downwards (y-axis points downwards) using
* the current color.
*
* @param x The x coordinate
* @param y The y coordinate
* @param width The width in pixels
* @param height The height in pixels */
public void fillRectangle (int x, int y, int width, int height) {
pixmap.fillRect(x, y, width, height, color);
}

/** Draws a circle outline with the center at x,y and a radius using the current color and stroke width.
*
* @param x The x-coordinate of the center
* @param y The y-coordinate of the center
* @param radius The radius in pixels */
public void drawCircle (int x, int y, int radius) {
pixmap.drawCircle(x, y, radius, color);
}

/** Fills a circle with the center at x,y and a radius using the current color.
*
* @param x The x-coordinate of the center
* @param y The y-coordinate of the center
* @param radius The radius in pixels */
public void fillCircle (int x, int y, int radius) {
pixmap.fillCircle(x, y, radius, color);
}

/** Returns the 32-bit RGBA8888 value of the pixel at x, y. For Alpha formats the RGB components will be one.
*
* @param x The x-coordinate
* @param y The y-coordinate
* @return The pixel color in RGBA8888 format. */
public int getPixel (int x, int y) {
return pixmap.getPixel(x, y);
}

/** @return The width of the Pixmap in pixels. */
public int getWidth () {
return pixmap.getWidth();
}

/** @return The height of the Pixmap in pixels. */
public int getHeight () {
return pixmap.getHeight();
}

/** Releases all resources associated with this Pixmap. */
public void dispose () {
if(disposed) throw new GdxRuntimeException("Pixmap already disposed!");
pixmap.dispose();
disposed = true;
}

/** Draws a pixel at the given location with the current color.
*
* @param x the x-coordinate
* @param y the y-coordinate */
public void drawPixel (int x, int y) {
pixmap.setPixel(x, y, color);
}

/** Draws a pixel at the given location with the given color.
*
* @param x the x-coordinate
* @param y the y-coordinate
* @param color the color in RGBA8888 format. */
public void drawPixel (int x, int y, int color) {
pixmap.setPixel(x, y, color);
}

/** Returns the OpenGL ES format of this Pixmap. Used as the seventh parameter to
* {@link GLCommon#glTexImage2D(int, int, int, int, int, int, int, int, java.nio.Buffer)}.
* @return one of GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA. */
public int getGLFormat () {
return pixmap.getGLFormat();
}

/** Returns the OpenGL ES format of this Pixmap. Used as the third parameter to
* {@link GLCommon#glTexImage2D(int, int, int, int, int, int, int, int, java.nio.Buffer)}.
* @return one of GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA. */
public int getGLInternalFormat () {
return pixmap.getGLInternalFormat();
}

/** Returns the OpenGL ES type of this Pixmap. Used as the eighth parameter to
* {@link GLCommon#glTexImage2D(int, int, int, int, int, int, int, int, java.nio.Buffer)}.
* @return one of GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4 */
public int getGLType () {
return pixmap.getGLType();
}

/** Returns the direct ByteBuffer holding the pixel data. For the format Alpha each value is encoded as a byte. For the format
* LuminanceAlpha the luminance is the first byte and the alpha is the second byte of the pixel. For the formats RGB888 and
* RGBA8888 the color components are stored in a single byte each in the order red, green, blue (alpha). For the formats RGB565
* and RGBA4444 the pixel colors are stored in shorts in machine dependent order.
* @return the direct {@link ByteBuffer} holding the pixel data. */
public ByteBuffer getPixels () {
if(disposed) throw new GdxRuntimeException("Pixmap already disposed");
return pixmap.getPixels();
}

/** @return the {@link Format} of this Pixmap. */
public Format getFormat () {
return Format.fromGdx2DPixmapFormat(pixmap.getFormat());
}

/** @return the currently set {@link Blending} */
public static Blending getBlending () {
return blending;
}
}

Change log

r3357 by badlogicgames on Feb 14, 2012   Diff
[fixed] LwjglInput#getTextInput didn't
post the result on the rendering thread
[fixed]  issue 705 , forgot to commit the
changes to the other Mesh constructors,
VertexArray needs IndexArray instead of
IndexBufferObject.
[fixed] CameraGroupStrategy
enables/disables depth testing
[added] Plane has a new setter
[changed] Pixmap will throw a runtime
exception in case pixels are accessed when
the Pixmap is already disposed.
Go to: 
Project members, sign in to write a code review

Older revisions

r3350 by badlogicgames on Feb 9, 2012   Diff
[added] new FileHandle#writeBytes
method that takes offsets...
[added] check in Pixmap#dispose() to
detect double disposing
r3339 by badlogicgames on Feb 6, 2012   Diff
[fixed] posting runnables that post
runnables via Gdx.app.postRunnable()
works now on all backends.
[added] new Pixmap#drawPixmap method
that eases the pain a little
...
r2964 by nathan.sweet on Dec 15, 2011   Diff
Javadocs.
All revisions of this file

File info

Size: 14199 bytes, 358 lines
Powered by Google Project Hosting