0

The output image appears to be correct, but only passes tests when resizing by 1. I tried to use peek and xxd to compare with staff implementation, my output appears to be equal.

// read infile's BITMAPFILEHEADER BITMAPFILEHEADER bf; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); // read infile's BITMAPINFOHEADER BITMAPINFOHEADER bi; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); //store old header values int width = bi.biWidth; int height = bi.biHeight; //update headers for the resized image bi.biWidth *= n; bi.biHeight *= n; bi.biSizeImage *= n * n; bf.bfSize = bi.biSizeImage + 54; // ensure infile is (likely) a 24-bit uncompressed BMP 4.0 if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || bi.biBitCount != 24 || bi.biCompression != 0) { fclose(outptr); fclose(inptr); fprintf(stderr, "Unsupported file format.\n"); return 4; } // write outfile's BITMAPFILEHEADER fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr); // write outfile's BITMAPINFOHEADER fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr); // determine padding for scanlines int padding = (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4; int outPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; //size of output line in bytes int size = bi.biWidth * sizeof(RGBTRIPLE); // iterate over infile's scanlines height = abs(height); for (int i = 0; i < height; i++) { // iterate over pixels in scanline for (int j = 0; j < width; j++) { // temporary storage RGBTRIPLE triple; // read RGB triple from infile fread(&triple, sizeof(RGBTRIPLE), 1, inptr); // stores position of output to go back later to prepare for next iteration int back = ftell(outptr); for(int k = 0; k < n; k++){ for(int l = 0; l < n; l++){ // write RGB triple to outfile fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); } // jumps line fseek(outptr, size + outPadding - n * sizeof(RGBTRIPLE), SEEK_CUR); } // backs to first unfilled pixel fseek(outptr, back + n*sizeof(RGBTRIPLE), SEEK_SET); } // skip over padding, if any fseek(inptr, padding, SEEK_CUR); // then add it back (to demonstrate how) for (int k = 0; k < outPadding; k++) { fputc(0x00, outptr); } for(int k = 0; k < n - 1; k++){ // skip lines in output fseek(outptr, size, SEEK_CUR); for (int l = 0; l < outPadding; l++) { fputc(0x00, outptr); } } } }

1 Answer 1

1

While the image data may be correct, there are two errors in header data due to incorrect calculation.

You need to run the same source file against your code and against the staff version of resize and compare the headers of the output files. If you are running your code only and manually calculating what values should result, you are probably making the same mistake in your manual calculations as the code is making, so it looks right to you.

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

2
  • How can i compare my output with output from staff implementation?
    – thomas
    Commented Aug 25, 2016 at 13:15
  • A staff version of the executable is provided (see pset instructions). Run their version and your version with the same input file and compare the two output files with the peek and xxd tools.
    – Cliff B
    Commented Aug 25, 2016 at 19:02

You must log in to answer this question.

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