Create Bitmap from palette and byte array ...

All your problems with Audio, Video and Images.

Create Bitmap from palette and byte array ...

Postby menion » Wed Jan 13, 2010 12:52 pm

unfortunately I'm trying to create Bitmap object, from not typical data source ...

my problem:
- I'm painting map tiles onto screen (tiles are 120 x 120px - 14400px)
- from map file with tiles I obtain
1. palette - array of int int[256] - 256 colors
2. image data, deflated by ZLIB, after decopress (by Inflater) i receive 14400b array of byte[], that contain indexes of colors in color array

currently i'm not using JNI so to create Bitmap I use this code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  2.   // used variables
  4.   int[] palette = new int[256]; // 256 int colors
  6.   byte[] imageBuffer = new byte[120 * 120]; // fill with loaded data
  8.   int[] imageData = new int[120 * 120]; // here i create new image data
  12.   // 12 ms / image - stupid and very slow cycle to fill image data with right colors
  14.   for (int i = 0; i < 14400; i++) {
  16.     imageData[i] = palette[imageBuffer[i] & 0xFF];
  18.   }
  22.   Bitmap bitmap = Bitmap.createBitmap(imageData, 120, 120, Bitmap.Config.RGB_565);
Parsed in 0.033 seconds, using GeSHi

- so, after loading image data, I have to create new array filled with right color for every pixel. This is very slow (cycle with 14400 computes for every image). I'm testing it on motorola milestone and cause of big resolution, I need around 40 images to cover whole screen. Because this is map application, so you move with tiles, this method is very slow. Because I'm not much familiar with C++, I'm asking if anyone have any idea how to make it faster, probably with using of JNI.

- best solution should be direct creating of Bitmap from palette and compressed datas, but this is surely not in API. I also tryed to create PNG image (directly from loaded bytes) which have similar PLTE and IDAT part, but this IDAT do not containg filter byte '0' in every image row.

any suggestions are very very welcome, thank you ...
Posts: 4
Joined: Wed Jan 13, 2010 12:36 pm


Postby nhenze » Thu Jan 14, 2010 12:19 am

Using JNI should be straightforward. If you aim at really high performance you might use ByteBuffer instead of arrays and process multiple pixels simultaneously.

Looking at your code I wonder if it contains a bug. I'm not completely sure how Java handles bytes but they can only contain values between -128 and 127. Using the "& 0xFF" you end up with values between 0 and 127. I assume that this is not what you want.

An idea to improve the performance is to process two bytes at a time by using a 256x256 palette containing all permutations of two palette entries. You would put an array of short values in the function containing two pixels each and process two pixels simultaneously.
Posts: 9
Joined: Thu Dec 10, 2009 10:34 am

Postby menion » Sun Jan 17, 2010 8:52 am

that you for your response

firstly I looked on line "palette[imageBuffer[i] & 0xFF]" and you're probably right with values that is should return, but i didn't. This code returns values in interval 0 to 255 as I need :).

I also tried change classic arrays to ByteBuffers and process two bytes at once, but speed improvement was about 20%. Good, but no enough. Finally I wrote own PNG Encoder and tried to create PNG image from loaded data and with code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. BitmapFactory.decodeByteArray(data, 0, data.length);
Parsed in 0.030 seconds, using GeSHi

I'm creating bitmap objects.
This should be enough fast but with one problem. Png images needs to have some "line filter" byte before every image row, which I don't have in loaded data. And solution as "decompress data, add zero byte before every row, compress data, create png, create bitmap" is totally unusable.
Damn, ...
If anyone have any idea, it should be very very welcome. Anyway thanks nhenze.
Posts: 4
Joined: Wed Jan 13, 2010 12:36 pm


Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 11 guests