impersonalis |
15.02.2008 16:04 |
ImageFromBank
Код:
Function ImageFromBank%(BANK%)
;impersonalis
;функция загрузки картинки (24BPP без сжатия)
;-----------------------------------------------
;неоптимизированный прототип (http://forum.boolean.name/showthread.php?t=1196
;http://www.freecoder.chat-blitz.de/viewtopic.php?t=290)
;------------------------------------------------------
Local ReadIndex%
Local bfType%
Local bfSize%
Local bfReserved%
Local bfOffBits%
Local biSize%
Local biWidth%
Local biHeight%
Local biPlanes%
Local biBitCount%
Local biCompression%
Local biSizeImage%
Local biXPelsPerMeter%
Local biYPelsPerMeter%
Local biClrUsed%
Local biClrImportant%
Local biMaskRed%
Local biMaskGreen%
Local biMaskBlue%
Local biDown%
Local ClrTable%[256]
Local tmp
Local tmp2
Local pixel
Local rgb
Local BetweenStringSpace%
Local img
;----------------------- Bitmap Header
ReadIndex=0
bfType%=PeekShort(BANK,ReadIndex)
ReadIndex=ReadIndex+2
bfSize%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
bfReserved%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
bfOffBits%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
;-----------------------
If bfType% <> 19778 Then RuntimeError("Error: File ist not a valid Windows Bitmap(.bmp)")
;----------------------- Bitmap InfoHeader
biSize%= PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biWidth%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biHeight%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biPlanes%=PeekShort(BANK,ReadIndex)
ReadIndex=ReadIndex+2
biBitCount%=PeekShort(BANK,ReadIndex)
ReadIndex=ReadIndex+2
biCompression%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biSizeImage%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biXPelsPerMeter%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biYPelsPerMeter%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biClrUsed%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biClrImportant%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
If biHeight%>0 Then biDown%=0
If biHeight%<0 Then biDown%=1
;---------------------- evt. Farbmasken
If biCompression%=3 Then
biMaskBlue%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biMaskGreen%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
biMaskBlue%=PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
EndIf
;---------------------- FarbTabellen
If biClrUsed%=0 Then
If biBitCount%=1 Or biBitCount%=4 Or biBitCount%=8 Then
For tmp=0 To 2^biBitCount%
ClrTable[tmp] =PeekInt(BANK,ReadIndex)
ReadIndex=ReadIndex+4
Next
EndIf
EndIf
;---------------------- BildDaten
BetweenStringSpace=((BankSize(BANK)-ReadIndex)-biWidth*biHeight*3)/Float(biHeight)
img=CreateImage( biWidth%,biHeight% )
;DebugLog biWidth%+" x "+biHeight%+" BPP="+biBitCount
;DebugLog BetweenStringSpace
SetBuffer ImageBuffer(img)
If biCompression%=0 Then ; BI-RGB
If biBitCount% = 1 Then
RuntimeError "Sorry, non-supported value of BPP ("+biBitCount+")"
ElseIf biBitCount% = 4
RuntimeError "Sorry, non-supported value of BPP ("+biBitCount+")"
ElseIf biBitCount% = 8
RuntimeError "Sorry, non-supported value of BPP ("+biBitCount+")"
ElseIf biBitCount% = 16
RuntimeError "Sorry, non-supported value of BPP ("+biBitCount+")"
ElseIf biBitCount% = 24 Then
If biDown% = 0 Then
LockBuffer ImageBuffer(img)
For tmp2=biHeight%-1 To 0 Step -1
For tmp=0 To biWidth%-1
blue=PeekByte(BANK,ReadIndex)
ReadIndex=ReadIndex+1
green=PeekByte(BANK,ReadIndex)
ReadIndex=ReadIndex+1
red=PeekByte(BANK,ReadIndex)
ReadIndex=ReadIndex+1
rgb=255*$1000000 + red*$10000 + green*$100 + blue
WritePixelFast tmp,tmp2,rgb
Next
ReadIndex=ReadIndex+BetweenStringSpace
Next
UnlockBuffer ImageBuffer(img)
ElseIf biDown%=1 Then
LockBuffer ImageBuffer(img)
For tmp2=0 To biHeight%-1
For tmp=0 To biWidth%-1
blue=PeekByte(BANK,ReadIndex)
ReadIndex=ReadIndex+1
green=PeekByte(BANK,ReadIndex)
ReadIndex=ReadIndex+1
red=PeekByte(BANK,ReadIndex)
ReadIndex=ReadIndex+1
rgb=255*$1000000 + red*$10000 + green*$100 + blue
WritePixelFast tmp,tmp2,rgb
Next
ReadIndex=ReadIndex+BetweenStringSpace
Next
UnlockBuffer ImageBuffer(img)
EndIf
ElseIf BiBitCount% = 32
RuntimeError "Sorry, non-supported value of BPP ("+biBitCount+")"
EndIf
ElseIf biCompression%=1 ; BI-RLE8
RuntimeError "Sorry, non-supported value of Compression (BI-RLE8)"
ElseIf biCompression%=2 ; BI-RLE4
RuntimeError "Sorry, non-supported value of Compression (BI-RLE4)"
ElseIf biCompression%=3 ; BI-BITFIELD
RuntimeError "Sorry, non-supported value of Compression (BI-BITFIELD)"
EndIf
SetBuffer BackBuffer()
Return img
End Function
;--- demo code
Graphics 800,600,32
name$="Imper.bmp"
size=FileSize(name)
bank=CreateBank(size)
file=OpenFile(name)
ReadBytes(bank,file,0,size-1)
CloseFile file
image=ImageFromBank(bank)
DrawImage image,0,0
Flip
|