Java Code Examples for com.badlogic.gdx.graphics.Pixmap#getPixels()

The following examples show how to use com.badlogic.gdx.graphics.Pixmap#getPixels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PL2.java    From riiablo with Apache License 2.0 6 votes vote down vote up
public Texture render() {
  Pixmap pl2Pixmap = new Pixmap(Palette.COLORS, size, Pixmap.Format.RGBA8888);
  ByteBuffer buffer = pl2Pixmap.getPixels();
  for (int i = 0, j = 0; i < size; i++) {
    //buffer.put(colormaps[i]);
    for (int k = 0; k < Palette.COLORS; k++, j += 4) {
      buffer.put(j, colormaps[i][k]);
    }
  }

  buffer.rewind();
  Texture texture = new Texture(pl2Pixmap);
  //texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
  texture.setWrap(Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);
  pl2Pixmap.dispose();
  return texture;
}
 
Example 2
Source File: Ssao.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
/** Computes random field for the ssao shader */
private void createRandomField (int width, int height, Format format) {
	randomField = new Texture(width, height, format);
	randomField.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
	randomField.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

	Pixmap pixels = new Pixmap(width, height, format);
	ByteBuffer bytes = pixels.getPixels();
	int wrote = 0;

	while (wrote < width * height * 4) {
		float x = (MathUtils.random() - 0.5f) * 2.0f;
		float y = (MathUtils.random() - 0.5f) * 2.0f;
		float z = (MathUtils.random() - 0.5f) * 2.0f;
		float l = (float)Math.sqrt(x * x + y * y + z * z);
		if (l <= 1.0f && l > 0.1f) {
			x = (x + 1.0f) * 0.5f;
			y = (y + 1.0f) * 0.5f;
			z = (z + 1.0f) * 0.5f;
			bytes.put((byte)(x * 255f));
			bytes.put((byte)(y * 255f));
			bytes.put((byte)(z * 255f));
			bytes.put((byte)255);
			wrote += 4;
		}
	}

	bytes.flip();
	randomField.draw(pixels, 0, 0);
	pixels.dispose();
}
 
Example 3
Source File: World.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
public void takeScreenshot(String filename, int w) {

		// get viewport
		IntBuffer results = BufferUtils.newIntBuffer(16);
		Gdx.gl20.glGetIntegerv(GL20.GL_VIEWPORT, results);

		int h = (int) (w * getSceneCamera().viewportHeight / getSceneCamera().viewportWidth);

		FrameBuffer fbo = new FrameBuffer(Format.RGB565, w, h, false);

		fbo.begin();
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		draw();
		Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, w, h);

		// restore viewport
		fbo.end(results.get(0), results.get(1), results.get(2), results.get(3));

		// Flip the pixmap upside down
		ByteBuffer pixels = pixmap.getPixels();
		int numBytes = w * h * 4;
		byte[] lines = new byte[numBytes];
		int numBytesPerLine = w * 4;
		for (int i = 0; i < h; i++) {
			pixels.position((h - i - 1) * numBytesPerLine);
			pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
		}
		pixels.clear();
		pixels.put(lines);

		PixmapIO.writePNG(EngineAssetManager.getInstance().getUserFile(filename), pixmap);

		fbo.dispose();
	}
 
Example 4
Source File: PixmapUtils.java    From libgdx-snippets with MIT License 4 votes vote down vote up
/**
 * Sets alpha for all pixels passing the RGB predicate function.
 */
public static void mask(Pixmap pixmap, float alpha, IntPredicate rgb) {

	ByteBuffer pixels = pixmap.getPixels();

	int a = MathUtils.floor(alpha * 255.0f) & 0xff;

	while (pixels.remaining() > 0) {

		int rgba = pixels.getInt();

		if (rgb.test(rgba >>> 8)) {
			pixels.position(pixels.position() - 4);
			pixels.putInt((rgba & 0xffffff00) | a);
		}
	}

	pixels.flip();
}
 
Example 5
Source File: PixmapUtils.java    From libgdx-snippets with MIT License 4 votes vote down vote up
/**
 * Calculates crop regions of the pixmap in up to four directions. Pixel rows/columns are subject
 * to removal if all their pixels have an alpha channel value of exact the same value as given in the parameter.
 */
public static void crop(Pixmap pixmap,
						boolean left,
						boolean bottom,
						boolean right,
						boolean top,
						float alpha,
						CropResult consumer) {

	int width = pixmap.getWidth();
	int height = pixmap.getHeight();

	int a = MathUtils.floor(alpha * 255.0f) & 0xff;

	int minX = left ? width - 1 : 0;
	int maxX = right ? 0 : width - 1;

	int minY = bottom ? height - 1 : 0;
	int maxY = top ? 0 : height - 1;

	ByteBuffer pixels = pixmap.getPixels();

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {

			int rgba = pixels.getInt();

			if ((rgba & 0xff) != a) {

				minX = Math.min(x, minX);
				maxX = Math.max(x, maxX);

				minY = Math.min(y, minY);
				maxY = Math.max(y, maxY);
			}
		}
	}

	pixels.flip();

	consumer.accept(minX, minY, maxX, maxY);
}
 
Example 6
Source File: PixmapRegion.java    From libgdx-snippets with MIT License 4 votes vote down vote up
private void changePixmap(Pixmap pixmap) {
	this.pixmap = pixmap;
	pixels = pixmap.getPixels();
	pixelStride = PixmapUtils.getPixelStride(pixmap.getFormat());
	lineStride = pixmap.getWidth() * pixelStride;
}
 
Example 7
Source File: PixmapUtils.java    From libgdx-snippets with MIT License 3 votes vote down vote up
/**
 * Horizontally mirrors the {@link Pixmap} content, in place, line by line.
 */
public static void flipX(Pixmap pixmap) {

	int width = pixmap.getWidth();
	int height = pixmap.getHeight();

	int bytesPerPixel = getPixelStride(pixmap.getFormat());
	int pitch = width * bytesPerPixel;

	ByteBuffer pixels = pixmap.getPixels();

	byte[] buffer = new byte[pitch];

	for (int y = 0; y < height; y++) {

		pixels.position(y * pitch);
		pixels.get(buffer, 0, pitch);

		pixels.position(y * pitch);

		for (int x = 0, offs = pitch - bytesPerPixel; x < width; x++, offs -= bytesPerPixel) {
			pixels.put(buffer, offs, bytesPerPixel);
		}
	}

	pixels.position(0);
}
 
Example 8
Source File: PixmapUtils.java    From libgdx-snippets with MIT License 3 votes vote down vote up
/**
 * Vertically mirrors the {@link Pixmap} content, in place, line by line.
 */
public static void flipY(Pixmap pixmap) {

	int width = pixmap.getWidth();
	int height = pixmap.getHeight();

	int pitch = width * getPixelStride(pixmap.getFormat());

	ByteBuffer pixels = pixmap.getPixels();

	byte[][] buffer = new byte[2][pitch];

	for (int y = 0; y < height / 2; y++) {

		pixels.position(y * pitch);
		pixels.get(buffer[0], 0, pitch);

		pixels.position((height - y - 1) * pitch);
		pixels.get(buffer[1], 0, pitch);

		pixels.position(y * pitch);
		pixels.put(buffer[1], 0, pitch);

		pixels.position((height - y - 1) * pitch);
		pixels.put(buffer[0], 0, pitch);
	}

	pixels.position(0);
}