reading BMP files and pixels' colours

This is a discussion on reading BMP files and pixels' colours within the C++ Programming forums, part of the General Programming Boards category; I want to write a function that can return the RGB value of a (x,y) pixel. I want it to ...

  1. #1
    Registered User Xavier's Avatar
    Join Date
    Mar 2005
    Posts
    3

    reading BMP files and pixels' colours

    I want to write a function that can return the RGB value of a (x,y) pixel. I want it to change it to a string that is interpreted in *.html files. For example #000000 stands for black and #FFFFFF makes white but you probably already know that.

    Yesterday I've written (with a little bit of help from my friend) this code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    int main()
    {
        int Width = 0;
        int Height = 0;
        int BitCount = 0;
        
        ifstream bmpFile;
        
        BITMAPFILEHEADER bmpFileHead;
        BITMAPINFOHEADER bmpInfoHead;
        
        bmpFile.open("image.bmp", ios::binary | ios::in);
        
        if( !bmpFile.is_open() )
            cout << "\nError loading image file.";
        else
            cout << "\nImage file loaded.";
        
        bmpFile.read( (char*)&bmpFileHead.bfType, 14 );
        
        if(bmpFileHead.bfType!=0x4d42) // 0x4d42 is BM
        cout << "\nWrong file format.";
        
        bmpFile.read( (char*)&bmpInfoHead, sizeof(BITMAPINFOHEADER) );
    
        if(bmpInfoHead.biCompression!=BI_RGB)
        cout << "\nSupports only BI_RGB files, sorry.";
        
        Width = bmpInfoHead.biWidth;
        Height = bmpInfoHead.biHeight;
        BitCount = bmpInfoHead.biBitCount;
        
        cout << "\nWidth: " << Width;
        cout << "\nHeight: " << Height;
        cout << "\nBits per pixel: " << BitCount;
    
        cin.get();
        return 0;
    }
    I've created the bitmap and it works good because it return valid height, width and bits per pixel values. Have you got any idea about what my next step should be?

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Maybe these perhaps?

    http://www.brackeen.com/home/vga/bitmaps.html

    http://www.lunatic.no/img2aschtml.php

    I'm assuming you can read each pixel one by one.

    For example, if the file was a 32x 64 pixels. You could create a nested for loop...

    Code:
    for(y=0;y<64;y++)
    {
      for(x=0;x<32;x++)
      {
        GetPixelInfo(x,y);
       }
    }
    And then use that info to find to closest match to the colour in html?

    I'm sure the game and win32 programers use bitmaps all the time. They might be able to provide something more useful.
    Last edited by treenef; 12-16-2005 at 04:45 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    i have found the next code to load the bmp by (Justin Deltener); I have modifyed a little to make more comprensive to me:
    Code:
    #include <windows.h>
    #include <conio.h>
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    
    #define MAX_COLORS 256
    
    void e(int n)
    {
    cout<<"Error "<<n<<endl;
    getch();
    }
    
    BITMAPFILEHEADER fh;
    BITMAPINFOHEADER ih;
    
    struct PALETA
    {
    unsigned char blau;     
    unsigned char verd;     
    unsigned char roig;        
    unsigned char res;   
    PALETA()
        {blau=verd=roig=0;
        res=0;  
        }
    };
    
    PALETA paleta[MAX_COLORS];
    
    int main()
    {
    FILE *arx;
    if(!(arx=fopen("imatge2.bmp","rb"))) {e(0);return 0;}
    if((fread(&fh,sizeof(BITMAPFILEHEADER),1,arx))==-1) {e(1);return 0;}
    if((fread(&ih,sizeof(BITMAPINFOHEADER),1,arx))==-1) {e(1);return 0;}
    printf("%d x %d\n\n",ih.biWidth,ih.biHeight);
    
    for(int l=0;l<MAX_COLORS;l++)
        {
        if((fread(&paleta[l],sizeof(PALETA),1,arx))==-1) {e(2);return 0;}
        }
    int nc=MAX_COLORS;
    if(ih.biClrImportant!=0) {nc=ih.biClrImportant;}
    cout<<"PALETA"<<endl;
    for(int l=0;l<nc;l++)
        {
        printf("%d %d %d ",paleta[l].roig,paleta[l].verd,paleta[l].blau);
        printf("(%x%x%x)--",paleta[l].roig,paleta[l].verd,paleta[l].blau);
        }
    
    long tmapa=ih.biWidth*ih.biHeight;
    long bloc=(ih.biHeight+3)&(~3);
    
    printf("\ntmapa: %d, bloc: %d\n",tmapa,bloc);
    
    unsigned long amp,alt;
    amp=(unsigned long)ih.biWidth;
    alt=(unsigned long)ih.biHeight;
    
    unsigned char *mapa=new unsigned char[tmapa];
    if((fread((unsigned char*)mapa,(long)tmapa,1,arx))== -1) {e(3);return 0;}
    for(int q=alt;q>=0;q--)
        {
        printf("%d(%x)\n",alt-q,mapa[q]);
        }
    
    
    fclose(arx);
    getch();
    return 0;
    }
    Take a look to it: first read the file and info headers. Filter the params if you want (the 'if(bmpFileHead.bfType!=0x4d42)' etc...). Right then read the palette header to see what colors are involved in it. In the sample I assume the will have a max of 256 colors. Then calcule the map size and read it from the file.
    Here suppose that with the treenef links you will be able to find aout how to relate each position of the map with its color from the palette.
    Take a look at your 'Graphics File Formats: Bitmap-File Formats', there's a description about which colors read from the palette dependiong on the biBitCount value.
    A very little help, I haven't more time to work with it )
    Regards
    Niara (jbosch(vosk))

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    ah, to get the rgb value:
    Definition: "24 Bitmap has a maximum of 2^24 colors. Each 3-byte sequence in the bitmap array represents the relative intensities of red, green, and blue, respectively, for a pixel."
    Here I have supposed that the bmp file is 24 bits so each 3 bytes will represent a rgb value, like (from my last post):
    Code:
    int c=0;
    for(int q=tmapa;q>=0;q--)
        {
        printf("%x ",mapa[q]);
        c++;
        if(c>2) {cout<<endl;c=0;}
        }
    you can sprintf it on a string and get the html rgb value.
    for other biBitCount (from wotsit.org -> Graphics File Formats: Bitmap-File Formats):
    Code:
    1       Bitmap is monochrome and the color table contains two entries. Each
    bit in the bitmap array represents a pixel. If the bit is clear, the pixel is
    displayed with the color of the first entry in the color table. If the bit is
    set, the pixel has the color of the second entry in the table.
    
    4       Bitmap has a maximum of 16 colors. Each pixel in the bitmap is
    represented by a 4-bit index into the color table. For example, if the first
    byte in the bitmap is 0x1F, the byte represents two pixels. The first pixel
    contains the color in the second table entry, and the second pixel contains
    the color in the sixteenth table entry.
    
    8       Bitmap has a maximum of 256 colors. Each pixel in the bitmap is
    represented by a 1-byte index into the color table. For example, if the first
    byte in the bitmap is 0x1F, the first pixel has the color of the
    thirty-second table entry.
    
    24      Bitmap has a maximum of 2^24 colors. The bmiColors (or bmciColors)
    member is NULL, and each 3-byte sequence in the bitmap array represents the
    relative intensities of red, green, and blue, respectively, for a pixel.
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. Replies: 4
    Last Post: 05-04-2007, 02:09 AM
  3. Problem reading BMP
    By Mortissus in forum C Programming
    Replies: 4
    Last Post: 02-02-2006, 06:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21