I've solved the problem by recopy method successfully and thanks to many helpful answers to others' questions. Now I'm trying to solve be rewrite method and I think I have a problem with storing triples in the array.The colors after the first line is changed totally for each pixel. I also have a question about the array of pixels in this line
RGBTRIPLE newtriple[nbi.biWidth + 1];
I need (nbi.biwidth) of pixels for each scanline in the resized file, do I need to add 1, accounting for (NULL) or the array of pixels differs from string??
// Copies a BMP file
include
include
include "bmp.h"
int main(int argc, char *argv[]) { // ensure proper usage if (argc != 4) { fprintf(stderr, "Usage: copy infile outfile\n"); return 1; }
// remember filenames
char *factor = argv[1];
int n = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];
if (n < 1 || n > 100)
{
printf("Write a valid scale value\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, nbf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// Giving value to nbf
nbf = bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, nbi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// Giving value to nbi header
nbi = 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;
}
nbi.biWidth = n * bi.biWidth;
nbi.biHeight = n * bi.biHeight;
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int newpadding = (4 - (nbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// Define image Size
nbi.biSizeImage = ((sizeof(RGBTRIPLE) * nbi.biWidth) + newpadding) * abs(nbi.biHeight);
// Define file Size
nbf.bfSize = nbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// temporary storage
RGBTRIPLE triple;
RGBTRIPLE newtriple[nbi.biWidth + 1];
// New intger for array length
int rlen = 0;
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
for(int h = 0; h < n; h++)
{
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
newtriple[rlen] = triple;
// write RGB triple to outfile
fwrite(&newtriple[rlen], sizeof(RGBTRIPLE), 1, outptr);
rlen++;
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < newpadding; k++)
{
fputc(0x00, outptr);
}
// Replicating the the line and adding the padding
for (int v = 1; v < n; v++)
{
fwrite(&newtriple[nbi.biWidth], sizeof(RGBTRIPLE) * nbi.biWidth, 1, outptr);
// Adding the padding to replicated lines
for (int replk = 0; replk < newpadding; replk++)
{
fputc(0x00, outptr);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}