public static void write(String filename, PackedImage image) throws IOException {
	File out = new File(filename);
	FileOutputStream fos = new FileOutputStream(out);
	
	String header = "P6\n" + image.cols() + " " + image.rows() + "\n255\n";
	fos.write(header.getBytes());

	byte[] buf = new byte [image.cols() * image.rows() * 3];
	for (int r = 0; r < image.rows(); r++)
	    for (int c = 0; c < image.cols(); c++)
		for (int ch = 0; ch < 3; ch++)
		    buf[3 * (r * image.cols() + c) + ch] = (byte)image.at(r, c, ch);
	fos.write(buf);
	fos.flush();

	fos.close();
    }