dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Bitmap to RGB565 format

  1. #1
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146

    Question Bitmap to RGB565 format

    Hello,
    I want to open a 24 bit bitmap file and convert that into
    RGB565 format . The output file should be a "C" file with
    the converted data in the following array format.

    const long data[] = {0xFFF5FFB0 , 0xFFFFFFF,..................};

    could any body give me a STARTUP hint?
    "They can because they think they can" - Voltaire.

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Bitmap to RGB565 format

    You can try this:

    Code:
    void ColorConversion24To16 ( BYTE *lpBits24, int Width, int Height, BYTE *lpBits16   )
    {
      int nPos16 = 0;
      int Size = Width*Height*3;
      for ( int nPos24 = 0 ; nPos24 < Size ; nPos24+=3 ,nPos16+=2)
      {
        BYTE Red24   = lpBits24[nPos24+2]; // 8-bit red
        BYTE Green24 = lpBits24[nPos24+1]; // 8-bit green
        BYTE Blue24  = lpBits24[nPos24+0]; // 8-bit blue
    
        BYTE Red16   = Red24   >> 3;  // 5-bit red
        BYTE Green16 = Green24 >> 2;  // 6-bit green
        BYTE Blue16  = Blue24  >> 3;  // 5-bit blue
    
        unsigned short RGB2Bytes = Blue16 + (Green16<<5) + (Red16<<(5+6));
    
        lpBits16[nPos16]  = LOBYTE(RGB2Bytes);
        lpBits16[nPos16+1]= HIBYTE(RGB2Bytes);
      }
    }
    
      // usage
      int Width  = 100;
      int Height = 100;
    
      BYTE *lpBits24 = new BYTE[Width*Height*3];
      BYTE *lpBits16 = new BYTE[Width*Height*2];
    
    
      ColorConversion24To16( lpBits24,Width,Height,lpBits16);
    
    
      delete [] lpBits24;
      delete [] lpBits16;
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146

    Re: Bitmap to RGB565 format

    thanks a lot for the hint.
    "They can because they think they can" - Voltaire.

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: Bitmap to RGB565 format

    Quote Originally Posted by kumaru_san
    thanks a lot for the hint.
    You are welcome

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    Aug 2003
    Location
    Japan
    Posts
    146

    Angry Re: Bitmap to RGB565 format

    Hello ,
    I tried the above program.
    But i got different output than what i expected.
    There was a problem in color combination.
    For example ,

    It was like this in my output.

    Blue --> green
    Red --> Blue
    green --> red

    I guess the colors are stored in bitmap image as BGR.

    In my program, i open the file and read the data
    and then convert it into RGB565 and write in RGB format.
    Should i write in BGR format in the output file?
    Is there any Endian format problem?
    Since i am novice in this area ,could you explain me in
    plain language?
    "They can because they think they can" - Voltaire.

  6. #6
    Join Date
    May 2005
    Posts
    4,954

    Re: Bitmap to RGB565 format

    Quote Originally Posted by kumaru_san
    Hello ,
    I tried the above program.
    But i got different output than what i expected.
    There was a problem in color combination.
    For example ,

    It was like this in my output.

    Blue --> green
    Red --> Blue
    green --> red

    I guess the colors are stored in bitmap image as BGR.

    In my program, i open the file and read the data
    and then convert it into RGB565 and write in RGB format.
    Should i write in BGR format in the output file?
    Is there any Endian format problem?
    Since i am novice in this area ,could you explain me in
    plain language?
    Well its very simple if your input image is in BGR format you just have to make the following modifications:

    Instead of:

    Code:
    BYTE Red24   = lpBits24[nPos24+2]; // 8-bit red
    BYTE Green24 = lpBits24[nPos24+1]; // 8-bit green
    BYTE Blue24  = lpBits24[nPos24+0]; // 8-bit blue
    Try:
    Code:
    BYTE Red24   = lpBits24[nPos24+0]; // 8-bit red
    BYTE Green24 = lpBits24[nPos24+1]; // 8-bit green
    BYTE Blue24  = lpBits24[nPos24+2]; // 8-bit blue
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    Oct 2008
    Posts
    1

    Re: Bitmap to RGB565 format

    Hi,

    I need to do this conversion too, and I wrote an application.

    EDIT:
    /*But it seems it does't work right. (I understand this by checking printf outputs). I couldnt see what is wrong. Do you have any suggestions?*/

    I had some modifications and now what I write to rgb565.bin and what I read from the it is identical.

    But now, what I do is just the image conversion. I don't know if we need any header or something similar in BMP? Also, do you know in which media application I can see the rgb565 image?

    Thank you in advance,

    A. Erman Kulunyar

    Correct code:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    #pragma pack(2) /*2 byte packing */
    
    typedef struct
    {
    unsigned short int type;
    unsigned int size;
    unsigned short int reserved1,reserved2;
    unsigned int offset;
    }Header;
    
    #pragma pack() /* Default packing */
    
    typedef struct
    {
       unsigned long size;
       unsigned long width;
       unsigned long height;
       unsigned short planes;
       unsigned short bitcount;
       unsigned long compression;
       unsigned long sizeimage;
       long xpelspermeter;
       long ypelspermeter;
       unsigned long colorsused;
       unsigned long colorsimportant;
    }Infoheader;
    
    typedef struct {
       BYTE red;
       BYTE green;
       BYTE blue;
    } Single_pixel;
    
    
    FILE *fp_rgb; //global file pointer to rgb image
    
    void BMP24ToRGB565 ( BYTE *bmp_24 )
    {
      
        BYTE *rgb_565 = new BYTE[2];
        
        BYTE Red24 = bmp_24[0];   // 8-bit red
        BYTE Green24 = bmp_24[1]; // 8-bit green
        BYTE Blue24 = bmp_24[2];  // 8-bit blue
        
        BYTE Red16   = Red24   >> 3;  // 5-bit red
        BYTE Green16 = Green24 >> 2;  // 6-bit green
        BYTE Blue16  = Blue24  >> 3;  // 5-bit blue
        
        printf("\n Red16: %x \n", Red16);
        printf(" Green16: %x \n", Green16);
        printf(" Blue16: %x \n", Blue16);
        
        unsigned short RGB2Bytes = Blue16 + (Green16<<5) + (Red16<<(5+6));
        printf(" RGB2Bytes: %d \n", RGB2Bytes);
    
        rgb_565[0] = LOBYTE(RGB2Bytes);
        rgb_565[1] = HIBYTE(RGB2Bytes);
        printf(" rgb_565[0]: %x \n", rgb_565[0]);
        printf(" rgb_565[1]: %x \n\n", rgb_565[1]);
        
        //fwrite(rgb_565,1, sizeof(rgb_565), fp_rgb);
        
        BYTE low = rgb_565[0];
        BYTE high = rgb_565[1];
        
        fwrite(&low, 1, sizeof(low), fp_rgb);
        fwrite(&high, 1, sizeof(high), fp_rgb); 
               
    }
    
    
    
    int main()
    {
       Header headfirst;
       Infoheader headsecond;
       Single_pixel single_pixel;
       
       int byte_border;
       
       /* binary opening of the input image file (24bit bmp) */
       FILE *fin;
       fin = fopen("resim.bmp","rb+");
       if(fin==NULL) //file doesnt exist
       {
          printf("Error opening first file");
          exit(0);
          return 0;
       }
       
       /* binary opening of the output image file (rgb565) */
       fp_rgb = fopen("rgb565.bin", "wb");
       
       //from first header
       fread(&headfirst,sizeof(headfirst),1,fin);
    
       printf("type: %x\n",headfirst.type);
       printf("total size: %u\n",headfirst.size);
       printf("offset: %u\n",headfirst.offset);
    
       //from second header
       fread(&headsecond,sizeof(headsecond),1,fin);
    
       printf("width: %u\n",headsecond.width);
       printf("width: %u\n",headsecond.height);
       printf("bitcount(color depth): %u\n",headsecond.bitcount);
       printf("size image (image size): %u\n",headsecond.sizeimage);
    
       //size calculations
       byte_border = headsecond.sizeimage / 1024; //calculate how many bytes the image size is.
       printf("image size in bytes: %d\n", byte_border); 
       
       //BMP pixel array
       BYTE *bmp_24 = new BYTE[3];
       
       for(int i = 0; i< byte_border / 3; i++)  //should loop for byte_border/3, consequently reading all BMP pixels in the image
       {
       fread(&single_pixel,sizeof(single_pixel),1,fin);
       
       printf("red: %x\n",single_pixel.red);
       printf("green: %x\n",single_pixel.green);
       printf("blue: %x\n",single_pixel.blue);
       
       bmp_24[0] = single_pixel.red;
       bmp_24[1] = single_pixel.green;
       bmp_24[2] = single_pixel.blue;
       
       BMP24ToRGB565(bmp_24);
       
       }
       
       fclose(fin);
       fclose(fp_rgb);
       
       /* TEST PART ---> checking output rgb565 file */
       FILE *fp_test;
       fp_test = fopen("rgb565.bin","rb+");
       if(fp_test==NULL) //file doesnt exist
       {
          printf("Error opening first file");
          exit(0);
          return 0;
       }
       
        unsigned short RGB2Bytes;
        BYTE *rgb565_test = new BYTE[2];
        BYTE low;
        BYTE high;
          
       for(int i = 0; i< 20 /*(byte_border / 3) * 2 */; i++)  //should loop for byte_border/3 *2 , consequently reading all RGB pixels in the image
       {
       
       //fread(&RGB2Bytes,sizeof(RGB2Bytes),1,fp_test);
       //rgb565_test[0] = LOBYTE(RGB2Bytes);
       //rgb565_test[1] = HIBYTE(RGB2Bytes);
       //printf("\n RGB TEST - RGB2Bytes: %d\n", RGB2Bytes);
       //printf("RGB TEST - rgb565_test[0]: %x\n",rgb565_test[0]);
       //printf("RGB TEST - rgb565_test[1]: %x\n\n",rgb565_test[1]);
        
       fread(&low,sizeof(low),1,fp_test);
       fread(&high,sizeof(high),1,fp_test);
    
       printf("\nRGB TEST - low: %x\n", low);
       printf("RGB TEST - high: %x\n", high);
       
       
       }
       
       system("pause");
    
       return 0;
    
    }
    Last edited by Elric; October 22nd, 2008 at 08:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)