1

My program compiles and everything, but when I try to open up the image it says the image has "bogus header files". I'm not sure what I'm doing wrong.

Here's some of my code:

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bfold;
fread(&bfold, sizeof(bfold), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER biold;
fread(&biold, sizeof(biold), 1, inptr);

// determine padding for scanlines
int padding = (4 - (biold.biWidth * atoi(n) * sizeof(RGBTRIPLE)) % 4) % 4

// write outfile's BITMAPFILEHEADER and BITMAPINFOHEADER

BITMAPFILEHEADER bfnew;
BITMAPINFOHEADER binew;

binew.biWidth = biold.biWidth * atoi(n);
binew.biHeight = biold.biHeight * atoi(n);
binew.biSizeImage = binew.biWidth * sizeof(RGBTRIPLE) * abs(binew.biHeight) + padding * abs(binew.biHeight);
bfnew.bfSize = binew.biSizeImage + 54;

fwrite(&bfnew, sizeof(bfnew), 1, outptr);
fwrite(&binew, sizeof(binew), 1, outptr);

Thanks!

4

You're wrongly calculating binew.biSizeImage and subsequently bfnew.bfSize. I'm also not sure if padding is the new padding, but assuming it's the new padding and it's calculated correctly, on a doubly-resized 3x3 bmp, padding should be 2, binew.biSizeImage should be 120 and binew.bfSize should be 174.

You may use the staff's implementation to resize an image by executing this command

~cs50/pset5/resize n original.bmp resized.bmp

where n is the factor, orignal.bmp is the name/path of the original bmp file and resized.bmp is the name/path of the resized bmp file.

You may also use peek (a program implemented by the staff to help you compare bmp headers) by executing this command

~cs50/pset5/peek first.bmp second.bmp

where first.bmp is the name/path of the first bmp file and second.bmp is the name/path of the second bmp file.

And lastly, you may use diff to compare the bytes of two bmp files as denoted by the pset specification page by executing this command

diff resized-0.bmp resized-1.bmp

Recall that this shouldn't output anything if the files (i.e., resized-0.bmp and resized-1.bmp) are identical.


Edit 0: the main reason your bmp file couldn't be recognized by peek is that you're not really initializing bfnew and binew with bfold and biold initially. Therefore, the members of bfnew and binew actually contain garbage values.


Edit 1: unfortunately, the logic of your program is wrong. My advice to you is to get a pencil and a paper, draw a 3x3 image and to try to resize that image by any factor (like a computer :) ), writing down the steps that you've done to accomplish the task.

Then compare these steps (the correct ones) with the steps that your program executes. In other words, try to execute your program by yourself (not a computer) see what it does right and wrong and try to fix that.

Here are some useful tips (hopefully):

First, calling atoi() multiple times decreases the efficiency of your program as you're executing the code of this function again and again while you could simply store the value it returns in an int variable and use it.

Second, after reviewing your code, it appears that you've chosen to follow the seeking approach. The idea behind this approach is quite as follows

  1. for each scanline in the original bmp, store the starting position of the current scanline (involves functions like fgetpos() and fsetpos()).
  2. for each RGBTRIPLE in the current scanline, read the current RGBTRIPLE and write it n times (to resize horizontally).
  3. write the newly calculated padding.
  4. set the file position indicator to the start of the recently read scanline.
  5. repeat the steps (2-4) n times (to resize vertically).

Hope that helps!

11
  • I changed my binew.biSizeImage formula to one that makes more sense (and gives the answers you gave above). I'm still having the same problem though. :( my padding formula seems to work too (for the new padding). Do I need to specify the old padding as well? Could that be my problem?
    – dottified
    Jul 23 '14 at 21:34
  • @user1647 you may update your question with these information to be able to format them well. The old padding is already calculated in the distribution code as far as I remember. Did you compare the bytes of your bmp with the bytes of a bmp resized by the staff's program? Did you also compare between their headers?
    – kzidane
    Jul 23 '14 at 21:34
  • I tried to use peek, but it says it cannot recognize my file.
    – dottified
    Jul 23 '14 at 21:42
  • @user1647 what command did you use to run peek? Were you sure that the name of your bmp file ( and the bmp file that's resized by the staff's implementation) is correct and that it exists in the location you specified? Did you include .bmp at the end of the name of the file?
    – kzidane
    Jul 23 '14 at 21:46
  • I ran "~cs50/pset5/peek student.bmp staff.bmp" and it said "Could not recognize student.bmp".
    – dottified
    Jul 23 '14 at 21:50

You must log in to answer this question.

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