Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bitmap Pixels - write directly to file?

Background:

The goal is to write a rather large (at least 2048 x 2048 pixels) image file with OpenGL rendered data.

Today I first use glReadPixels in order to get the 32-bit (argb8888) pixel data into an int array.

Then I copy the data into a new short array, converting the 32-bit argb values into 16-bit (rgb565) values. At this point I also turn the image upside down and change the color order to make the opengl-image data compatible with android bitmap data (different row order and color channel order).

Finally I create a Bitmap() instance and .copyPixelsFromBuffer(Buffer b) in order to be able to save it to disk as a png-file.

However I want to use memory more efficient in order to avoid out of memory crashes on some phones.


Question:

  1. Can I skip the first transformation from int[] -> short[] in some way (and avoid the allocation of a new array for pixel data)? Maybe just use byte arrays / buffers and write the converted pixels to the same array I read from...

  2. More important: Can I skip the bitmap creation (here's where the program crash) and somehow write the data directly to disk as a working image file (and avoid allocation of the pixel data again in the bitmap object)?

EDIT: If I could write the data directly to file, maybe I don't need to convert to 16-bit pixel data, depending on the file size and how fast the file can be read into memory at a later point.

like image 279
cmbellman Avatar asked Sep 01 '12 08:09

cmbellman


1 Answers

I'm not sure that this could help but, this PNGJ library allows to write a PNG sequentially, line by line. If memory usage if your primary concern (and if you can access the pixels values in the order of the final PNG file from the rendered data) it could be useful.

like image 102
leonbloy Avatar answered Sep 22 '22 20:09

leonbloy