Thread: Bitmap in C programming

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Question Bitmap in C programming

    Pls tell me how can i produce a code that will write lets say a line or rectangle in a bitmap file. I understand that i need to have a header file which I already had written. What i do not understand is how can I exactly write to a binary file like a bitmap. I need explanations on how can i work out the problem. thank you.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    use fopen to open the file. If you want to read as well as write use "r+b" as the second parameter. You can then use fseek to get where you want in the file. If you are writing multibyte data then I think you will also want to cast it to a character array before writing, and you may need to reverse it when reading IIRC.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    53
    It depends in what format the bitmap file is. Many image formats can be read/written with freely available libraries (libjpeg, libpng, libtiff); other formats are relatively simple and can be decoded and encoded "manually".

    I don't understand your remark about a header file.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    what i know is that each bitmap file has a file header which contains the size of the image, the bits per pixel etc.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://en.wikipedia.org/wiki/BMP_file_format
    "draw" your line in a large array of pixels, then write them out the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    a header file is source code. (stdio.h, stdlib.h, etc)

    a file header is data that goes before other data in a file.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by aaron08 View Post
    what i know is that each bitmap file has a file header which contains the size of the image, the bits per pixel etc.
    That's what all but the most trivial bitmap file formats have. It depends on the exact file format (i.e., is it a BMP file? GIF, JPEG, TIFF, PNG?) what the precise layout of this file header is. Some file formats are relatively simple and you can read the bitmap data as a simple array of pixels, others have some form of compression and are going to be difficult to parse. Fortunately, like I said, there are often libraries available; or perhaps your OS has built-in functions to deal with these files.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, but GIF, JPEG, TIFF and PNG are not bitmaps. Bitmaps should have "BM" (edit: in ASCII) as the first two bytes in the header. Palleted bitmaps can complicate things tho.
    Last edited by mike_g; 07-28-2008 at 04:40 PM.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    53
    That depends on what your definition of "bitmap" is. If you mean "the contents of a BMP file" then you are right. But you could also mean "a rectangular array of pixel values" (as opposed to a "vector image"). It wasn't clear to me from the original poster what he meant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  2. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  3. Loading a Bitmap resource for OpenGL Texture[x]
    By the dead tree in forum Game Programming
    Replies: 4
    Last Post: 08-26-2004, 01:12 PM
  4. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 10:07 AM
  5. texture is all white in opengl!
    By Crossbow in forum Game Programming
    Replies: 7
    Last Post: 03-31-2002, 11:54 AM

Tags for this Thread