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

The following examples show how to use com.badlogic.gdx.graphics.Pixmap#getWidth() . 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: 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 2
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 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: BitmapFilm.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public BitmapFilm( Pixmap bitmap, int width, int height ) {
	this.bitmap = bitmap;
	int cols = bitmap.getWidth() / width;
	int rows = bitmap.getHeight() / height;
	for (int i=0; i < rows; i++) {
		for (int j=0; j < cols; j++) {
			Rect rect = new Rect( j * width, i * height, (j+1) * width, (i+1) * height );
			add( i * cols + j, rect );
		}
	}
}
 
Example 5
Source File: AOTextureGenerator.java    From Cubes with MIT License 5 votes vote down vote up
private static void pixmapToDouble(Pixmap in, double[][] out) {
  Color c = new Color();
  for (int x = 0; x < in.getWidth(); x++) {
    for (int y = 0; y < in.getHeight(); y++) {
      c.set(in.getPixel(x, y));
      out[x][y] = c.r;
    }
  }
}
 
Example 6
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 7
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 8
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 9
Source File: SmartTexture.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public SmartTexture( Pixmap bitmap, int filtering, int wrapping ) {

		this.bitmap = bitmap;
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		this.fModeMin = this.fModeMax = filtering;
		this.wModeH = this.wModeV = wrapping;

	}
 
Example 10
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 11
Source File: DebugMeter.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
public DebugMeter (int width, int height) {
	assert (width < 256 && height < 256);

	this.showLabel = true;
	this.name = "";
	this.width = width;
	this.height = height;
	this.pos = new Vector2();

	pixels = new Pixmap(this.width, this.height, Format.RGBA8888);
	texture = new Texture(width, height, Format.RGBA8888);
	texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
	region = new TextureRegion(texture, 0, 0, pixels.getWidth(), pixels.getHeight());
}
 
Example 12
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 ) ) );
}
 
Example 13
Source File: Utils.java    From skin-composer with MIT License 4 votes vote down vote up
/**
 * Does not dispose pixmap.
 * @param pixmap
 * @param ninePatch
 * @return 
 */
public static Color averageEdgeColor(Pixmap pixmap, boolean ninePatch) {
    int border = 0;
    if (ninePatch) {
        border = 1;
    }
    
    Color temp = new Color();
    float sumR = 0.0f;
    float sumG = 0.0f;
    float sumB = 0.0f;
    int count = 0;

    //left edge
    for (int y = border; y < pixmap.getHeight() - border; y++) {
        for (int x = border; x < pixmap.getWidth() - border; x++) {
            temp.set(pixmap.getPixel(x, y));
            if (temp.a > 0) {
                sumR += temp.r;
                sumG += temp.g;
                sumB += temp.b;
                count++;
                break;
            }
        }
    }
    
    //right edge
    for (int y = border; y < pixmap.getHeight() - border; y++) {
        for (int x = pixmap.getWidth() - 1 - border; x > border; x--) {
            temp.set(pixmap.getPixel(x, y));
            if (temp.a > 0) {
                sumR += temp.r;
                sumG += temp.g;
                sumB += temp.b;
                count++;
                break;
            }
        }
    }
    
    //top edge
    for (int x = border; x < pixmap.getWidth() - border; x++) {
        for (int y = border; y < pixmap.getHeight() - border; y++) {
            temp.set(pixmap.getPixel(x, y));
            if (temp.a > 0) {
                sumR += temp.r;
                sumG += temp.g;
                sumB += temp.b;
                count++;
                break;
            }
        }
    }
    
    //bottom edge
    for (int x = border; x < pixmap.getWidth() - border; x++) {
        for (int y = pixmap.getHeight() - 1 - border; y > border; y--) {
            temp.set(pixmap.getPixel(x, y));
            if (temp.a > 0) {
                sumR += temp.r;
                sumG += temp.g;
                sumB += temp.b;
                count++;
                break;
            }
        }
    }
    
    if (count == 0) {
        return new Color(Color.BLACK);
    } else {
        return new Color(sumR / count, sumG / count, sumB / count, 1.0f);
    }
}
 
Example 14
Source File: PixmapRegion.java    From libgdx-snippets with MIT License 4 votes vote down vote up
private void changePixmap(Pixmap pixmap) {
	this.pixmap = pixmap;
	pixels = pixmap.getPixels();
	pixelStride = PixmapUtils.getPixelStride(pixmap.getFormat());
	lineStride = pixmap.getWidth() * pixelStride;
}
 
Example 15
Source File: PixmapUtils.java    From libgdx-snippets with MIT License 4 votes vote down vote up
/**
 * Calculates crop regions of the pixmap in up to four directions. Pixel rows/columns are subject
 * to removal if all their pixels have an alpha channel value of exact the same value as given in the parameter.
 */
