0

I have been trying to copy the file data from one BMP file to another. However, the values that are copied are incorrect or doesn't align with the values that are shown on: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader and https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapfileheader

For Example: On The Site: bfType = BM (0x4D42) My Code: bfType = 55551

bmp.h

#include <stdint.h>

typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;

typedef struct
{
    WORD   bfType;
    DWORD  bfSize;
    WORD   bfReserved1;
    WORD   bfReserved2;
    DWORD  bfOffBits;
} 
BITMAPFILEHEADER;


typedef struct
{
    DWORD  biSize;
    LONG   biWidth;
    LONG   biHeight;
    WORD   biPlanes;
    WORD   biBitCount;
    DWORD  biCompression;
    DWORD  biSizeImage;
    LONG   biXPelsPerMeter;
    LONG   biYPelsPerMeter;
    DWORD  biClrUsed;
    DWORD  biClrImportant;
} 
BITMAPINFOHEADER;

typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} 
RGBTRIPLE;

main.c

// Public Libraries

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

// Private Libraries
#include "bmp.h"

// Global Variables
#define ONCE 1

// Start
int main(int argc, char* argv[])
{

    if (argc != 4)
    {
        printf("ERROR: Command Line\n");
        printf("EXE -> In -> Out -> Filter\n\n");
        return 1;
    }

    // Open Original Image
    FILE* Original = fopen(argv[1], "rb");
    if (Original == NULL)
    {
        printf("ERROR: Original Image Can't Be Found\n");
        printf("Check File Name\n\n");
        return 1;
    }
    else
    {
        printf("Image Found!\n\n");
    }

    // Original Image Data

    // Read Bitmap File Header
    BITMAPFILEHEADER BF;
    fread(&BF, ONCE, sizeof(BITMAPFILEHEADER), Original);
    printf("Bitmap File Header Read...\n\n");

    // Read Bitmap Info Header
    BITMAPINFOHEADER BI;
    fread(&BI, ONCE, sizeof(BITMAPINFOHEADER), Original);
    printf("Bitmap Info Header Read...\n\n");
}

I have also hardcoded every value to see what results they produced

bfType: 55551 bfSize: 1179258880 bfReserved1: 17993 bfReversed2: 256 bfOffBits: 1207959809

biSize: 18432 biWidth: 1477239551 biHeight: 1598243657 biPlanes: 21072 biBitCount: 17999 biCompression: 4541513 biSizeImage: 257 biXPelsPerMeter: 1766606860 biYPelsPerMeter: 268595054 biClrUsed: 1852637184 biClrImportant: 1196585588

  • Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman Mar 01 '23 at 18:49
  • 3
    The structures for a BMP header need to be [packed](https://stackoverflow.com/questions/3318410/pragma-pack-effect). – user3386109 Mar 01 '23 at 18:54
  • Aside: please do not use `ONCE` for `1`. It just makes the code hard to read. It's conceivable that you might want the code to work with some other size, but then the name would be misleading. – Weather Vane Mar 01 '23 at 19:09
  • 1
    More: *always* check that `fread` read the number of elements that you expected. – Weather Vane Mar 01 '23 at 19:10
  • 1
    `55551 == 0xd8ff` it looks like what you are reading is not BMP but JPG file. – MikeCAT Mar 01 '23 at 22:57

0 Answers0