int n = atoi(argv[1]);
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf,bfR;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfR = bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi,biR;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
biR = 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 4;
}
// modify bitmapinfoheader for height an width
biR.biWidth = bi.biWidth*n;
biR.biHeight = bi.biHeight*n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outpadding = (4 - (biR.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// modify bisizeimage
biR.biSizeImage = (((biR.biWidth*(sizeof(RGBTRIPLE)))+outpadding)*abs(biR.biHeight));
// write outfile's BITMAPFILEHEADER
fwrite(&bfR, sizeof(BITMAPFILEHEADER), 1, outptr);
// modify bfsize before writing it
bfR.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(biR.biSizeImage));
// write outfile's BITMAPINFOHEADER
fwrite(&biR, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
for(int m = 0; m < n; m++)
{
// 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);
// write RGB triple to outfile
for (int k = 0; k < n; k++)
{
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
//go back to reprint the same line
if(m != n-1)
{
fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE),SEEK_CUR);
}
// then add it back (to demonstrate how)
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
}
1 Answer
The problems revolve around how the pointer is moved at the end of each line in the input file and how padding is added to the output file. There are two issues.
First, the output file padding. It works for n=1 because the padding sizes are identical. The problem here is that the code is writing the input padding to the output file.
Next, when handling files where n != 1, the input file pointer is being moved back to the beginning of the current line, but after moving forward over the input file padding. It's not including the distance of the input file padding when moving back, so the pointer is being positioned in the wrong place.
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)
-
It works for any value of n for a particular image,IDK why? I'll try to implement what tlyou suggested and let you know.Thanks! Commented Jul 7, 2019 at 4:05
-