I am currently undertaking the horizontal resizing in pset4 resize. Although I believe my code is sound, it does not resize small.bmp, and instead turns it into a gray block of the same size. I am not sure why; my code seems logical and correct. If you could help me identify any issues, it would be much appreciated. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./resize n infile outfile\n");
return 1;
}
// remember filenames
string nn = argv[1];
char *infile = argv[2];
char *outfile = argv[3];
int n = atoi(nn);
if ((n < 0) || (n > 100))
{
fprintf(stderr, "n must be a positive integer less than or equal to 100.\n");
return 1;
}
// 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 BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 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;
//}
// Store original header values
int origbiWidth, origbiHeight, origpadding;
origbiWidth = bi.biWidth;
origbiHeight = bi.biHeight;
origpadding = (4 - (origbiWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// Declare new headers
BITMAPINFOHEADER newbi;
newbi = bi;
BITMAPFILEHEADER newbf;
newbf = bf;
// header redefinition
newbi.biWidth = (bi.biWidth) * n;
newbi.biHeight = (bi.biHeight) * n;
int newpadding = (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
newbi.biSizeImage = (((newbi.biWidth) * (abs(newbi.biHeight))) * sizeof(RGBTRIPLE)) + (newpadding * abs(newbi.biHeight) * sizeof(RGBTRIPLE));
newbf.bfSize = (((newbi.biWidth) * (abs(newbi.biHeight))) * sizeof(RGBTRIPLE)) + (newpadding * abs(newbi.biHeight) * sizeof(RGBTRIPLE)) + 54;
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, counter = abs(origbiHeight); i < counter; i++)
{
// iterate over pixels in scanline
for (int j = 0; j < origbiWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
for (int counter2 = 0; counter2 < n; counter2++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
//skip over padding, if any
fseek(inptr, origpadding, SEEK_CUR);
for (int k = 0; k < newpadding; k++)
{
fputc(0x00, outptr);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
Here is the peek output (STAFF)
- offset type name argv[1]
- 0 WORD bfType 4d42
- 2 DWORD bfSize 0000005a
- 6 WORD bfReserved1 0000
- 8 WORD bfReserved2 0000
- 10 DWORD bfOffBits 00000036
- 14 DWORD biSize 00000028
- 18 LONG biWidth 00000003
- 22 LONG biHeight fffffffd
- 26 WORD biPlanes 0001
- 28 WORD biBitCount 0018
- 30 DWORD biCompression 00000000
- 34 DWORD biSizeImage 00000024
- 38 LONG biXPelsPerMeter 00000b12
- 42 LONG biYPelsPerMeter 00000b12
- 46 DWORD biClrUsed 00000000
- 50 DWORD biClrImportant 00000000
Here is the peek output (MY PROGRAM)
- 0 WORD bfType 4d42
- 2 DWORD bfSize 000001e6
- 6 WORD bfReserved1 0000
- 8 WORD bfReserved2 0000
- 10 DWORD bfOffBits 00000036
- 14 DWORD biSize 00000028
- 18 LONG biWidth 0000000c
- 22 LONG biHeight fffffff4
- 26 WORD biPlanes 0001
- 28 WORD biBitCount 0018
- 30 DWORD biCompression 00000000
- 34 DWORD biSizeImage 000001b0
- 38 LONG biXPelsPerMeter 00000b12
- 42 LONG biYPelsPerMeter 00000b12
- 46 DWORD biClrUsed 00000000
- 50 DWORD biClrImportant 00000000