Android fast bitmap drawing - java

I need to optimize this portion of code which consume about 70ms (on my Samsung Galaxy Tab S2 - Android 7.0):
private static final int IR_FRAME_WIDTH = 160;
private static final int IR_FRAME_HEIGHT = 120;
...
final int[] bmp_data = NormalizeBmp(image_data, IR_FRAME_WIDTH, IR_FRAME_HEIGHT, Polarity.WhiteHot);
Bitmap bitmap = Bitmap.createBitmap(IR_FRAME_WIDTH, IR_FRAME_HEIGHT, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, 0, null);
final Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
for (int y = 0; y < IR_FRAME_HEIGHT; y++)
for(int x = 0; x < IR_FRAME_WIDTH; x++) {
final int _gray = bmp_data[(y * IR_FRAME_WIDTH) + x];
paint.setColor(Color.rgb(_gray, _gray, _gray));
canvas.drawPoint(x, y, paint);
}
I'm not used to native code so I would prefer not-to use it if there's a standard code way to improve this performances.
I think the code is pretty self-explaining NormalizeBmp create an array of 160x120 integers containing the gray tone to use. I can easily change NormalizeBmp to produce a different color output.

Instead of drawing pixel by pixel over the canvas, what you can do is to create a bitmap directly from the data returned by NormalizeBmp(). All you need is to modify the returned int array to pack the colors in RGB format. Since you are using the same color to set your Paint() in RGB, this would be pretty straightforward. Here's a simple way to pack the color:
retVal[i] = (0xFF << 24) | (retVal[i] & 0xFF)<< 16 | (retVal[i] & 0xFF) << 8 | retVal[i]
and then use the modified array to draw a bitmap using Bitmap.Create(int[] colors, width, height, bitmap_format).

Related

How to display HDR picture in Android?

I'm doing an android app to decompress, decode and display HDR pictures.
These HDR pictures use 2 bytes per component (A,R,G,B) so one pixel is represented by a 8 bytes value that can only fit with the long type.
I'm using android's Bitmap to display picture as they have a constructor allowing to do HDR by using Bitmap.Config.RGBA_F16:
int width = 1;
int height = 1;
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.RGBA_F16);
Unfortunately I can't find any way to fill a pixel of the Bitmap. I use the recommended formula but it cannot be used in the setPixel(x,y,color) method of Bitmap because color has to be a int:
long color = (A & 0xffff) << 48 | (B & 0xffff) << 32 | (G & 0xffff) << 16 | (R & 0xffff);
image.setPixel(0,0,color); //Argument type error
.
I have also tried with Color (which has a HDR compatible method), Paint and Canvas but no Bitmap method accepts them to set only one pixel.
Thanks for any help!
If you need to open an HDR file, such as 16-bit png, and then display it, you can use ImageDecoder.setTargetColorSpace to create bitmap with format Bitmap.Config.RGBA_F16 like so:
File file = new File(...);
ImageDecoder.Source source = ImageDecoder.createSource(file);
Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, src) -> {
decoder.setTargetColorSpace(ColorSpace.Named.EXTENDED_SRGB);
});
If you need to display HDR image that is stored in memory you can use Bitmap.copyPixelsFromBuffer, as this method allows to set pixels of the bitmap without conversion of color space the way Bitmap.setPixel does. In this case you need to pack 4 channels represented by Half values into long for each pixel, then write these long values into a Buffer and finally copy pixels from the buffer to the bitmap.
LongBuffer buffer = LongBuffer.allocate(width * height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// fill pixels values as needed
float r = (float)y / height;
float g = (float)y / height;
float b = (float)y / height;
float a = 1f;
long rBits = Half.halfToShortBits(Half.toHalf(r));
long gBits = Half.halfToShortBits(Half.toHalf(g));
long bBits = Half.halfToShortBits(Half.toHalf(b));
long aBits = Half.halfToShortBits(Half.toHalf(a));
long color = aBits << 48 | bBits << 32 | gBits << 16 | rBits;
buffer.put(color);
}
}
buffer.rewind();
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGBA_F16);
bitmap.copyPixelsFromBuffer(buffer);

Fill not-transparent part of image by color

