Function LoadBrushPixmap:TBrush(pixmapin:TPixmap, flags:Int = 1, u_scale:Float = 1.0, v_scale:Float = 1.0)

	Return PixTBrush.LoadBrushPixmap(pixmapin, flags, u_scale, v_scale)
	
End Function

Function LoadTexturePixmap:TTexture(pixmapin:TPixmap, flags:Int = 1)

	Return PixTTexture.LoadTexturePixmap(pixmapin, flags)
	
End Function

Type PixTBrush Extends TBrush

	Function LoadBrushPixmap:TBrush(pixmapin:TPixmap, flags:Int = 1, u_scale:Float = 1.0, v_scale:Float = 1.0)
	
		Local brush:TBrush = New TBrush
		
		brush.tex[0] = PixTTexture.LoadTexturePixmap:TTexture(pixmapin, flags)
		brush.no_texs = 1
		
		'brush.tex[0].u_scale#=u_scale#
		'brush.tex[0].v_scale#=v_scale#
				
		Return brush
		
	End Function
	
End Type

Type PixTTexture Extends TTexture

	Function LoadTexturePixMap:TTexture(pixmapin:TPixmap, flags:Int = 1, tex:TTexture = Null)
	
		Return LoadAnimTexturePixMap:TTexture(pixmapin, flags, 0, 0, 0, 1, tex)
		
	End Function

	Function LoadAnimTexturePixMap:TTexture(pixmapin:TPixmap, flags:Int, frame_width:Int, frame_height:Int, first_frame:Int, frame_count:Int, tex:TTexture = Null)
	
		Local pixmapFileName:String = "pixmap" + MilliSecs() + Rnd()
		
		If flags & 128 Then Return LoadCubeMapTexture(pixmapFileName:String, flags, tex)
	
		If tex = Null Then tex:TTexture = New TTexture
		
		tex.file:String = pixmapFileName
		tex.file_abs:String = pixmapFileName
		
		' set tex.flags before TexInList
		tex.flags = flags
		tex.FilterFlags()
		
		' check to see if texture with same properties exists already, if so return existing texture
		Local old_tex:TTexture
		old_tex = tex.TexInList()
		If old_tex <> Null And old_tex <> tex
			Return old_tex
		Else
			If old_tex <> tex
				ListAddLast(tex_list, tex)
			EndIf
		EndIf
	
		' load pixmap
		tex.pixmap = CopyPixmap(pixmapin)
		
		' check to see if pixmap contain alpha layer, set alpha_present to true if so (do this before converting)
		Local alpha_present:Int = False
		If tex.pixmap.format = PF_RGBA8888 Or tex.pixmap.format = PF_BGRA8888 Or tex.pixmap.format = PF_A8 Then alpha_present = True
	
		' convert pixmap to appropriate format
		If tex.pixmap.format <> PF_RGBA8888
			tex.pixmap = tex.pixmap.Convert(PF_RGBA8888)
		EndIf
		
		' if alpha flag is true and pixmap doesn't contain alpha info, apply alpha based on color values
		If tex.flags & 2 And alpha_present = False
			tex.pixmap = ApplyAlpha(tex.pixmap)
		EndIf
	
		' if mask flag is true, mask pixmap
		If tex.flags & 4
			tex.pixmap = MaskPixmap(tex.pixmap, 0, 0, 0)
		EndIf
		
		' ---
		
		' if tex not anim tex, get frame width and height
		If frame_width = 0 And frame_height = 0
			frame_width = tex.pixmap.width
			frame_height = tex.pixmap.height
		EndIf
	
		' ---
		
		tex.no_frames = frame_count
		tex.gltex = tex.gltex[..tex.no_frames]
	
		' ---
		
		' pixmap -> tex
	
		Local xframes:Int = tex.pixmap.width / frame_width
		Local yframes:Int = tex.pixmap.height / frame_height
			
		Local startx:Int = first_frame Mod xframes
		Local starty:Int = (first_frame / yframes) Mod yframes
			
		Local x:Int = startx
		Local y:Int = starty
	
		Local pixmap:TPixmap
	
		For Local i:Int = 0 To tex.no_frames - 1
	
			' get static pixmap window. when resize pixmap is called new pixmap will be returned.
			pixmap = tex.pixmap.Window(x * frame_width, y * frame_height, frame_width, frame_height)
			x = x + 1
			If x >= xframes
				x = 0
				y = y + 1
			EndIf
		
			' ---
		
			pixmap = AdjustPixmap(pixmap)
			tex.width = pixmap.width
			tex.height = pixmap.height
			Local width:Int = pixmap.width
			Local height:Int = pixmap.height
	
			Local name:Int
			glGenTextures 1, Varptr name
			glBindTexture GL_TEXTURE_2D, name
	
			Local mipmap:Int
			If tex.flags & 8 Then mipmap = True
			Local mip_level:Int = 0
			Repeat
			glPixelStorei GL_UNPACK_ROW_LENGTH, pixmap.pitch / BytesPerPixel[pixmap.format]
			glTexImage2D GL_TEXTURE_2D, mip_level, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixmap.pixels
			If Not mipmap Then Exit
			If width = 1 And height = 1 Exit
			If width > 1 width:/2
			If height > 1 height:/2
	
			pixmap = ResizePixmap(pixmap, width, height)
			mip_level:+1
			Forever
			tex.no_mipmaps = mip_level
	
			tex.gltex[i] = name
	
		Next
				
		Return tex
		
	End Function
	
