List of usage examples for com.badlogic.gdx.graphics Pixmap getPixel
public int getPixel(int x, int y)
From source file:at.tugraz.ist.catroid.content.Costume.java
License:Open Source License
protected Pixmap adjustBrightness(Pixmap currentPixmap) { Pixmap newPixmap = new Pixmap(currentPixmap.getWidth(), currentPixmap.getHeight(), currentPixmap.getFormat());//from w w w .java 2 s . c o m for (int y = 0; y < currentPixmap.getHeight(); y++) { for (int x = 0; x < currentPixmap.getWidth(); x++) { int pixel = currentPixmap.getPixel(x, y); int r = (int) (((pixel >> 24) & 0xff) + (255 * (brightnessValue - 1))); int g = (int) (((pixel >> 16) & 0xff) + (255 * (brightnessValue - 1))); int b = (int) (((pixel >> 8) & 0xff) + (255 * (brightnessValue - 1))); int a = pixel & 0xff; if (r > 255) { r = 255; } else if (r < 0) { r = 0; } if (g > 255) { g = 255; } else if (g < 0) { g = 0; } if (b > 255) { b = 255; } else if (b < 0) { b = 0; } newPixmap.setColor(r / 255f, g / 255f, b / 255f, a / 255f); newPixmap.drawPixel(x, y); } } currentPixmap.dispose(); return newPixmap; }
From source file:com.crashinvaders.texturepackergui.utils.PNG8.java
License:Open Source License
/** * Attempts to write the given Pixmap exactly as a PNG-8 image to output; this attempt will only succeed if there * are no more than 256 colors in the Pixmap (treating all partially transparent colors as fully transparent). * If the attempt fails, this falls back to calling {@link #write(OutputStream, Pixmap, boolean, boolean)}, which * can dither the image to use no more than 255 colors (plus fully transparent) based on ditherFallback and will * always analyze the Pixmap to get an accurate-enough palette, using the given threshold for analysis (which is * typically between 1 and 1000, and most often near 200-400). All other write() methods in this class will * reduce the color depth somewhat, but as long as the color count stays at 256 or less, this will keep the * non-alpha components of colors exactly. * @param output an OutputStream that will not be closed * @param pixmap a Pixmap to write to the given output stream * @param ditherFallback if the Pixmap contains too many colors, this determines whether it will dither the output * @param threshold the analysis threshold to use if there are too many colors (min 0, practical max is over 100000) * @throws IOException if OutputStream things fail for any reason *//*from www . java 2s . c om*/ public void writePrecisely(OutputStream output, Pixmap pixmap, boolean ditherFallback, int threshold) throws IOException { IntIntMap colorToIndex = new IntIntMap(256); colorToIndex.put(0, 0); int color; int hasTransparent = 0; final int w = pixmap.getWidth(), h = pixmap.getHeight(); for (int y = 0; y < h; y++) { int py = flipY ? (h - y - 1) : y; for (int px = 0; px < w; px++) { color = pixmap.getPixel(px, py); if ((color & 0xFE) != 0xFE) { if (hasTransparent == 0 && colorToIndex.size >= 256) { write(output, pixmap, true, ditherFallback, threshold); return; } hasTransparent = 1; } else if (!colorToIndex.containsKey(color)) { colorToIndex.put(color, colorToIndex.size & 255); if (colorToIndex.size == 257 && hasTransparent == 0) { colorToIndex.remove(0, 0); } if (colorToIndex.size > 256) { write(output, pixmap, true, ditherFallback, threshold); return; } } } } int[] paletteArray = new int[colorToIndex.size]; for (IntIntMap.Entry ent : colorToIndex) { paletteArray[ent.value] = ent.key; } DeflaterOutputStream deflaterOutput = new DeflaterOutputStream(buffer, deflater); DataOutputStream dataOutput = new DataOutputStream(output); dataOutput.write(SIGNATURE); buffer.writeInt(IHDR); buffer.writeInt(pixmap.getWidth()); buffer.writeInt(pixmap.getHeight()); buffer.writeByte(8); // 8 bits per component. buffer.writeByte(COLOR_INDEXED); buffer.writeByte(COMPRESSION_DEFLATE); buffer.writeByte(FILTER_NONE); buffer.writeByte(INTERLACE_NONE); buffer.endChunk(dataOutput); buffer.writeInt(PLTE); for (int i = 0; i < paletteArray.length; i++) { int p = paletteArray[i]; buffer.write(p >>> 24); buffer.write(p >>> 16); buffer.write(p >>> 8); } buffer.endChunk(dataOutput); if (hasTransparent == 1) { buffer.writeInt(TRNS); buffer.write(0); buffer.endChunk(dataOutput); } buffer.writeInt(IDAT); deflater.reset(); int lineLen = pixmap.getWidth(); byte[] lineOut, curLine, prevLine; if (lineOutBytes == null) { lineOut = (lineOutBytes = new ByteArray(lineLen)).items; curLine = (curLineBytes = new ByteArray(lineLen)).items; prevLine = (prevLineBytes = new ByteArray(lineLen)).items; } else { lineOut = lineOutBytes.ensureCapacity(lineLen); curLine = curLineBytes.ensureCapacity(lineLen); prevLine = prevLineBytes.ensureCapacity(lineLen); for (int i = 0, n = lastLineLen; i < n; i++) { prevLine[i] = 0; } } lastLineLen = lineLen; for (int y = 0; y < h; y++) { int py = flipY ? (h - y - 1) : y; for (int px = 0; px < w; px++) { color = pixmap.getPixel(px, py); curLine[px] = (byte) colorToIndex.get(color, 0); } lineOut[0] = (byte) (curLine[0] - prevLine[0]); //Paeth for (int x = 1; x < lineLen; x++) { int a = curLine[x - 1] & 0xff; int b = prevLine[x] & 0xff; int c = prevLine[x - 1] & 0xff; int p = a + b - c; int pa = p - a; if (pa < 0) pa = -pa; int pb = p - b; if (pb < 0) pb = -pb; int pc = p - c; if (pc < 0) pc = -pc; if (pa <= pb && pa <= pc) c = a; else if (pb <= pc) c = b; lineOut[x] = (byte) (curLine[x] - c); } deflaterOutput.write(PAETH); deflaterOutput.write(lineOut, 0, lineLen); byte[] temp = curLine; curLine = prevLine; prevLine = temp; } deflaterOutput.finish(); buffer.endChunk(dataOutput); buffer.writeInt(IEND); buffer.endChunk(dataOutput); output.flush(); }
From source file:com.crashinvaders.texturepackergui.utils.PNG8.java
License:Open Source License
private void writeSolid(OutputStream output, Pixmap pixmap) throws IOException { final int[] paletteArray = palette.paletteArray; final byte[] paletteMapping = palette.paletteMapping; DeflaterOutputStream deflaterOutput = new DeflaterOutputStream(buffer, deflater); DataOutputStream dataOutput = new DataOutputStream(output); dataOutput.write(SIGNATURE);//from ww w. j av a2s.co m buffer.writeInt(IHDR); buffer.writeInt(pixmap.getWidth()); buffer.writeInt(pixmap.getHeight()); buffer.writeByte(8); // 8 bits per component. buffer.writeByte(COLOR_INDEXED); buffer.writeByte(COMPRESSION_DEFLATE); buffer.writeByte(FILTER_NONE); buffer.writeByte(INTERLACE_NONE); buffer.endChunk(dataOutput); buffer.writeInt(PLTE); for (int i = 0; i < paletteArray.length; i++) { int p = paletteArray[i]; buffer.write(p >>> 24); buffer.write(p >>> 16); buffer.write(p >>> 8); } buffer.endChunk(dataOutput); boolean hasTransparent = false; if (paletteArray[0] == 0) { hasTransparent = true; buffer.writeInt(TRNS); buffer.write(0); buffer.endChunk(dataOutput); } buffer.writeInt(IDAT); deflater.reset(); int lineLen = pixmap.getWidth(); byte[] lineOut, curLine, prevLine; if (lineOutBytes == null) { lineOut = (lineOutBytes = new ByteArray(lineLen)).items; curLine = (curLineBytes = new ByteArray(lineLen)).items; prevLine = (prevLineBytes = new ByteArray(lineLen)).items; } else { lineOut = lineOutBytes.ensureCapacity(lineLen); curLine = curLineBytes.ensureCapacity(lineLen); prevLine = prevLineBytes.ensureCapacity(lineLen); for (int i = 0, n = lastLineLen; i < n; i++) { prevLine[i] = 0; } } lastLineLen = lineLen; int color; final int w = pixmap.getWidth(), h = pixmap.getHeight(); for (int y = 0; y < h; y++) { int py = flipY ? (h - y - 1) : y; for (int px = 0; px < w; px++) { color = pixmap.getPixel(px, py); if ((color & 0x80) == 0 && hasTransparent) curLine[px] = 0; else { int rr = ((color >>> 24)); int gg = ((color >>> 16) & 0xFF); int bb = ((color >>> 8) & 0xFF); curLine[px] = paletteMapping[((rr << 7) & 0x7C00) | ((gg << 2) & 0x3E0) | ((bb >>> 3))]; } } lineOut[0] = (byte) (curLine[0] - prevLine[0]); //Paeth for (int x = 1; x < lineLen; x++) { int a = curLine[x - 1] & 0xff; int b = prevLine[x] & 0xff; int c = prevLine[x - 1] & 0xff; int p = a + b - c; int pa = p - a; if (pa < 0) pa = -pa; int pb = p - b; if (pb < 0) pb = -pb; int pc = p - c; if (pc < 0) pc = -pc; if (pa <= pb && pa <= pc) c = a; else if (pb <= pc) c = b; lineOut[x] = (byte) (curLine[x] - c); } deflaterOutput.write(PAETH); deflaterOutput.write(lineOut, 0, lineLen); byte[] temp = curLine; curLine = prevLine; prevLine = temp; } deflaterOutput.finish(); buffer.endChunk(dataOutput); buffer.writeInt(IEND); buffer.endChunk(dataOutput); output.flush(); }
From source file:com.crashinvaders.texturepackergui.utils.PNG8.java
License:Open Source License
private void writeDithered(OutputStream output, Pixmap pixmap) throws IOException { DeflaterOutputStream deflaterOutput = new DeflaterOutputStream(buffer, deflater); final int[] paletteArray = palette.paletteArray; final byte[] paletteMapping = palette.paletteMapping; DataOutputStream dataOutput = new DataOutputStream(output); dataOutput.write(SIGNATURE);/* w w w . j ava2 s .com*/ buffer.writeInt(IHDR); buffer.writeInt(pixmap.getWidth()); buffer.writeInt(pixmap.getHeight()); buffer.writeByte(8); // 8 bits per component. buffer.writeByte(COLOR_INDEXED); buffer.writeByte(COMPRESSION_DEFLATE); buffer.writeByte(FILTER_NONE); buffer.writeByte(INTERLACE_NONE); buffer.endChunk(dataOutput); buffer.writeInt(PLTE); for (int i = 0; i < paletteArray.length; i++) { int p = paletteArray[i]; buffer.write(p >>> 24); buffer.write(p >>> 16); buffer.write(p >>> 8); } buffer.endChunk(dataOutput); boolean hasTransparent = false; if (paletteArray[0] == 0) { hasTransparent = true; buffer.writeInt(TRNS); buffer.write(0); buffer.endChunk(dataOutput); } buffer.writeInt(IDAT); deflater.reset(); final int w = pixmap.getWidth(), h = pixmap.getHeight(); byte[] lineOut, curLine, prevLine; byte[] curErrorRed, nextErrorRed, curErrorGreen, nextErrorGreen, curErrorBlue, nextErrorBlue; if (lineOutBytes == null) { lineOut = (lineOutBytes = new ByteArray(w)).items; curLine = (curLineBytes = new ByteArray(w)).items; prevLine = (prevLineBytes = new ByteArray(w)).items; } else { lineOut = lineOutBytes.ensureCapacity(w); curLine = curLineBytes.ensureCapacity(w); prevLine = prevLineBytes.ensureCapacity(w); for (int i = 0, n = lastLineLen; i < n; i++) { prevLine[i] = 0; } } if (palette.curErrorRedBytes == null) { curErrorRed = (palette.curErrorRedBytes = new ByteArray(w)).items; nextErrorRed = (palette.nextErrorRedBytes = new ByteArray(w)).items; curErrorGreen = (palette.curErrorGreenBytes = new ByteArray(w)).items; nextErrorGreen = (palette.nextErrorGreenBytes = new ByteArray(w)).items; curErrorBlue = (palette.curErrorBlueBytes = new ByteArray(w)).items; nextErrorBlue = (palette.nextErrorBlueBytes = new ByteArray(w)).items; } else { curErrorRed = palette.curErrorRedBytes.ensureCapacity(w); nextErrorRed = palette.nextErrorRedBytes.ensureCapacity(w); curErrorGreen = palette.curErrorGreenBytes.ensureCapacity(w); nextErrorGreen = palette.nextErrorGreenBytes.ensureCapacity(w); curErrorBlue = palette.curErrorBlueBytes.ensureCapacity(w); nextErrorBlue = palette.nextErrorBlueBytes.ensureCapacity(w); for (int i = 0; i < w; i++) { nextErrorRed[i] = 0; nextErrorGreen[i] = 0; nextErrorBlue[i] = 0; } } lastLineLen = w; int color, used, rdiff, gdiff, bdiff, state = 0xFEEDBEEF; byte er, eg, eb, paletteIndex; float xi1, xi2, w1 = palette.ditherStrength * 0.125f, w3 = w1 * 3f, w5 = w1 * 5f, w7 = w1 * 7f; for (int y = 0; y < h; y++) { int py = flipY ? (h - y - 1) : y; int ny = flipY ? (h - y - 2) : y + 1; for (int i = 0; i < w; i++) { curErrorRed[i] = nextErrorRed[i]; curErrorGreen[i] = nextErrorGreen[i]; curErrorBlue[i] = nextErrorBlue[i]; nextErrorRed[i] = 0; nextErrorGreen[i] = 0; nextErrorBlue[i] = 0; } for (int px = 0; px < w; px++) { color = pixmap.getPixel(px, py) & 0xF8F8F880; if ((color & 0x80) == 0 && hasTransparent) curLine[px] = 0; else { er = curErrorRed[px]; eg = curErrorGreen[px]; eb = curErrorBlue[px]; color |= (color >>> 5 & 0x07070700) | 0xFE; int rr = MathUtils.clamp(((color >>> 24)) + (er), 0, 0xFF); int gg = MathUtils.clamp(((color >>> 16) & 0xFF) + (eg), 0, 0xFF); int bb = MathUtils.clamp(((color >>> 8) & 0xFF) + (eb), 0, 0xFF); curLine[px] = paletteIndex = paletteMapping[((rr << 7) & 0x7C00) | ((gg << 2) & 0x3E0) | ((bb >>> 3))]; used = paletteArray[paletteIndex & 0xFF]; rdiff = (color >>> 24) - (used >>> 24); gdiff = (color >>> 16 & 255) - (used >>> 16 & 255); bdiff = (color >>> 8 & 255) - (used >>> 8 & 255); state += (color + 0x41C64E6D) ^ color >>> 7; state = (state << 21 | state >>> 11); xi1 = randomXi(state); state = (state << 15 | state >>> 17) ^ 0x9E3779B9; xi2 = randomXi(state); if (px < w - 1) { curErrorRed[px + 1] += rdiff * w7 * (1f + xi1); curErrorGreen[px + 1] += gdiff * w7 * (1f + xi1); curErrorBlue[px + 1] += bdiff * w7 * (1f + xi1); } if (ny < h) { if (px > 0) { nextErrorRed[px - 1] += rdiff * w3 * (1f + xi2); nextErrorGreen[px - 1] += gdiff * w3 * (1f + xi2); nextErrorBlue[px - 1] += bdiff * w3 * (1f + xi2); } if (px < w - 1) { nextErrorRed[px + 1] += rdiff * w1 * (1f - xi2); nextErrorGreen[px + 1] += gdiff * w1 * (1f - xi2); nextErrorBlue[px + 1] += bdiff * w1 * (1f - xi2); } nextErrorRed[px] += rdiff * w5 * (1f - xi1); nextErrorGreen[px] += gdiff * w5 * (1f - xi1); nextErrorBlue[px] += bdiff * w5 * (1f - xi1); } } } lineOut[0] = (byte) (curLine[0] - prevLine[0]); //Paeth for (int x = 1; x < w; x++) { int a = curLine[x - 1] & 0xff; int b = prevLine[x] & 0xff; int c = prevLine[x - 1] & 0xff; int p = a + b - c; int pa = p - a; if (pa < 0) pa = -pa; int pb = p - b; if (pb < 0) pb = -pb; int pc = p - c; if (pc < 0) pc = -pc; if (pa <= pb && pa <= pc) c = a; else if (pb <= pc) c = b; lineOut[x] = (byte) (curLine[x] - c); } deflaterOutput.write(PAETH); deflaterOutput.write(lineOut, 0, w); byte[] temp = curLine; curLine = prevLine; prevLine = temp; } deflaterOutput.finish(); buffer.endChunk(dataOutput); buffer.writeInt(IEND); buffer.endChunk(dataOutput); output.flush(); }
From source file:com.kotcrab.vis.editor.module.physicseditor.util.Tracer.java
License:Apache License
public static Vector2[][] trace(String path, float hullTolerance, int alphaTolerance, boolean multiPartDetection, boolean holeDetection) { Blending blending = Pixmap.getBlending(); Pixmap.setBlending(Blending.None);//from w ww.j av a 2 s. c o m Pixmap pixmap = new Pixmap(Gdx.files.absolute(path)); int w = pixmap.getWidth(); int h = pixmap.getHeight(); int size = w * h; int[] array = new int[size]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int color = pixmap.getPixel(x, y); array[x + y * w] = color; } } pixmap.dispose(); Pixmap.setBlending(blending); Array<Array<Vector2>> outlines; try { outlines = TextureConverter.createPolygon(array, w, h, hullTolerance, alphaTolerance, multiPartDetection, holeDetection); } catch (Exception e) { return null; } TextureRegion region = new TextureRegion(new Texture(path)); float tw = region.getRegionWidth(); float th = region.getRegionHeight(); Vector2[][] polygons = new Vector2[outlines.size][]; for (int i = 0; i < outlines.size; i++) { Array<Vector2> outline = outlines.get(i); polygons[i] = new Vector2[outline.size]; for (int ii = 0; ii < outline.size; ii++) { polygons[i][ii] = outline.get(ii); polygons[i][ii].x /= tw; polygons[i][ii].y /= tw; polygons[i][ii].y = 1 * th / tw - polygons[i][ii].y; } } return polygons; }
From source file:com.kotcrab.vis.editor.ui.dialog.PolygonAutoTraceDialog.java
License:Apache License
private void tracePolygon() { TextureRegion region = textureCacheModule.getRegion(assetDescriptor); int width = region.getRegionWidth(); int height = region.getRegionHeight(); int[] pixelArray = new int[width * height]; Texture texture = region.getTexture(); if (texture.getTextureData().isPrepared() == false) { texture.getTextureData().prepare(); }//from ww w. ja v a2 s .co m Pixmap pixmap = texture.getTextureData().consumePixmap(); for (int x = 0; x < region.getRegionWidth(); x++) { for (int y = 0; y < region.getRegionHeight(); y++) { pixelArray[x + y * width] = pixmap.getPixel(region.getRegionX() + x, region.getRegionY() + y); } } Vector2[][] vertices = Tracer.trace(pixelArray, width, height, hullTolerance.getValue(), (int) alphaTolerance.getValue()); if (vertices == null || vertices.length == 0) { Dialogs.showErrorDialog(stage, "Auto tracer could not create polygon, please create points manually."); Log.warn(PolygonTool.TAG, "Failed to auto trace polygon for asset descriptor: " + assetDescriptor); fadeOut(); return; } if (vertices.length > 1) { Log.warn(PolygonTool.TAG, "Auto tracer found multiple parts, will be discarded. Asset descriptor: " + assetDescriptor); } resultListener.finished(vertices[0]); }
From source file:com.mbrlabs.mundus.commons.terrain.SplatMap.java
License:Apache License
public void clearChannel(SplatTexture.Channel channel) { Pixmap pixmap = getPixmap(); for (int smX = 0; smX < pixmap.getWidth(); smX++) { for (int smY = 0; smY < pixmap.getHeight(); smY++) { c0.set(pixmap.getPixel(smX, smY)); if (channel == SplatTexture.Channel.R) { c0.set(0, c0.g, c0.b, c0.a); } else if (channel == SplatTexture.Channel.G) { c0.set(c0.r, 0, c0.b, c0.a); } else if (channel == SplatTexture.Channel.B) { c0.set(c0.r, c0.g, 0, c0.a); } else if (channel == SplatTexture.Channel.A) { c0.set(c0.r, c0.g, c0.b, 0); }/* w w w .j av a2s.c om*/ pixmap.drawPixel(smX, smY, Color.rgba8888(c0)); } } }
From source file:com.mbrlabs.mundus.editor.tools.brushes.TerrainBrush.java
License:Apache License
private void paint() { Terrain terrain = terrainAsset.getTerrain(); SplatMap sm = terrain.getTerrainTexture().getSplatmap(); if (sm == null) return;// ww w . ja v a 2 s . com Vector3 terrainPos = terrain.getPosition(tVec1); final float splatX = ((brushPos.x - terrainPos.x) / (float) terrain.terrainWidth) * sm.getWidth(); final float splatY = ((brushPos.z - terrainPos.z) / (float) terrain.terrainDepth) * sm.getHeight(); final float splatRad = (radius / terrain.terrainWidth) * sm.getWidth(); final Pixmap pixmap = sm.getPixmap(); for (int smX = 0; smX < pixmap.getWidth(); smX++) { for (int smY = 0; smY < pixmap.getHeight(); smY++) { final float dst = MathUtils.dst(splatX, splatY, smX, smY); if (dst <= splatRad) { final float opacity = getValueOfBrushPixmap(splatX, splatY, smX, smY, splatRad) * 0.5f * strength; int newPixelColor = sm.additiveBlend(pixmap.getPixel(smX, smY), paintChannel, opacity); pixmap.drawPixel(smX, smY, newPixelColor); } } } sm.updateTexture(); splatmapModified = true; getProjectManager().current().assetManager.addDirtyAsset(terrainAsset); }
From source file:com.mbrlabs.mundus.editor.tools.picker.GameObjectPicker.java
License:Apache License
public GameObject pick(EditorScene scene, int screenX, int screenY) { begin(scene.viewport);//from w w w. ja va 2s . com renderPickableScene(scene.sceneGraph); end(); Pixmap pm = getFrameBufferPixmap(scene.viewport); int x = screenX - scene.viewport.getScreenX(); int y = screenY - (Gdx.graphics.getHeight() - (scene.viewport.getScreenY() + scene.viewport.getScreenHeight())); int id = PickerColorEncoder.decode(pm.getPixel(x, y)); for (GameObject go : scene.sceneGraph.getGameObjects()) { if (id == go.id) return go; for (GameObject child : go) { if (id == child.id) return child; } } return null; }
From source file:com.mbrlabs.mundus.editor.tools.picker.ToolHandlePicker.java
License:Apache License
public ToolHandle pick(ToolHandle[] handles, EditorScene scene, int screenX, int screenY) { begin(scene.viewport);//from ww w . j a v a 2s.c o m renderPickableScene(handles, scene.sceneGraph.batch, scene.cam); end(); Pixmap pm = getFrameBufferPixmap(scene.viewport); int x = screenX - scene.viewport.getScreenX(); int y = screenY - (Gdx.graphics.getHeight() - (scene.viewport.getScreenY() + scene.viewport.getScreenHeight())); int id = PickerColorEncoder.decode(pm.getPixel(x, y)); Log.trace("ToolHandlePicker", "Picking handle with id {}", id); for (ToolHandle handle : handles) { if (handle.getId() == id) { return handle; } } return null; }