Dear Cerberus X User!
As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.
Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.
Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!
Accepting ensures:
- Continued access to your account with a short break for the actual transfer.
- Retention of your data under the same terms.
Without consent:
- You don't have further access to your forum user account.
- Your account and personal data will be deleted after April 5, 2024.
- Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.
We hope to keep you in our community and see you on the forum soon!
All the best
Your Cerberus X Team
Class BmpFile
Field Width:Int ' bmp image width
Field Height:Int ' bmp image height
Field Pixels:Int[1] ' array for screen pixels
Field FileSize:Int ' bmp file size
Field Buffer:DataBuffer ' bmp file data (header + pixels)
Field PadLineWidth:Int ' bmp real line size
' default header for 24bpp image
Field Header:Int[] = [ $42,$4D,$D6,$83,$00,$00,$00,$00,$00,$00,$36,$00,$00,$00,$28,$00,$00,$00,$6C,$00,$00,$00,$68,$00,$00,
$00,$01,$00,$18,$00,$00,$00,$00,$00,$00,$00,$00,$00,$C3,$0E,$00,$00,$C3,$0E,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00 ]
Const FILE_SIZE := $2
Const IMAGE_WIDTH := $12
Const IMAGE_HEIGHT := $16
Const RAW_DATA_SIZE := $22
Method Grab:Void( x:Int, y:Int, w:Int, h:Int ) ' Call within OnRender after drawing the image to grab
Width = w
Height = h
PadLineWidth = (((Width*3) + 3) / 4) * 4
Pixels = Pixels.Resize( Width * Height )
ReadPixels Pixels, x, y, Width, Height
FileSize = Header.Length() + PadLineWidth * Height
Buffer = New DataBuffer( FileSize )
local ptr:Int
for ptr = 0 until Header.Length
Buffer.PokeByte ptr, Header[ptr]
next
Buffer.PokeInt FILE_SIZE, FileSize
Buffer.PokeInt IMAGE_WIDTH, Width
Buffer.PokeInt IMAGE_HEIGHT, Height
Buffer.PokeInt RAW_DATA_SIZE, PadLineWidth * Height
local i:Int = 0
local pix:Int
for local ys:Int = 0 until Height
ptr = (Height - ys - 1) * PadLineWidth + Header.Length
for local xs:Int = 0 until Width
pix = Pixels[i]
Buffer.PokeByte ptr+0, pix & $ff
Buffer.PokeByte ptr+1, (pix shr 8) & $ff
Buffer.PokeByte ptr+2, (pix shr 16) & $ff
ptr += 3
i += 1
next
next
End
Method Save:Void( filename:String ) ' filename without .bmp
Local file:=FileStream.Open( "monkey://internal/"+filename+".bmp","w" )
If file
file.Write Buffer, 0, FileSize
file.Close
Endif
End
End
Pixels = Pixels.Resize( Width * Height )
ReadPixels Pixels, x, y, Width, Height
for local ys:Int = 0 until Height
ptr = (Height - ys - 1) * PadLineWidth + Header.Length
for local xs:Int = 0 until Width
pix = Pixels[i]
Buffer.PokeByte ptr+0, pix & $ff
Buffer.PokeByte ptr + 1, (pix shr 8) & $ff
Buffer.PokeByte ptr+2, (pix shr 16) & $ff
ptr += 3
i += 1
next
next
If file
file.Write Buffer, 0, FileSize
file.Close
EndIf
For Local ptr:Int = Header.Length To Header.Length + 1000
Print Buffer.PeekByte(ptr)
Next
Method WritePixels : Void ( pixels:Int[], x:Int, y:Int, width:Int, height:Int, arrayOffset:Int=0, arrayPitch:Int=0, frame:Int=0 )
Copies a rectangular section of pixels from an int array to the image.
The pixel data must be stored in int-per-pixel ARGB format, with the alpha component stored in bits 24-31, the red component in bits 16-23, the green component in bits 8-15 and the blue component in bits 0-7.
Function LoadBmp:Image(path:String)
Local file:= FileStream.Open(path, "r")
Local bmpimage:BmpFile
Local image:Image
bmpimage = New BmpFile
bmpimage.Buffer = New DataBuffer(file.Length)
If file
file.ReadAll(bmpimage.Buffer, 0, file.Length)
file.Close
bmpimage.Width = bmpimage.Buffer.PeekInt($12)
bmpimage.Height = bmpimage.Buffer.PeekInt($16)
bmpimage.Pixels = bmpimage.Pixels.Resize(bmpimage.Width * bmpimage.Height)
Local ptr:Int = 0
Local scanpixelsarray:Int = (bmpimage.Height - 1) * bmpimage.Width 'starting to write into the Pixels array where the data for the last line of the image begins
For Local i:Int = bmpimage.Height - 1 To 0 Step - 1 ' pixel info in the BMP file is coded upside down for some reason
For Local j:Int = 0 To bmpimage.Width - 1
Local b:Int = bmpimage.Buffer.PeekInt(ptr + 0) ' blue component
Local g:Int = bmpimage.Buffer.PeekInt(ptr + 1) ' green component
Local r:Int = bmpimage.Buffer.PeekInt(ptr + 2) ' red component
bmpimage.Pixels[scanpixelsarray + j] = (255 Shl 24) | (r Shl 16) | (g Shl 8) | b ' Data in the Pixels array must be stored in int-per-pixel ARGB format, with the alpha component stored in bits 24-31, the red component in bits 16-23, the green component in bits 8-15 and the blue component in bits 0-7
ptr += 3
If j = bmpimage.Width - 1 Then scanpixelsarray = (i - 1) * bmpimage.Width
Next
Next
image = CreateImage(bmpimage.Width, bmpimage.Height)
image.WritePixels(bmpimage.Pixels, 0, 0, bmpimage.Width, bmpimage.Height, 0, 0)
EndIf
Return image
End Function
Function LoadBMP:Image(path:String)
Local db:DataBuffer = DataBuffer.Load(path)
If db
'Print("length="+db.Length())
'Print("type="+db.PeekString(0,2))
'Print("size="+db.PeekInt(2))
Local offset:Int=db.PeekInt(10)
'Print("offet="+offset)
'Print("header size="+db.PeekInt(14))
Local width:Int=db.PeekInt(18)
'Print("image width="+width)
Local height:Int=db.PeekInt(22)
Local img:Image = CreateImage( width, height, 1 )
'Print("image height="+height)
'Print("image planes="+db.PeekShort(26))
Local bitcount:Int= +db.PeekShort(28)
'Print("image bitcount="+bitcount)
'Print("image compression="+db.PeekInt(30))
Local imagesize:Int=db.PeekInt(34)
'Print("image Size="+imagesize)
Local pixels:Int[width*height]
For Local y:Int= 0 To (height-1)
For Local x:Int=0 To (width-1)
Local index:Int=y*width+x
Local i:Int = offset+index*(bitcount/8)
Local alpha:Int = 255
Local red:Int = db.PeekByte(i)+256
Local green:Int = db.PeekByte(i+1)+256
Local blue:Int = db.PeekByte(i+2)+256
Local rgba:Int = alpha Shl 24 | blue Shl 16 | green Shl 8 | red
pixels[width*height-index-1]=rgba
Next
Next
If img
img.WritePixels(pixels,0,0,width,height)
Return img
Endif
Else
Print ("Error loading file "+path)
Endif
Return Null
End
Strict
'Simple mojo(1) script
'It should behave the same on all targets
#IMAGE_FILES+="*.bmp"
Import mojo
Import brl.databuffer
'-----------------------------------------------------------------
Class myClass Extends App
Field angle:Float = 0.0
Field img:Image
'-----------------------------------------------------------------
Method OnCreate:Int()
SetUpdateRate 60
img = LoadBMP("cerberus://data/gold2.bmp")
Return 0
End
'-----------------------------------------------------------------
Method OnUpdate:Int()
' update your content here
angle += 0.5
If angle > 360.0 Then angle -= 360.0
Return 0
End
'-----------------------------------------------------------------
Method OnRender:Int()
' here you render your app
Cls 0,0,255
DrawImage(img,10,10)
Return 0
End
End
Function LoadBMP:Image(path:String)
Local db:DataBuffer = DataBuffer.Load(path)
If db
'Print("length="+db.Length())
'Print("type="+db.PeekString(0,2))
'Print("size="+db.PeekInt(2))
Local offset:Int=db.PeekInt(10)
'Print("offet="+offset)
'Print("header size="+db.PeekInt(14))
Local width:Int=db.PeekInt(18)
'Print("image width="+width)
Local height:Int=db.PeekInt(22)
Local img:Image = CreateImage( width, height, 1 )
'Print("image height="+height)
'Print("image planes="+db.PeekShort(26))
Local bitcount:Int= +db.PeekShort(28)
'Print("image bitcount="+bitcount)
'Print("image compression="+db.PeekInt(30))
Local imagesize:Int=db.PeekInt(34)
'Print("image Size="+imagesize)
Local pixels:Int[width*height]
For Local y:Int= 0 To (height-1)
For Local x:Int=0 To (width-1)
Local index:Int=y*width+x
Local i:Int = offset+index*(bitcount/8)
Local alpha:Int = 255
Local red:Int = db.PeekByte(i)+256
Local green:Int = db.PeekByte(i+1)+256
Local blue:Int = db.PeekByte(i+2)+256
Local rgba:Int = alpha Shl 24 | blue Shl 16 | green Shl 8 | red
pixels[width*height-index-1]=rgba
Next
Next
If img
img.WritePixels(pixels,0,0,width,height)
Return img
Endif
Else
Print ("Error loading file "+path)
Endif
Return Null
End
'-----------------------------------------------------------------
Function Main:Int()
New myClass
Return 0
End
Function LoadBmp:Image(path:String)
Local Width:Int ' bmp image width
Local Height:Int ' bmp image height
Local Pixels:Int[1] ' array for screen pixels
Local Buffer:DataBuffer ' bmp file data (header + pixels)
Local file:= FileStream.Open(path, "r")
Local image:Image
Buffer = New DataBuffer(file.Length)
If file
file.ReadAll(Buffer, 0, file.Length)
file.Close
Width = Buffer.PeekInt($12)
Height = Buffer.PeekInt($16)
Pixels = Pixels.Resize(Width * Height)
Local PadLineWidth:Int = ( ( (Width * 3) + 3) / 4) * 4
Local OriginalLineWidth:Int = Width * 3
Local Padding:Int = PadLineWidth - OriginalLineWidth
Local ptr:Int = 0
Local scanpixelsarray:Int = (Height - 1) * Width 'starting to write into the Pixels array where the data for the last line of the image begins
For Local i:Int = Height - 1 To 0 Step - 1 ' pixel info in the BMP file is coded upside down for some reason
For Local j:Int = 0 To Width - 1
Local b:Int = Buffer.PeekInt(ptr + 0) ' blue component
Local g:Int = Buffer.PeekInt(ptr + 1) ' green component
Local r:Int = Buffer.PeekInt(ptr + 2) ' red component
Pixels[scanpixelsarray + j] = (255 Shl 24) | (r Shl 16) | (g Shl 8) | b ' Data in the Pixels array must be stored in int-per-pixel ARGB format, with the alpha component stored in bits 24-31, the red component in bits 16-23, the green component in bits 8-15 and the blue component in bits 0-7
ptr += 3
If j = Width - 1
scanpixelsarray = (i - 1) * Width
If Padding<> 0 Then ptr += Padding
EndIf
Next
Next
image = CreateImage(Width, Height)
image.WritePixels(Pixels, 0, 0, Width, Height, 0, 0)
EndIf
Return image
End Function