[bb] Transparent TGA (Load/Draw) by Baystep Productions [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Transparent TGA (Load/Draw)
Author : Baystep Productions
Posted : 1+ years ago

Description : I dunno about speed or nothing but it seemed to work fine for my purposes. This is for Blitz3D. It draws directly onto whatever buffer is set. I would like credit please.

Usage is...
Global myImage.TGAImage = LoadTGAImage("test.tga"[,blend])
DrawTGAImage(myImage,x%,y%,alpha#,[use alpha? True])
FreeTGAImage(myImage)

Blend modes are...
-1 = Subtractive (harsh)
0 = Multiply (recommended)
1 = Additive (harsh)


Code :
Code (blitzbasic) Select
;CREATED BY CHRIS PIKUL
;----------------------

Type TGAImage
Field file$,width%,height%,depth%,IMGBank,RGBImg,blend%
End Type
Global tga.TGAImage

Function LoadTGAImage.TGAImage(file$,blend%=0) ;Blend: -1=Sub 0=Multiply 1=Additive
img = ReadFile(file$)
If Not img Then RuntimeError("Could not load specified image!")
Local idlen%=ReadByte(img)
Local clrmap%=ReadByte(img)
Local imgtyp%=ReadByte(img)
Local cm_index%=ReadShort(img)
Local cm_length%=ReadShort(img)
Local cm_entsize%=ReadByte(img)
Local img_xorg%=ReadShort(img)
Local img_yorg%=ReadShort(img)
Local img_width%=ReadShort(img)
Local img_height%=ReadShort(img)
Local img_depth%=ReadByte(img)
Local img_desc%=ReadByte(img)
Local img_info$=""
If idlen%<>0 Then
For i=1 To idlen%
img_info$=img_info$+Chr$(ReadByte(img))
Next
EndIf
Local cm_data$=""
If clrmap%<>0 Then
For i=1 To cm_entsize%*cm_length%
cm_data$=cm_data$+Chr$(ReadByte(img))
Next
EndIf
tga.TGAImage = New TGAImage
tgaIMGBank = CreateBank((4*img_width%)*(4*img_height))
tgaRGBImg = CreateImage(img_width%,img_height%)
tgawidth%=img_width%
tgaheight%=img_height%
tgadepth%=img_depth%
tgalend%=blend%
intwrite=0
SetBuffer ImageBuffer(tgaRGBImg)
LockBuffer
For y=img_height%-1 To 0 Step -1
For x=0 To img_width-1
If img_depth%=32
pix%=ReadInt(img)
PokeInt(tgaIMGBank,intwrite,pix%)
r%=(pix Shr 16) And $FF
g%=(pix Shr 8) And $FF
b%=pix And $FF
out_pix% = b% Or (g% Shl 8) Or (r% Shl 16)
intwrite=intwrite+4
WritePixelFast(x,y,out_pix%)
Else

EndIf
Next
Next
UnlockBuffer
SetBuffer BackBuffer()

Return tga.TGAImage
End Function

Function DrawTGAStatic(hnd.TGAImage,sx%,sy%)
DrawImage(hndRGBImg,sx%,sy%)
End Function

Function DrawTGAImage(hnd.TGAImage,sx%,sy%,a#=1,aopt#=True) ;Handle,XCord,YCord,Alpha,ARGB(FALSE=RGB)
intread=0
Local iA#=0.0
If aopt#=False Or hnddepth%<>32
DrawImage(hndRGBImg,sx%,sy%)
Else
LockBuffer()
For y=hndheight%-1 To 0 Step -1
For x=0 To hndwidth%-1
pix% = PeekInt(hndIMGBank,intread)
opix% = ReadPixelFast(sx%+x,sy%+y)

iA#=((pix Shr 24) And $FF)/255.0
Select hndlend%
Case 1
iR%=((pix Shr 16) And $FF)*(a#*iA#)
iG%=((pix Shr 8) And $FF)*(a#*iA#)
iB=(pix And $FF)*(a#*iA#)
gR%=((opix Shr 16) And $FF)+iR%
gG%=((opix Shr 8) And $FF)+iG%
gB%=(opix And $FF)+iB%
Case 0
iR%=((pix Shr 16) And $FF)
iG%=((pix Shr 8) And $FF)
iB=(pix And $FF)
gR%=((opix Shr 16) And $FF)
gG%=((opix Shr 8) And $FF)
gB%=(opix And $FF)
gR%=gR%+(iR%-gR%)*iA#:gG%=gG%+(iG%-gG%)*iA#:gB%=gB%+(iB%-gB%)*iA#
Case -1
iR%=((pix Shr 16) And $FF)*(a#*iA#)
iG%=((pix Shr 8) And $FF)*(a#*iA#)
iB=(pix And $FF)*(a#*iA#)
gR%=((opix Shr 16) And $FF)-iR%
gG%=((opix Shr 8) And $FF)-iG%
gB%=(opix And $FF)-iB%
End Select

gR%=gR% And 255:gG%=gG% And 255:gB%=gB% And 255
rpix% = gB% Or (gG% Shl 8) Or (gR% Shl 16)
WritePixelFast(sx%+x,sy%+y,rpix%)
intread=intread+4
Next
Next
UnlockBuffer()
EndIf
End Function

Function FreeTGAImage(hnd.TGAImage)
FreeImage(hndRGBImg)
FreeBank(hndIMGBank)
Delete hnd
End Function


Comments :


Christer(Posted 1+ years ago)

 It dont work for me... No errors no image nothing...


K@li(Posted 1+ years ago)

 any zip file ?


Nate the Great(Posted 1+ years ago)

 I made a function that does alpha here it is<a href="codearcsed4b.html?code=2299#comments" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=2299#comments</a>


ShadowTurtle(Posted 1+ years ago)

 1. debug test: invert alpha value2. debug test: use argb instead of rgbatry and see...