Java Examples for com.googlecode.pngtastic.core.PngOptimizer

The following java examples will help you to understand the usage of com.googlecode.pngtastic.core.PngOptimizer. These source code samples are taken from different open source projects.

Example 1
Project: voxels-master  File: FileOut.java View source code
// write an image file compressed
public void writeImageCompressed(BufferedImage img) throws IOException {
    // convert to png
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
    ImageIO.write(img, "png", tmp);
    tmp.close();
    // compress
    PngImage pngImage = new PngOptimizer().optimize(new PngImage(new ByteArrayInputStream(tmp.toByteArray())), 9);
    // extract compressed data as png
    ByteArrayOutputStream compressedPngData = new ByteArrayOutputStream();
    DataOutputStream outputStreamWrapper = new DataOutputStream(compressedPngData);
    outputStreamWrapper.writeLong(PngImage.SIGNATURE);
    for (PngChunk chunk : pngImage.getChunks()) {
        outputStreamWrapper.writeInt(chunk.getLength());
        outputStreamWrapper.write(chunk.getType());
        outputStreamWrapper.write(chunk.getData());
        int i = (int) chunk.getCRC();
        outputStreamWrapper.writeInt(i);
    }
    outputStreamWrapper.close();
    compressedPngData.close();
    // convert to png byte array
    byte[] data = compressedPngData.toByteArray();
    int contentLength = data.length;
    // write the size
    this.writeIntRev(contentLength);
    // write the data
    p.write(data);
}