Bläddra i källkod

BRL.StbImageLoader. Initial Import.

woollybah 8 år sedan
förälder
incheckning
1d38ae5ac1
2 ändrade filer med 153 tillägg och 0 borttagningar
  1. 28 0
      stbimageloader.mod/glue.c
  2. 125 0
      stbimageloader.mod/stbimageloader.bmx

+ 28 - 0
stbimageloader.mod/glue.c

@@ -0,0 +1,28 @@
+
+#include "pub.mod/stbimage.mod/stb/stb_image.h"
+
+#include "brl.mod/blitz.mod/blitz.h"
+
+#ifdef BMX_NG
+#define CB_PREF(func) func
+#else
+#define CB_PREF(func) _##func
+#endif
+
+
+int CB_PREF(brl_stbimageloader_TStbioCallbacks__Read)(BBObject * cb, char * data,int size);
+void CB_PREF(brl_stbimageloader_TStbioCallbacks__Skip)(BBObject * cb, int n);
+int CB_PREF(brl_stbimageloader_TStbioCallbacks__Eof)(BBObject * cb);
+
+
+
+stbi_uc * bmx_stbi_load_image(BBObject * cb, int * width, int * height, int * channels) {
+
+	stbi_io_callbacks callbacks;
+	callbacks.read = brl_stbimageloader_TStbioCallbacks__Read;
+	callbacks.skip = brl_stbimageloader_TStbioCallbacks__Skip;
+	callbacks.eof = brl_stbimageloader_TStbioCallbacks__Eof;
+
+	return stbi_load_from_callbacks(&callbacks, cb, width, height, channels, 0);
+
+}

+ 125 - 0
stbimageloader.mod/stbimageloader.bmx

@@ -0,0 +1,125 @@
+SuperStrict
+
+Rem
+bbdoc: Graphics/Stb Image loader
+about:
+The stb image loader module provides the ability to load different image format #pixmaps.
+Supported formats include, BMP, PSD, TGA, GIF, HDR, PIC and PNM
+End Rem
+Module BRL.StbImageLoader
+
+ModuleInfo "Version: 1.00"
+ModuleInfo "Author: Bruce A Henderson"
+ModuleInfo "License: zlib/libpng"
+ModuleInfo "Copyright: Bruce A Henderson"
+
+ModuleInfo "History: 1.00"
+ModuleInfo "History: Initial Release."
+
+Import Pub.StbImage
+Import BRL.Pixmap
+
+Import "glue.c"
+
+
+Extern
+
+	Function bmx_stbi_load_image:Byte Ptr(cb:Object, width:Int Ptr, height:Int Ptr, channels:Int Ptr)
+
+End Extern
+
+
+Type TPixmapLoaderSTB Extends TPixmapLoader
+
+	Method LoadPixmap:TPixmap( stream:TStream )
+	
+		Local pixmap:TPixmap
+	
+		Local cb:TStbioCallbacks = New TStbioCallbacks
+		cb.stream = stream
+		
+		Local width:Int, height:Int, channels:Int
+		
+		Local imgPtr:Byte Ptr = bmx_stbi_load_image(cb, Varptr width, Varptr height, Varptr channels)
+		
+		If imgPtr Then
+		
+			Local pf:Int
+		
+			Select channels
+				Case STBI_grey
+					pf = PF_I8
+				Case STBI_rgb
+					pf = PF_RGB888
+				Case STBI_rgb_alpha
+					pf = PF_RGBA8888
+				Case STBI_grey_alpha
+					pixmap = CreatePixmap( width,height,PF_RGBA8888 )
+					
+					Local src:Byte Ptr = imgPtr
+					Local dst:Byte Ptr = pixmap.pixels
+
+					For Local y:Int = 0 Until height
+						For Local x:Int = 0 Until width
+							Local a:Int=src[0]
+							Local i:Int=src[1]
+							dst[0] = i
+							dst[1] = i
+							dst[2] = i
+							dst[3] = a
+							src:+2
+							dst:+4
+						Next
+					Next
+			End Select
+			
+			
+			
+			If pf
+				pixmap = CreatePixmap( width, height, pf )
+
+				MemCopy(pixmap.pixels, imgPtr, width * height * BytesPerPixel[pf])
+			End If
+			
+			stbi_image_free(imgPtr)
+		
+		End If
+	
+		Return pixmap
+		
+	End Method
+
+End Type
+
+
+Type TStbioCallbacks
+
+	Field stream:TStream
+	
+	Method Read:Int(buffer:Byte Ptr, size:Int)
+		Return stream.Read(buffer, size)
+	End Method
+	
+	Method Skip(n:Int)
+		stream.Seek(SEEK_CUR_, n)
+	End Method
+	
+	Method Eof:Int()
+		Return stream.Eof()
+	End Method
+
+	Function _Read:Int(cb:TStbioCallbacks, buffer:Byte Ptr, size:Int) { nomangle }
+		Return cb.Read(buffer, size)
+	End Function
+
+	Function _Skip(cb:TStbioCallbacks, n:Int) { nomangle }
+		cb.Skip(n)
+	End Function
+
+	Function _Eof:Int(cb:TStbioCallbacks) { nomangle }
+		Return cb.Eof()
+	End Function
+	
+End Type
+
+New TPixmapLoaderSTB