Android Question Library "Native DirectBuffer / keylabDirectBuffer" in 2018?

W. Graf

Member
Licensed User
Longtime User
Hi!

This is a question to the lib "Native DirectBuffer" (see: https://www.b4x.com/android/forum/threads/native-directbuffer-library.30891 )

I'm drawing a Bitmap in an ImageView. Later I have to modify it (for example: Change all black Pixel to blue Pixel).

At first I was using Canvas.Bitmap.GetPixel and Canvas.DrawPoint. It works, but it wasn't very fast.
Then I found this lib. I used it and it speeds up this task by factor 289!! I couldn't believe it. Really a great lib! :)
@keylab83: Congratulations, you did a great job!!!

So I only have one question: is there any reason, not to use this lib in newer apps (because the lib is from 2013 and maybe there are any problems with newer Android versions)?

Thank you & kind regards!
Wolfgang
 

DonManfred

Expert
Licensed User
Longtime User
@keylab83: Congratulations, you did a great job!!!

So I only have one question: is there any reason, not to use this lib in newer apps (because the lib is from 2013 and maybe there are any problems with newer Android versions)?
keylab83 was last seen: Jun 24, 2015

You may expect not to get an answer from him/her.
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
So I only have one question: is there any reason, not to use this lib in newer apps (because the lib is from 2013 and maybe there are any problems with newer Android versions)?

yes there is a reason , the author is nowhere to be found
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Hi and good evening,

at first, I want to thank you all for your inputs and infos!
I confirm, that it is no good idea to use the lib, because the memory will not be deallocated (the amount of used RAM always is increasing...).

I tried Erel's advice to use the BitmapCreator. But I have no idea, how I can use it.

My task is:
I've an ImageView. I'm using Canvas to draw lines and circles and ... into the ImageView. This is working well.
Then I'm calling a method, which should change many pixels in this ImageView. In the following code, I'm setting the red-value of any pixel to 255 (it is only an example).

My not-working-sourcecode is:
B4X:
Dim bmpCreator As BitmapCreator
bmpCreator.Initialize(ImgMinimap.Width,ImgMinimap.Height)
bmpCreator.CopyPixelsFromBitmap(ImgMinimap.Bitmap)  '<- compile-error: incompatible types: Bitmap cannot be converted to B4XBitmapWrapper
 
Dim bmpArray (ImgMinimap.Width * ImgMinimap.Height * 4) As Byte
bmpArray = bmpCreator.Buffer
 
Dim x As Int
For x = 0 To ImgMinimap.Width * ImgMinimap.Height * 4 - 1 Step 4
   bmpArray(x) = 255
Next

bmpCreator.Buffer = bmpArray  '<- of course an error, because .Buffer is Readonly
 
ImgMinimap.Invalidate

My problem is:
There are two compile-errors. The first error tells me, that I have another object (B4XBitmapWrapper), but what is this and how can I use it?
The second error is, because the BitmapCreator.Buffer() is readonly. Is there a way, to put my modified array back to the creator to change the ImageView.

I don't want to let you work for me. But I was searching many threads, which couldn't help me. The DirectBuffer-lib was the only thing, which does exactly, what I'm looking for. But - like I wrote before - it doesn't clean the memory and it is not safe (like Erel wrote).

Do you have an idea for a solution of my problem?

Thank you again and have a nice evening. Best regards,
Wolfgang
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
If you want to perform the same operation on all pixels (set Red to 255, invert them, or add a fixed value to any of their components), the fastest approach is Android ColorMatrix.
You can learn how it works from THIS example and later modify it for your purposes, (I think that there are also libraries that give you direct access to it). Anyhow, you'll need to understand how the ColorMatrix works
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two compile-errors. The first error tells me, that I have another object (B4XBitmapWrapper), but what is this and how can I use it?
Add a reference to XUI library.

The second error is, because the BitmapCreator.Buffer() is readonly. Is there a way, to put my modified array back to the creator to change the ImageView.
You shouldn't use this property at all.

Start with this code:
B4X:
Dim xui As XUi
Dim bc As BitmapCreator
bc.Initialize(ImgMinimap.Width / xui.Scale ,ImgMinimap.Height / xui.Scale)
bc.CopyPixelsFromBitmap(ImgMinimap.Bitmap)
Dim FromColor,ToColor As ARGBColor
bc.ColorToARGB(xui.Color_Blue, ToColor)
For y = 0 To bc.mHeight - 1
   For x = 0 To bc.mWidth - 1
       bc.GetARGB(x, y, FromColor)
       If FromColor.r = 0 And FromColor.g = 0 And FromColor.b = 0 Then
           bc.SetARGB(x, y, ToColor)
       End If
   Next
Next
ImgMinimap.Bitmap = bc.Bitmap
 
Upvote 0
Top