For some reason my resize code only resizes the top half of an image and stops after that. Any help will be much appreciated.
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: 1st argument should be positive integer less than or equal to 100\n 2nd argument should be be file to be resized\n 3rd argument should be file name of rewritten, resized file\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
//make sure argv[1], n, is positive integer <= 100
string ninput = argv[1];
int n = atoi(ninput);
///need to change char to int
if (n <= 0 || n > 100)
{
fprintf(stderr, "Number needs to positive and less than or equal to 100\n");
return 2;
}
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 3;
}
// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 4;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf, bfout;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfout = bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, biout;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
biout = bi;
// 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 5;
}
//update biout and bfout values for biHeight, biWidth, biSizeImage, and bfSize
biout.biHeight = bi.biHeight * n;
biout.biWidth = bi.biWidth * n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int paddingout = (4 - (biout.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
biout.biSizeImage = ((sizeof(RGBTRIPLE) * biout.biWidth + paddingout) * abs(biout.biHeight));
bfout.bfSize = biout.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bfout, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&biout, sizeof(BITMAPINFOHEADER), 1, outptr);
//keep track of horizontal lines in regards to n
int counter = 1;
// iterate over infile's scanlines
for (int o = 0, biHeight = abs(bi.biHeight); o < biHeight; o++)
{
// 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);
// horizontally write pixel n times
for (int k = 0; k < n; k++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < paddingout; k++)
{
fputc(0x00, outptr);
}
if (counter < n)
{
//go to beginning of infile line based on counter (in regards to n)
fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE) + padding), SEEK_CUR);
//update counter
counter++;
} else
{
counter = 1;
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}