0
// copy infile's BITMAPINFOHEADER
BITMAPINFOHEADER new_bi = bi;

// read new_bi
fread(&new_bi, sizeof(new_bi), 1, inptr);

// update biWidth of new_bi
new_bi.biWidth = new_bi.biWidth * factor;

// update biHeight of new_bi
new_bi.biHeight = new_bi.biHeight *factor;

// determine padding for scanlines in new image
int new_padding =  (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

// update biSizeImage of new_bi
new_bi.biSizeImage = ((new_bi.biWidth *  sizeof(RGBTRIPLE) + new_padding) * abs(new_bi.biHeight));

// copy infile's BITMAPFILEHEADER
BITMAPFILEHEADER new_bf = bf;

// read new_bf
fread(&new_bf, sizeof(new_bf), 1, inptr);

// update bfSize of new_bf
new_bf.bfSize = new_bi.biSizeImage + sizeof(new_bf) + sizeof(new_bi);

What am I missing here? I have hit a wall with resize and can't find out where I am going wrong. I think my problem is with the headers so here is my code. Help!

1 Answer 1

0

It's not all of your code, but I think I can make a good guess at the needed missing code. It looks like you're reading the two headers bi and bf from the input file first. Following that, you're trying to repeat the process to load new_bi and new_bf. Unfortunately, unless you reset the file pointer to the beginning of the file, you're loading the new_* structs with image data, in other words, garbage data. Following that, the code will start processing image data well into the image.

To load the correct data into the new_* structures, you have two alternatives. Either reset the file pointer to the start of the file, or simply load the new structs from the old one. It is actually simple. new_bi = bi; will copy all of the structure elements from one to the other. After that, you can alter individual elements as needed.

It is also necessary to calculate the padding for both the input and output files independently.

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

3
  • Did I not copy the structs to the new headers with the code: BITMAPINFOHEADER new_bi = bi; and BITMAPFILEHEADER new_bf = bf? Are you telling me I need to repeat this elsewhere in my code with new_bi = bi; and new_bf = bf;? Sep 29, 2016 at 1:05
  • You did, but I missed that because I was focused on the very next line: fread(&new_bi, sizeof(new_bi), 1, inptr); Same applies to new_bf. So, no, you don't have to do it again, but the additiional freads are breaking the code. Of course, this assumes that you did indeed fread the original headers into bi and bf. If something else is going on, then the previous code needs to be posted to see what it is, as well as all of the following code through the header writes.
    – Cliff B
    Sep 29, 2016 at 1:19
  • Thanks! I didn't realize that I wasn't supposed to fread the copied header. Sep 29, 2016 at 2:58

You must log in to answer this question.

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