GLBasic forum

Main forum => GLBasic - en => Topic started by: r0ber7 on 2012-Sep-29

Title: Saving PNG, reloading PNG, transparency RGB(255,0,128)
Post by: r0ber7 on 2012-Sep-29
Hi.

Ran into something weird, thought I'd mention it.
My game is using several tiles that use the color RGB(255,0,128) for their transparency. I've made one single sheet of tiles for easy editing, but the game itself loads all tiles seperately from independent PNG files. So I wrote a little program that takes the single sheet and slices it into seperate sprites which are then saved as PNG files. This is all working fine, but I noticed that the PNG files that are saved, when they are loaded into the game, the pixels with the color RGB(255,0,128) are not treated as transparent. But when I load those same PNG files into my graphics program, the value of the pixels is still RGB(255,0,128). And then, if I save the file from the graphics program, suddenly the transparency is back again... I think it's got something to do with the way SAVESPRITE writes a PNG file. Not a big problem, but odd nonetheless.
Title: Re: Saving PNG, reloading PNG, transparency RGB(255,0,128)
Post by: spacefractal on 2012-Sep-29
I guess the RGB(255, 0, 128) have been auto converted to been use alpha in SAVESPRITE? That mean single transparency have been lost, but have used alpha info instead. Here its might have been confuction what to use: The single transparency or alpha info.

To fix that you should property use SPRITE2MEM and MEM2SPRITE.

I did need similar when I saved screenshots in Greedy Mouse to remove all alpha info (elswise I see strange multiply issues). here is the code to give ideas:

Code (glbasic) Select

FUNCTION Image_Snap: File$
GLOBAL IDSNAP=0
LOCAL Width, Height, ok, a, r, g, b
LOCAL pix%[]
GETSCREENSIZE Width, Height
IF IDSNAP=0 THEN IDSNAP=GENSPRITE()
GRABSPRITE IDSNAP, 0, 0, Width, Height
IF SPRITE2MEM(pix[], IDSNAP)
LOCAL total=BOUNDS(pix[],0)
FOR i=0 TO total-1
r = bAND(pix[i], 0xff)
g = bAND(ASR(pix[i],8), 0xff)
b = bAND(ASR(pix[i],16), 0xff)
a = bAND(ASR(pix[i],24), 0xff)
a = 255
pix[i] = bOR(RGB(r,g,b), ASL(a, 24))
NEXT
IF MEM2SPRITE(pix, IDSNAP, Width, Height)
SAVESPRITE File$, IDSNAP
ENDIF
GRABSPRITE IDSNAP, 0, 0, 8, 8
ENDIF
ENDFUNCTION


Title: Re: Saving PNG, reloading PNG, transparency RGB(255,0,128)
Post by: jestermon on 2012-Sep-29
Fascinating. I was wondering how to apply pixel filters to an image in GLB. This explains the low level stuff perfectly. Thanks for the snippit.
Title: Saving PNG, reloading PNG, transparency RGB(255,0,128)
Post by: Kitty Hello on 2012-Sep-30
Or settransparency before savespite?
Title: Re: Saving PNG, reloading PNG, transparency RGB(255,0,128)
Post by: Slydog on 2012-Oct-01
Do you need the transparency saved as (255,0,128), or could you change those to true alpha values, such as:

Code (glbasic) Select
// same code from spacefractal's snippet (ie, for each pixel . . .)
r = bAND(pix[i], 0xff)
g = bAND(ASR(pix[i],8), 0xff)
b = bAND(ASR(pix[i],16), 0xff)
a = bAND(ASR(pix[i],24), 0xff)
a = 255 // Default Alpha, no transparency

// new code . . . If the pixel is (255,0,128), then set it's alpha to 0
IF (r=255) AND (g=0) AND (b=128) THEN a = 0

pix[i] = bOR(RGB(r,g,b), ASL(a, 24))