ImageIO以最大速度比GZIPStream更快

时间:2013-01-09 12:40:14

标签: java image bytebuffer

我正在寻找将图像从GPU下载到文件的最快方法,以便以后加载到同一个应用程序中(例如,不一定是PNG)。

但是我注意到,当我直接使用DeflaterStream时,它比使用DeflaterStream的ImageIO慢得多(所以使用ImageIO.write(deflaterstream))。

我做错了吗?或者ImageIO刚刚优化/优于最快的GZIP压缩?

  glBindTexture(GL_TEXTURE_2D, textureId);

        int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
        ByteBuffer buffer = BufferUtils.createByteBuffer(SAVE_WIDTH * SAVE_HEIGHT * bpp);
        glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA8, GL_UNSIGNED_BYTE, buffer );

//        File file = new File("saves/s-" + layer + ".png"); // The file to save to.
        String format = "png"; // Example: "PNG" or "JPG"
//        BufferedImage image = new BufferedImage(SAVE_WIDTH, SAVE_HEIGHT, BufferedImage.TYPE_INT_ARGB);

        try {
            FileOutputStream bOut = new FileOutputStream("saves/p-"+ layer + ".gz");
            DeflaterOutputStream  gzipOut = new DeflaterOutputStream (bOut, new Deflater(Deflater.BEST_SPEED));
            //buffer.flip();
            System.out.println("Bytes remaining " + buffer.remaining());
            while (buffer.hasRemaining()) gzipOut.write(buffer.get());
            gzipOut.close();
            bOut.close();
        } catch (IOException ex) {
            Logger.getLogger(SaveStateManager.class.getName()).log(Level.SEVERE, null, ex);
        }

1 个答案:

答案 0 :(得分:1)

压缩总是很昂贵,但您可以通过

进行改进
OutputStream bOut = new BufferedOutputStream(new FileOutputStream("saves/p-" + layer + ".gz"));
DeflaterOutputStream defOut = new DeflaterOutputStream(bOut, new Deflater(Deflater.BEST_SPEED));
//buffer.flip();
byte[] bytes = new byte[1024];
while (buffer.hasRemaining()) {
    int len = Math.min(buffer.remaining(), bytes.length);
    buffer.get(bytes, 0, len);
    defOut.write(bytes, 0, len);
}
defOut.close();