com.badlogic.gdx.graphics.PixmapIO Java Examples

The following examples show how to use com.badlogic.gdx.graphics.PixmapIO. 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: BitmapFontWriter.java    From gdx-smart-font with MIT License 6 votes vote down vote up
/** A utility method to write the given array of pixmaps to the given output directory, with the specified file name. If the
 * pages array is of length 1, then the resulting file ref will look like: "fileName.png".
 * 
 * If the pages array is greater than length 1, the resulting file refs will be appended with "_N", such as "fileName_0.png",
 * "fileName_1.png", "fileName_2.png" etc.
 * 
 * The returned string array can then be passed to the <tt>writeFont</tt> method.
 * 
 * Note: None of the pixmaps will be disposed.
 * 
 * @param pages the pages of pixmap data to write
 * @param outputDir the output directory
 * @param fileName the file names for the output images
 * @return the array of string references to be used with <tt>writeFont</tt> */
public static String[] writePixmaps (Pixmap[] pages, FileHandle outputDir, String fileName) {
	if (pages==null || pages.length==0)
		throw new IllegalArgumentException("no pixmaps supplied to BitmapFontWriter.write");
	
	String[] pageRefs = new String[pages.length];
	
	for (int i=0; i<pages.length; i++) {
		String ref = pages.length==1 ? (fileName+".png") : (fileName+"_"+i+".png");
		
		//the ref for this image
		pageRefs[i] = ref;
		
		//write the PNG in that directory
		PixmapIO.writePNG(outputDir.child(ref), pages[i]);
	}
	return pageRefs;
}
 
Example #2
Source File: Screenshot.java    From Cubes with MIT License 6 votes vote down vote up
private static void writeScreenshot(final Pixmap pixmap) {
  FileHandle dir = Compatibility.get().getBaseFolder().child("screenshots");
  dir.mkdirs();
  final FileHandle f = dir.child(System.currentTimeMillis() + ".png");
  Log.info("Writing screenshot '" + f.file().getAbsolutePath() + "'");
  Executor.executeNotSided(new Runnable() {
    @Override
    public void run() {
      long start = System.currentTimeMillis();
      try {
        PixmapIO.PNG writer = new PixmapIO.PNG((int) (pixmap.getWidth() * pixmap.getHeight() * 1.5f));
        try {
          writer.setFlipY(true);
          writer.write(f, pixmap);
        } finally {
          writer.dispose();
        }
      } catch (IOException ex) {
        throw new CubesException("Error writing PNG: " + f, ex);
      } finally {
        pixmap.dispose();
      }
      Log.debug("Finished writing screenshot '" + f.file().getAbsolutePath() + "' (took " + (System.currentTimeMillis() - start) + "ms)");
    }
  });
}
 
Example #3
Source File: BitmapFontWriter.java    From Cubes with MIT License 6 votes vote down vote up
/** A utility method to write the given array of pixmaps to the given output directory, with the specified file name. If the
 * pages array is of length 1, then the resulting file ref will look like: "fileName.png".
 *
 * If the pages array is greater than length 1, the resulting file refs will be appended with "_N", such as "fileName_0.png",
 * "fileName_1.png", "fileName_2.png" etc.
 *
 * The returned string array can then be passed to the <tt>writeFont</tt> method.
 *
 * Note: None of the pixmaps will be disposed.
 *
 * @param pages the pages of pixmap data to write
 * @param outputDir the output directory
 * @param fileName the file names for the output images
 * @return the array of string references to be used with <tt>writeFont</tt> */
public static String[] writePixmaps (Pixmap[] pages, FileHandle outputDir, String fileName) {
  if (pages == null || pages.length == 0) throw new IllegalArgumentException("no pixmaps supplied to BitmapFontWriter.write");
  
  String[] pageRefs = new String[pages.length];
  
  for (int i = 0; i < pages.length; i++) {
    String ref = pages.length == 1 ? (fileName + ".png") : (fileName + "_" + i + ".png");
    
    // the ref for this image
    pageRefs[i] = ref;
    
    // write the PNG in that directory
    PixmapIO.writePNG(outputDir.child(ref), pages[i]);
  }
  return pageRefs;
}
 
