0

I am having problem with changing file header. Specifically:

    BITMAPINFOHEADER new_bi;
    fread(&new_bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    BITMAPFILEHEADER new_bf;
    fread(&new_bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    //changing any necessary values
    new_bi.biWidth =  bi.biWidth * n;
    new_bi.biHeight=  bi.biHeight * n;
    int old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight);
    new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
    // write outfile's BITMAPINFOHEADER
    fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
    fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);

So in these lines of code, I declare new struct new_bi and new_bf belongs to BITMAPINFOHEADER and BITMAPFILEHEADER respectively, also fread() them as well.

I changed any needed value and fwrite() them into outptr file. By now it should work.

However it won't work. I got it working before, but by overwriting bi.biWidth and bi.biHeight value with *= n.

Any ideas why it just will not work this time?

Thank you.

1 Answer 1

1

I'd like to see what came before this code. Before you did the freads to load the new headers, did you reset the file pointer to the beginning of the file? If not, then it isn't reading the headers. Instead, it is reading image data into the headers. In other words, the new headers are filled with garbage data.

You need to either reset the position of the file pointer within the file, or you could simply copy the header structures directly. new_bi = bi; It is both possible and proper to copy entire structures in this way.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • I don't understand what it means by "reset the pointer to the beginning of the file" so i guess i haven't done so. Before these lines of code I have BITMAPINFO, BITMAPFILE, bi, bf, fread(&bi, sizeof(), inptr) just like normal copy.c.
    – Ha Tran
    Commented Jul 3, 2017 at 19:14
  • The file pointer var is actually a structure that contains multiple fields. One of them remembers the position in the file. When you read from a file, that position pointer advances to the first byte following the data just read. Since you didn't reposition it, the position pointer is actually set to the first byte of the image data when you tried to reread the headers and put the data into the two new header vars.
    – Cliff B
    Commented Jul 3, 2017 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .