DevIl image saver.

Community Forums/Showcase/DevIl image saver.

AntonyWells(Posted 2006) [#1]
This wraps devil and provides a function that convetrs and saves an image into either png/bmp/tga/gif etc for you. Not all of devil is wrapped, only enough to create and save images. You need the devil dll in the same directory as the source.


Strict
Global cir:Int[] = New Int[20]
Local lib =loadlibrarya("DevIl.dll")
Global ilInit() "win32"
Global ilGenImages(size:Int,images:Byte Ptr) "win32"
Global ilBindImage(image:Int) "win32"
Global ilTexImage(width:Int,height:Int,depth:Int,bpp:Int,format:Int,Typ:Int,data:Byte Ptr) "win32"
Global ilSaveImage(file:Byte Ptr) "win32"
Global ilSave(typ:Int,file:Byte Ptr) "win32"

' IL_COLOUR_INDEX							0x1900
'#define IL_COLOR_INDEX							0x1900
Const IL_RGB = $1907
'#define IL_RGBA								0x1908
'#define IL_BGR								0x80E0
Const IL_BGRA = $80E1
'#define IL_LUMINANCE							0x1909
'#define IL_LUMINANCE_ALPHA						0x190A
Const IL_BMP =$0420
Const IL_JPG=$0425
Const IL_PNG=$042A
Const IL_TGA=$042D
Const IL_GIF=$0436
Const IL_UNSIGNED_BYTE =$1401

If lib
	Print "opened dll"
	ilInit = getProcAddress(lib,"ilInit")
	ilGenImages = getProcAddress(lib,"ilGenImages")
	ilBindImage = getProcAddress(lib,"ilBindImage")
	ilTexImage = getprocAddress(lib,"ilTexImage")
	ilSaveImage = getprocaddress(lib,"ilSaveImage")
	ilSave = getprocaddress(lib,"ilSave")
	If ilInit = Null Or ilGenImages=Null Or ilTexImage=Null Or ilSaveImage = Null
		Print "No @#!*."
	EndIf
Else
	Print "Failed to open dll"
EndIf




ilInit()


Function ilSaveFile( img:Timage,file:String ,format:Int = il_bmp)
	
	Select format
		Case il_bmp
			file:+".bmp"
		Case il_jpg
			file:+".jpg"
		Case il_png
			file:+".png"
		Case il_gif
			file:+".gif"
		Case il_Tga
			file:+".tga"
	End Select
	
	
	If FileType(file)=1
		DeleteFile file
	EndIf
	
	Local ilImg = ilCreateImage()
	ilBindImage(ilimg)
	ilTexImg( img )
	ilSave( format,file.tocstring() )
	
End Function

Function ilTexImg( img:Timage )
	
	Local width,height
	
	width = ImageWidth(img)
	height =ImageHeight(img)
	
	Local rgb:Byte Ptr = MemAlloc(width*height*4)
	Local pix:Tpixmap = LockImage(img,0,True,False)
	For Local y=0 Until height
	For Local x=0 Until width
		Local boff = y*(width*4)+(x*4)
		Local b:Byte Ptr = pix.pixelptr(x,y)
		rgb[boff] = b[0]
		rgb[boff+1]=b[1]
		rgb[boff+2]=b[2]
		rgb[boff+3]=b[3]
	Next
	Next
	UnlockImage( img )
	ilteximage(width,height,1,4,il_bgra,il_unsigned_byte,rgb)
	MemFree rgb
End Function

Function ilCreateImage()
	Local b:Int[20]
	Local d:Byte Ptr = Byte Ptr(b)
	ilGenImages(1,d)
	Return b[0]
End Function




AntonyWells(Posted 2006) [#2]
Example,

IlInit()
local test:Timage = createimage(128,128)
ilsaveFile( test,"Test",il_jpg)



Damien Sturdy(Posted 2006) [#3]
You're alive then??? I thought the Garry crew got you!

nice work.


John Blackledge(Posted 2006) [#4]
Where is this devil dll?


AntonyWells(Posted 2006) [#5]
Cyngus, the garry who what when? :p

John, do a google search of DEvil. It's in amoungst a bunch of satanic ritual links:()


Damien Sturdy(Posted 2006) [#6]
Don't pretend you don't know them :P