I am stuck since I have no idea what is wrong with my code. I believe my resizing code is correct. I think I'm not changing the headers correctly, which is giving me this image:
Also, i think it doesn't resize after i give a resize value of bigger than 20. It outputs Incorrect file type, which makes me even more suspectible of the file headers.
The CS50's peek line:
./resize 4 small.bmp student.bmp
~cs50/pset4/resize 4 small.bmp staff.bmp
~cs50/pset4/peek student.bmp staff.bmp
outputs all correct (green).
The other problem is that sometimes it resizes sometimes it keeps the same size even after inputting different value.
This code is a mess, and I've been stuck on resize for well over a week now. I've looked through some help here but couldn't come up with a solution. Any help is aprreciated.
/**
* Copies a BMP piece by piece, just because.
*/
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./copy n infile outfile\n");
return 1;
}
// remember filenames
int resize = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];
// open input file (small.bmp)
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// open output file (larger.bmp)
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;
// add another variable for headers
BITMAPINFOHEADER bi_out = bi;
BITMAPFILEHEADER bf_out = bf;
// change members' sizes before writing
bi_out.biWidth *= resize;
// copy padding for scanlines
int paddingChanged = (4 - (bi_out.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi_out.biHeight *= resize;
bi_out.biSizeImage = ((sizeof(RGBTRIPLE) * bi_out.biWidth) + paddingChanged) * abs(bi_out.biHeight);
bf_out.bfSize = bi_out.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf_out, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi_out, sizeof(BITMAPINFOHEADER), 1, outptr);
// (recopy method) (Zamyla's pseudocode) FOR EACH ROW
// iterate over infile's scanlines
for (int i = 0, y = abs(bi_out.biHeight); i < y; i++)
{
// FOR n-1 TIMES
// write same lines n times
for (int m = 0, x = resize - 1; m < x; m++)
{
// WRITE PIXELS TO OUTFILE
// iterate over pixels in scanline
for (int j = 0; j < bi_out.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write triple n times
for (int k = 0; k < resize; k++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// WRITE PADDING TO OUTFILE
for (int k = 0; k < paddingChanged; k++)
{
fputc(0x00, outptr);
}
// SEND INFILE COURSOR BACK
fseek(inptr, 0, SEEK_SET);
}
// ADD PIXELS TO OUTFILE
for (int j = 0; j < bi_out.biWidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write triple n times
for (int k = 0; k < resize; k++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// ADD PADDING TO OUTFILE
for (int k = 0; k < paddingChanged; k++)
{
fputc(0x00, outptr);
}
// SKIP OVER INFILE PADDING
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
UPDATE: The problems were:
- I was using too many iterations on the
for()
loops. I changedbi_out.biWidth
andbi_out.biHeight
tobi.biWidth
andbi.biHeight
inside these loops. - My
fseek()
line was incorrect. I changed it tofseek(inptr, -(long)bi.biWidth * (long)sizeof(RGBTRIPLE), SEEK_CUR);
and it is finally working now.