I'm not sure where the issue is here, but from the looks of debug I only store anything in my horizontal array (line) once despite seeing the debugger hitting that section of code properly. I assume I misunderstand how pointers work and i'm assigning incorrectly but I don't know how. My instincts tell me that my treating it like an array with [count] is not properly moving positions down the line but I don't know how to properly do this.
// 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)
{
fprintf(stderr, "Usage: ./resize n infile outfile.\n");
return 1;
}
// remember filenames
int n = abs(atoi(argv[1]));
char *infile = argv[2];
char *outfile = argv[3];
//checks the factor size is within 0 - 100
if (n < 0 || n > 100)
{
printf("Please enter a factor size between 0 and 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 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;
}
//create new info and file header for outfile
BITMAPINFOHEADER bi2;
BITMAPFILEHEADER bf2;
bi2 = bi;
bf2 = bf;
//assign new Height and Width to new header files
bi2.biWidth = bi.biWidth * n;
bi2.biHeight = bi.biHeight * n;
// determine padding for infile scanlines
int paddingIn = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine padding for outfile scanlines
int paddingOut = (4 - (bi2.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//edit SizeImage and size in outfile headerfile
bi2.biSizeImage = ((sizeof(RGBTRIPLE) * bi2.biWidth) + paddingOut) * abs(bi2.biHeight);
bf2.bfSize = bi2.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf2, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi2, sizeof(BITMAPINFOHEADER), 1, outptr);
//create array to store scanlines in
RGBTRIPLE *line = malloc(sizeof(RGBTRIPLE) * bi2.biWidth);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
int count = 0;
// iterate over pixels in scanline
for (int j = 0; j < abs(bi.biWidth); j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//write pixel to array n times
for (int k = 0; k < n; k++)
{
line[count] = triple;
count++;
}
}
// skip over padding, if any
fseek(inptr, paddingIn, SEEK_CUR);
// write array to outfile n times
for (int l = 0; l < n; l++)
{
fwrite(line, sizeof(sizeof(RGBTRIPLE) * bi2.biWidth ) + 1, n, outptr);
}
//add padding
for (int p = 0; p < paddingOut; p++)
{
fputc(0x00, outptr);
}
}
free(line);
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}