Pre multiply the alpha.

This commit is contained in:
Bram Stolk 2018-04-20 12:02:53 -07:00
parent 49e3970474
commit 55cf55cc60
2 changed files with 6 additions and 4 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 51 KiB

10
imcat.c
View file

@ -99,9 +99,10 @@ static void print_image_single_res( int w, int h, unsigned char* data )
# define HALFBLOCK "▀" // Uses Unicode char U+2580 # define HALFBLOCK "▀" // Uses Unicode char U+2580
#endif #endif
// Note: image has alpha pre-multied. Mimic GL_ONE + GL_ONE_MINUS_SRC_ALPHA
#define BLEND \ #define BLEND \
{ \ { \
const int t0 = a; \ const int t0 = 255; \
const int t1 = 255-a; \ const int t1 = 255-a; \
r = ( r * t0 + termbg[0] * t1 ) / 255; \ r = ( r * t0 + termbg[0] * t1 ) / 255; \
g = ( g * t0 + termbg[1] * t1 ) / 255; \ g = ( g * t0 + termbg[1] * t1 ) / 255; \
@ -192,9 +193,10 @@ static int process_image( const char* nm )
for ( int xx = sx; xx <= ex; ++xx ) for ( int xx = sx; xx <= ex; ++xx )
{ {
unsigned char* reader = data + ( yy * imw * 4 ) + xx * 4; unsigned char* reader = data + ( yy * imw * 4 ) + xx * 4;
acc[ 0 ] += reader[0]; const int a = reader[3];
acc[ 1 ] += reader[1]; acc[ 0 ] += a * reader[0] / 255;
acc[ 2 ] += reader[2]; acc[ 1 ] += a * reader[1] / 255;
acc[ 2 ] += a * reader[2] / 255;
acc[ 3 ] += reader[3]; acc[ 3 ] += reader[3];
numsamples++; numsamples++;
} }