End Type

' ----------------------------------------------------------------------------
' Return A value of a given RGB value
' ----------------------------------------------------------------------------
Function GetA:Int(RGB:Int)
	
	Return RGB Shr 24 & %11111111
	
End Function

' ----------------------------------------------------------------------------
' Return R value of a given RGB value
' ----------------------------------------------------------------------------
Function GetR:Int(RGB:Int)
	
	Return RGB Shr 16 & %11111111
	
End Function

' ----------------------------------------------------------------------------
' Return G value of a given RGB value
' ----------------------------------------------------------------------------
Function GetG:Int(RGB:Int)
	
	Return RGB Shr 8 & %11111111
	
End Function

' ----------------------------------------------------------------------------
' Return B value of a given RGB value
' ----------------------------------------------------------------------------
Function GetB:Int(RGB:Int)
	
	Return RGB & %11111111
	
End Function

' ----------------------------------------------------------------------------
' Return ARGB value of given R,G,B,A single values
' ----------------------------------------------------------------------------
Function CombineARGB:Int(a:Int, r:Int, g:Int, b:Int)

	Return b | (g Shl 8) | (r Shl 16) | (a Shl 24)
	
End Function

' ----------------------------------------------------------------------------
' Return RGB value of given R,G,B single values
' ----------------------------------------------------------------------------
Function CombineRGB:Int(r:Int, g:Int, b:Int)

	Return b | (g Shl 8) | (r Shl 16)
	
End Function

Function Levels:Int(inPixel:Int, inBlack:Int = 0, inGamma:Float = 1.0, inWhite:Int = 255, outBlack:Int = 0, outWhite:Int = 255)

	Return (((inPixel-inBlack)/(inWhite-inBlack)^inGamma*(outWhite-outBlack))+outBlack)

End Function

Function Desaturate:Int(rgb:Int)

	Local r:Int=GetR(rgb)
	Local g:Int=GetG(rgb)
	Local b:Int=GetB(rgb)

	Local mx:Int
	Local MaxColor:Int
	Local MinColor:Int

	Local L:Int
	Local S:Int
	Local delta:Int

	If r>g Then

		MaxColor=r
		MinColor=g

	Else

		MaxColor=g
		MinColor=r

	EndIf

	If b>MaxColor Then

		MaxColor=b

	ElseIf b<MinColor Then

		MinColor=b

	EndIf

	Return 255-(127.5*(MaxColor-MinColor)/(255-MaxColor+MinColor))

End Function



Function SavePixmapTGA(pixmap:TPixmap, filename:String, depth:Int = 32)

    Local width:Int = pixmap.width
    Local height:Int = pixmap.height
	
	Local x:Int, y:Int
	
	If Lower(Right(filename, 4)) <> ".tga" Then filename:+".tga"
	
	Local f:TStream = WriteFile(filename)
	
    WriteByte(f, 0) 'idlength
    WriteByte(f, 0) 'colormaptype
    WriteByte(f, 2) 'imagetype 2=rgb
    WriteShort(f, 0) 'colormapindex
    WriteShort(f, 0) 'colormapnumentries
    WriteByte(f, 0) 'colormapsize 
    WriteShort(f, 0) 'xorigin
    WriteShort(f, 0) 'yorigin
    WriteShort(f, width) 'width
    WriteShort(f, height) 'height
    WriteByte(f, 32) 'pixsize
    WriteByte(f, 8) 'attributes
	
	For y = height - 1 To 0 Step - 1
	
        For x = 0 To width - 1
		
			WriteInt(f, ReadPixel(pixmap, x, y))
			
        Next
    Next
	
    CloseFile f
		
End Function