I have one task in Libgdx:
Change color of image for example triangle,star, heart and others shapes.
All shapes are given in png with transparent background.
I'm doing this with Pixmap, checking every pixel if it is not-transparent fill pixel with needed color.
Here is the code:
for (int y = 0; y < pixmap.getHeight(); y++) {
for (int x = 0; x < pixmap.getWidth(); x++) {
Color color = new Color();
Color.rgba8888ToColor(color, pixmap.getPixel(x, y));
if(color.r != 1 || color.b != 1 && color.g != 1){
pixmap.setColor(setColor);
pixmap.fillRectangle(x, y, 1, 1);
}
}
}
Is there any other way to do this?
Because method below works too long.
You can certainly speed up the way you're doing it, because right now for every pixel in the image you are instantiating a new Color object and converting the pixel components into separate floats. And then the GC will have to take time to clear up all those Color objects you are generating. Those extra intermediate steps are unnecessary.
Also, you only need to call pixmap.setColor one time (although that is fairly trivial). And you can use drawPixel instead of fillRectangle to more efficiently draw a single pixel.
static final int R = 0xFF000000;
static final int G = 0x00FF0000;
static final int B = 0x0000FF00;
pixmap.setColor(setColor);
for (int y = 0; y < pixmap.getHeight(); y++) {
for (int x = 0; x < pixmap.getWidth(); x++) {
int pixel = pixmap.getPixel(x, y);
if((pixel & R) != R || (pixel & B) != B && (pixel & G) != G){
pixmap.drawPixel(x, y);
}
}
}
(By the way, did you mean to check red or blue and green? Seems like odd criteria unless you only want to change the color if the original color is pure yellow, cyan, or white.)
If you are merely drawing the images as Textures, then there is no need to be operating on the Pixmaps like this. You could make your source image white and tint the image when drawing it with SpriteBatch, for example, and this would have no impact on performance.
Support library provides utilities to tint drawable.
// create a drawable from the bitmap
BitmapDrawable tintedDrawable = DrawableCompat.wrap(new BitmapDrawable(getResources(), pixmap));
// Apply a Tint, it will color all non-transparent pixel
DrawableCompat.setTint(setColor);
// Draw it back on a bitmap
Bitmap b = Bitmap.createBitmap(pixmap.getWidth(), pixmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
tintedDrawable.setBounds(0, 0, pixmap.getWidth(), pixmap.getHeight());
tintedDrawable.draw(c);
If you just need to show these pictures with a specific color in your application you can simply do it with setColorFilter
ImageView ivEx = (ImageView) findViewById(R.id.ivEx);
int color = Color.parseColor("your color's code");
ivEx.setColorFilter(color);

Why raster.setPixels() is returning a grayscale image

Here i'm trying to do a fastest method to save 3 matrix(R, G and B) into a BufferedImage.
I've found this method here at StackExchange, but it doesn't work for me because the image it's being saved in a grayscale color.
If I'm doing something wrong or if there's a way of doing this faster than bufferimage.setRGB(), please help me. Thanks!
public static BufferedImage array_rasterToBuffer(int[][] imgR,
int[][]imgG, int[][] imgB) {
final int width = imgR[0].length;
final int height = imgR.length;
int numBandas = 3;
int[] pixels = new int[width*height*numBandas];
int cont=0;
System.out.println("max: "+width*height*3);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
for (int band = 0; band < numBandas; band++) {
pixels[(((i*width)+j)*numBandas +band)] =Math.abs(( (imgR[i][j] & 0xff) >> 16 | (imgG[i][j] & 0xff) >> 8 | (imgB[i][j] & 0xff)));
cont+=1;
}
}
}
BufferedImage bufferImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
WritableRaster rast = (WritableRaster) bufferImg.getData();
rast.setPixels(0, 0, width, height, pixels);
bufferImg.setData(rast);
return bufferImg;
}
I think you are getting grey because the expression
Math.abs(( (imgR[i][j] & 0xff) >> 16 | (imgG[i][j] & 0xff) >> 8 | (imgB[i][j] & 0xff)));
does not depend on band, so your rgb values are all the same.
The expression looks dodgy anyway because you normally use the left shift operator << when packing rgb values into a single int.
I don't know for sure, as I'm not familiar with the classes you are using, but I'm guessing something like this might work
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixels[(((i*width)+j)*numBandas)] = imgR[i][j] & 0xFF;
pixels[(((i*width)+j)*numBandas + 1)] = imgG[i][j] & 0xFF;
pixels[(((i*width)+j)*numBandas + 2)] = imgB[i][j] & 0xFF;
}
}
If you want a faster approach, you need to get the "live" WritableRaster from the BufferedImage and set pixels in the "native" format of the image, which is "pixel packed" for TYPE_INT_RGB. This will save you multiple (at least two) array copies and some data conversion. It will also save you 2/3rds of the memory used for the conversion, as we only need a single array component per pixel.
The below method should be quite a bit faster:
public static BufferedImage array_rasterToBuffer(int[][] imgR, int[][] imgG, int[][] imgB) {
final int width = imgR[0].length;
final int height = imgR.length;
// The bands are "packed" for TYPE_INT_RGB Raster,
// so we need only one array component per pixel
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// "Pack" RGB values to native TYPE_INT_RGB format
// (NOTE: Do not use Math.abs on these values, and without alpha there won't be negative values)
pixels[((y * width) + x)] = ((imgR[y][x] & 0xff) << 16 | (imgG[y][x] & 0xff) << 8 | (imgB[y][x] & 0xff));
}
}
BufferedImage bufferImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// NOTE: getRaster rather than getData for "live" view
WritableRaster rast = bufferImg.getRaster();
// NOTE: setDataElements rather than setPixels to avoid conversion
// This requires pixels to be in "native" packed RGB format (as above)
rast.setDataElements(0, 0, width, height, pixels);
// No need for setData as we were already working on the live data
// thus saving at least two expensive array copies
return bufferImg;
}
// Test method, displaying red/green/blue stripes
public static void main(String[] args) {
int[][] fooR = new int[99][99];
int[][] fooG = new int[99][99];
int[][] fooB = new int[99][99];
for (int i = 0; i < 33; i++) {
Arrays.fill(fooR[i], 0xff);
Arrays.fill(fooG[i + 33], 0xff);
Arrays.fill(fooB[i + 66], 0xff);
}
BufferedImage image = array_rasterToBuffer(fooR, fooG, fooB);
showIt(image);
}
// For demonstration only
private static void showIt(final BufferedImage image) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("JPEGTest");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JScrollPane scroll = new JScrollPane(new JLabel(new ImageIcon(image)));
scroll.setBorder(BorderFactory.createEmptyBorder());
frame.add(scroll);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
It is possible to optimize this further, if you don't need a "managed" (possible hardware accelerated for display) image. The trick is to create the image directly "around" your pixels array, thus saving one more array allocation and array copy in setDataElements. The downside is that in some cases the image will be a little slower to draw onto the screen. This is mainly a concern for games or smooth animations though.
Replace the lines from BufferedImage bufferImg = new BufferedImage... until the return statement, with the following code:
DataBufferInt buffer = new DataBufferInt(pixels, pixels.length);
int[] bandMasks = {0xFF0000, 0xFF00, 0xFF}; // RGB (no alpha)
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandMasks, null);
ColorModel cm = new DirectColorModel(32,
0x00ff0000, // Red
0x0000ff00, // Green
0x000000ff, // Blue
0x00000000 // No Alpha
);
BufferedImage bufferImg = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
PS: Note that I also changed the shifts inside the x/y loop, from right to left shifts. Might have been just a minor typo. :-)

