0

I am working on C++ code to read and write .bmp image. Below is my code.

However, I encountered some problems that I couldn't fix. enter image description here

I have googled a lot but none of them solve my problem. Sorry if my coding style doesn't look good to you, I'm new to Xcode and C++.

Please help me. I will be really appreciated.

#include <iostream>
#include <stdio.h>

#pragma pack(2)

typedef struct                      // BMP file header structure
{
    unsigned short bfType ;         // Magic number for file
    unsigned int bfSize ;           // Size of file
    unsigned short bfReserved1 ;    // Reserved, usually set to 0
    unsigned short bfReserved2 ;    // Reserved, usually set to 0
    unsigned int bfoffBits ;        // Offset to bitmap data
}BITMAPFILEHEADER;

#pragma pack()

typedef struct
{
    unsigned int   biSize ;        // Size of info header
    int            biWidth ;       //  Width of image
    int            biHeight ;      // Height of image
    unsigned short biPlanes ;      // Number of color planes
    unsigned short biBitCount ;    // Number of bits per pixel
    unsigned int biCompression ;   // Type of compression to use, 0 if there is no compression
    unsigned int biSizeImage ;     // Size of image data
    int biXPelsPerMeter ;          // X pixels per meter
    int biYPelsPerMeter ;          // Y pixels per meter
    unsigned int biClrUsed ;       // Number of color used
    unsigned int biClrImportant ;  // Number of important color
}BITMAPINFOHEADER;


unsigned char *ReadBitmapFile(const char  *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
    FILE* file ;                        //file pointer
    BITMAPFILEHEADER bitmapFileHeader ; //bitmap file header
    unsigned char *bitmapimage ;        //store image data
    int imageIdx = 0 ;
    unsigned char tempRGB ;             //swap

    // open file in read binary mode
    file = fopen(filename, "rb");
    if (file == NULL)
        return NULL;

    // read the bitmap file header
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, file);

    // read the bitmap info header
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,file);

    //move file point to the begging of bitmap data
    fseek(file, bitmapFileHeader.bfoffBits, SEEK_SET);

    //allocate enough memory for the bitmap image data
    bitmapimage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

    //verify memory allocation
    if(!bitmapimage)
    {
        free(bitmapimage);
        fclose(file);
        return NULL;
    }

    // read in the bitmap image data
    fread(bitmapimage, bitmapInfoHeader->biSizeImage, 1, file);

    //swap the r and b value to get RGB (bitmap is BGR)
    for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3){
        tempRGB = bitmapimage[imageIdx];
        bitmapimage[imageIdx] = bitmapimage[imageIdx + 2];
        bitmapimage[imageIdx + 2] = tempRGB;
    }

    //close file and return bitmap data
    fclose(file);
    return bitmapimage;

}

BITMAPINFOHEADER bitmapInfoHeader;

unsigned char *bitmapData = ReadBitmapFile("input1.bmp", &bitmapInfoHeader);

By the way, I am using Xcode8.3.3

Arthur Hsieh
  • 13
  • 1
  • 6
  • You probably should get [a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read. You seem to be missing some very basic knowledge about C++ that a book would have told you very early. – Some programmer dude Oct 05 '17 at 03:42
  • Thank you for your comment. But from which part of my code do you see that? Can you let me know? Thanks. – Arthur Hsieh Oct 05 '17 at 05:31
  • It's partly the code, but also the error itself: Where is you `main` function? And if you don't know what it's for or why you need it then you definitely need to read some books. Also note that I linked to a list of books for C++. While almost all your code is plain C you have *one* line that makes it C++: `#include `. Even though you don't use anything else C++ specific, that single line is enough to make your code C++. Either you learn C++ properly, or you rename your file to have a `.c` ending and continue to program plain C. – Some programmer dude Oct 05 '17 at 05:35
  • Thank you! I will take a look. – Arthur Hsieh Oct 05 '17 at 06:47

0 Answers0