Exemple #1
0
 /**
  * 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);
 }
Exemple #2
0
 /**
  * Uploads the pixel data to the given texture at the specified position. This only needs to be
  * called once, after the pixel data has changed.
  *
  * @param texture the texture to modify
  * @param x the x position to place the pixel data on the texture
  * @param y the y position to place the pixel data on the texture
  */
 public void apply(Texture texture, int x, int y) {
   if (x + width > texture.getTextureWidth() || y + height > texture.getTextureHeight())
     throw new IndexOutOfBoundsException("pixel data won't fit in given texture");
   position(length());
   pixels.flip();
   int glFmt = format.getOGLType();
   final SGL GL = Renderer.get();
   texture.bind();
   GL.glTexSubImage2D(
       SGL.GL_TEXTURE_2D, 0, x, y, width, height, glFmt, SGL.GL_UNSIGNED_BYTE, pixels);
 }
Exemple #3
0
 /**
  * Convenience method to create a texture with a given internal format.
  *
  * @param width the width of the empty texture
  * @param height the height of the empty texture
  * @param filter the filter to use
  * @param format the internal format
  * @return a generated texture
  */
 public static Texture createTexture(int width, int height, ImageData.Format format, int filter)
     throws SlickException {
   EmptyImageData data = new EmptyImageData(width, height);
   ByteBuffer dataBuffer = data.getImageBufferData();
   String ref = "pixelhandler:" + width + "x" + height + ":" + format.toString();
   try {
     return InternalTextureLoader.get()
         .createTexture(data, dataBuffer, ref, GL11.GL_TEXTURE_2D, filter, filter, false, format);
   } catch (IOException e) {
     throw new SlickException("Error generating texture", e);
   }
 }
Exemple #4
0
 /**
  * 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;
 }
Exemple #5
0
 /**
  * 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);
 }
Exemple #6
0
 /**
  * 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());
 }