Reputation: 4459
I am looking for the fastest way to download an image from the GPU to a file, for later loading in the same application (so not necessarily PNG e.g.).
I noticed however that when I use a DeflaterStream directly, it is considerably slower than ImageIO with DeflaterStream ( so using ImageIO.write(deflaterstream) ).
Am I doing something wrong? Or is ImageIO just heavily optimized/better than fastest GZIP compression?
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);
}
Upvotes: 0
Views: 287
Reputation: 533560
Conmpression is always expensive but you might be able to improve with
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();
Upvotes: 1