so every time I run my program I get the error message: "unsupported file format". What's more, even if I delete the line handling the file format, the program still fails due to "File size limit exceeded". Any tips please?
Hopefully the code isn't too messy, I tried to keep it commented.
int main(int argc, char* argv[])
{
if (argc != 4)
{
printf("Proper usage: n, infile, outfile\n");
return 1;
}
char* infile = argv[2];
char* outfile = argv[3];
int n = atoi(argv[1]);
//open infile
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("Could not open %s.\n", infile);
return 2;
}
//open outfile
FILE* outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 3;
}
//read infile BFHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
//read outfile BIHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// 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;
}
//save for future purpose
int width = bi.biWidth;
int height = bi.biHeight;
//edit the headers
bf.bfSize = 54 + bi.biSizeImage * n;
bi.biSizeImage = bi.biSizeImage * n;
bi.biWidth = bi.biWidth * n;
bi.biHeight = bi.biHeight * n;
//write in the updated BFHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
//write in the updated BIHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
//determine padding
int padding = (4 - (width * sizeof(RGBTRIPLE) % 4));
int paddingNew = (4 - (bi.biWidth * sizeof(RGBTRIPLE) % 4));
//iterate each scanline
RGBTRIPLE triple;
for (int i = 0, l = abs(height); i < l; i++)
{
for (int h = 0; h < n; h++)
{
for (int j = 0; j < width; j++)
{
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);
}
//skip the padding
fseek(inptr, padding, SEEK_CUR);
//write in the new padding
for (int o = 0; i < paddingNew; o++)
fputc(0x00, outptr);
}
}
fclose(inptr);
fclose(outptr);
}