Android parse base64Binary pgm to Bitmap

I need to convert a base64Binary-string in the format pgm to a bitmap in android. So I don't have an usual base64-encoded Bitmap.
The base64binary-string came from a xml file
<ReferenceImage Type="base64Binary" Format="pgm" WidthPX="309" HeightPX="233" BytesPerPixel="1" >
NDY4Ojo9QEFDRUVHRklLTE9OUFFTU1VWV1hZWltZWVlZWlpbW1xdXmBgYmJjZGNlZWRkZGRlZmZnZ2ZnaWpqa21ub29ubm9vb3BwcHBxcHFyc3FzcnJzcnJydH[...]VlaW1xbWltcXFxcXFxd.
Pattern.compile("<ReferenceImage .*>((?s).*)<\\/ReferenceImage>");
...
String sub = r; //base64binary string pattern-matched from xml file
byte[] decodedString = Base64.decode(sub.getBytes(), Base64.NO_WRAP); //probably wrong decoding (needs to be ASCII to binary?)
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); //always null due to wrong byte-array
I think I understand that pgm images are typically stored as ASCII (like in my xml) or binary (0..255). I also think that Base64.decode needs the binary-variant, not the ASCII that I have.
However BitmapFactory.decodeByteArray doesn't understand the decoded byte-array and returns null.
So how can I convert my base64binary-pgm-ASCII-string to a valid byte-array in order to create a valid bitmap?
I think your Base64-decoding is fine. But Android's BitmapFactory probably has no direct support for PGM format. I'm not sure how to add support to it, but it seems you could create a Bitmap using one of the createBitmap(...) factory methods quite easily.
See PGM spec for details on how to parse the header, or see my implementation for Java SE (if you look around, you'll also find a class that supports ASCII reading, if needed).
Could also be that there's no header, and that you can get the height/width from the XML. In that case, dataOffset will be 0 below.
When the header is parsed, you know width, height and where the image data starts:
int width, height; // from header
int dataOffset; // == end of header
// Create pixel array, and expand 8 bit gray to ARGB_8888
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int gray = decodedString[dataOffset + i] & 0xff;
pixels[i] = 0xff000000 | gray << 16 | gray << 8 | gray;
}
}
Bitmap pgm = Bitmap.createBitmap(metrics, pixels, width, height, BitmapConfig.Config. ARGB_8888);
Thanks for your answer!
You solved me the day!
I found a little error in your code, index i is never initialized or incremented.
I corrected your code and tested it, here is my code:
private static Bitmap getBitmapFromPgm(byte[] decodedString, int width, int height, int dataOffset){
// Create pixel array, and expand 8 bit gray to ARGB_8888
int[] pixels = new int[width * height];
int i = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int gray = decodedString[dataOffset + i] & 0xff;
pixels[i] = 0xff000000 | gray << 16 | gray << 8 | gray;
i++;
}
}
Bitmap pgm = Bitmap.createBitmap(pixels, width, height, android.graphics.Bitmap.Config.ARGB_8888);
return pgm;
}

