/** Draws an area from 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 from 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()); }
@Override protected void processFile (Entry entry) throws Exception { System.out.println("Processing " + entry.inputFile); Pixmap pixmap = new Pixmap(new FileHandle(entry.inputFile)); if (pixmap.getFormat() != Format.RGB888 && pixmap.getFormat() != Format.RGB565) { System.out.println("Converting from " + pixmap.getFormat() + " to RGB888!"); Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Format.RGB888); tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); pixmap.dispose(); pixmap = tmp; } ETC1.encodeImagePKM(pixmap).write(new FileHandle(entry.outputFile)); pixmap.dispose(); }
@Override protected void processFile (Entry entry) throws Exception { System.out.println("Processing " + entry.inputFile); Pixmap pixmap = new Pixmap(new FileHandle(entry.inputFile)); if (pixmap.getFormat() != Format.RGB888 && pixmap.getFormat() != Format.RGB565) { System.out.println("Converting from " + pixmap.getFormat() + " to RGB888!"); Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Format.RGB888); tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); pixmap.dispose(); pixmap = tmp; } ETC1.encodeImagePKM(pixmap).write(new FileHandle(entry.outputFile)); pixmap.dispose(); }
/** Returns a portion of the default framebuffer contents specified by x, y, width and height as a {@link TextureRegion} with * the same dimensions. The base {@link Texture} always has {@link MathUtils#nextPowerOfTwo} dimensions and RGBA8888 * {@link Format}. It can be accessed via {@link TextureRegion#getTexture}. This texture is not managed and has to be reloaded * manually on a context loss. If the width and height specified are larger than the framebuffer dimensions, the Texture will * be padded accordingly. Pixels that fall outside of the current screen will have RGBA values of 0. * * @param x the x position of the framebuffer contents to capture * @param y the y position of the framebuffer contents to capture * @param w the width of the framebuffer contents to capture * @param h the height of the framebuffer contents to capture */ public static TextureRegion getFrameBufferTexture (int x, int y, int w, int h) { final int potW = MathUtils.nextPowerOfTwo(w); final int potH = MathUtils.nextPowerOfTwo(h); final Pixmap pixmap = getFrameBufferPixmap(x, y, w, h); final Pixmap potPixmap = new Pixmap(potW, potH, Format.RGBA8888); potPixmap.drawPixmap(pixmap, 0, 0); Texture texture = new Texture(potPixmap); TextureRegion textureRegion = new TextureRegion(texture, 0, h, w, -h); potPixmap.dispose(); pixmap.dispose(); return textureRegion; }
/** Returns a portion of the default framebuffer contents specified by x, y, width and height as a {@link TextureRegion} with * the same dimensions. The base {@link Texture} always has {@link MathUtils#nextPowerOfTwo} dimensions and RGBA8888 * {@link Format}. It can be accessed via {@link TextureRegion#getTexture}. This texture is not managed and has to be reloaded * manually on a context loss. If the width and height specified are larger than the framebuffer dimensions, the Texture will * be padded accordingly. Pixels that fall outside of the current screen will have RGBA values of 0. * * @param x the x position of the framebuffer contents to capture * @param y the y position of the framebuffer contents to capture * @param w the width of the framebuffer contents to capture * @param h the height of the framebuffer contents to capture */ public static TextureRegion getFrameBufferTexture (int x, int y, int w, int h) { final int potW = MathUtils.nextPowerOfTwo(w); final int potH = MathUtils.nextPowerOfTwo(h); final Pixmap pixmap = getFrameBufferPixmap(x, y, w, h); final Pixmap potPixmap = new Pixmap(potW, potH, Format.RGBA8888); potPixmap.drawPixmap(pixmap, 0, 0); Texture texture = new Texture(potPixmap); TextureRegion textureRegion = new TextureRegion(texture, 0, h, w, -h); potPixmap.dispose(); pixmap.dispose(); return textureRegion; }
@Override public void create () { if (spriteBatch != null) return; spriteBatch = new SpriteBatch(); Matrix4 transform = new Matrix4(); transform.setToTranslation(0, Gdx.graphics.getHeight(), 0); transform.mul(new Matrix4().setToScaling(1, -1, 1)); spriteBatch.setTransformMatrix(transform); pixS1 = new Pixmap(Gdx.files.getFileHandle("data/test4.png", Files.FileType.Internal)); pixS2 = new Pixmap(Gdx.files.getFileHandle("data/test3.png", Files.FileType.Internal)); pixD = new Pixmap(512, 1024, Pixmap.Format.RGBA8888); pixD.setBlending(Pixmap.Blending.SourceOver); pixD.setFilter(Pixmap.Filter.NearestNeighbour); pixD.drawPixmap(pixS1, 0, 0, 38, 76, 0, 0, 512, 1024); pixD.drawPixmap(pixS2, 0, 0, 38, 76, 0, 0, 512, 1024); logoSprite = new Sprite(new Texture(pixD)); logoSprite.flip(false, true); pixS1.dispose(); pixS2.dispose(); pixD.dispose(); }
@Override public void run () { byte[] bytes = new byte[200 * 1024]; // assuming the content is not bigger than 200kb. int numBytes = download(bytes, "http://www.badlogicgames.com/wordpress/wp-content/uploads/2012/01/badlogic-new.png"); if (numBytes != 0) { // load the pixmap, make it a power of two if necessary (not needed for GL ES 2.0!) Pixmap pixmap = new Pixmap(bytes, 0, numBytes); final int originalWidth = pixmap.getWidth(); final int originalHeight = pixmap.getHeight(); int width = MathUtils.nextPowerOfTwo(pixmap.getWidth()); int height = MathUtils.nextPowerOfTwo(pixmap.getHeight()); final Pixmap potPixmap = new Pixmap(width, height, pixmap.getFormat()); potPixmap.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); pixmap.dispose(); Gdx.app.postRunnable(new Runnable() { @Override public void run () { image = new TextureRegion(new Texture(potPixmap), 0, 0, originalWidth, originalHeight); } }); } } }).start();
static void setIcon(long windowHandle, Pixmap[] images) { if (SharedLibraryLoader.isMac) return; GLFWImage.Buffer buffer = GLFWImage.malloc(images.length); Pixmap[] tmpPixmaps = new Pixmap[images.length]; for (int i = 0; i < images.length; i++) { Pixmap pixmap = images[i]; if (pixmap.getFormat() != Pixmap.Format.RGBA8888) { Pixmap rgba = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Pixmap.Format.RGBA8888); rgba.setBlending(Pixmap.Blending.None); rgba.drawPixmap(pixmap, 0, 0); tmpPixmaps[i] = rgba; pixmap = rgba; } GLFWImage icon = GLFWImage.malloc(); icon.set(pixmap.getWidth(), pixmap.getHeight(), pixmap.getPixels()); buffer.put(icon); icon.free(); } buffer.position(0); GLFW.glfwSetWindowIcon(windowHandle, buffer); buffer.free(); for (Pixmap pixmap : tmpPixmaps) { if (pixmap != null) { pixmap.dispose(); } } }
static void setIcon(long windowHandle, Pixmap[] images) { if (SharedLibraryLoader.isMac) return; GLFWImage.Buffer buffer = GLFWImage.malloc(images.length); Pixmap[] tmpPixmaps = new Pixmap[images.length]; for (int i = 0; i < images.length; i++) { Pixmap pixmap = images[i]; if (pixmap.getFormat() != Pixmap.Format.RGBA8888) { Pixmap rgba = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Pixmap.Format.RGBA8888); rgba.setBlending(Pixmap.Blending.None); rgba.drawPixmap(pixmap, 0, 0); tmpPixmaps[i] = rgba; pixmap = rgba; } GLFWImage icon = GLFWImage.malloc(); icon.set(pixmap.getWidth(), pixmap.getHeight(), pixmap.getPixels()); buffer.put(icon); icon.free(); } buffer.position(0); GLFW.glfwSetWindowIcon(windowHandle, buffer); buffer.free(); for (Pixmap pixmap : tmpPixmaps) { if (pixmap != null) { pixmap.dispose(); } } }
@Override public void consumeTextureArrayData () { for (int i = 0; i < textureDatas.length; i++) { if (textureDatas[i].getType() == TextureData.TextureDataType.Custom) { textureDatas[i].consumeCustomData(GL30.GL_TEXTURE_2D_ARRAY); } else { TextureData texData = textureDatas[i]; Pixmap pixmap = texData.consumePixmap(); boolean disposePixmap = texData.disposePixmap(); if (texData.getFormat() != pixmap.getFormat()) { Pixmap temp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), texData.getFormat()); temp.setBlending(Pixmap.Blending.None); temp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); if (texData.disposePixmap()) { pixmap.dispose(); } pixmap = temp; disposePixmap = true; } Gdx.gl30.glTexSubImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, pixmap.getWidth(), pixmap.getHeight(), 1, pixmap.getGLInternalFormat(), pixmap.getGLType(), pixmap.getPixels()); if (disposePixmap) pixmap.dispose(); } } }
@Override public void consumeTextureArrayData () { for (int i = 0; i < textureDatas.length; i++) { if (textureDatas[i].getType() == TextureData.TextureDataType.Custom) { textureDatas[i].consumeCustomData(GL30.GL_TEXTURE_2D_ARRAY); } else { TextureData texData = textureDatas[i]; Pixmap pixmap = texData.consumePixmap(); boolean disposePixmap = texData.disposePixmap(); if (texData.getFormat() != pixmap.getFormat()) { Pixmap temp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), texData.getFormat()); temp.setBlending(Pixmap.Blending.None); temp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); if (texData.disposePixmap()) { pixmap.dispose(); } pixmap = temp; disposePixmap = true; } Gdx.gl30.glTexSubImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, pixmap.getWidth(), pixmap.getHeight(), 1, pixmap.getGLInternalFormat(), pixmap.getGLType(), pixmap.getPixels()); if (disposePixmap) pixmap.dispose(); } } }
@Override public void consumeCubemapData () { for (int i = 0; i < data.length; i++) { if (data[i].getType() == TextureData.TextureDataType.Custom) { data[i].consumeCustomData(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i); } else { Pixmap pixmap = data[i].consumePixmap(); boolean disposePixmap = data[i].disposePixmap(); if (data[i].getFormat() != pixmap.getFormat()) { Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), data[i].getFormat()); tmp.setBlending(Blending.None); tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); if (data[i].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, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); if (disposePixmap) pixmap.dispose(); } } }
@Override public void consumeCubemapData () { for (int i = 0; i < data.length; i++) { if (data[i].getType() == TextureData.TextureDataType.Custom) { data[i].consumeCustomData(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i); } else { Pixmap pixmap = data[i].consumePixmap(); boolean disposePixmap = data[i].disposePixmap(); if (data[i].getFormat() != pixmap.getFormat()) { Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), data[i].getFormat()); tmp.setBlending(Blending.None); tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); if (data[i].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, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); if (disposePixmap) pixmap.dispose(); } } }
private static void generateMipMapCPU (int target, Pixmap pixmap, int textureWidth, int textureHeight) { Gdx.gl.glTexImage2D(target, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); if ((Gdx.gl20 == null) && textureWidth != textureHeight) throw new GdxRuntimeException("texture width and height must be square when using mipmapping."); int width = pixmap.getWidth() / 2; int height = pixmap.getHeight() / 2; int level = 1; while (width > 0 && height > 0) { Pixmap tmp = new Pixmap(width, height, pixmap.getFormat()); tmp.setBlending(Blending.None); tmp.drawPixmap(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight(), 0, 0, width, height); if (level > 1) pixmap.dispose(); pixmap = tmp; Gdx.gl.glTexImage2D(target, level, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); width = pixmap.getWidth() / 2; height = pixmap.getHeight() / 2; level++; } } }
private static void generateMipMapCPU (int target, Pixmap pixmap, int textureWidth, int textureHeight) { Gdx.gl.glTexImage2D(target, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); if ((Gdx.gl20 == null) && textureWidth != textureHeight) throw new GdxRuntimeException("texture width and height must be square when using mipmapping."); int width = pixmap.getWidth() / 2; int height = pixmap.getHeight() / 2; int level = 1; while (width > 0 && height > 0) { Pixmap tmp = new Pixmap(width, height, pixmap.getFormat()); tmp.setBlending(Blending.None); tmp.drawPixmap(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight(), 0, 0, width, height); if (level > 1) pixmap.dispose(); pixmap = tmp; Gdx.gl.glTexImage2D(target, level, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); width = pixmap.getWidth() / 2; height = pixmap.getHeight() / 2; level++; } } }
Lwjgl3Cursor(Lwjgl3Window 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.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); }
Lwjgl3Cursor(Lwjgl3Window 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.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); }
Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), data.getFormat()); tmp.setBlending(Blending.None); tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); if (data.disposePixmap()) { pixmap.dispose();
Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), data.getFormat()); tmp.setBlending(Blending.None); tmp.drawPixmap(pixmap, 0, 0, 0, 0, pixmap.getWidth(), pixmap.getHeight()); if (data.disposePixmap()) { pixmap.dispose();