Ooops
January 26, 2021, 05:51:05 AM

Author Topic: [bb] Convert 2 Antialiased Images into single image with alpha channel by Matty [ 1+ years ago ]  (Read 503 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Convert 2 Antialiased Images into single image with alpha channel
Author : Matty
Posted : 1+ years ago

Description : This program was put together in response to a need I had to convert a model I'd rendered as a series of bitmaps in blitz3d into a format which had alpha information from antialiasing.  It requires 2 images - 1 with the anti aliased image against a white background and 1 with the anti aliased image against a black background.  It then creates a single TGA image with an alpha channel which retains the antialiasing information.  

Hopefully someone else may find a use for this.  Some of the code below comes from other people's code archives (notably the GetRed/GetBlue/GetGreen/GetRGB code and a few lines of the saveTGA code archive)


Code :
Code: BlitzBasic
  1. ;Just a useful bit of code I put together as I needed to create some antialiased images from a model
  2. ;rendered in blitz3d as I couldn't get the model I had into a renderer that would keep the animation which
  3. ;I wanted to render out as anti aliased tgas.....
  4. ;
  5. ;
  6. ;
  7. ;
  8. ;It is written in blitzplus but the only blitzplus specific code is the requestfile commands in the example
  9. ;
  10. ;The purpose of this program is to take 2 images with no alpha which have been anti aliased against both a
  11. ;black and a white background and convert it into a single image with an alpha channel which retains the antialiasing
  12. ;information in the alpha channel.  
  13. ;
  14. ;Seems to work fine for what I used it for.
  15. ;
  16. ;
  17. ;
  18. ;
  19. ;
  20. ;
  21.  
  22. Graphics 800,600,32,2
  23.  
  24. ;Supply your own images to test.
  25.  
  26. RGBBlack=GetColorMap(RequestFile("Anti Aliased Image File with Black Background","jpg;*.png;*.bmp"))
  27. RGBWhite=GetColorMap(RequestFile("Anti Aliased Image File with White Background","jpg;*.png;*.bmp"))
  28. CreateTGAWithAlpha(RequestFile("TGA Image With Alpha To Save","tga",True),RGBBlack,RGBWhite)
  29. FreeBank RGBBlack
  30. FreeBank RGBWhite
  31. End
  32.  
  33.  
  34. ;CODE
  35.  
  36.  
  37.  
  38. Function GetColorMap(FileName$)
  39.  
  40. image=LoadImage(FileName$)
  41. If image=0 Then RuntimeError("Image Does Not Exist")
  42. width=ImageWidth(image)
  43. height=ImageHeight(image)
  44. bank=CreateBank(8+width*height*4)
  45. PokeInt bank,0,width
  46. PokeInt bank,4,height
  47. SetBuffer ImageBuffer(image)
  48. LockBuffer
  49. For y=0 To height-1
  50. For x=0 To width-1
  51. PokeInt bank,8+4*(x+y*width),ReadPixelFast(x,y)
  52.  
  53. Next
  54. Next
  55. UnlockBuffer
  56. SetBuffer BackBuffer()
  57. Flip
  58. FreeImage image
  59. Return bank
  60. End Function
  61.  
  62. Function CreateTGAWithAlpha(FileName$,BlackImageBank,WhiteImageBank)
  63. RGBBlack=BlackImageBank
  64. RGBWhite=WhiteImageBank
  65. If BlackImageBank=0 Or WhiteImageBank=0 Then RuntimeError("Need to pass white and black image banks")
  66. If BankSize(BlackImageBank)<>BankSize(WhiteImageBank) Then RuntimeError("Black / White Image Banks should be the same size")
  67. Width=PeekInt(BlackImageBank,0)
  68. Height=PeekInt(BlackImageBank,4)
  69. Offset=8
  70. ImageBank=CreateBank(BankSize(BlackImageBank))
  71. PokeInt ImageBank,0,Width
  72. PokeInt ImageBank,4,Height
  73. For Y=height-1 To 0 Step -1
  74. For X=0 To Width-1
  75. RGBBlack=PeekInt(BlackImageBank,8+4*(width*y+x))
  76. RGBWhite=PeekInt(WhiteImageBank,8+4*(width*y+x))
  77. DiffRed=GetRed(RGBBlack)-GetRed(RGBWhite)
  78. DiffGreen=GetGreen(RGBBlack)-GetGreen(RGBWhite)
  79. DiffBlue=GetBlue(RGBBlack)-GetBlue(RGBWhite)
  80.  
  81. AverageColorDifference=(Abs(DiffRed)+Abs(DiffGreen)+Abs(DiffBlue))/3
  82. RGBAlpha=255-AverageColorDifference
  83. If RGBWhite=RGBBlack Then RGBAlpha=255
  84. If RGBBlack=GetRGB(0,0,0) And RGBWhite=GetRGB(255,255,255) Then RGBAlpha=0
  85. PokeByte ImageBank,9+4*(x+((Height-1)-y)*width),getred(RGBBlack)
  86. PokeByte ImageBank,10+4*(x+((Height-1)-y)*width),getgreen(RGBBlack)
  87. PokeByte ImageBank,11+4*(x+((Height-1)-y)*width),getblue(RGBBlack)
  88. PokeByte ImageBank,8+4*(x+((Height-1)-y)*width),RGBAlpha
  89.  
  90. Next
  91. Next
  92. f=WriteFile(FileName$)
  93. If f=0 Then RuntimeError("Error Writing File")
  94. ;Borrowed from Save TGA Example in Code Archives
  95.         WriteByte(f,0) ;idlength
  96.     WriteByte(f,0) ;colormaptype
  97.     WriteByte(f,2) ;imagetype 2=rgb
  98.     WriteShort(f,0) ;colormapindex
  99.     WriteShort(f,0) ;colormapnumentries
  100.     WriteByte(f,0) ;colormapsize
  101.     WriteShort(f,0) ;xorigin
  102.     WriteShort(f,0) ;yorigin
  103.     WriteShort(f,width) ;width
  104.     WriteShort(f,height) ;height
  105.     WriteByte(f,32) ;pixsize
  106.     WriteByte(f,8) ;attributes
  107.         For offset=8 To BankSize(ImageBank)-4 Step 4
  108.                 WriteByte f,PeekByte(ImageBank,offset+3)
  109.                 WriteByte f,PeekByte(ImageBank,offset+2)
  110.                 WriteByte f,PeekByte(ImageBank,offset+1)
  111.                 WriteByte f,PeekByte(imagebank,offset)
  112.        
  113.         Next
  114.  
  115.         ;WriteBytes ImageBank,f,8,width*height*4       
  116.  
  117.  
  118.  
  119.  
  120. CloseFile f
  121.  
  122. FreeBank ImageBank
  123.  
  124. End Function
  125.  
  126.  
  127.  
  128. ;From Code Archives
  129. Function GetRGB(Red,Green,Blue)
  130.  
  131.  
  132. Return Blue Or (Green Shl 8) Or (Red Shl 16)
  133.  
  134. End Function
  135. Function GetRed(RGB)
  136.  
  137. Return RGB Shr 16 And %11111111
  138.  
  139. End Function
  140. Function GetBlue(RGB)
  141.  
  142. Return RGB And %11111111
  143.  
  144. End Function
  145. Function GetGreen(RGB)
  146.  
  147. Return RGB Shr 8 And %11111111
  148. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal