123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- SuperStrict
- Rem
- bbdoc: Image/BMP loader
- about:
- The BMP loader module provides the ability to load BMP format #pixmaps.
- End Rem
- Module Image.BMP
- ModuleInfo "Version: 1.08"
- ModuleInfo "Author: Simon Armstrong"
- ModuleInfo "License: zlib/libpng"
- ModuleInfo "Copyright: Blitz Research Ltd"
- ModuleInfo "Modserver: BRL"
- ModuleInfo "History: 1.08"
- ModuleInfo "History: Moved to Image namespace"
- ModuleInfo "History: 1.07 Release"
- ModuleInfo "History: Added 32 bit alpha support"
- ModuleInfo "History: 1.06 Release"
- ModuleInfo "History: Fixed inverted 1 bit bitmaps"
- ModuleInfo "History: 1.05 Release"
- ModuleInfo "History: Fixed palettized bitmaps failing when biClrUsed=0"
- Import BRL.Pixmap
- Import BRL.EndianStream
- Type TPixmapLoaderBMP Extends TPixmapLoader
- Method LoadPixmap:TPixmap( stream:TStream ) Override
- stream=LittleEndianStream( stream )
-
- Local line:Int[],palette:Int[],pix:Byte[],buf:Byte[64]
- Local pixmap:TPixmap
- Local hsize:Int,hoffset:Int,pad:Int
- Local size:Int,width:Int,height:Int
- Local planes:Int,bits:Int,compression:Int,isize:Int,xpels:Int,ypels:Int,COLS:Int,inuse:Int
- Local w:Int,x:Int,y:Int,c0:Int,c1:Int,p:Int
- If stream.ReadBytes( buf,2 )=2
- If buf[0]=Asc("B") And buf[1]=Asc("M")
- hsize=ReadInt(stream)
- pad=ReadInt(stream)
- hoffset=ReadInt(stream)
- size=ReadInt(stream)
- width=ReadInt(stream)
- height=ReadInt(stream)
- planes=ReadShort(stream)
- bits=ReadShort(stream)
- compression=ReadInt(stream)
- isize=ReadInt(stream)
- xpels=ReadInt(stream)
- ypels=ReadInt(stream)
- COLS=ReadInt(stream)
- inuse=ReadInt(stream)
- hoffset:-54
- If Not COLS COLS=1 Shl bits
- If bits=32
- pixmap=TPixmap.Create( width,height,PF_BGRA8888 )
- Else
- pixmap=TPixmap.Create( width,height,PF_BGR888 )
- EndIf
- Select bits
- Case 1
- c0=ReadInt(stream)
- c1=ReadInt(stream)
- w=(width+7)/8
- w=(w+3)&$fffc
- pix=New Byte[w]
- For y=height-1 To 0 Step -1
- stream.ReadBytes(pix,w)
- For x=0 Until width
- If pix[x Shr 3]&(128 Shr (x&7))
- ConvertPixels(Varptr c1,PF_BGR888,pixmap.pixelptr(x,y),pixmap.format,1)
- Else
- ConvertPixels(Varptr c0,PF_BGR888,pixmap.pixelptr(x,y),pixmap.format,1)
- EndIf
- Next
- Next
- Case 4
- palette=New Int[16]
- line=New Int[width]
- stream.ReadBytes(palette,COLS*4)
- w=(width+1)/2
- w=(w+3)&$fffc
- pix=New Byte[w]
- For y=height-1 To 0 Step -1
- stream.ReadBytes(pix,w)
- For x=0 Until width
- p=(pix[x Shr 1]Shr((1-x&1)*4))&15
- line[x]=palette[p]
- Next
- ConvertPixels(line,PF_BGRA8888,pixmap.pixelptr(0,y),pixmap.format,width)
- Next
- Case 8
- palette=New Int[256]
- line=New Int[width]
- stream.ReadBytes(palette,COLS*4)
- w=(width+3)&$fffc
- pix=New Byte[w]
- For y=height-1 To 0 Step -1
- stream.ReadBytes(pix,w)
- For x=0 Until width
- line[x]=palette[pix[x]&255]
- Next
- ConvertPixels(line,PF_BGRA8888,pixmap.pixelptr(0,y),pixmap.format,width)
- Next
- Case 24
- w=width*3
- w=(w+3)&$fffc
- pix=New Byte[w]
- For y=height-1 To 0 Step -1
- stream.ReadBytes(pix,w)
- ConvertPixels(pix,PF_BGR888,pixmap.pixelptr(0,y),pixmap.format,width)
- Next
- Case 32
- w=width*4
- pix=New Byte[w]
- For y=height-1 To 0 Step -1
- stream.ReadBytes(pix,w)
- ConvertPixels(pix,PF_BGRA8888,pixmap.pixelptr(0,y),pixmap.format,width)
- Next
- Default
- pixmap=Null
- End Select
- Return pixmap
- EndIf
- EndIf
- End Method
- End Type
- New TPixmapLoaderBMP
|