Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

can you help me to convert a 24bit RGB image to 8 bit RGB image...

and 16bit grey scale to 24 bit RGB..

Thanks,
PS: i am new to image programming and learning new aspects

What I have tried:

i have tried to shift bits as below ...
r = r >> 1;
g = g >> 2;
b = b >> 1;
Posted
Updated 22-Jun-19 15:36pm

As noted for instance here "colors - How to convert 24 bit RGB to 8 bit RGB - Stack Overflow"[^] the 8 bit per pixel format is usually used as palette index.
However, suppose you have the following formats:

C
bit number 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
           b7 b6 b5 b4 b3 b2 b1 b0 g7 g6 g5 g4 g3 g2 g1 g0 r7 r6 r5 r4 r3 r2 r1 r0

bit number  7  6  5  4  3  2  1  0
           B1 B0 G2 G1 G0 R2 R1 R0


Then 
     B1 = b7, B0 = b6
     G2 = g7, G1 = g6, G0 = g5
     R2 = r7, r1 = r6, r0 = r5

that is 

     b = (rgb >> 16) & 0xFF;
     g = (rgb >> 8) & 0xFF;
     r = rgb & 0xFF;

     B = b >> 6;
     G = g >> 5;
     R = r >> 5;

     RGB = (B << 6) | ( G << 3 ) | R;
 
Share this answer
 
Comments
tasumisra 8-Feb-16 10:54am    
Thank you so much Pallini,i have been waiting for your answer and i know you are champion :) can you please help me with some good tutorial\links for the same...

apart from that how to convert 16bit grey scale to 24 bit RGB ... just exploring few things around
CPallini 8-Feb-16 16:32pm    
See here
http://stackoverflow.com/questions/835753/convert-grayscale-value-to-rgb-representation
The only difference is your image is 16-bit greyscale, so you have to perform an additional preliminary step:
grey = grey >> 8
jartinez 22-Jun-19 21:33pm    
Your solution is not correct. To convert 24 bit color to 8 bit color you must consider that 255 should be converted to 7, so the formula should be Red3 = Red8 * 7 / 255
8bit Color = (Red * 7 / 255) << 5 + (Green * 7 / 255) << 2 + (Blue * 3 / 255)

With your example color #c0c0c0 would lead to a wrong 8 bit RGB. Test it with this link:

http://neildowning.com/HEX_to_RGB_color_converter.php
CPallini 23-Jun-19 4:05am    
My solution is correct, of course :-D
0xC0C0C0
b = 0xC0 = 10100000b
g = 0xC0 = 10100000b
r = 0xC0 = 10100000b

B = b >> 6 = 00000010b
G = g >> 5 = 00000101b
R = r >> 5 = 00000101b

RGB = (B << 6) | ( G << 3 ) | R = 10101101b

That is 10110110b with bit reversed.
Code for VB.NET

Private Function ColorRGB8bits(ByVal RGBColor As Color) As Byte
        'Converts RGB (Hex 24 bits) to 8-bit color 
        '(3 bits for red, 3 bits for green and 2 bits for blue) 
        'To convert 8bit [0 - 255] value into 3bit [0, 7]
        'the 0 Is Not a problem, but remember 255 should be converted To 7 
        'so the formula should be Red3 = Red8 * 7 / 255.
        'To convert 24bit color into 8bit (RRRGGGBB):
        '8bit Color = (Red * 7 / 255) << 5 + (Green * 7 / 255) << 2 + (Blue * 3 / 255)

        Dim r, g, b As Byte
        Dim RGB8BIT As Byte
        Dim RGB24BIT As Long

        RGB24BIT = RGBColor.ToArgb And &HFFFFFF 'Mask with &H00FFFFFF to remove the Alpha

        b = RGB24BIT And &HFF
        g = (RGB24BIT >> 8) And &HFF
        r = (RGB24BIT >> 16) And &HFF

        RGB8BIT = ((r * 7 / 255) << 5) + ((g * 7 / 255) << 2) + (b * 3 / 255)

        Return RGB8BIT
    End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900