Question: I am getting error messages. resize.c:102:9: error: expected statement } ^ resize.c:106:29: error: use of undeclared identifier 'triple' fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); I am trying
I am getting error messages. resize.c:102:9: error: expected statement } ^ resize.c:106:29: error: use of undeclared identifier 'triple' fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
I am trying to get my program to do the following.
Implement a program called resize that resizes (i.e., enlarges) 24-bit uncompressed BMPs by a factor of n.
Implement your program in a file called resize.c in a directory called resize.
Your program should accept exactly three command-line arguments, whereby
the first (n) must be a positive integer less than or equal to 100,
the second must be the name of a BMP to be resized, and
the third must be the name of the resized version to be written.
+ If your program is not executed with such, it should remind the user of correct usage, as with fprintf (to stderr), and main should return 1.
Your program, if it uses malloc, must not leak any memory.
Here is my code.
/** * Copies a BMP piece by piece, just because. */
#include
#include "bmp.h"
int main(int argc, char *argv[]) { // ensure proper usage if (argc != 4) { fprintf(stderr, "Usage: ./copy infile outfile "); return 1; }
// remember filenames int factor = atoi(argv[1]); char *infile = argv[1]; char *outfile = argv[2];
// open input file FILE *inptr = fopen(infile, "r"); if (inptr == NULL) { fprintf(stderr, "Could not open %s. ", infile); return 2; }
// open output file FILE *outptr = fopen(outfile, "w"); if (outptr == NULL) { fclose(inptr); fprintf(stderr, "Could not create %s. ", outfile); return 3; }
// read infile's BITMAPFILEHEADER BITMAPFILEHEADER bf; fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr); BITMAPFILEHEADER out_bf = bf;
// read infile's BITMAPINFOHEADER BITMAPINFOHEADER bi; fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr); BITMAPINFOHEADER out_bi = bi; int inHeight = bi.biHeight; int inWidth = bi.biWidth;
// 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. "); return 4; }
// determine padding for scanlines int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; int AllinPadding = padding * bi.biHeight; int outPadding = (4 - ((bi.biWidth * factor) * sizeof(RGBTRIPLE)) % 4) % 4; int AlloutPadding = outPadding * (bi.biHeight * factor);
DWORD newBfSize = ((bf.bfSize - 54 - AllinPadding) * factor) + AlloutPadding + 54;
out_bf.bfSize = newBfSize;
// write outfile's BITMAPFILEHEADER fwrite(&out_bf, sizeof(BITMAPFILEHEADER), 1, outptr);
DWORD newBiSize = ((bi.biSize - AllinPadding) * factor) + AlloutPadding; LONG newBiWidth = bi.biWidth * factor; LONG newBiHeight = bi.biHeight * factor;
out_bi.biSize = newBiSize; out_bi.biWidth = newBiWidth; out_bi.biHeight = newBiHeight;
fwrite(&out_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines for (int i = 0, biHeight = abs(inHeight); i < biHeight; i++) { // iterate over pixels in scanline for (int j = 0; j < inWidth; 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 < factor; k++) }
for (int l = 0; l < factor; l++) { fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); }
// skip over padding, if any fseek(inptr, padding, SEEK_CUR);
// then add it back (to demonstrate how) for (int m = 0; m < outPadding; outPadding++) { fputc(0x00, outptr); } }
// close infile fclose(inptr);
// close outfile fclose(outptr);
// success return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
