Code archives/3D Graphics - Misc/LoadAlphaChannel

This code has been declared by its author to be Public Domain code.

Download source code

LoadAlphaChannel by jfk EO-111102005
Huge Textures in 32 Bit TGA or PNG with Alpha Channel usually have big filesizes. To shrink the Release Size it is sometimes better to use a JPG for the RGB Channels and a 8-Bit PNG for the Alpha Channel.
; example , texture flags have to be used individually, but
; of course the Flag 2 (alpha) must be set.
; Both textures need to have the same size.

; loading the RGB channels from a lossy compressed JPG:
hudtex=LoadTexture("data\hud.jpg",256 Or 16 Or 32 Or 2)
; Assigning the Alpha Channel form a 8- Bit PNG:
LoadAlphaChannel(hudtex,"data\hud_mask.png")


Function LoadAlphaChannel(id,file$)
 tex4=LoadTexture(file$,2)
 If tex4<>0
  If TextureWidth(id)=TextureWidth(tex4)
   If TextureHeight(id)=TextureHeight(tex4)
    SetBuffer TextureBuffer(id)
    LockBuffer(TextureBuffer(tex4))
    LockBuffer(TextureBuffer(id))
    For j=0 To TextureHeight(tex4)-1
     For i=0 To TextureWidth(tex4)-1
      argb=ReadPixelFast(i,j,TextureBuffer(tex4)) And $ffffff
      r=(argb Shr 16)And $FF
      g=(argb Shr 8)And $FF
      b=argb And $FF
      grey= ((r+g+b)/3)
      If grey > 255 Then grey=255
      rgb=(ReadPixelFast(i,j,TextureBuffer(id))) And $FFFFFF
      WritePixelFast i,j,rgb Or (grey Shl 24),TextureBuffer(id)
     Next
    Next
    UnlockBuffer(TextureBuffer(tex4))
    UnlockBuffer(TextureBuffer(id))
    SetBuffer BackBuffer()
   EndIf
  EndIf
  FreeTexture tex4
 EndIf
End Function

Comments

None.

Code Archives Forum