Have been trying to figure out what's wrong with my code, but having issues with it. Can someone please point me in the right direction?
What I'm getting with check50
:) 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 27 of pixel data doesn't match. Expected 0x00, not 0xff
:( resizes small.bmp correctly when n is 3 Byte 38 of pixel data doesn't match. Expected 0x00, not 0xff
:( resizes small.bmp correctly when n is 4 Byte 49 of pixel data doesn't match. Expected 0x00, not 0xff
:( resizes small.bmp correctly when n is 5 Byte 64 of pixel data doesn't match. Expected 0x00, not 0xff
:( 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
My code
// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4) //has to be 4
{
fprintf(stderr, "Usage: resize n infile outfile\n");
return 1;
}
int n = atoi(argv[1]); // resize factor
if (n >= 1 || n <= 100 );
// resize factor has to be higher than 1
else
{
printf("MAKE SURE RESIZE FACTOR IS EQUAL TO (OR GREATER THAN) ONE\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;
}
int oldheight = bi.biHeight;
int oldwidth = bi.biWidth;
bi.biWidth *= n;
bi.biHeight *= n;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int oldpadding = (4 - (oldwidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = (((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight));
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(oldheight); i < biHeight; i++)
{
for (int p = 0; p < n; p ++) //interating up to n factor
{
// iterate over pixels in scanline
for (int j = 0; j < oldwidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile * n to enlarge
for (int out = 0; out < n; out ++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// adding padding to outfile
for (int k = 0; k < padding; k ++)
{
fputc(0x00, outptr);
}
// skip over padding, if any
fseek(inptr, oldpadding, SEEK_CUR);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
The output I get is this. There's a weird line of white pixels in the middle and can't figure out how to fix it. Been looking at this for hours.
Thanks for the help!