I am receiving the below when I check Pset4:
:) resize.c and bmp.h exist.
:) resize.c compiles.
:) doesn't resize small.bmp when n is 1
:( resizes small.bmp correctly when n is 2
Byte 22 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes small.bmp correctly when n is 3
Byte 30 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes small.bmp correctly when n is 4
Byte 38 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes small.bmp correctly when n is 5
Byte 50 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes large.bmp correctly when n is 2
Byte 313 of pixel data doesn't match. Expected 0x00, not 0xff
:( resizes smiley.bmp correctly when n is 2
Byte 55 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes smiley.bmp correctly when n is 3
Byte 82 of pixel data doesn't match. Expected 0xff, not 0x00
Here is my code:
// Implements a program resize that enlarges 24-bit uncompressed BMPs by a factor of n
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: Improper number of command-line arguments\n");
return 1;
}
// Resize by factor of n; 0 < n >= 100
int n = atoi(argv[1]);
if (n < 1 || n > 100)
{
fprintf(stderr, "Resize of n must be a positive integer and cannot be greater than 100\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 3;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// 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;
}
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// Update width and height for new BITMAPHEADER | old = bi.biWidth and bi.biHeight
bi.biWidth *= n;
bi.biHeight *= n;
// Update padding for outfile
int newPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// Update BITMAPHEADERINFO: change image size for outfile | old = bi.biSizeImage
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + newPadding) * abs(bi.biHeight);
// Update BITMAPFILEHEADER: change file size for outfile | old = bi.biSize
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// iterate over pixels in scanline
for (int j = 0; j < n; j++)
{
// Set pointer to beginning of file
fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE) + padding * i, SEEK_SET);
// Iterate over pixels in scanline
for (int k = 0; k < bi.biWidth; k++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// Iterate over pixels in scanline n times
for (int l = 0; l < n; l++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// then add it back (to demonstrate how)
for (int m = 0; m < padding; m++)
{
fputc(0x00, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
Any helps is appreciated!