I am doing CS50x and just finished resize.c; I checked it with various values of enlargement and it returns the correct results. I compared this with staff solution, using diff, peek, and xxd, my results match the staff solution. However, check50 is giving showing that my solution, save for the first two criteria. I ran update50 as well and it is still giving me a problem. So far the headers are correct and I can't seem to locate any extra pixels. Can someone please look at my code and help me because this is quite frustrating.
/**
* copy.c
*
* Computer Science 50
* Problem Set 4
*
* Copies a BMP piece by piece, just because.
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: ./copy factor infile outfile\n");
return 1;
}
// remember filenames
char* factor = argv[1];
char* infile = argv[2];
char* outfile = argv[3];
int n = atoi(factor);
if (n < 1 || n > 100)
{
printf("Please enter a scale factor between one and hundred.");
return 1;
}
// open input file
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("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;
}
//"save" old variables
int height = abs(bi.biHeight);
int width = bi.biWidth;
int padding_a = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//new variables
bi.biHeight = bi.biHeight * n;
bi.biWidth = bi.biWidth * n;
int padding_b = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = (((bi.biWidth) * abs(bi.biHeight)) * 3) + (padding_b * abs(bi.biHeight));
bf.bfSize = bi.biSizeImage + 54;
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// reads each row
for (int i = 0; i < height; i++)
{
//prints each row n times
for (int j = 0; j < n; j++)
{
//read each pixel
for (int k = 0; k < width; k++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//prints each pixel n times
for (int l = 0; l < n; l++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// places the new padding at the end of each line
for (int m = 0 ; m < padding_b; m++)
{
fputc(0x00, outptr);
}
//goes back to the beginning of the orignal line
fseek(inptr,(-1 * width * sizeof(RGBTRIPLE)), SEEK_CUR);
}
fseek(inptr, sizeof(RGBTRIPLE)*(width + padding_a), SEEK_CUR);
}
/** 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 < bi.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
}
*/
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}
*NOTE the greyed out code is from copy.c which I used for easy reference.