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

The following examples show how to use com.badlogic.gdx.graphics.Pixmap#setBlending() . 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: FacedMultiCubemapData.java    From gdx-gltf with 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
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 3
Source File: NinePatchEditorModel.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
/** @return original image pixmap with 1-pixel border 9-patch markup. */
public Pixmap prepareNinePatchPixmap() {
    Pixmap patchPixmap = new Pixmap(this.pixmap.getWidth() + 2, this.pixmap.getHeight() + 2, this.pixmap.getFormat());
    patchPixmap.setBlending(Pixmap.Blending.None);
    patchPixmap.drawPixmap(pixmap, 1, 1);

    patchPixmap.setColor(0x000000ff);
    if (patchValues.left.get() != 0 && patchValues.right.get() != 0) {
        for (int x = patchValues.left.get(); x < (pixmap.getWidth() - patchValues.right.get()); x++) {
            patchPixmap.drawPixel(x+1, 0);
        }
    }
    if (patchValues.top.get() != 0 && patchValues.bottom.get() != 0) {
        for (int y = patchValues.top.get(); y < (pixmap.getHeight() - patchValues.bottom.get()); y++) {
            patchPixmap.drawPixel(0, y+1);
        }
    }
    if (contentValues.left.get() != 0 && contentValues.right.get() != 0) {
        for (int x = contentValues.left.get(); x < (pixmap.getWidth() - contentValues.right.get()); x++) {
            patchPixmap.drawPixel(x+1, pixmap.getHeight()+1);
        }
    }
    if (contentValues.top.get() != 0 && contentValues.bottom.get() != 0) {
        for (int y = contentValues.top.get(); y < (pixmap.getHeight() - contentValues.bottom.get()); y++) {
            patchPixmap.drawPixel(pixmap.getWidth()+1, y+1);
        }
    }
    return patchPixmap;
}
 
Example 4
Source File: PixmapUtils.java    From libgdx-snippets with MIT License 5 votes vote down vote up
/**
 * Create {@link Pixmap} with given blending and filter.
 */
public static Pixmap create(int width, int height, Format format,
							Blending blending, Filter filter) {

	Pixmap pixmap = new Pixmap(width, height, format);

	pixmap.setBlending(blending);
	pixmap.setFilter(filter);

	return pixmap;
}
 
Example 5
Source File: FogOfWar.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public FogOfWar( int mapWidth, int mapHeight ) {
	
	super();

	this.mapWidth = mapWidth;
	this.mapHeight = mapHeight;
	mapLength = mapHeight * mapWidth;
	
	pWidth = mapWidth * PIX_PER_TILE;
	pHeight = mapHeight * PIX_PER_TILE;
	
	width2 = 1;
	while (width2 < pWidth) {
		width2 <<= 1;
	}
	
	height2 = 1;
	while (height2 < pHeight) {
		height2 <<= 1;
	}
	
	float size = DungeonTilemap.SIZE / PIX_PER_TILE;
	width = width2 * size;
	height = height2 * size;

	//TODO might be nice to compartmentalize the pixmap access and modification into texture/texturecache
	Pixmap px = new Pixmap(width2, height2, Pixmap.Format.RGBA8888);
	px.setBlending(Pixmap.Blending.None);
	px.setColor(0x000000FF);
	px.fill();
	SmartTexture tx = new SmartTexture(px, Texture.LINEAR, Texture.CLAMP, false);
	TextureCache.add(FogOfWar.class, tx);
	texture( tx );
	
	scale.set( size, size );

	toUpdate = new ArrayList<>();
	toUpdate.add(new Rect(0, 0, mapWidth, mapHeight));
}
 
Example 6
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;
}