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

The following examples show how to use com.badlogic.gdx.graphics.Pixmap#getPixel() . 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: PixmapTransform.java    From libgdx-snippets with MIT License 6 votes vote down vote up
public static int getPixelMirrored(Pixmap pixmap, int x, int y, Mirror mirror) {
	int widthMinusOne = pixmap.getWidth() - 1;
	int heightMinusOne = pixmap.getHeight() - 1;

	int px = x, py = y;

	if (mirror == Mirror.X || mirror == Mirror.XY) {
		px = widthMinusOne - x;
	}

	if (mirror == Mirror.Y || mirror == Mirror.XY) {
		py = heightMinusOne - y;
	}

	return pixmap.getPixel(px, py);
}
 
Example 2
Source File: TextureUtil.java    From seventh with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applying mask into image using specified masking color. Any Color in the
 * image that matches the masking color will be converted to transparent.
 * 
 * @param img The source image
 * @param keyColor Masking color
 * @return Masked image
 */
public static Pixmap applyMask(Pixmap img, Color keyColor) {
    Pixmap alpha = new Pixmap(img.getWidth(), img.getHeight(), Format.RGBA8888);        
    //alpha.drawPixmap(img, 0, 0);
        
    int width = alpha.getWidth();
    int height = alpha.getHeight();
    
    int colorMask = Color.rgba8888(keyColor);
    
    //alpha.setColor(0xff00009f);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int col = img.getPixel(x, y);
            if ( col != colorMask ) {
                alpha.drawPixel(x, y, img.getPixel(x, y));
            }
        }
    }
    return alpha;
}
 
Example 3
Source File: TextureUtil.java    From seventh with GNU General Public License v2.0 6 votes vote down vote up
public static Pixmap toWhite(Pixmap img) {
    Pixmap alpha = new Pixmap(img.getWidth(), img.getHeight(), Format.RGBA8888);        
    //alpha.drawPixmap(img, 0, 0);
        
    int width = alpha.getWidth();
    int height = alpha.getHeight();
            
    //alpha.setColor(0xff00009f);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int col = img.getPixel(x, y);
            Color color = new Color(col);
            if ( color.a > 0.2f ) {
                alpha.drawPixel(x, y, Color.WHITE.toIntBits());
            }
        }
    }
    
    img.dispose();
    
    return alpha;
}
 