public static void crop(Pixmap pixmap,
						boolean left,
						boolean bottom,
						boolean right,
						boolean top,
						float alpha,
						CropResult consumer) {

	int width = pixmap.getWidth();
	int height = pixmap.getHeight();

	int a = MathUtils.floor(alpha * 255.0f) & 0xff;

	int minX = left ? width - 1 : 0;
	int maxX = right ? 0 : width - 1;

	int minY = bottom ? height - 1 : 0;
	int maxY = top ? 0 : height - 1;

	ByteBuffer pixels = pixmap.getPixels();

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {

			int rgba = pixels.getInt();

			if ((rgba & 0xff) != a) {

				minX = Math.min(x, minX);
				maxX = Math.max(x, maxX);

				minY = Math.min(y, minY);
				maxY = Math.max(y, maxY);
			}
		}
	}

	pixels.flip();

	consumer.accept(minX, minY, maxX, maxY);
}
 
Example 16
Source File: BitmapText.java    From shattered-pixel-dungeon-gdx 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 (colorNotMatch(bitmap, 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 ) ) );
		}
 
Example 17
Source File: PixmapRegion.java    From libgdx-snippets with MIT License 4 votes vote down vote up
public PixmapRegion(Pixmap pixmap) {
	this(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight());
}
 
Example 18
Source File: PixmapAtlas.java    From libgdx-snippets with MIT License 3 votes vote down vote up
public static PixmapAtlas createFromPixmap(FileHandle pixmapFile, String regionName) {

		Pixmap pixmap = new Pixmap(pixmapFile);

		PixmapAtlas atlas = new PixmapAtlas();

		// add single pixmap
		atlas.pixmaps.add(pixmap);

		// add one page
		AtlasPage atlasPage = new AtlasPage(0, pixmapFile, pixmap);
		atlas.pages.add(atlasPage);

		// add one region
		int width = pixmap.getWidth();
		int height = pixmap.getHeight();

		AtlasRegion atlasRegion = new AtlasRegion(pixmap, 0, 0, width, height);

		atlasRegion.page = atlasPage;
		atlasRegion.index = 0;
		atlasRegion.name = regionName;
		atlasRegion.rotate = false;

		atlas.regions.add(atlasRegion);

		return atlas;
	}
 
Example 19
Source File: PixmapUtils.java    From libgdx-snippets with MIT License 3 votes vote down vote up
/**
 * Vertically mirrors the {@link Pixmap} content, in place, line by line.
 */
public static void flipY(Pixmap pixmap) {

	int width = pixmap.getWidth();
	int height = pixmap.getHeight();

	int pitch = width * getPixelStride(pixmap.getFormat());

	ByteBuffer pixels = pixmap.getPixels();

	byte[][] buffer = new byte[2][pitch];

	for (int y = 0; y < height / 2; y++) {

		pixels.position(y * pitch);
		pixels.get(buffer[0], 0, pitch);

		pixels.position((height - y - 1) * pitch);
		pixels.get(buffer[1], 0, pitch);

		pixels.position(y * pitch);
		pixels.put(buffer[1], 0, pitch);

		pixels.position((height - y - 1) * pitch);
		pixels.put(buffer[0], 0, pitch);
	}

	pixels.position(0);
}
 
Example 20
Source File: NinePatchEditorDialog.java    From gdx-skineditor with Apache License 2.0 2 votes vote down vote up
public void refreshPreview() {

		Gdx.app.log("NinePatchEditorDialog","refresh preview.");

		Pixmap pixmapImage = new Pixmap(Gdx.files.internal(textSourceImage.getText()));
		
		Pixmap pixmap = new Pixmap((int) (pixmapImage.getWidth()+2), (int) (pixmapImage.getHeight()+2), Pixmap.Format.RGBA8888);
		pixmap.drawPixmap(pixmapImage,1,1);

		pixmap.setColor(Color.BLACK);
		
		// Range left
		int h = pixmapImage.getHeight()+1;
		pixmap.drawLine(0, (int) (h * rangeLeft.rangeStart),0, (int) (h * rangeLeft.rangeStop));
		

		// Range top
		int w = pixmapImage.getWidth()+1;
		pixmap.drawLine((int) (w * rangeTop.rangeStart), 0,(int) (w * rangeTop.rangeStop), 0);

		// Range right
		h = pixmapImage.getHeight()+1;
		pixmap.drawLine(pixmapImage.getWidth()+1, (int) (h * rangeRight.rangeStart),pixmapImage.getWidth()+1, (int) (h * rangeRight.rangeStop));

		// Range bottom
		w = pixmapImage.getWidth()+1;
		pixmap.drawLine((int) (w * rangeBottom.rangeStart), pixmap.getHeight()-1,(int) (w * rangeBottom.rangeStop), pixmap.getHeight()-1);
		
		
		PixmapIO.writePNG(tmpFile, pixmap);
		
		pixmapImage.dispose();
		pixmap.dispose();

		FileHandle fh = new FileHandle(System.getProperty("java.io.tmpdir")).child("skin_ninepatch");
		TexturePacker.Settings settings = new TexturePacker.Settings();
		TexturePacker.process(settings, fh.path(), fh.path(), "pack");

		TextureAtlas ta = new TextureAtlas(fh.child("pack.atlas"));
		NinePatch np = ta.createPatch("button");
		NinePatchDrawable drawable = new NinePatchDrawable(np);
		reviewTablePreview();	
		buttonPreview1.getStyle().up = drawable;
		

	}