Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

BMP in c

  1. Jun 3, 2010 #1
    Hello every body :
    i am very new to programming ,i am trying to write a programme that will work on the LSB of each byte in a 24 bit picture.
    now my question is how to treat a .bmp file ,i mean can i consider it as an array of integers with leth (for example)800x600.
    and start to changing the LSB of bytes after a specific element (0x36 where the image datas strat) or i should define some structure for keeping the different parts of the bmp like header and...

    or i should treat it as a binary file or a text file?
  2. jcsd
  3. Jun 3, 2010 #2


    User Avatar

    Staff: Mentor

  4. Jun 3, 2010 #3


    Staff: Mentor

    A .bmp file is a binary file, not a text file. And you don't want an array of integers, since they won't align with the data in your file, which is 24 bits/pixel, probably with 8 bits each for red, green, and blue. The int data type in C is typically 32 bits, but might be 16 bits for an older compiler.
  5. Jun 3, 2010 #4
    I know the internal structure of .bmp and i know that it is made of headers which contains information about the .bmp like width,height ,bitdepth.
    but my problem is that how to open it into my program .i have seen lots of codes on the internet that are named for example "bitmaploader" or thing like this which are concerned with complicated structures and use of I/O functions which are far from the contents of what i have learned .my knowledge of programming is very little and i got confused when i go through them can any one give me some make it more clear for me .
  6. Jun 3, 2010 #5


    User Avatar

    Staff: Mentor

    Sorry, these are not things that can be done without some knowledge. If you don't understand code that you see, you probably need to learn more before trying.

    You have to treat the file as binary, open it, read part of the content into header structure, then the rest into allocated memory.
  7. Jun 3, 2010 #6
    in page http://en.wikipedia.org/wiki/BMP_file_format" [Broken] at the bottom ther is a table which draws the contents of the a 2x2 bmp pictures
    the are in 0x :42 4D 46 00 00 00 00 00 36 00 ................00 FF 00 00 00
    so they are seperate numbers stored in bytes,and if i (for example) want to modify the LSB of pixels
    i start to modifying from the 0x36th byte is it legal?
    Last edited by a moderator: May 4, 2017
  8. Jun 3, 2010 #7
    Dear Borek
    Can you translate the last sentence into C language i would be very thankful:blushing:
  9. Jun 3, 2010 #8


    User Avatar

    Staff: Mentor

    This is your lucky day, I found something almost ready on my disk. This can be my unlucky day, as I guess posting it I risk of being bombarded with tons of questions about the code. So here goes disclaimer - this is part of an old project, part that I have not tested now. Basically I am posting it 'as is' - I can't guarantee it will work nor I I can't guarantee I will answer any questions you may ask.

    Code (Text):
    #include "stdafx.h"

    int main(int argc, char* argv[])
        FILE *f;
        BITMAPINFOHEADER bmphdr;
        BITMAPFILEHEADER bmpfhdr;
        char *pBMP,*pch;
        int iBW;

    // open file
        if ((f = fopen("name.bmp","rb")) == NULL) return -1;
    // read headers
    // width of the picture as stored in memory - note, it works only for RGB pictures
        iBW = (3*bmphdr.biWidth + 3) & (0xFFFFFFFF ^ 0x3);

    // allocate memory for image data
        pBMP = new char[iBW*bmphdr.biHeight];
    // read image data

    // calculate pointer to point at coordinates (100,100)
        int x = 100, y = 100;
        pch = pBMP + 3*x + (bmphdr.biHeight-y-1)*iBW;


        delete pBMP;

        return 0;
    To save file to disk write same data in the same order to file.

    Note that you may expect problems with signed/unsigned char when trying to modify picture in memory, I don't remember what were compiler settings at the time.
  10. Jun 3, 2010 #9
    Thank you Borek for this piece of code.i will go to implement it to what i want.and don't worry i will not bombard you with questions until i read it (the codes)carefully and check my courses stuff.but be ready !
  11. Jun 4, 2010 #10
    Code (Text):

    what is wrong with this code???

    typedef unsigned long DWORD;
    typedef unsigned short WORD;

    typedef struct {
        int Signature;
        DWORD Size;
        DWORD Reserved;
        DWORD BitsOffset;

    typedef struct {
        DWORD HeaderSize;
        DWORD Width;
        DWORD Height;
        WORD Planes;
        WORD BitCount;
        DWORD Compression;
        DWORD SizeImage;
        DWORD PelsPerMeterX;
        DWORD PelsPerMeterY;
        DWORD ClrUsed;
        DWORD ClrImportant;


    #include <stdio.h>
        FILE * fp;
        BITMAP_FILEHEADER bmpheader;
        BITMAP_HEADER bmpinfheader;
        int raw_data_size;
        char *pixel;

        //openning file

            printf("error in opennig file");

        fread(&bmpheader,sizeof(bmpheader),1,fp);//reads the header
        fread(&bmpinfheader,sizeof(bmpinfheader),1,fp);//reads the info header

        raw_data_size=bmpheader.SizeImage;//stores the size of raw bitmaps

        pixel=new char [raw_data_size];//create an array for holding the pixels

        fread(pixel,sizeof(char),raw_data_size,fp);//reads them from memory to aray

        printf("%x",bmpheader.Signature);//prints 424D

        return 0;

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook