image processing - How to convert bitmap 16-bit RGBA4444 to 16-bit Grayscale in C++? -


i used method:

val = 0.299 * r + 0.587 * g + 0.114 * b; image.setrgba(val, val, val, 0); 

to convert bitmap (24-bit rgb888 , 32-bit rgba8888) grayscale successfully.

example: bmp 24-bit: 0e 0f ef (bgr) --> 51 51 51 (grayscale) // using above method

but can't apply bitmap 16-bit rgba4444.

example: bmp 16-bit: 'a7 36' (bgra) --> 'ce 39' (grayscale) // ???

anyone know how?

are sure need rgba4444 format? maybe need old format green channel gets 6 bits while red , blue 5 bits (total 16 bits)

in case of 5-6-5 format - answer simple. r = (r>>3); g = (g>>2); b = (b>>3); reduce 24 bits 16. combine them using | operation.

here sample code in c

// combine rgb 16bits 565 representation. assuming inputs in range of 0..255 static int16  make565(int red, int green, int blue){     return (int16)( ((red   << 8) & 0xf800)|                     ((green << 2) & 0x03e0)|                     ((blue  >> 3) & 0x001f)); } 

the method above uses same structure regular argb construction method squeezes colors 16 bits instead of 32 in following example:

// combine rgb 32bit argb representation. assuming inputs in range of 0..255 static int32  makeargb(int red, int green, int blue){     return (int32)((red)|(green << 8)|(blue<<16)|(0xff000000)); // alpha ff } 

if need rgba4444 method combination of 2 above

// combine rgba 32bit 4444 representation. assuming inputs in range of 0..255 static int16  make4444(int red, int green, int blue, int alpha){     return (int32)((red>>4)|(green&0xf0)|((blue&0xf0)<<4)|((alpha&0xf0)<<8)); } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -