Browse Source

Moved BMP loader to Image namespace.

Brucey 3 years ago
parent
commit
9751575e7f
4 changed files with 187 additions and 0 deletions
  1. 134 0
      bmp.mod/bmp.bmx
  2. BIN
      bmp.mod/examples/blackbuck.bmp
  3. BIN
      bmp.mod/examples/bmp_24.bmp
  4. 53 0
      bmp.mod/examples/example_01.bmx

+ 134 - 0
bmp.mod/bmp.bmx

@@ -0,0 +1,134 @@
+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

BIN
bmp.mod/examples/blackbuck.bmp


BIN
bmp.mod/examples/bmp_24.bmp


+ 53 - 0
bmp.mod/examples/example_01.bmx

@@ -0,0 +1,53 @@
+SuperStrict
+
+Framework SDL.SDLRenderMax2D
+Import Image.BMP
+
+Local w:Int = DesktopWidth() * .75
+Local h:Int = DeskTopHeight() * .75
+
+Graphics w, h, 0
+
+AutoMidHandle(True)
+
+Local img1:TImage = Loader("blackbuck.bmp", w, h)
+Local img2:TImage = Loader("bmp_24.bmp", w, h)
+
+If Not img1 Or Not img2 Then
+	Print "Failed to load image"
+	End
+End If
+
+Local image:Int
+Local img:TImage = img1
+
+While Not KeyDown(Key_ESCAPE)
+
+	Cls
+
+	If KeyHit(KEY_SPACE) Then
+		image = Not image
+		If image Then
+			img = img2
+		Else
+			img = img1
+		End If
+	End If
+
+	DrawImage img, w / 2, h / 2
+
+	Flip
+
+Wend
+
+Function Loader:TImage(path:String, maxWidth:Int, maxHeight:Int)
+	Local pix:TPixmap = LoadPixmap( path )
+	If pix Then
+		If pix.width > maxWidth Or pix.height > maxHeight Then
+			Local ratio:Float = Min(maxWidth / Float(pix.width), maxHeight / Float(pix.height))
+			pix = ResizePixmap(pix, Int(pix.width * ratio), Int(pix.height * ratio))
+		End If
+		Return LoadImage(pix)
+	End If
+End Function
+