B4J Question SetPixel

MarkusR

Well-Known Member
Licensed User
Longtime User
hello,
i convert one app into B4J and i fail at one row to compleate it.

i need read pixel from one image and set into a new image.
i made a special texture tool at PC that transform a photo selection to a texture.

Pixel=ImA.GetPixel(pc.x,pc.y)
'ImB.SetPixel(x,y)=Pixel 'Arrrgg

why is this easy opposite function missing???
and how to set a pixel?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
ohh,
u mean with the draw commands at canvas? but there i saw it have only a color enum (paint).
me just have the idea to stream a bitmap file simulation into image with Initialize2.
so the result are a image and then in can save it as png. i think this woule work for me. :)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
ok, i will try this first :)
then i have a byte array to fill out.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
here my solution with generating a bitmap byte array and the image read it :)
B4X:
Sub Render(ImA As Image,ImB As Image,Width As Float,Height As Float)
   
    Dim x As Float
    Dim y As Float
    Dim a As Float
    Dim b As Float
   
    Dim pa As Point
    Dim pb As Point
    Dim pc As Point
    Dim Pixel As Int

    Dim BMPSize As Int
    BMPSize=(14+40)+(Width*3)*Height
   
    'Header ist 14 Bytes + 40 Bytes Beschreibung
    '+ Breite*3*Höhe
    '14+40+256x3x256 = 196662 Bytes
    Dim In As InputStream
   
    Dim Buffer(BMPSize) As Byte
   
    Dim Value As Int

    'https://de.wikipedia.org/wiki/Windows_Bitmap#Dateikopf
    '--------------------------- BITMAPFILEHEADER        
    'bfType BM
    Value=19778
    Buffer(0)=Byte1(Value) '66
    Buffer(1)=Byte2(Value) '77

    'bfSize
    Buffer(2)=Byte1(BMPSize)
    Buffer(3)=Byte2(BMPSize)
    Buffer(4)=Byte3(BMPSize)
    Buffer(5)=Byte4(BMPSize)
   
    'Reserved
    Buffer(6)=0
    Buffer(7)=0
    Buffer(8)=0
    Buffer(9)=0
   
    'bfOffBits 54
    Value=14+40
    Buffer(10)=Byte1(Value) '54
    Buffer(11)=Byte2(Value)
    Buffer(12)=Byte3(Value)
    Buffer(13)=Byte4(Value)

    '--------------------------- BITMAPINFOHEADER
    'biSize
    Value=40
    Buffer(14)=Byte1(Value) '40
    Buffer(15)=Byte2(Value)
    Buffer(16)=Byte3(Value)
    Buffer(17)=Byte4(Value)

    'biWidth
    Value=Width
    Buffer(18)=Byte1(Value)
    Buffer(19)=Byte2(Value)
    Buffer(20)=Byte3(Value)
    Buffer(21)=Byte4(Value)

    'biHeight
    Value=Height
    Buffer(22)=Byte1(Value)
    Buffer(23)=Byte2(Value)
    Buffer(24)=Byte3(Value)
    Buffer(25)=Byte4(Value)
   
    'biPlanes
    Buffer(26)=1
    Buffer(27)=0

    'biBitCount
    Buffer(28)=24
    Buffer(29)=0

    'biCompression  BI_RGB
    Buffer(30)=0
    Buffer(31)=0
    Buffer(32)=0
    Buffer(33)=0

    'biSizeImage
    Value=(Width*3)*Height
    Buffer(34)=Byte1(Value)
    Buffer(35)=Byte2(Value)
    Buffer(36)=Byte3(Value)
    Buffer(37)=Byte4(Value)
   
    'biXPelsPerMeter
    Buffer(38)=0
    Buffer(39)=0
    Buffer(40)=0
    Buffer(41)=0
   
    'biYPelsPerMeter
    Buffer(42)=0
    Buffer(43)=0
    Buffer(44)=0
    Buffer(45)=0

    'biClrUsed no color table   
    Buffer(46)=0
    Buffer(47)=0
    Buffer(48)=0
    Buffer(49)=0

    'biClrImportant
    Buffer(50)=0
    Buffer(51)=0
    Buffer(52)=0
    Buffer(53)=0
   
    Dim Offset As Int

    If 1=1 Then 'On/Off
               
    For x=0.0 To Width-1.0
    a=x / (Width-1.0)
    pa=Linear(Point(1),Point(2),a)
    pb=Linear(Point(4),Point(3),a)

    For y=0 To Height-1.0
        b=y / (Height-1.0)
        b=1.0-b 'auf dem Kopf (upside down because bitmap)
        pc=Linear(pa,pb,b)
               
        Pixel=ImA.GetPixel(pc.x,pc.y)       
        'ImB.SetPixel(x,y)=Pixel 'Arrrgg

        Try
        Offset = (14+40) + x*3 + (Width*3*y)
        Buffer(Offset+0)=Byte1(Pixel)
        Buffer(Offset+1)=Byte2(Pixel)
        Buffer(Offset+2)=Byte3(Pixel)
        Catch
            Log(Offset)
        End Try
       
    Next
    Next
   
    End If

    'Byte Array to Input Stream
    In.InitializeFromBytesArray( Buffer,0,BMPSize)
    Log(In.BytesAvailable)
       
    'now can the image read from the input stream
    'ImB.InitializeSample2(In,Width,Height)
    ImB.Initialize2(In)
   
    'here maybe a extra preview window
    'Canvas1.DrawImage(ImB,0,0,ImB.Width,ImB.Height)
   
    In.Close 'close Input Stream
       
End Sub

Sub Linear(p1 As Point, p2 As Point, mu As Float) As Point

    Dim pn As Point

    pn.x = LinearInterpolate(p1.x,p2.x,mu)
    pn.y = LinearInterpolate(p1.y,p2.y,mu)

    Return pn

End Sub

Sub LinearInterpolate( y1 As Float, y2 As Float, mu As Float) As Float

    Dim f As Float
    f=(y1*(1.0-mu)+y2*mu)

    Return f
   
End Sub

Sub Byte1(Value As Int) As Byte

    Return Bit.And(Value, 0xff)

End Sub

Sub Byte2(Value As Int) As Byte

    Return Bit.UnsignedShiftRight(Bit.And(Value, 0xff00), 8)

End Sub

Sub Byte3(Value As Int) As Byte

    Return Bit.UnsignedShiftRight(Bit.And(Value, 0xff0000), 16)

End Sub

Sub Byte4(Value As Int) As Byte

    Return Bit.UnsignedShiftRight(Bit.And(Value, 0xff000000), 24)

End Sub
 
Upvote 0
Top