flat assembler
Message board for the users of flat assembler.

Index > Main > color space conversion

Author
Thread Post new topic Reply to topic
Ali.Z



Joined: 08 Jan 2018
Posts: 715
Ali.Z 25 Mar 2022, 07:31
perhaps this topic is more suited to be in heap section, feel free to move it.

while this is not a big issue, but it is something that I want to consider and think about it carefully.

say I have a png file loaded in memory and I can read/write to all pixel data, and it uses 8bpc, so one pixel is one byte (rgba).

now I want to convert this colorful image to grayscale and that is not an issue for me, it is simple to weight the r, g, and b components.

how would you interpret the alpha component? it is safe to ignore the alpha channel and get accurate result, but with the alpha we can get different results depending on how we interpret it. (this is the question how would you interpret it)

another reason regarding why I care about interpreting the alpha channel is because I want to use simd instruction set instead of regular 386 instructions, to either load each component of the pixel as dword and store in a 128-bit register and do stuff with it or load it as each pixel packed into a dword and stored in a 128-bit register (so carrying 4 pixels in one register) and I want to use both methods for sake of experiment. (there are also a series of operations that include division and multiplication)

so again how would you interpret the alpha channel?

_________________
Asm For Wise Humans
Post 25 Mar 2022, 07:31
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
macomics 25 Mar 2022, 08:38
In the descriptions that I have available, the following interpretation of the Alpha channel is proposed in PNG format.
Code:
MAXVAL := ( 1 << BITPERPIX ) - 1
BKGND.RED := ( ALPHA * IMAGE.RED + ( MAXVAL - ALPHA ) * BKGND.RED ) / MAXVAL
BKGND.GREEN := ( ALPHA * IMAGE.GREEN + ( MAXVAL - ALPHA ) * BKGND.GREEN ) / MAXVAL
BKGND.BLUE := ( ALPHA * IMAGE.BLUE + ( MAXVAL - ALPHA ) * BKGND.BLUE ) / MAXVAL    
But are you sure that the palette is not used at 8bpc?
Post 25 Mar 2022, 08:38
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 26 Mar 2022, 03:27
After converting RGB to grey scale, the alpha channel doesn't need any changes, it is still valid.

So you have grey+alpha. Then use the normal alpha merging code to place it onto the screen.

If you choose to ignore the alpha channel then the edges of the image can appear "blocky".

If you decide to use the alpha channel then a simple linear merge as suggested in the above code from macomics will work, but it isn't always what one may want. Another method is to use a gamma correction curve to get a better approximation matching the response of the human eye. This produces more pleasing results and has less noticeable artefacts around the edges.

Edit: A common gamma correction value I've used a lot is this:
Code:
TO_GAMMA                = dword 0.4545454545    ;1 / 2.2
TO_LINEAR               = dword 2.2    
Post 26 Mar 2022, 03:27
View user's profile Send private message Visit poster's website Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 715
Ali.Z 26 Mar 2022, 16:02
for merging alpha with other components, I used lerp. (linear interpolation)

for gamma, I used different numbers from 2.224 down to 0.359 or 0.360. (calculated them on the fly because the image resolution was relatively small, 64x64, a look-up table is required for larger resolutions)

as for color weighting I used what is provided in Rec. 601 standard. (a.k.a. CCIR 601 ----- although 601 is meant for SDTV and not for HDTV) (again didn't use a look-up table cuz small data)

the attachment:
top left cube = original image 50% alpha
2nd row, first cube, alpha got merged using lerp
2nd row, 2nd cube, lerp + CCIR 601 weighting
2nd row, last 4 cubes, lerp + CCIR 601 color weighting + gamma correction
last row, CCIR 601 color weighting only (no gamma, no lerping)

I'm still not pleased with the results, I still want to throw more arguments in the lerp formula and interpret alpha differently to get more variants.

edit:
Ali.Z wrote:
last row, CCIR 601 color weighting only (no gamma, no lerping)

alpha set to 255.


Description:
Filesize: 790.49 KB
Viewed: 2368 Time(s)

z.png



_________________
Asm For Wise Humans
Post 26 Mar 2022, 16:02
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.