reading bitmap into array

This is a discussion on reading bitmap into array within the C Programming forums, part of the General Programming Boards category; I have a question regarding reading a bitmap image into an array. I understand that usually the first 54 bytes ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    14

    reading bitmap into array

    I have a question regarding reading a bitmap image into an array. I understand that usually the first 54 bytes of the bitmap is for the header information, and basically I need to be able to read the whole image into the array, alter the non-header parts to encrypt the image and and write the array back out into a new bitmap image. I'm not sure how to extract the height and width information nor how the reading of the bytes to an array works.

    For example, I have
    Code:
    typedef struct {
      char* data;
      char* header;
      int height;
      int width;
      int size;
    } Bitmap;
    and I want to be able to do this
    Code:
    /* The following function takes a file name and an address of
       a character array and read the file of characters into the
       array. Array and size are passed by their addresses
       Reads the BMP image from a file to an array
    */
    void readImage(char* infile, Bitmap *bitmap){
      FILE * input = fopen(infile, "br");
    
    }
    Currently I'm a little clueless and have only gotten as far as opening the file in binary, can anyone give me a few hints as to how I go about this? Thanks a lot in advance.
    Last edited by kallistine; 07-26-2009 at 09:29 AM. Reason: code tags

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Several recent threads discussed reading BMP files - search the board.

    And don't forget to use code tags in future.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    14
    I searched back a few pages but didn't find anything, could you direct me to some of those pages? Thanks a lot.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Showing results 1 to 25 of 77
    Search took 0.73 seconds. Search: Keyword(s): BMP
    time limited offer
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    14
    Thanks for the links, I looked into them and they really seem very complicated. I think I'm supposed to be doing something much simpler.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    You can get an idea from those threads what they are doing and try writing your own code to begin with. If you falter in between just post the code till you have gotten so far and you will be helped with.

    Dont expect that you will get an exact replica of the program what you are trying to do.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Bitmap Format.

    Start reading!

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Why do you need the width/height anyway?
    Keep 54 bytes, encrypt the rest - easy?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    14
    that's actually what I was thinking, but I'm not sure about the struct, it seems like those things need to be declared at some point.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kallistine View Post
    that's actually what I was thinking, but I'm not sure about the struct, it seems like those things need to be declared at some point.
    The data is in the header. The header is the first thing in the image file. Do you understand how to read the information from the header (Y/N)?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    14
    Some links have been given to me with good information on the header, but the way my assignment reads, the header part doesn't need to be encrypted, and should just be copied back to the new image, so it's unclear to me why I need to know the exact height and width of the image.

    My question is mainly regarding how to read the bytes into the array, and where to pass it to. Should I read the first 54 bytes to the 'header' struct? I'm not sure what the bitmap struct is doing either. I'm assuming I should be using fread to get the bytes but really not sure where things should be put.

    I don't understand why the readImage function needs the char* infile and the Bitmap, the infile is necessary to read the correct file, but what its the bitmap for? Why isn't one of the parameters the array? Or is the bitmap the array?

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kallistine View Post
    My question is mainly regarding how to read the bytes into the array, and where to pass it to. Should I read the first 54 bytes to the 'header' struct?
    That might work, but it's not a good idea. You need to dissemble the header properly if you are going to put data from it into a struct. Altho from the looks of things, you are not using a "header" struct. You are using a struct for the whole bitmap, with a ptr to the header in it.

    I'm not sure what the bitmap struct is doing either. I'm assuming I should be using fread to get the bytes but really not sure where things should be put.

    I don't understand why the readImage function needs the char* infile and the Bitmap, the infile is necessary to read the correct file, but what its the bitmap for? Why isn't one of the parameters the array? Or is the bitmap the array?
    The Bitmap is the struct. Without dealing with width and length yet, the idea would be to copy the data fread'd into a malloc'd array, then Bitmap->header points to array[0] and Bitmap->data to array[54], since that is where the actual image data begins.
    Last edited by MK27; 07-26-2009 at 11:40 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    14
    So the struct of the bitmap is like this, and I'm not sure how to get height and width if I don't read specifics in the header.
    Code:
    typedef struct {
      char* data;
      char* header;
      int height;
      int width;
      int size;
    } Bitmap;
    Here is what I have so for for readImage, I'm not sure if I'm tracking the size correctly or freading correctly, I looked at the man page but it's still confusing.
    Code:
    void readImage(char* infile, Bitmap *bitmap){
      int i;
      FILE* input = fopen(infile, "rb");
      char* array = malloc(sizeof(n)); /*n is defined earlier to be 10000*/
      for(i=0; i<n; i++){
        fread(&array[i], 1, 1, input);
        bitmap->size +=1;
        if((bitmap->size)==n){
          array = realloc(array, (2*n)); /*I'm supposed to double if n isn't enought*/
        }
      }
      bitmap->header = &array[0];
      bitmap->data = &array[54];
      fclose(input);
    }
    Last edited by kallistine; 07-26-2009 at 12:30 PM. Reason: changes

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Why do you need the width and height?

    - read 54 bytes
    - write 54 bytes
    - read the rest
    - encrypt
    - write the rest

    No need for a structure, no need to interpret the data - just do it?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    14
    I think I can agree with you now Salem. Do you think then my readImage function is written okay? So now in my encrypt function I just start applying changes to the bytes starting from array[54]?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 05:45 AM
  3. Reading integers from a File into an Array
    By bobby19 in forum C Programming
    Replies: 13
    Last Post: 10-09-2006, 01:36 AM
  4. Reading a file into an array for fast access later
    By matsharp in forum C Programming
    Replies: 10
    Last Post: 08-03-2006, 02:42 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

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