Author Topic: Saving Alpha Info [BB2D]  (Read 1219 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Saving Alpha Info [BB2D]
« on: August 08, 2007 »
Ok I'm working on a prog that creates a greyscale bumpmap of an image (that part works). Now i want to save it to the high 8 bits of an image, but it dosent work. For some reason it seems as if its not saving the info in the high 8 bits, or the bits are being reverted to 'all on'.

To run the code you will need an image in the same directory called: 'test.bmp'. Best keep it small, this prog is sloooow. Its meant to output the bumpmap on the left - image on the right. Anyway heres the code:

Code: [Select]
;-------------- GLOBALS ------------------;
Const FILTER_W = 3
Const FILTER_H = 3
Const FACTOR# = 1.0
Const BIAS# = 128.0
Dim filter(FILTER_W, FILTER_H)
;------------- INIT FILTER ---------------;
For y = 0 To FILTER_H-1
For x = 0 To FILTER_W-1
Read filter(x, y)
Next
Next
Data -1, -1,  0
Data -1,  0,  1
Data  0,  1,  1
;-------------- TEST PROG ----------------;
Graphics 640, 480, 32, 2
SetBuffer BackBuffer()
image = LoadImage("test.bmp")
BumpMap(image)

DrawImage image, 256, 0
DrawBump(image, 0, 0)

WaitKey
;-----------------------------------------;
Function BumpMap(image)

hx = FILTER_W / 2
hy = FILTER_H / 2

w = ImageWidth(image)
h = ImageHeight(image)

SetBuffer ImageBuffer(image)
LockBuffer

For y=0 To h-1
For x=0 To w-1

r#=0: g#=0: b#=0

For fy=0 To FILTER_H-1
    For fx=0 To FILTER_W-1
ix = (x-hx+fx+w) Mod w
iy = (y-hy+fy+h) Mod h
col = ReadPixel(ix, iy)
r# = r# + GetGElement(col) * filter(fx, fy)
g# = g# + GetGElement(col) * filter(fx, fy)
b# = b# + GetBElement(col) * filter(fx, fy)
Next
Next

r# = r# * FACTOR + BIAS
g# = g# * FACTOR + BIAS
b# = b# * FACTOR + BIAS

If r# < 0 Then r# = 0
If r# > 255 Then r# = 255
If g# < 0 Then g# = 0
If g# > 255 Then g# = 255
If b# < 0 Then b# = 0
If b# > 255 Then b# = 255

col = col And 16777215 ;& out alpha bits
alpha = ((r# * 61)+(g# * 174)+(b# * 21)) / 256 ;greyscale bumpmap
alpha = alpha Shl 24
;WRITE PIXEL BACK WITH NEW ALPHA INFO
WritePixel(x, y, col+alpha)
Next
Next

UnlockBuffer
SetBuffer BackBuffer()

End Function

;----------- RGB CONVERSION ----------------;
Function GetRElement(col)
Return (col Shr 16) And 255
End Function

Function GetGElement(col)
Return (col Shr 8)  And 255
End Function

Function GetBElement(col)
Return col And 255
End Function

Function ToRGB(r, g, b)
Return (r Shl 16) + (g Shl 8) + b
End Function
;------------- TEST FUNCTION ---------------;
Function DrawBump(image, x_pos, y_pos)
w = ImageWidth(image)
h = ImageHeight(image)
For y=0 To h-1
For x=0 To w-1
col = ReadPixel(x, y, ImageBuffer(image))
col = col Shr 24
col = ToRGB(col, col, col)
WritePixel(x_pos+x, y_pos+y, col)
Next
Next
End Function

Does anyone know how to fix this?

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Saving Alpha Info [BB2D]
« Reply #1 on: August 09, 2007 »
I got this one sorted now :)

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Saving Alpha Info [BB2D]
« Reply #2 on: August 09, 2007 »
Would you mind sharing the answer?  I guess it had something to with saving 24 or 32bit bmp files?  Windows+BMP+Alpha is never a happy arrangement, half the APIs just ignore the top 24bits.

Jim
Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Saving Alpha Info [BB2D]
« Reply #3 on: August 09, 2007 »
Yeah now its saving to a TGA using some code from BB.com which seems to work.