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

The following examples show how to use com.badlogic.gdx.graphics.Pixmap#getFormat() . These examples are extracted from open source projects. 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 Project: gdx-gltf   File: FacedMultiCubemapData.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void consumeCubemapData () {
	for(int level = 0 ; level<levels ; level++){
		for (int i = 0; i < 6; i++) {
			int index = level * 6 + i;
			if (data[index].getType() == TextureData.TextureDataType.Custom) {
				data[index].consumeCustomData(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
			} else {
				Pixmap pixmap = data[index].consumePixmap();
				boolean disposePixmap = data[index].disposePixmap();
				if (data[index].getFormat() != pixmap.getFormat()) {
					Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), data[index].getFormat());
					tmp.setBlending(Blending.None);
					tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight());
					if (data[index].disposePixmap()) pixmap.dispose();
					pixmap = tmp;
					disposePixmap = true;
				}
				Gdx.gl.glPixelStorei(GL20.GL_UNPACK_ALIGNMENT, 1);
				Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, level, pixmap.getGLInternalFormat(), pixmap.getWidth(),
					pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
				if (disposePixmap) pixmap.dispose();
			}
		}
	}
}
 
Example 2
/**
 * Splits the image 
 * @param image
 * @param width
 * @param height
 * @param row
 * @param col
 * @return
 */
public static Pixmap[] splitPixmap(Pixmap image, int width, int height, int row, int col) {                
    int total = col * row; // total returned images
    int frame = 0; // frame counter

    int w = width / col;
    int h = height / row;

    Pixmap[] images = new Pixmap[total];

    for (int j = 0; j < row; j++) {
        for (int i = 0; i < col; i++) {                
            Pixmap region = new Pixmap(w, h, image.getFormat());
            region.drawPixmap(image, 0, 0, i * w, j * h, w, h);
            
            images[frame++] = region;
        }
    }

    return images;
}
 
Example 3
Source Project: gdx-fireapp   File: ImageHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Transforms byte[] to Texture Region.
 * <p>
 * If you are going to call this method inside firebase callback remember to wrap it<p>
 * into {@code Gdx.app.postRunnable(Runnable)}.
 * The texture will be changed so that it has sides with length of power of 2.
 *
 * @param bytes Byte array with image description
 * @return Texture region representation of given byte array
 */
public TextureRegion createTextureFromBytes(byte[] bytes) {
    Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
    final int orgWidth = pixmap.getWidth();
    final int orgHeight = pixmap.getHeight();
    int width = MathUtils.nextPowerOfTwo(orgWidth);
    int height = MathUtils.nextPowerOfTwo(orgHeight);
    final Pixmap potPixmap = new Pixmap(width, height, pixmap.getFormat());
    potPixmap.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight());
    pixmap.dispose();
    TextureRegion region = new TextureRegion(new Texture(potPixmap), 0, 0, orgWidth, orgHeight);
    potPixmap.dispose();
    return region;
}
 
Example 4
Source Project: libgdx-snippets   File: PixmapUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Returns a copy of the given pixmap, cropped about the given dimensions.
 */
public static Pixmap crop(Pixmap pixmap, int left, int bottom, int width, int height) {

	Pixmap result = new Pixmap(width, height, pixmap.getFormat());
	result.drawPixmap(pixmap, 0, 0, left, bottom, width, height);

	return result;
}
 
Example 5
Source Project: Cubes   File: AOTextureGenerator.java    License: MIT License 5 votes vote down vote up
protected static void generate(FileHandle file) {
  final long startTime = System.currentTimeMillis();
  startExecutor();
  Pixmap weak = generate(0.5f);
  Pixmap strong = generate(0.1f);

  Pixmap out = new Pixmap(weak.getWidth(), weak.getHeight(), weak.getFormat());
  Color outColor = new Color();
  Color tempColor = new Color();
  for (int x = 0; x < out.getWidth(); x++) {
    for (int y = 0; y < out.getHeight(); y++) {
      outColor.r = tempColor.set(weak.getPixel(x, y)).r;
      outColor.g = tempColor.set(strong.getPixel(x, y)).r;
      outColor.b = 1f;
      outColor.a = 1f;
      out.drawPixel(x, y, Color.rgba8888(outColor));
    }
  }
  weak.dispose();
  strong.dispose();

  System.out.println("Took " + ((System.currentTimeMillis() - startTime) / 1000 / 60) + " minutes");
  System.out.println("Writing to: " + file.path());
  System.out.println();

  PixmapIO.writePNG(file, out);
  out.dispose();
}
 
Example 6
Source Project: gdx-proto   File: HeightMap.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param file 8-bit PNG file heightmap data
 * @param heightScale vertical scaling (y axis)
 * @param widthScale width on the x-z plane (distance between points)
 * @param whiteHigh height is represented by white (instead of black)
 */
