i'm feeling so close to solve it but have some issues with the for loops i tried to debug it and see what's going on but yet there's some things i can't explain specifically with triple variable i'm trying to see how it works but couldn't also i have some doubts with how to skip old padding and when to put the new one, i think it works fine with scale of n but produces colorful image without padding, Any pointers would be much appreciated, here's my code
// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./resize n infile outfile\n");
return 1;
}
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
// making argv[1] is factor number
int n = atoi(argv[1]);
//ensure n for valid input
if (n <= 0 || n >= 100)
{
printf("n Must be between 0 and 100\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;
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 for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//coping the infile headers to new variables
BITMAPFILEHEADER new_bf = bf;
BITMAPINFOHEADER new_bi = bi;
// calculating new width and height for the image
new_bi.biWidth *= n;
new_bi.biHeight *= n;
//creating new padding with the new width
int pading = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// checking if padding works
printf("%i\n", pading);
// calculating the new sizes for the image and the file
new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + pading * abs(new_bi.biHeight));
new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write new outfile's BITMAPFILEHEADER
fwrite(&new_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write new outfile's BITMAPINFOHEADER
fwrite(&new_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
//iterate over row to write it pixel by pixel n times
for (int s = 0; s < n; s++)
{
// 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);
//printf("%lu\n", sizeof(triple)); it prints so many 3 but idk why! was checking the value of triple to determine how it works in the loop btw
// write RGB triple to outfile mult by n
for (int r = 0; r < n; r++)
{
//storing the value of multiplying j's value in the loop from infile to triple variable to write it to outfile
//triple = (triple + padding) * j;
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over old padding
fseek(inptr, padding, SEEK_CUR);
// then add new padding if needed
for (int k = 0; k < pading; k++)
{
fputc(0x00, outptr);
}
//move the cursor back to the begining of the row
if(s < n-1)
{
fseek(inptr, -(new_bi.biWidth * sizeof(RGBTRIPLE) + pading), SEEK_CUR);
}
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}
Thanks in advance.