Hi everyone,
I am working on a HOMEWORK assignment to resize an image... As this is something that I will be graded on, I am NOT asking you do "do it all for me" I am asking for hints and help so that I may continue to learn (and get a good grade at the same time).
The assignment I am currently working on involves resizing a bmp image by a factor of n. I believe I am close to having a fully working program, The part I am stuck on is writing from an RGB tripple to an array "n times" In my code I have coloured it in red. I know the code that I have there now won't work, but I am not sure what will.
So my question is: "how do I write the RGB tripple to an array n times"
Also if you find anything else that is worrisome don't hesitate to tell me!
Code:
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4)
{
printf("Usage: copy N infile outfile\n");
return 1;
}
// remember filenames
char* nchar = argv[1];
char* infile = argv[2];
char* outfile = argv[3];
int timeswriten = 0;
//convert nchar to an int
int n = atoi (nchar);
if (n < 1 || n > 100)
{
printf("Please try again with a positive number less then 100\n");
return 1;
}
// open input file
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("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, bf2;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bi2;
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;
}
bi2.biHeight = bi.biHeight *n;
bi2.biWidth = bi.biWidth *n;
int padding = (4 -(bi2.biWidth * sizeof(RGBTRIPLE))%4)%4;
bi2.biSizeImage = (bi2.biWidth * sizeof(RGBTRIPLE) + padding) * abs(bi2.biHeight);
bf2.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi2.biSizeImage;
// write outfile's BITMAPFILEHEADER and BITMAPINFOHEADER
fwrite(&bf2, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi2, sizeof(BITMAPINFOHEADER), 1, outptr);
RGBTRIPLE *buffer = (RGBTRIPLE *)malloc(sizeof(RGBTRIPLE) * bi2.biWidth);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// 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 tripple to array n times
for (int z = 0; z < n; z++)
{
buffer[j]=triple[j];
}
}
// write RGB triple to outfile
fwrite(buffer, sizeof(RGBTRIPLE), 1, outptr);
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how)
for (int k = 0; k < padding; k++)
fputc(0x00, outptr);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}