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

The following examples show how to use com.badlogic.gdx.graphics.Pixmap#drawPixel() . 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: Utils.java    From skin-composer with MIT License 6 votes vote down vote up
/**
 * Does not dispose pixmap
 * @param pixmap
 * @return 
 */
public static Pixmap tintPixmap(Pixmap pixmap, Color color) {
    Color tempColor = new Color();
    for (int y = 0; y < pixmap.getHeight(); y++) {
        for (int x = 0; x < pixmap.getWidth(); x++) {
            tempColor.set(pixmap.getPixel(x, y));
            float a = tempColor.a;
            tempColor.mul(color);
            tempColor.a = a;
            pixmap.setColor(tempColor);
            pixmap.drawPixel(x, y);
            tempColor.set(pixmap.getPixel(x, y));
        }
    }
    return pixmap;
}
 
Example 2
Source File: SplatMap.java    From Mundus with Apache License 2.0 6 votes vote down vote up
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);
            }
            pixmap.drawPixel(smX, smY, Color.rgba8888(c0));
        }
    }
}
 
Example 3
Source File: TerrainBrush.java    From Mundus with Apache License 2.0 6 votes vote down vote up
private void paint() {
    Terrain terrain = terrainAsset.getTerrain();
    SplatMap sm = terrain.getTerrainTexture().getSplatmap();
    if (sm == null) return;

    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);
}
 
Example 4
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 5
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 6
Source File: LaneRenderer.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
public LaneRenderer(BMSPlayer main, BMSModel model) {

		this.main = main;
		Pixmap hp = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
		hp.drawPixel(0, 0, Color.toIntBits(255, 255, 255, 255));
		blank = new TextureRegion(new Texture(hp));
		hp.dispose();

		FreeTypeFontGenerator generator = new FreeTypeFontGenerator(
				Gdx.files.internal("skin/default/VL-Gothic-Regular.ttf"));
		FreeTypeFontParameter parameter = new FreeTypeFontParameter();
		parameter.size = 18;
		font = generator.generateFont(parameter);
		generator.dispose();

		this.skin = (PlaySkin) main.getSkin();
		this.conf = main.main.getPlayerResource().getConfig();
		this.config = main.main.getPlayerResource().getPlayerConfig();
		this.playconfig = config.getPlayConfig(model.getMode()).getPlayconfig().clone();

		init(model);

		for (CourseData.CourseDataConstraint i : main.main.getPlayerResource().getConstraint()) {
			if (i == NO_SPEED) {
				playconfig.setHispeed(1.0f);
				playconfig.setLanecover(0);
				playconfig.setLift(0);
				playconfig.setHidden(0);
			}
		}
	}
 
Example 7
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 8
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 9
Source File: PixmapTransform.java    From libgdx-snippets with MIT License 5 votes vote down vote up
public static void copyToRotated(Pixmap source, Pixmap target, Rotate rotate){
	for(int y=0; y<target.getHeight(); y++){
		for(int x=0; x<target.getWidth(); x++){
			int rgba = PixmapTransform.getPixelRotated(source, x, y, rotate);
			target.drawPixel(x, y, rgba);
		}
	}
}
 
Example 10
Source File: PixmapTransform.java    From libgdx-snippets with MIT License 5 votes vote down vote up
public static void copyToMirrored(Pixmap source, Pixmap target, Mirror mirror){
	for(int y=0; y<target.getHeight(); y++){
		for(int x=0; x<target.getWidth(); x++){
			int rgba = PixmapTransform.getPixelMirrored(source, x, y, mirror);
			target.drawPixel(x, y, rgba);
		}
	}
}
 
Example 11
Source File: AOTextureGenerator.java    From Cubes with 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 12
Source File: AOTextureGenerator.java    From Cubes with MIT License 5 votes vote down vote up
private static void doubleToPixmap(double[][] in, Pixmap out) {
  Color c = new Color(0, 0, 0, 1);
  for (int x = 0; x < out.getWidth(); x++) {
    for (int y = 0; y < out.getHeight(); y++) {
      float d = (float) in[x][y];
      c.r = d;
      c.g = d;
      c.b = d;
      c.clamp();
      out.drawPixel(x, y, Color.rgba8888(c));
    }
  }
}
 
Example 13
Source File: Crosshair.java    From gdx-proto with Apache License 2.0 5 votes vote down vote up
public static Sprite create() {
	int w = View.width() / 100;
	int h = w;
	Pixmap pix = new Pixmap(w, h, Pixmap.Format.RGBA8888);
	pix.setColor(1f, 1f, 1f, 0.8f);
	pix.drawLine(w/2, 0, w/2, h);
	pix.drawLine(0, h/2, w, h/2);
	pix.setColor(0f, 0f, 0f, 0f);
	pix.drawPixel(w/2, h/2);
	Texture tex = new Texture(pix);
	Sprite sprite = new Sprite(tex);
	return sprite;
	// TODO we don't dispose anything, this is just a temporary crosshair
}
 
Example 14
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 15
Source File: TextureCache.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
public synchronized static SmartTexture createGradient( int... colors ) {
	
	final String key = "" + colors;
	
	if (all.containsKey( key )) {
		
		return all.get( key );
		
	} else {
		
		Pixmap pixmap = new Pixmap( colors.length, 1, Pixmap.Format.RGBA8888);
		for (int i=0; i < colors.length; i++) {
			// In the rest of the code ARGB is used
			pixmap.drawPixel( i, 0, (colors[i] << 8) | (colors[i] >>> 24) );
		}
		SmartTexture tx = new SmartTexture( pixmap );

		tx.filter( Texture.LINEAR, Texture.LINEAR );
		tx.wrap( Texture.CLAMP, Texture.CLAMP );

		all.put( key, tx );
		return tx;
	}
	
}
 
Example 16
Source File: TextureCache.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public synchronized static SmartTexture createGradient( int... colors ) {

		final String key = "" + colors;

		if (all.containsKey( key )) {

			return all.get( key );

		} else {

			Pixmap pixmap = new Pixmap( colors.length, 1, Pixmap.Format.RGBA8888);
			for (int i=0; i < colors.length; i++) {
				// In the rest of the code ARGB is used
				pixmap.drawPixel( i, 0, (colors[i] << 8) | (colors[i] >>> 24) );
			}
			SmartTexture tx = new SmartTexture( pixmap );

			tx.filter( Texture.LINEAR, Texture.LINEAR );
			tx.wrap( Texture.CLAMP, Texture.CLAMP );

			all.put( key, tx );
			return tx;
		}

	}