Allocating Memory when Saving to a Byte Array

Snowbound Software’s RasterMaster for Java includes the IMG_save_bitmap(int, int, int, int) method that allows you to save to a byte array without knowing or guessing the final size of the compressed image or document. 

To determine the buffer size for the byte array, the method takes the following steps. First, the method allocates the initial size parameter. If the output data saved becomes larger than the initial size, then the buffer is reallocated to the initial_size variable plus the increment size specified by the buff_inc variable. Each time the output data becomes larger than the current buffer size when saving, the buffer is increased in size by the increment size. The caller must be careful in choosing the buffer increment size. If the buffer increment is too small, multiple reallocations will cause the saving process to be slow. If it is too large, too much memory will be allocated.

Note: We recommend an increment size of 10-25% of the initial buffer size.

The following describes file formats and their compression ratio:

Group 4 compression found in TIFF and AFP file formats typically compress at a rate of 10:1. You can choose an initial size of 1/8 to 1/10 of the uncompressed size.

A JPEG file format typically compresses at a rate of 10:1.

An LZW file format found in TIFF and GIF file formats compress at a rate of about 3:1. A .Zip or flate file found in a PNG file format also compresses at a rate of about 3:1.

Note: Compression results for your images may vary. For best results, test a number of images to see what the resultant output size will be. Also, it is suggested to test out smaller and larger increment sizes to find the best level of performance.

The following shows the IMG_save_bitmap(int, int, int, int)method syntax and variable descriptions:


int IMG_save_bitmap(int initial_size, int buff_inc, int comp_type, int error[]);


The table below lists the IMG_save_bitmap(int, int, int, int) method variable descriptions.




The buffer is initially allocated to this size exactly.


The amount to grow the buffer if needed.


Output file format to write.


Returns the size of the data. If the value is less than zero, it is an error code.



Integer. Any positive value (as well as 0) is a valid return


Online Demo