Example #4
Source File: BitmapFontWriter.java    From gdx-proto with Apache License 2.0 6 votes vote down vote up
/** A utility method to write the given array of pixmaps to the given output directory, with the specified file name. If the
 * pages array is of length 1, then the resulting file ref will look like: "fileName.png".
 * 
 * If the pages array is greater than length 1, the resulting file refs will be appended with "_N", such as "fileName_0.png",
 * "fileName_1.png", "fileName_2.png" etc.
 * 
 * The returned string array can then be passed to the <tt>writeFont</tt> method.
 * 
 * Note: None of the pixmaps will be disposed.
 * 
 * @param pages the pages of pixmap data to write
 * @param outputDir the output directory
 * @param fileName the file names for the output images
 * @return the array of string references to be used with <tt>writeFont</tt> */
public static String[] writePixmaps (Pixmap[] pages, FileHandle outputDir, String fileName) {
	if (pages==null || pages.length==0)
		throw new IllegalArgumentException("no pixmaps supplied to BitmapFontWriter.write");
	
	String[] pageRefs = new String[pages.length];
	
	for (int i=0; i<pages.length; i++) {
		String ref = pages.length==1 ? (fileName+".png") : (fileName+"_"+i+".png");
		
		//the ref for this image
		pageRefs[i] = ref;
		
		//write the PNG in that directory
		PixmapIO.writePNG(outputDir.child(ref), pages[i]);
	}
	return pageRefs;
}
 
Example #5
Source File: DialogTenPatch.java    From skin-composer with MIT License 5 votes vote down vote up
private void saveToImageFile(FileHandle fileHandle) {
    var source = loadPixmapFile(this.fileHandle);
    var pixmap = new Pixmap(source.getWidth() + 2, source.getHeight() + 2, source.getFormat());
    pixmap.setBlending(Pixmap.Blending.None);
    
    pixmap.drawPixmap(source, 1, 1);
    source.dispose();
    
    var tenPatchData = drawableData.tenPatchData;
    
    pixmap.setColor(Color.BLACK);
    var stretchAreas = tenPatchData.horizontalStretchAreas;
    for (var i = 0; i + 1 < stretchAreas.size; i += 2) {
        pixmap.drawRectangle(stretchAreas.get(i) + 1, 0, stretchAreas.get(i + 1) - stretchAreas.get(i) + 1, 1);
    }
    
    stretchAreas = tenPatchData.verticalStretchAreas;
    for (var i = 0; i + 1 < stretchAreas.size; i += 2) {
        pixmap.drawRectangle(0, pixmap.getHeight() - stretchAreas.get(i + 1) - 2, 1, stretchAreas.get(i + 1) - stretchAreas.get(i) + 1);
    }
    
    pixmap.drawRectangle(tenPatchData.contentLeft, pixmap.getHeight() - 1, (pixmap.getWidth() - tenPatchData.contentRight) - tenPatchData.contentLeft, 1);
    
    pixmap.drawRectangle(pixmap.getWidth() - 1, tenPatchData.contentTop, 1, (pixmap.getHeight() - tenPatchData.contentBottom) - tenPatchData.contentTop);
    
    PixmapIO.writePNG(fileHandle, pixmap);
    pixmap.dispose();
}
 
Example #6
Source File: Dialog9Patch.java    From skin-composer with MIT License 5 votes vote down vote up
private void saveNinePatch(FileHandle originalFile, FileHandle targetFile, int ninePatchLeft, int ninePatchRight, int ninePatchBottom, int ninePatchTop, int ninePatchContentLeft, int ninePatchContentRight, int ninePatchContentBottom, int ninePatchContentTop) {
    var originalImage = new Pixmap(originalFile);
    
    if (originalFile.path().toLowerCase(Locale.ROOT).endsWith(".9.png")) {
        var cropped = new Pixmap(originalImage.getWidth() - 2, originalImage.getHeight() - 2, Pixmap.Format.RGBA8888);
        cropped.setBlending(Pixmap.Blending.None);
        
        cropped.drawPixmap(originalImage, 0, 0, 1, 1, originalImage.getWidth() - 2, originalImage.getHeight() - 2);
        originalImage.dispose();
        originalImage = cropped;
    }
    
    var savePixmap = new Pixmap(originalImage.getWidth() + 2, originalImage.getHeight() + 2, Pixmap.Format.RGBA8888);
    savePixmap.setBlending(Pixmap.Blending.None);
    savePixmap.drawPixmap(originalImage, 1, 1);
    
    savePixmap.setColor(Color.BLACK);
    savePixmap.drawRectangle(ninePatchLeft + 1, 0, savePixmap.getWidth() - ninePatchLeft - ninePatchRight - 2, 1);
    savePixmap.drawRectangle(0, ninePatchTop + 1, 1, savePixmap.getHeight() - ninePatchBottom - ninePatchTop - 2);
    savePixmap.drawRectangle(ninePatchContentLeft + 1, savePixmap.getHeight() - 1, savePixmap.getWidth() - ninePatchContentLeft - ninePatchContentRight - 2, 1);
    savePixmap.drawRectangle(savePixmap.getWidth() - 1, ninePatchContentTop + 1, 1, savePixmap.getHeight() - ninePatchContentBottom - ninePatchContentTop - 2);
    
    PixmapIO.writePNG(targetFile, savePixmap);
    
    originalImage.dispose();
    savePixmap.dispose();
}
 
Example #7
Source File: World.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
public void takeScreenshot(String filename, int w) {

		// get viewport
		IntBuffer results = BufferUtils.newIntBuffer(16);
		Gdx.gl20.glGetIntegerv(GL20.GL_VIEWPORT, results);

		int h = (int) (w * getSceneCamera().viewportHeight / getSceneCamera().viewportWidth);

		FrameBuffer fbo = new FrameBuffer(Format.RGB565, w, h, false);

		fbo.begin();
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		draw();
		Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, w, h);

		// restore viewport
		fbo.end(results.get(0), results.get(1), results.get(2), results.get(3));

		// Flip the pixmap upside down
		ByteBuffer pixels = pixmap.getPixels();
		int numBytes = w * h * 4;
		byte[] lines = new byte[numBytes];
		int numBytesPerLine = w * 4;
		for (int i = 0; i < h; i++) {
			pixels.position((h - i - 1) * numBytesPerLine);
			pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
		}
		pixels.clear();
		pixels.put(lines);

		PixmapIO.writePNG(EngineAssetManager.getInstance().getUserFile(filename), pixmap);

		fbo.dispose();
	}
 
Example #8
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 #9
Source File: ShareChallenge.java    From Klooni1010 with GNU General Public License v3.0 4 votes vote down vote up
public boolean saveChallengeImage(final int score, final boolean timeMode) {
    final File saveAt = getShareImageFilePath();
    if (!saveAt.getParentFile().isDirectory())
        if (!saveAt.mkdirs())
            return false;

    final FileHandle output = new FileHandle(saveAt);

    final Texture shareBase = new Texture(Gdx.files.internal("share.png"));
    final int width = shareBase.getWidth();
    final int height = shareBase.getHeight();

    final FrameBuffer frameBuffer = new FrameBuffer(Pixmap.Format.RGB888, width, height, false);
    frameBuffer.begin();

    // Render the base share texture
    final SpriteBatch batch = new SpriteBatch();
    final Matrix4 matrix = new Matrix4();
    matrix.setToOrtho2D(0, 0, width, height);
    batch.setProjectionMatrix(matrix);

    Gdx.gl.glClearColor(Color.GOLD.r, Color.GOLD.g, Color.GOLD.b, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    batch.draw(shareBase, 0, 0);

    // Render the achieved score
    final Label.LabelStyle style = new Label.LabelStyle();
    style.font = new BitmapFont(Gdx.files.internal("font/x1.0/geosans-light64.fnt"));
    Label label = new Label("just scored " + score + " on", style);
    label.setColor(Color.BLACK);
    label.setPosition(40, 500);
    label.draw(batch, 1);

    label.setText("try to beat me if you can");
    label.setPosition(40, 40);
    label.draw(batch, 1);

    if (timeMode) {
        Texture timeModeTexture = new Texture("ui/x1.5/stopwatch.png");
        batch.setColor(Color.BLACK);
        batch.draw(timeModeTexture, 200, 340);
    }

    batch.end();

    // Get the framebuffer pixels and write them to a local file
    final byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, width, height, true);

    final Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);

    BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length);
    PixmapIO.writePNG(output, pixmap);

    // Dispose everything
    pixmap.dispose();
    shareBase.dispose();
    batch.dispose();
    frameBuffer.end();

    return true;
}
 
Example #10
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;
		

	}