Thanks for tips and advices. I will look for that fbimage library and test it.
For now i am enjoying this idea of making this sub. Testing first bytes of a
application. A black limited square painted in yellow. I know this will get more complicated from now on.
. Maybe we can make this sub/function for the next version of FB. LOL Just kiding i am not the boss of anything in here. :)
Code: Select all
'*******************************************************
'filename: pngtoimage.bas
'
'function: LoadPNG
'
'Description: loads a png image to an freebasic image
'
'Syntax: LoadPNG(file,imageid)
'
'parameters:
'
'> file: filepath to png image
'> imageid: id of image
'*******************************************************
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using FB '' Screen mode flags are in the FB namespace in lang FB
#endif
'************************************************
' HIDE CONSOLE
'************************************************
#If Defined(__FB_WIN32__)
declare function hideConsoleWindow alias "FreeConsole"() as long
hideConsoleWindow
#EndIf
declare sub loadPNG(file as string, image as any pointer)
windowtitle("LoadPNG")
screen 20,32 '1024x768
dim image as any pointer
dim file as string
dim as integer x, y
dim f as integer
f=freefile
x=400
y=400
image=imagecreate(32,32, rgba(255,255,255,255)) 'white background
file="test.png" 'make 32 x 32 png file called test.png
cls
loadPNG(file,image)
do
put (x,y), image 'position image in screen
sleep
loop until inkey=chr$(27)
imagedestroy image
end 'program
sub loadPNG(file as string,image as any pointer)
'type pngheader
' byte1 as ubyte '89 - ???
' byte2 as ubyte '50 - letter P
' byte3 as ubyte '4e - letter N
' byte4 as ubyte '47 - letter G
' byte5 as ubyte '0d - DOS Carrige Return
' byte6 as ubyte '0a - DOS Linefeed
' byte7 as ubyte '1a - DOS end of file
' byte8 as ubyte '0a - Unix LineFeed
'end type
dim chunklen as ubyte
dim chunkletter as ubyte
dim chunkwidth as ubyte
dim chunkheight as ubyte
dim chunkbitdepth as ubyte
dim chunkcolortype as ubyte
dim chunkcompressionmethod as ubyte
dim chunkfiltermethod as ubyte
dim chunkinterlacemethod as ubyte
'dim header as pngheader
dim ub as ubyte
dim passed as integer
If Open(file For Binary Access Read As #1) = 0 Then
print "open file with sucess"
passed=0
'**********************************
'read png header and validate it
'**********************************
get #1, ,ub
if hex(ub)="89" then passed=passed+1
get #1, ,ub
if hex(ub)="50" then passed=passed+1
get #1, ,ub
if hex(ub)="4E" then passed=passed+1
get #1, ,ub
if hex(ub)="47" then passed=passed+1
get #1, ,ub
if hex(ub)="D" then passed=passed+1
get #1, ,ub
if hex(ub)="A" then passed=passed+1
get #1, ,ub
if hex(ub)="1A" then passed=passed+1
get #1, ,ub
if hex(ub)="A" then passed=passed+1
if passed=8 then
print "it's a png file"
else
print "this is not a png file"
endif
print
get #1, ,chunklen
get #1, ,chunklen
get #1, ,chunklen
get #1, ,chunklen
print ("chunksize="+str(chunklen)+" bytes") '13 bytes
'read the 4 letters of first critical chunk named IHDR
get #1, ,chunkletter
print ("chunknameletter1="+chr$(chunkletter)+" 1 byte")
get #1, ,chunkletter
print ("chunknameletter2="+chr$(chunkletter)+" 1 byte")
get #1, ,chunkletter
print ("chunknameletter3="+chr$(chunkletter)+" 1 byte")
get #1, ,chunkletter
print ("chunknameletter4="+chr$(chunkletter)+" 1 byte")
'read 13 bytes of chunk info
print
print "read 13 bytes of chunk info"
print
'first chunk width
get #1, ,chunkwidth
get #1, ,chunkwidth
get #1, ,chunkwidth
get #1, ,chunkwidth
print ("chunkwidth="+str(chunkwidth)+" pixels (4 bytes)")
'first chunk height
get #1, ,chunkheight
get #1, ,chunkheight
get #1, ,chunkheight
get #1, ,chunkheight
print ("chunkheight="+str(chunkheight)+" pixels (4 bytes)")
get #1, ,chunkbitdepth
print ("chunkbitdepth="+str(chunkbitdepth)+" (1 byte)")
get #1, ,chunkcolortype
print ("chunkcolortype="+str(chunkcolortype)+" (1 byte)")
get #1, ,chunkcompressionmethod
print ("chunkcompressionmethod="+str(chunkcompressionmethod)+" (1 byte)")
get #1, ,chunkfiltermethod
print ("chunkfiltermethod="+str(chunkfiltermethod)+" (1 byte)")
get #1, ,chunkinterlacemethod
print ("chunkinterlacemethod="+str(chunkinterlacemethod)+" (1 byte)")
close #1
else
print "error opening file"
end if
end sub