0

I'm trying to read a Bitmap file in the C++ way. This file has two headers (a file header and an image header). I succeed to read both of them. But now I'm trying to read data, the same way I red the headers, I fail. When I use the C way to read binary files everything is fine. Here is the codes: im_bmp.cpp

#include <iostream>
#include <fstream>
#include "im_bmp.h"

using namespace std;

void read_bmp(const char* f)
{
   pict I; unsigned char pix[3]; px pxl; int i = 0;

   FILE* fl = fopen(f, "rb");
   fread(&I, sizeof(pict), 1, fl);
   printf("%d\n%d\n",I.im.bpp,I.fhd.f_off); // A test to show the bit per pixel and the offset(where image data begin)

   while(i<2)
   {
       fread(&pix,1,3,fl);
       printf("%d %d %d ",pix[2],pix[1],pix[0]); // A test to show the first two pixels
       i++;
   }

// The code below read binary files in the C++ way
/*
   ifstream ifs;
   ifs.open(f,ios::binary);
   ifs.read((char *)&I,sizeof(pict));
   cout << I.im.bpp << endl;           // It works here. It's headers
   ifs.read((char *)&pxl,sizeof(pxl));
   cout << pxl.r << endl;              // It fails here to read the first pixel
*/
   fclose(fl);
}

im_bmp.h

#ifndef IM_BMP_H_INCLUDED
#define IM_BMP_H_INCLUDED

#include <iostream>
#include <fstream>
#pragma pack(1)

using namespace std;
/*
typedef int int32;
typedef short int16;

typedef struct px
{
    unsigned char r, g, b;
} px;

typedef struct pict
{
    int w, h;
    px dt;
} pict;

struct im_hd
{
    int32 hd_sz;
    int32 wdt;
    int32 hgt;
    int16 im_pl;
    int16 bpp;
    int32 cmp;
    int32 im_sz;
    int32 hr;
    int32 vr;
    int32 clr;
    int32 mclr;
};

struct fl_hd
{
    char hd[2];
    int32 sz;
    int32 rsv;
    int32 f_off;
    im_hd im;
};
*/

struct im_hd
{
    int hd_sz;
    int wdt;
    int hgt;
    short im_pl;
    short bpp;
    int cmp;
    int im_sz;
    int hr;
    int vr;
    int clr;
    int mclr;
};

struct fl_hd
{
    char hd[2];
    int sz;
    int rsv;
    int f_off;
    //im_hd im;
};

typedef struct px
{
    unsigned char r, g, b;
} px;

typedef struct pict
{
    fl_hd fhd;
    im_hd im;
    //int w, h;
    //px* dt;
} pict;

void read_bmp(const char* f);

#endif // IM_BMP_H_INCLUDED 

I try to do this in C++ but it doesn't work:

   ifstream ifs;
   ifs.open(f,ios::binary);
   ifs.read((char *)&I,sizeof(pict));
   cout << I.im.bpp << endl;           // It works here. It's headers
   ifs.read((char *)&pxl,sizeof(pxl));
   cout << pxl.r << endl;              // It fails here to read the first pixel
2
  • Have you checked the internal flags of ifs ?
    – Jaffa
    Oct 10, 2013 at 9:01
  • You say "it fails", but what output are you having ? What are you expecting ?
    – zakinster
    Oct 10, 2013 at 9:26

1 Answer 1

1

Your C++ code works perfectly fine. It prints you a weird character because cout interprets it as a char :

cout << pxl.r << endl; 

Try to cast it as an int in order to see the integer value :

cout << static_cast<int>(pxl.r) << endl; 

Although, you may have enforce the structure alignment using #pragma pack, it may be safer to read the pixel without taking the structure size into account :

ifs.read((char *)&pxl.r, sizeof (unsigned char));
ifs.read((char *)&pxl.g, sizeof (unsigned char));
ifs.read((char *)&pxl.b, sizeof (unsigned char));
2
  • 2
    A great example on why "It fails here" is not a sufficient problem description.
    – Adam
    Oct 10, 2013 at 9:04
  • Thank you Zakinster, that was exactly my problem, and you solved it.
    – Patrik
    Oct 10, 2013 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.