B4J Question Replace pixels

jroriz

Active Member
Licensed User
Longtime User
Hi.

In a png file, I need to replace all pixels that are not white to yellow, then all pixels that are white to black, producing a black and yellow image.

I am having difficulty using BitmapCreator. The example covers many aspects.

Can anyone help?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
example
ARGBColor means Alpha,Red,Green,Blue (normal 32 bit in 4 bytes each from 0 to 255)
B4X:
Sub Process_Globals
   Private xui As XUI
   Private ImageView1 As ImageView
   ...

Sub Test
   
    Dim Bitmap1 As B4XBitmap = xui.LoadBitmap(File.DirAssets,"test.jpg")
   
    Dim bc As BitmapCreator
    bc.Initialize(Bitmap1.Width,Bitmap1.Height)
    bc.CopyPixelsFromBitmap(Bitmap1)
   
    Dim x As Int
    Dim y As Int
    Dim pixel As ARGBColor
    For x = 0 To bc.mWidth-1
    For y = 0 To bc.mHeight-1
        bc.GetARGB(x,y,pixel)
            If (pixel.r > 200 And pixel.g > 200 And pixel.b > 200 ) Then
                pixel.r = 255 - pixel.r
                pixel.g = 255 - pixel.g
                pixel.b = 255 - pixel.b
            End If
        bc.SetARGB(x,y,pixel)
    Next
    Next
   
    ImageView1.SetImage(bc.Bitmap) '< a ImageView in layout
   
    Bitmap1 = bc.Bitmap
    Dim Out As OutputStream
    Log(File.DirTemp)
    Out = File.OpenOutput(File.DirTemp, "Output.png", False)
    Bitmap1.WriteToStream(Out, 100, "PNG")
    Out.Close
   
End Sub
 
Upvote 0
Top