So I have looked through every response to this and I still for the life of me can't figure out what I am doing wrong here. The output is repeating each line twice all the way down. I have tried moving the vertical "repeat n times" loop around to no avail. Any insights would be greatly appreciated. Thanks!
// scales a BMP file by n
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper useage
if (argc != 4)
{
fprintf(stderr, "Usage: n infile outfile\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
int n = atoi(argv[1]);
if(n <0 || n>100)
{
fprintf(stderr, "Usage: n must be a value from 1 to 100\n");
return 1;
}
// 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 outfile's BITMAPFILEHEADER
BITMAPFILEHEADER oBf = bf;
BITMAPINFOHEADER oBi = bi;
//Assign outfile BITMAPINFOHEADER values for width and hieght
oBi.biWidth *= n;
oBi.biHeight *= n;
// assigning values to infile and outfile padding
int iPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int oPadding = (4 - ((oBi.biWidth) * sizeof(RGBTRIPLE)) % 4) % 4;
//Change Outfile BITMAPINFOHEADER to new dimensions
bi.biSizeImage = ((sizeof(RGBTRIPLE)*bi.biWidth)+oPadding)*abs(bi.biHeight);
int headers = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
//change outfile Image size in BITMAPFILEHEADER
oBf.bfSize = headers + bi.biSizeImage;
// write outfile's BITMAPFILEHEADER
fwrite(&oBf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&oBi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int j = 0, iHeight = abs(bi.biHeight); j < iHeight; j++)
{
for(int i = 0; i < n; i++)
{
//Sets inptr to the beginning of each scanline
fseek(inptr, headers + ((bi.biWidth * sizeof(RGBTRIPLE) + iPadding)*i), SEEK_SET);
//iterates through each pixel in a scanline
for(int x = 0; x < bi.biWidth; x++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
for(int t = 0; t < n; t++)
{
// write RGB triple to outfile n times
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
//writes outfile padding in each row
for (int k = 0; k < oPadding; k++)
{
fputc(0x00, outptr);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}