Example 4
Source File: PaletteReducer.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Modifies the given Pixmap so it only uses colors present in this PaletteReducer, without dithering. This produces
 * blocky solid sections of color in most images where the palette isn't exact, instead of checkerboard-like
 * dithering patterns. If you want to reduce the colors in a Pixmap based on what it currently contains, call
 * {@link #analyze(Pixmap)} with {@code pixmap} as its argument, then call this method with the same
 * Pixmap. You may instead want to use a known palette instead of one computed from a Pixmap;
 * {@link #exact(int[])} is the tool for that job.
 * @param pixmap a Pixmap that will be modified in place
 * @return the given Pixmap, for chaining
 */
public Pixmap reduceSolid (Pixmap pixmap) {
    boolean hasTransparent = (paletteArray[0] == 0);
    final int lineLen = pixmap.getWidth(), h = pixmap.getHeight();
    Pixmap.Blending blending = pixmap.getBlending();
    pixmap.setBlending(Pixmap.Blending.None);
    int color;
    for (int y = 0; y < h; y++) {
        for (int px = 0; px < lineLen; px++) {
            color = pixmap.getPixel(px, y);
            if ((color & 0x80) == 0 && hasTransparent)
                pixmap.drawPixel(px, y, 0);
            else {
                int rr = ((color >>> 24)       );
                int gg = ((color >>> 16) & 0xFF);
                int bb = ((color >>> 8)  & 0xFF);
                pixmap.drawPixel(px, y, paletteArray[
                        paletteMapping[((rr << 7) & 0x7C00)
                                | ((gg << 2) & 0x3E0)
                                | ((bb >>> 3))] & 0xFF]);
            }
        }

    }
    pixmap.setBlending(blending);
    return pixmap;
}
 
Example 5
Source File: PixmapTransform.java    From libgdx-snippets with MIT License 5 votes vote down vote up
public static int getPixelRotated(Pixmap pixmap, int x, int y, Rotate rotate) {
	int widthMinusOne = pixmap.getWidth() - 1;
	int heightMinusOne = pixmap.getHeight() - 1;

	int px = x, py = y;
	int outX, outY;



	// rotate

	switch (rotate) {
		case _90:
			outX = py;
			outY = heightMinusOne - px;
			break;
		case _180:
			outX = widthMinusOne - px;
			outY = heightMinusOne - py;
			break;
		case _270:
			outX = widthMinusOne - py;
			outY = px;
			break;
		default:
		case None:
			outX = px;
			outY = py;
			break;
	}

	return pixmap.getPixel(outX, outY);
}
 
Example 6
Source File: BitmapText.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
private boolean colorNotMatch(Pixmap pixmap, int x, int y, int color) {
	int pixel = pixmap.getPixel(x, y);
	if ((pixel & 0xFF) == 0) {
		return color != 0;
	}
	return pixel != color;
}
 
Example 7
Source File: HeightMap.java    From gdx-proto with 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 8
Source File: BitmapText.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
private boolean colorNotMatch(Pixmap pixmap, int x, int y, int color) {
	int pixel = pixmap.getPixel(x, y);
	if ((pixel & 0xFF) == 0) {
		return color != 0;
	}
	return pixel != color;
}
 
Example 9
Source File: PaletteReducer.java    From gdx-texture-packer-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Modifies the given Pixmap so it only uses colors present in this PaletteReducer, dithering when it can.
 * If you want to reduce the colors in a Pixmap based on what it currently contains, call
 * {@link #analyze(Pixmap)} with {@code pixmap} as its argument, then call this method with the same
 * Pixmap. You may instead want to use a known palette instead of one computed from a Pixmap;
 * {@link #exact(int[])} is the tool for that job.
 * <br>
 * This method is not incredibly fast because of the extra calculations it has to do for dithering, but if you can
 * compute the PaletteReducer once and reuse it, that will save some time.
 * @param pixmap a Pixmap that will be modified in place
 * @return the given Pixmap, for chaining
 */
public Pixmap reduce (Pixmap pixmap) {
    boolean hasTransparent = (paletteArray[0] == 0);
    final int lineLen = pixmap.getWidth(), h = pixmap.getHeight();
    byte[] curErrorRed, nextErrorRed, curErrorGreen, nextErrorGreen, curErrorBlue, nextErrorBlue;
    if (curErrorRedBytes == null) {
        curErrorRed = (curErrorRedBytes = new ByteArray(lineLen)).items;
        nextErrorRed = (nextErrorRedBytes = new ByteArray(lineLen)).items;
        curErrorGreen = (curErrorGreenBytes = new ByteArray(lineLen)).items;
        nextErrorGreen = (nextErrorGreenBytes = new ByteArray(lineLen)).items;
        curErrorBlue = (curErrorBlueBytes = new ByteArray(lineLen)).items;
        nextErrorBlue = (nextErrorBlueBytes = new ByteArray(lineLen)).items;
    } else {
        curErrorRed = curErrorRedBytes.ensureCapacity(lineLen);
        nextErrorRed = nextErrorRedBytes.ensureCapacity(lineLen);
        curErrorGreen = curErrorGreenBytes.ensureCapacity(lineLen);
        nextErrorGreen = nextErrorGreenBytes.ensureCapacity(lineLen);
        curErrorBlue = curErrorBlueBytes.ensureCapacity(lineLen);
        nextErrorBlue = nextErrorBlueBytes.ensureCapacity(lineLen);
        for (int i = 0; i < lineLen; i++) {
            nextErrorRed[i] = 0;
            nextErrorGreen[i] = 0;
            nextErrorBlue[i] = 0;
        }

    }
    Pixmap.Blending blending = pixmap.getBlending();
    pixmap.setBlending(Pixmap.Blending.None);
    int color, used, rdiff, gdiff, bdiff;
    byte er, eg, eb, paletteIndex;
    for (int y = 0; y < h; y++) {
        int ny = y + 1;
        for (int i = 0; i < lineLen; 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 < lineLen; px++) {
            color = pixmap.getPixel(px, y) & 0xF8F8F880;
            if ((color & 0x80) == 0 && hasTransparent)
                pixmap.drawPixel(px, y, 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);
                paletteIndex =
                        paletteMapping[((rr << 7) & 0x7C00)
                                | ((gg << 2) & 0x3E0)
                                | ((bb >>> 3))];
                used = paletteArray[paletteIndex & 0xFF];
                pixmap.drawPixel(px, y, used);
                rdiff = (color>>>24)-    (used>>>24);
                gdiff = (color>>>16&255)-(used>>>16&255);
                bdiff = (color>>>8&255)- (used>>>8&255);
                if(px < lineLen - 1)
                {
                    curErrorRed[px+1]   += rdiff * ditherStrength;
                    curErrorGreen[px+1] += gdiff * ditherStrength;
                    curErrorBlue[px+1]  += bdiff * ditherStrength;
                }
                if(ny < h)
                {
                    if(px > 0)
                    {
                        nextErrorRed[px-1]   += rdiff * halfDitherStrength;
                        nextErrorGreen[px-1] += gdiff * halfDitherStrength;
                        nextErrorBlue[px-1]  += bdiff * halfDitherStrength;
                    }
                    nextErrorRed[px]   += rdiff * halfDitherStrength;
                    nextErrorGreen[px] += gdiff * halfDitherStrength;
                    nextErrorBlue[px]  += bdiff * halfDitherStrength;
                }
            }
        }

    }
    pixmap.setBlending(blending);
    return pixmap;
}
 
Example 10
Source File: PixmapTransform.java    From libgdx-snippets with MIT License 4 votes vote down vote up
/**
 * Read pixel from {@link Pixmap}, using back-transformed flipped and rotated pixel coordinates.
 */
public static int getPixel(Pixmap pixmap, int x, int y, Mirror mirror, Rotate rotate) {

	int widthMinusOne = pixmap.getWidth() - 1;
	int heightMinusOne = pixmap.getHeight() - 1;

	int px = x, py = y;
	int outX, outY;

	// mirror

	if (mirror == Mirror.X || mirror == Mirror.XY) {
		px = widthMinusOne - x;
	}

	if (mirror == Mirror.Y || mirror == Mirror.XY) {
		py = heightMinusOne - y;
	}

	// rotate

	switch (rotate) {
		case _90:
			outX = py;
			outY = heightMinusOne - px;
			break;
		case _180:
			outX = widthMinusOne - px;
			outY = heightMinusOne - py;
			break;
		case _270:
			outX = widthMinusOne - py;
			outY = px;
			break;
		default:
		case None:
			outX = px;
			outY = py;
			break;
	}

	return pixmap.getPixel(outX, outY);
}
 
Example 11
Source File: BitmapText.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
protected void splitBy( Pixmap bitmap, int height, int color, String chars ) {
	
	int length = chars.length();
	
	int width = bitmap.getWidth();
	float vHeight = (float)height / bitmap.getHeight();
	
	int pos;
	int line = 0;
	
spaceMeasuring:
	for (pos=0; pos <  width; pos++) {
		for (int j=0; j < height; j++) {
			if (bitmap.getPixel( pos, j ) != color) {
				break spaceMeasuring;
			}
		}
	}
	add( ' ', new RectF( 0, 0, (float)pos / width, vHeight-0.01f ) );

	int separator = pos;
	
	for (int i=0; i < length; i++) {
		
		char ch = chars.charAt( i );
		if (ch == ' ') {
			continue;
		} else {

			boolean found;

			do{
				if (separator >= width) {
					line += height;
					separator = 0;
				}
				found = false;
				for (int j=line; j < line + height; j++) {
					if (colorNotMatch( bitmap, separator, j, color)) {
						found = true;
						break;
					}
				}
				if (!found) separator++;
			} while (!found);
			int start = separator;
			
			do {
				if (++separator >= width) {
					line += height;
					separator = start = 0;
					if (line + height >= bitmap.getHeight())
						break;
				}
				found = true;
				for (int j=line; j < line + height; j++) {
					if (colorNotMatch( bitmap, separator, j, color)) {
						found = false;
						break;
					}
				}
			} while (!found);
			
			add( ch, new RectF( (float)start / width, (float)line / bitmap.getHeight(), (float)separator / width, (float)line / bitmap.getHeight() + vHeight) );
			separator++;
		}
	}
	
	lineHeight = baseLine = height( frames.get( chars.charAt( 0 ) ) );
}