tags:

views:

539

answers:

6

How do I read the bytes from a bmp file using C?

+1  A: 

ImageMagick supports BMP. You can use either of two C APIs, the low-level MagickCore or the more high level Magick Wand.

Matthew Flaschen
+4  A: 

fopen followed by fread

Barry Kelly
This answer is technically correct, and don't get me wrong, I agree 100% that it is a funny answer, and that it matches the "style" of the question posed by abhijit, but it is completely against the spirit of Stack Overflow. We are here to provide solutions to people who come seeking help. I may get trouble for saying so, but we should do everything we can to help the OP solve whatever problem they are trying to solve.I voted for Naveen and Mattthew Flaschen.
e.James
eJames - the answer to the question is indeed fopen followed by fread. Solving the problem further comes down to how to use those functions effectively, or what the on-disk format of a bitmap is, but those are distinct questions of their own. From how the question was asked, it's not clear where the roadblock is, but fopen and fread should open the door to further investigation. I wasn't trying to be funny, it was just the top question when I opened SO, so I chipped in my 2 cents. Sorry you didn't like.
Barry Kelly
A: 

Look at this Link

Aamir
+3  A: 

Use fopen and fread as suggested by others. For the format of the bmp header take a look here

Naveen
+4  A: 

Here's a general-purpose skeleton to just load a binary file, and return a pointer to the first byte. This boils down to "fopen() followed by fread()", but is a ... bit more verbose. There's no error-handling, although errors are checked for and I believe this code to be correct. This code will reject empty files (which, by definition, don't contain any data to load anyway).

#include <stdio.h>
#include <stdlib.h>

static int file_size(FILE *in, size_t *size)
{
  if(fseek(in, 0, SEEK_END) == 0)
  {
    long len = ftell(in);
    if(len > 0)
    {
      if(fseek(in, 0, SEEK_SET) == 0)
      {
        *size = (size_t) len;
        return 1;
      }
    }
  }
  return 0;
}

static void * load_binary(const char *filename, size_t *size)
{
  FILE *in;
  void *data = NULL;
  size_t len;

  if((in = fopen(filename, "rb")) != NULL)
  {
    if(file_size(in, &len))
    {
      if((data = malloc(len)) != NULL)
      {
        if(fread(data, 1, len, in) == len)
          *size = len;
        else
        {
          free(data);
          data = NULL;
        }
      }
    }
    fclose(in);
  }
  return data;
}

int main(int argc, char *argv[])
{
  int i;

  for(i = 1; argv[i] != NULL; i++)
  {
    void *image;
    size_t size;

    if((image = load_binary(argv[i], &size)) != NULL)
    {
      printf("Loaded BMP from '%s', size is %u bytes\n", argv[i], (unsigned int) size);
      free(image);
    }
  }
}

You can easily add the code to parse the BMP header to this, using links provided in other answers.

unwind
A: 

make sure this file is not compressed using RLE method. otherwise, you'll have to read from the file and dump into a buffer to reconstruct the image, after reading the header file and knowing it's dimensions.

A.Rashad