public HeightMap(FileHandle file, float heightScale, float widthScale, boolean whiteHigh, int smoothingPasses) {
	// TODO whiteHigh seems to breaking the normals
	this.heightScale = heightScale;
	this.widthScale = widthScale;

	Pixmap pix = new Pixmap(file);
	if (pix.getFormat() != Pixmap.Format.Alpha) {
		throw new GdxRuntimeException("Pixmap must be format Pixmap.Alpha (8-bit Grayscale), not: " + pix.getFormat());
	}
	int pixWidth = pix.getWidth();
	int pixHeight = pix.getHeight();
	int w = pixWidth;
	int h = pixHeight;
	System.out.println("w,h: " + w + ", " + h);
	heights = new float[h][w];

	boolean countWidth = true;
	for (int z = 0; z < pixHeight; z ++) {
		depth++;
		for (int x = 0; x < pixWidth; x ++) {
			if (countWidth) width++;
			//System.out.printf("pix value: (%d, %d): %d\n", x, z,  pix.getPixel(x, z));
			int height;
			if (whiteHigh) {
				height = 256 - (-1 * pix.getPixel(x, z));
			} else {
				height = -1 * pix.getPixel(x, z);
			}
			heights[z][x] = height;
			numPoints++;
		}
		countWidth = false;
	}
	smoothVertexPositions(smoothingPasses);
	updateDimensions();
}
 
Example 7
Source Project: mini2Dx   File: Lwjgl3Mini2DxCursor.java    License: Apache License 2.0 5 votes vote down vote up
Lwjgl3Mini2DxCursor(Lwjgl3Mini2DxWindow window, Pixmap pixmap, int xHotspot, int yHotspot) {
	this.window = window;
	if (pixmap.getFormat() != Pixmap.Format.RGBA8888) {
		throw new GdxRuntimeException("Cursor image pixmap is not in RGBA8888 format.");
	}

	if ((pixmap.getWidth() & (pixmap.getWidth() - 1)) != 0) {
		throw new GdxRuntimeException(
				"Cursor image pixmap width of " + pixmap.getWidth() + " is not a power-of-two greater than zero.");
	}

	if ((pixmap.getHeight() & (pixmap.getHeight() - 1)) != 0) {
		throw new GdxRuntimeException("Cursor image pixmap height of " + pixmap.getHeight()
				+ " is not a power-of-two greater than zero.");
	}

	if (xHotspot < 0 || xHotspot >= pixmap.getWidth()) {
		throw new GdxRuntimeException("xHotspot coordinate of " + xHotspot
				+ " is not within image width bounds: [0, " + pixmap.getWidth() + ").");
	}

	if (yHotspot < 0 || yHotspot >= pixmap.getHeight()) {
		throw new GdxRuntimeException("yHotspot coordinate of " + yHotspot
				+ " is not within image height bounds: [0, " + pixmap.getHeight() + ").");
	}

	this.pixmapCopy = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Pixmap.Format.RGBA8888);
	this.pixmapCopy.setBlending(Pixmap.Blending.None);
	this.pixmapCopy.drawPixmap(pixmap, 0, 0);

	glfwImage = GLFWImage.malloc();
	glfwImage.width(pixmapCopy.getWidth());
	glfwImage.height(pixmapCopy.getHeight());
	glfwImage.pixels(pixmapCopy.getPixels());
	glfwCursor = GLFW.glfwCreateCursor(glfwImage, xHotspot, yHotspot);
	cursors.add(this);
}
 
Example 8
Source Project: libgdx-snippets   File: PixmapTransform.java    License: MIT License 4 votes vote down vote up
public static Pixmap getRotatedMirroredCopy(Pixmap pixmap, Mirror mirror, Rotate rotate){

		Pixmap mirrored = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), pixmap.getFormat());
		copyToMirrored(pixmap, mirrored, mirror);

		int newWidth = PixmapTransform.getWidth(pixmap, rotate);
		int newHeight = PixmapTransform.getHeight(pixmap, rotate);

		Pixmap copy = new Pixmap(newWidth, newHeight, pixmap.getFormat());
		copyToRotated(mirrored, copy, rotate);

		/** Only temp for copying around */
		mirrored.dispose();

		return copy;
	}
 
Example 9
public static Pixmap subPixmap(Pixmap pix, int x, int y, int width, int height) {
    Pixmap sub = new Pixmap(width, height, pix.getFormat());
    sub.drawPixmap(pix, x, y, width, height, 0, 0, width, height);
    return sub;
}
 
Example 10
/**
 * Resizes the image
 *
 * @param image
 * @param width
 * @param height
 * @return
 */
public static Pixmap resizePixmap(Pixmap image, int width, int height) {
    Pixmap pix = new Pixmap(width, height, image.getFormat());
    pix.drawPixmap(image, 0, 0, width, height, // destination
             0, 0, image.getWidth(), image.getHeight()); // source                         
    return pix;
}