Image Processing in Java

I want to extract the pixel values of the jpeg image using the JAVA language, and need to store it in array(bufferdArray) for further manipulation. So how i can extract the pixel values from jpeg image format?
Have a look at BufferedImage.getRGB().
Here is a stripped-down instructional example of how to pull apart an image to do a conditional check/modify on the pixels. Add error/exception handling as necessary.
public static BufferedImage exampleForSO(BufferedImage image) {
BufferedImage imageIn = image;
BufferedImage imageOut =
new BufferedImage(imageIn.getWidth(), imageIn.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
int width = imageIn.getWidth();
int height = imageIn.getHeight();
int[] imageInPixels = imageIn.getRGB(0, 0, width, height, null, 0, width);
int[] imageOutPixels = new int[imageInPixels.length];
for (int i = 0; i < imageInPixels.length; i++) {
int inR = (imageInPixels[i] & 0x00FF0000) >> 16;
int inG = (imageInPixels[i] & 0x0000FF00) >> 8;
int inB = (imageInPixels[i] & 0x000000FF) >> 0;
if ( conditionChecker_inRinGinB ){
// modify
} else {
// don't modify
}
}
imageOut.setRGB(0, 0, width, height, imageOutPixels, 0, width);
return imageOut;
}
The easiest way to get a JPEG into a java-readable object is the following:
BufferedImage image = ImageIO.read(new File("MyJPEG.jpg"));
BufferedImage provides methods for getting RGB values at exact pixel locations in the image (X-Y integer coordinates), so it'd be up to you to figure out how you want to store that in a single-dimensional array, but that's the gist of it.
There is a way of taking a buffered image and converting it into an integer array, where each integer in the array represents the rgb value of a pixel in the image.
int[] pixels = ((DataBufferInt)image.getRaster().grtDataBuffer()).getData();
The interesting thing is, when an element in the integer array is edited, the corresponding pixel in the image is as well.
In order to find a pixel in the array from a set of x and y coordinates, you would use this method.
public void setPixel(int x, int y ,int rgb){
pixels[y * image.getWidth() + x] = rgb;
}
Even with the multiplication and addition of coordinates, it is still faster than using the setRGB() method in the BufferedImage class.
EDIT:
Also keep in mind, the image needs type needs to be that of TYPE_INT_RGB, and isn't by default. It can be converted by creating a new image of the same dimensions, and of the type of TYPE_INT_RGB. Then using the graphics object of the new image to draw the original image to the new one.
public BufferedImage toIntRGB(BufferedImage image){
if(image.getType() == BufferedImage.TYPE_INT_RGB)
return image;
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight, BufferedImage.TYPE_INT_RGB);
newImage.getGraphics().drawImage(image, 0, 0, null);
return newImage;
}

Categories