Ooops
October 28, 2020, 06:14:59 AM

Author Topic: [bb] Save TGA 32 by Hulk [ 1+ years ago ]  (Read 1234 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Save TGA 32 by Hulk [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Save TGA 32
Author : Hulk
Posted : 1+ years ago

Description : Original code from Skidracer

Save buffer to TGA 32, with or without alpha
Alpha was calculated by RGB sum, very nice..., very fast!


Code :
Code: BlitzBasic
  1. ; ID: 1
  2. ; Author: skidracer
  3. ; Date: 2001-08-16 16:24:30
  4. ; Title: SaveTGA
  5. ; Description: save texture in TGA format
  6. ;
  7. ; Addition coding HULK on 12/2004
  8. ; Description: save buffer to TGA format
  9. ; flag: pix_depth=24 no alpha save
  10. ;       pix_depth=32 save alpha from rgb sum ;nice!
  11.  
  12.  
  13. Graphics3D 640,480,32,2
  14. SetBuffer FrontBuffer()
  15.  
  16. ;**************************************************************
  17. ; MISC STUFF
  18. camera%=CreateCamera()
  19.  
  20. PositionEntity camera,0,0,-10
  21.  
  22. Dim p(100)
  23. For a=1 To 100
  24.         p(a)=CreateSprite()
  25.         EntityAlpha p(a),Rnd(.1,.5)
  26.         EntityColor p(a),Rnd(255),Rnd(255),Rnd(255)
  27.         PositionEntity p(a),Rnd(-10,10),Rnd(-10,10),Rnd(0,10)
  28. Next
  29.  
  30. RenderWorld()
  31. Color 255,0,0
  32. Rect 0,0,640,480,0
  33. WaitKey()
  34. ;**************************************************************
  35.  
  36. Save_buffer_tga("c:     est.tga",FrontBuffer(),640,480,32)
  37. End
  38.  
  39.  
  40.  
  41.  
  42.  
  43. Function Save_buffer_TGA(name$,buffer%,sx%,sy%,pix_depth%=24)
  44.     Local f,width%,height%,x%,y%
  45.     width=sx
  46.     height=sy
  47.     f=WriteFile(name$)
  48.     WriteByte(f,0) ;idlength
  49.     WriteByte(f,0) ;colormaptype
  50.     WriteByte(f,2) ;imagetype 2=rgb
  51.     WriteShort(f,0) ;colormapindex
  52.     WriteShort(f,0) ;colormapnumentries
  53.     WriteByte(f,0) ;colormapsize
  54.     WriteShort(f,0) ;xorigin
  55.     WriteShort(f,0) ;yorigin
  56.     WriteShort(f,width) ;width
  57.     WriteShort(f,height) ;height
  58.     WriteByte(f,32) ;pixsize
  59.     WriteByte(f,8) ;attributes
  60.  
  61.     LockBuffer buffer
  62.         For y=height-1 To 0 Step -1
  63.         For x=0 To width-1
  64.  
  65.                         If pix_depth=24
  66.                                 pix%=ReadPixelFast(x,y,buffer)
  67.                                 WriteInt f,pix
  68.                         Else
  69.                                 pix%=(ReadPixelFast(x,y,buffer) Shl 8) Shr 8
  70.  
  71.                                 ir%=((pix Shr 16) And $00ff)
  72.                                 ig%=((pix Shr 8) And $00ff)    
  73.                                 ib%=((pix And $00ff))
  74.                                
  75.                                 alpha#=(ir + ig +ib) * 1.5              ;1.5 alpha power based on rgb sum :) 1.5, 1.7, 1.9 increase visibility but decrease alpha
  76.                                
  77.                                 If alpha>255 Then alpha=255
  78.                                
  79.                                 WriteInt f,( alpha Shl 24 ) Or pix
  80.                         EndIf      
  81.                        
  82.         Next
  83.     Next
  84.     CloseFile f
  85.         UnlockBuffer buffer
  86. End Function


Comments :


Gabriel(Posted 1+ years ago)

 Doesn't seem to successfully save the alpha from a texture. I tried your function and just got opaque white where the alpha should have been and anything that had >0.0 and <1.0 alpha was written as 0.0 alpha.I replaced your function with SkidRacer's original and it worked perfectly.


jfk EO-11110(Posted 1+ years ago)

 This may be wrong:ir%=((pix Shr 16) And $00ff)ig%=((pix Shr 8) And $00ff)ib%=((pix And $00ff))alpha#=(ir + ig +ib) * 1.5it gives values between 0 and 1147. Id rather use:alpha#=(ir + ig +ib) / 3to get the exact brightness alpha value, or maybe something like
Code: [Select]
ir#=((pix Shr 16) And $00ff)
ig#=((pix Shr 8) And $00ff)
ib3=((pix And $00ff))

alpha#=(ir^2 + ig^2 +ib^2) /16
If alpha>255 Then alpha=255
To get some kind of nonlinear transparency [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal