All pastes #384242 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-06 22:44 UTC
rendered paste body
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.util.zip.*;
import java.io.*;

public class PixelPacker {

	private String file;
	private int COMPRESSION_LEVEL;

	public static void main(String args[]) {
		try {
			new PixelPacker(args[0]).pack();
		} catch (Exception _ex) { ex.printStackTrace(); return; }
	}

	public PixelPacket(final String file, final int COMPRESSION_LEVEL) {
		this.file = file;
		this.COMPRESSION_LEVEL = COMPRESSION_LEVEL;
	}

	public void pack()
		throws Exception {
		
		// Read sprite sheet
		FileInputStream in = new FileInputStream(file);
		BufferedImage img = ImageIO.read(in);
		
		// Store dimensions
		final int w, h;
		w = img.getWidth();
		h = img.getHeight();
		
		// Grab image data
		PixelGrabber g = new PixelGrabber(img, 0, 0, w, h, false);
		g.grabPixels();
		pixels = (int[])g.getPixels();

		// Make output streams
		FileOutputStream out = new FileOutputStream("compressed.cmp");
		ZipOutputStream zipOut = new ZipOutputStream(out);
		zipOut.setLevel(compression);

		// Generate header file
		zipOut.putNextEntry(new ZipEntry(file));
		byte[] header = (w + "x" + h).getBytes();
		zipOut.write(header, 0, header.length);

		// Generate sprite files
		int buffer = new int[w * h * 3];
		// Increment Y axis
		for(int y = 0; y < h; y++) {
			// Increment X axis
			for(int x = 0; x < w; x++) {
			}
		}
	}
}