/** * Sets the position of the buffer to the offset given by the (x, y) coordinates, in pixels. * * @param x the x pixel to offset to * @param y the y pixel to offset to */ public void position(int x, int y) { if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) throw new IndexOutOfBoundsException( "Specified location: " + x + "," + y + " outside of region"); int bpp = format.getColorComponents(); int offset = ((x + (y * width)) * bpp); position(offset); }
/** * Relative <i>put</i> method which handles color components based on image formats. Does not * offset the buffer. * * <p>See setPixel for details. * * @param c the color to put * @return this object, for chaining */ public PixelData putPixel(int r, int g, int b, int a) { boolean hasAlpha = getFormat().hasAlpha(); int bpp = format.getColorComponents(); if (bpp == 1) { pixels.put((byte) (hasAlpha ? a : avg(r, g, b))); } else if (bpp == 2) { pixels.put(avg(r, g, b)).put((byte) a); } else { pixels.put((byte) r).put((byte) g).put((byte) b); if (bpp >= 4) pixels.put((byte) a); } return this; }
/** * Relative <i>get</i> method which handles color components based on image formats. Does not * offset the buffer. * * <p>Color is converted like so based on the Format's color components: - If getColorComponents * returns 1 and hasAlpha is true: Color(0xFF, 0xFF, 0xFF, A) - If getColorComponents returns 1 * and hasAlpha is false: Color(L, L, L, 0xFF) - If getColorComponents returns 2: Color(L, L, L, * A) - If getColorComponents returns 3: Color(R, G, B, 0xFF) - If getColorComponents returns 4: * Color(R, G, B, A) * * <p>See setPixel for details. * * @param dst the color instance to place the values in * @return the given Color */ public void getPixel(Color dst) { boolean hasAlpha = getFormat().hasAlpha(); int c1 = translate(pixels.get()); int bpp = format.getColorComponents(); if (bpp == 1) { if (hasAlpha) ret(dst, 0xFF, 0xFF, 0xFF, c1); else ret(dst, c1, c1, c1, 0xFF); return; } int c2 = translate(pixels.get()); if (bpp == 2) { ret(dst, c1, c1, c1, c2); return; } int c3 = translate(pixels.get()); if (bpp == 3) { ret(dst, c1, c2, c3, 0xFF); return; } int c4 = translate(pixels.get()); ret(dst, c1, c2, c3, c4); }
/** * Creates a PixelData buffer with the specified size and format. * * <p>Note that Slick currently loads textures with an internal RGBA format; this means that even * if we upload, say, 2-component (e.g. GRAYALPHA) texture data, it will eventually be stored in * OpenGL video memory using RGBA. For better performance and memory management, create textures * with the same internal format as the format given to PixelData. The static 'createTexture' * utility method is intended for this purpose. * * <p>Also note that most drivers will expand formats to RGBA internally, so this optimization may * not be necessary. * * @param width the width in pixels of our data * @param height the height in pixels of our data * @param format the desired format to use during uploading (RGB, RGBA, GRAY, GRAYALPHA, ALPHA) */ public PixelData(int width, int height, ImageData.Format format) { this.format = format; this.width = width; this.height = height; this.pixels = BufferUtils.createByteBuffer(width * height * format.getColorComponents()); }