After a long struggle with this one I finally got close to completing it. When I open the enlarged file, the lower rows are missing. However I can see these seem to be ok when I open the file as hexadecimal.
File opene as hex (seems fine):
Code:
// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
#include <errno.h>
int main(int argc, char *argv[])
{
// convert argv[1] into long resize factor n
char *p;
errno = 0;
long n = strtol(argv[1], &p, 10);
if (*p != '\0' || errno != 0)
{
printf("Usage: ./resize n infile outfile (where n is a factor from 1 to 100 to resize the file)\n");
return 1;
}
// ensure proper usage
if (argc != 4 || n < 1 || n > 100)
{
fprintf(stderr, "Usage: ./resize n infile outfile (where n is a factor from 1 to 100 to resize the file)\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;
}
// 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;
}
// determine padding input file
int padinp = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// modify outfile BITMAPINFOHEADER Height & Width
bi.biHeight = bi.biHeight * n;
bi.biWidth = bi.biWidth * n;
// determine padding outputfile
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine biSizeImage
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs( bi.biHeight);
// modify outfile BITMAPFILEHEADER
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight)/n; i < biHeight; i++)
{
// malloc array for copying vertical rows
int c = 0;
RGBTRIPLE vert[3*n];
/**vert = malloc(sizeof(bi.biWidth * sizeof(RGBTRIPLE)));
if (vert == NULL)
{
printf("Couldn't allocate requested memory for Vert\n");
return 4;
}*/
// iterate over pixels in scanline
for (int j = 0; j < (bi.biWidth/n); j++)
{
// temporary storage
//RGBTRIPLE triple;
// read RGB triple from infile
fread(&vert[c], sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple_n to outfile
for (int z = 0; z < n; z++)
{
fwrite(&vert[c], sizeof(RGBTRIPLE), 1, outptr);
vert[c+(z+1)] = vert[c];
}
c = c + n;
}
// skip over padding input, if any
fseek(inptr, padinp, SEEK_CUR);
// add padding to output file
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
// add additional n scanlines
for (int w = 0; w < n; w++)
{
fwrite(&vert[0], sizeof(vert), 1, outptr);
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
}
//free(vert);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}