Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problems reading Bitmaps

Problems reading Bitmaps

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manfr3d
    wrote on last edited by
    #1

    Hello guys, I'm trying to write a funtion to read a simple bitmap, but I've encountered a serious problem. As model I used the description of the bitmap file format from wikipedia http://en.wikipedia.org/wiki/BMP_file_format[^] and as far as I know this is definitely the correct format. I also used the same 2x2 24-bit bitmap[^] to test my function, but except for the first value (the magic number or identifier) all other read values are wrong.

    #include <stdint.h>
    #include <stdio.h>

    /*Data types-->*/
    typedef uint8_t BYTE;
    typedef uint16_t BYTE2;
    typedef uint32_t BYTE4;
    /*<--Data types*/

    /*Bitmap file header-->*/
    typedef struct BMP_FILE_HEADER
    {
    BYTE2 MagicNumber;
    BYTE4 FileSize;
    BYTE2 Reserved1;
    BYTE2 Reserved2;
    BYTE4 Offset;
    } BMP_FILE_HEADER;
    /*<--Bitmap file header*/

    /*Bitmap information header-->*/
    typedef struct BMP_INFO_HEADER
    {
    BYTE4 HeaderSize;
    BYTE4 ImageWidth;
    BYTE4 ImageHeight;
    BYTE2 NumberOfColorPanels;
    BYTE2 ColorDepth;
    BYTE4 Compression;
    BYTE4 ImageSize;
    BYTE4 HorizontalResolution;
    BYTE4 VerticalResolution;
    BYTE4 NumberOfColors;
    BYTE4 NumberOfImportantColors;
    } BMP_INFO_HEADER;
    /*<--Bitmap information header*/

    /*Virtual bitmap-->*/
    typedef struct BITMAP
    {
    BMP_FILE_HEADER bmpFileHeader;
    BMP_INFO_HEADER bmpInfoHeader;
    BYTE* bmpData;
    } BITMAP;
    /*<--Virtual bitmap*/

    /*Compression types and values-->*/
    #define BI_RGB 0
    #define BI_RLE8 1
    #define BI_RLE4 2
    #define BI_BITFIELDS 3
    #define BI_JPEG 4
    #define BI_PNG 5
    /*<--Compression types and values*/

    BITMAP readbmp(const char* _pcFileName)
    {
    FILE* bmp = fopen(_pcFileName, "rb");

    BITMAP bitmap;
    fread(&bitmap.bmpFileHeader, sizeof(BMP\_FILE\_HEADER), 1, bmp);
    
    fread(&bitmap.bmpInfoHeader, sizeof(BMP\_INFO\_HEADER), 1, bmp);
    
    fseek(bmp, bitmap.bmpFileHeader.Offset, SEEK\_SET);
    
        bitmap.bmpData = (BYTE\*)malloc(bitmap.bmpInfoHeader.ImageSize);
    fread(bitmap.bmpData, bitmap.bmpInfoHeader.ImageSize, 1, bmp);
    
    fclose(bmp);
    
    return bitmap;
    

    }

    To test my function i wrote a short program, which reads the above mentioned bitmap.

    #include <iostream>
    #include "readbmp.h"

    C 1 Reply Last reply
    0
    • M Manfr3d

      Hello guys, I'm trying to write a funtion to read a simple bitmap, but I've encountered a serious problem. As model I used the description of the bitmap file format from wikipedia http://en.wikipedia.org/wiki/BMP_file_format[^] and as far as I know this is definitely the correct format. I also used the same 2x2 24-bit bitmap[^] to test my function, but except for the first value (the magic number or identifier) all other read values are wrong.

      #include <stdint.h>
      #include <stdio.h>

      /*Data types-->*/
      typedef uint8_t BYTE;
      typedef uint16_t BYTE2;
      typedef uint32_t BYTE4;
      /*<--Data types*/

      /*Bitmap file header-->*/
      typedef struct BMP_FILE_HEADER
      {
      BYTE2 MagicNumber;
      BYTE4 FileSize;
      BYTE2 Reserved1;
      BYTE2 Reserved2;
      BYTE4 Offset;
      } BMP_FILE_HEADER;
      /*<--Bitmap file header*/

      /*Bitmap information header-->*/
      typedef struct BMP_INFO_HEADER
      {
      BYTE4 HeaderSize;
      BYTE4 ImageWidth;
      BYTE4 ImageHeight;
      BYTE2 NumberOfColorPanels;
      BYTE2 ColorDepth;
      BYTE4 Compression;
      BYTE4 ImageSize;
      BYTE4 HorizontalResolution;
      BYTE4 VerticalResolution;
      BYTE4 NumberOfColors;
      BYTE4 NumberOfImportantColors;
      } BMP_INFO_HEADER;
      /*<--Bitmap information header*/

      /*Virtual bitmap-->*/
      typedef struct BITMAP
      {
      BMP_FILE_HEADER bmpFileHeader;
      BMP_INFO_HEADER bmpInfoHeader;
      BYTE* bmpData;
      } BITMAP;
      /*<--Virtual bitmap*/

      /*Compression types and values-->*/
      #define BI_RGB 0
      #define BI_RLE8 1
      #define BI_RLE4 2
      #define BI_BITFIELDS 3
      #define BI_JPEG 4
      #define BI_PNG 5
      /*<--Compression types and values*/

      BITMAP readbmp(const char* _pcFileName)
      {
      FILE* bmp = fopen(_pcFileName, "rb");

      BITMAP bitmap;
      fread(&bitmap.bmpFileHeader, sizeof(BMP\_FILE\_HEADER), 1, bmp);
      
      fread(&bitmap.bmpInfoHeader, sizeof(BMP\_INFO\_HEADER), 1, bmp);
      
      fseek(bmp, bitmap.bmpFileHeader.Offset, SEEK\_SET);
      
          bitmap.bmpData = (BYTE\*)malloc(bitmap.bmpInfoHeader.ImageSize);
      fread(bitmap.bmpData, bitmap.bmpInfoHeader.ImageSize, 1, bmp);
      
      fclose(bmp);
      
      return bitmap;
      

      }

      To test my function i wrote a short program, which reads the above mentioned bitmap.

      #include <iostream>
      #include "readbmp.h"

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      This is because the compiler will add padding-zeros in the structure to align the different members on a specific boundary. You can read more about the subject here[^] (or google to find additional information). To disable that, you have to pack your structure on a 1 byte boundary using #pragma pack(1). See here[^]. On the other hand, why are you writing the code to read a bitmap ? There are already quite some libraries which can do that, so why reinventing the wheel ? (unless for learning purpose, in which case it is understandable).

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      M 1 Reply Last reply
      0
      • C Cedric Moonen

        This is because the compiler will add padding-zeros in the structure to align the different members on a specific boundary. You can read more about the subject here[^] (or google to find additional information). To disable that, you have to pack your structure on a 1 byte boundary using #pragma pack(1). See here[^]. On the other hand, why are you writing the code to read a bitmap ? There are already quite some libraries which can do that, so why reinventing the wheel ? (unless for learning purpose, in which case it is understandable).

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        M Offline
        M Offline
        Manfr3d
        wrote on last edited by
        #3

        Learning purpose ... not really. I know that there are a lot of libraries out there, which can load bitmaps. Actually I was just bored to death and wanted to do something. And I wanted a bitmap structure/class which gives me full control over all parameters and single values. Oh, and thanks, the parameters are now displayed correctly. There is just one thing I do not understand: The value which contains the image size is always 0x00.

        J 1 Reply Last reply
        0
        • M Manfr3d

          Learning purpose ... not really. I know that there are a lot of libraries out there, which can load bitmaps. Actually I was just bored to death and wanted to do something. And I wanted a bitmap structure/class which gives me full control over all parameters and single values. Oh, and thanks, the parameters are now displayed correctly. There is just one thing I do not understand: The value which contains the image size is always 0x00.

          J Offline
          J Offline
          Jonathan Davies
          wrote on last edited by
          #4

          Manfr3d wrote:

          There is just one thing I do not understand: The value which contains the image size is always 0x00

          It may be you won't need to use image-size, just width and height, and so don't need to worry too much about it. A quick search of (a limited amount of) code on my laptop that deals with bitmaps did not find it being used. Though Width and Height occur all the time as in a call to, for example, the Win32 function:

          BitBlt(hDC,xDest,yDest,bmInfo.biWidth,bmInfo.biHeight,hDCMem,0,0,SRCCOPY);

          M 1 Reply Last reply
          0
          • J Jonathan Davies

            Manfr3d wrote:

            There is just one thing I do not understand: The value which contains the image size is always 0x00

            It may be you won't need to use image-size, just width and height, and so don't need to worry too much about it. A quick search of (a limited amount of) code on my laptop that deals with bitmaps did not find it being used. Though Width and Height occur all the time as in a call to, for example, the Win32 function:

            BitBlt(hDC,xDest,yDest,bmInfo.biWidth,bmInfo.biHeight,hDCMem,0,0,SRCCOPY);

            M Offline
            M Offline
            Manfr3d
            wrote on last edited by
            #5

            Yeah, I think you're right. However, the image size would make memory allocation easier.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups