BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"TGA's "
, by poedboy
Allows you to Create, Modify, Save, and Load tga's with alpha information. Works in blitz 2d, 3d, and plus.
Code
;============================================================================== ;Name: TGA.bb ;Author: A Pissed Off Boy ;Date: 1:00am, 4/13/04 ;Updated: n/a ;============================================================================== ;A few functions for working with Truevision Targa's (".tga"): ; ; ; NewTGA.TGA(Width%,Height%) ; DeleteTGA(t.TGA) ; LoadTGA.TGA(Filename$) ; SaveTGA(t.TGA,Filename$) ; ; ; ;The Good: ; ;-Allows you to Create, Modify, Save, and Load tga's with alpha information. ;-Easy and Flexible; creates 2 images within a type; stores rgb in one image, ; alpha channel in second image...makes for easy editing ;) ;-Should work in all versions of blitz(dun remember when the read/writepixfast stuff was added). ; ; ;The Bad: ; ;-Currently, only supports Uncompressed, TrueColor(32bit) tga's. I did leave in ; most of the ignored header stuff in LoadTGA(), in case someone needed to use it ; (can't imagine why though). No footer crap is added. It really is just crap at the end :) ;-doesn't take advantage of any b+ specific features(just to keep it compatible) ;-code is very messy (but straightforward) ;-dunno if this will blow up in 16bit mode :D ; ; ;The Ugly: ; ;- Photoshop's handling of alpha'd PNG's is , *ahem*, "flawed" ;- Couldn't be arsed to write png functions(yet) ;- PNG > TGA ; ; ;Useage: ; ; Blah you figure it out, I'm done -_- ; ; ;============================================================================== ;TYPES ;============================================================================== Type TGA Field Image%,Alpha%,Width%,Height% End Type ;============================================================================== ;FUNCTIONS ;============================================================================== Function NewTGA.TGA(Width%,Height%) Local t.TGA=New TGA t\Image%=CreateImage(Width%,Height%) t\Alpha%=CreateImage(Width%,Height%) t\Width%=Width% t\Height%=Height% Return t.TGA End Function Function DeleteTGA(t.TGA) FreeImage t\Image% FreeImage t\Alpha% Delete t.TGA End Function Function LoadTGA.TGA(Filename$) Local File%,t.TGA,Width%,Height%,Depth%,ImageDescriptor%,AlphaChannelBits%,Index%,ARGB% If FileType(Filename$)<>1 Then Return File%=ReadFile(Filename$) ;**18 byte header... ;IDLength%=ReadByte(File%) ;ColorMapType%=ReadByte(File%) SeekFile(File%,2) ImageType%=ReadByte(File%) If ImageType%<>2;2 = Uncompressed, True Color RuntimeError "TGA Image Type Not Supported." EndIf ;DebugLog "ImageType%="+ImageType% ;**color map specification ;FirstEntryIndex%=ReadShort(File%) ;ColorMapLength%=ReadShort(File%) ;ColorMapEntrySize%=ReadByte(File%) ;**image specification ;Xorigin%=ReadShort(File%) ;Yorigin%=ReadShort(File%) SeekFile(File%,12) Width%=ReadShort(File%) Height%=ReadShort(File%) Depth%=ReadByte(File%) If Depth%<>32 RuntimeError "TGA Image Depth Not Supported." EndIf ImageDescriptor%=ReadByte(File%) AlphaChannelBits%=(ImageDescriptor% And 15) ;DebugLog "Width%="+Width% ;DebugLog "Height%="+Height% ;DebugLog "Depth%="+Depth% ;DebugLog "AlphaChannelBits="+AlphaChannelBits% ;**image\color map data ;If IDLength%>0 ; For i%=1 To IDLength% ; ReadByte(File%) ; Next ;EndIf ;If ColorMapType%>0 ; For i%=1 To IDLength% ; ReadByte(File%) ; Next ;EndIf ;load in image data now... t.TGA=NewTGA.TGA(Width%,Height%) Bank%=CreateBank(Width%*Height%*4) ReadBytes Bank%,File%,0,BankSize(Bank%) ;write RGB Data... SetBuffer ImageBuffer(t\Image%) : LockBuffer For y%=Height%-1 To 0 Step -1 For x%=0 To Width%-1 ARGB%=PeekInt(Bank%,Index%) And $FFFFFF WritePixelFast x%,y%,ARGB% Index%=Index%+4 Next Next Index%=0 UnlockBuffer ;write Alpha Data... SetBuffer ImageBuffer(t\Alpha%) : LockBuffer For y%=Height%-1 To 0 Step -1 For x%=0 To Width%-1 ARGB%=PeekInt(Bank%,Index%) Shr 24 WritePixelFast x%,y%,(ARGB% Shl 16)+(ARGB% Shl 8)+ARGB% Index%=Index%+4 Next Next UnlockBuffer FreeBank Bank% CloseFile File% Return t.TGA End Function Function SaveTGA(t.TGA,Filename$) Local File%=WriteFile(Filename$),Bank%,Index%,ARGB%,X%,Y% Bank%=CreateBank(18);header PokeByte Bank%,2,2 PokeShort Bank%,12,t\Width% PokeShort Bank%,14,t\Height% PokeByte Bank%,16,32 PokeByte Bank%,17,8 WriteBytes Bank%,File%,0,BankSize(Bank%) FreeBank Bank% Bank%=CreateBank(t\Width%*t\Height*4);image data ;Alpha... SetBuffer ImageBuffer(t\Alpha%) : LockBuffer For y%=t\Height%-1 To 0 Step -1 For x%=0 To t\Width%-1 ARGB%=(ReadPixelFast(x%,y%) And $FF) Shl 24;blue channel determines alpha :P PokeInt Bank%,Index%,ARGB% Index%=Index%+4 Next Next UnlockBuffer Index%=0 ;RGB... SetBuffer ImageBuffer(t\Image%) : LockBuffer For y%=t\Height%-1 To 0 Step -1 For x%=0 To t\Width%-1 ARGB%=ReadPixelFast(x%,y%) And $FFFFFF PokeInt Bank%,Index%,PeekInt(Bank%,Index%)+ARGB% Index%=Index%+4 Next Next UnlockBuffer WriteBytes Bank%,File%,0,BankSize(Bank%) FreeBank Bank% CloseFile File% End Function
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations