In resize less comfortable I need help with bi.sizeImage and bf.bfsize
write outfile's BITMAPFILEHEADER
// I Tried placing new_new_bfsize here
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
write outfile's BITMAPINFOHEADER
// I Tried placing new_biSizeImage here.
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
BITMAPFILEHEADER is before BITMAPINFOHEADER so that causes some problems. I also moved some code around when attempting the process above.
The program won't compile because new_bfsize is unused. Should I call "new_bfsize" just "bi.bfsize"?
So what I think I have solved is
Vertical old and new.
Horizontal old and new.
padding old and new.
bi.SizeImage old and new
bf.bfsize old and new
> #include <stdio.h>
> #include <stdlib.h>
> #include <cs50.h>
> #include "bmp.h"
>
> int main(int argc, char *argv[]) {
> // ensure proper usage
> if (argc != 4)
> {
> fprintf(stderr, "Usage: ./copy infile outfile\n");
> return 1;
> }
>
> //atoi converts string ex "6" to 6
> int n = atoi(argv[1]);
> char *infile = argv[2];
> char *outfile = argv[3];
>
> //n is a positive integer less than or equal to 100
> if( (n > 100) && (n <= 0) )
> {
> fprintf(stderr, "(n) must be a positive integer less than or equal to 100");
> return 2;
> }
> // open input file
> FILE *inptr = fopen(infile, "r");
> if (inptr == NULL)
> {
> fprintf(stderr, "Could not open %s.\n", infile);
> return 3;
> }
>
> // open output file
> FILE *outptr = fopen(outfile, "w");
> if (outptr == NULL)
> {
> fclose(inptr);
> fprintf(stderr, "Could not create %s.\n", outfile);
> return 4;
> }
> // 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;
> }
>
> // write outfile's BITMAPFILEHEADER
> fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
>
> // write outfile's BITMAPINFOHEADER
>
> fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
>
> //new bi.biWidth and bi.biHeight
> int new_biWidth = bi.biWidth * n;
> int new_biHeight = bi.biHeight * n;
>
>
>
>
>
> // determine padding for scanlines
> int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
> int new_padding = (4 - (new_biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
>
> //new headerfiles BITMAPFILEHEADER and BITMAPINFOHEADER
> int new_biSizeImage = ((sizeof(RGBTRIPLE)*new_biWidth) + new_padding)*abs(new_biHeight);
> int new_bfsize = new_biSizeImage + sizeof(BITMAPFILEHEADER) +sizeof(BITMAPINFOHEADER);
>
> // iterate over infile's scanlines createes height
> for (int i = 0, biHeight = abs(new_biHeight); i < biHeight; i++)
> {
> // iterate over pixels in scanline creates width
> for (int j = 0; j < new_biWidth; j++)
> {
> // temporary storage
> RGBTRIPLE triple;
>
> // read RGB triple from infile
> fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
>
> // write RGB triple to outfile nth time triples
> fwrite(&triple, sizeof(RGBTRIPLE), n, outptr);
> }
> }
>
>
> // skip over padding, if any
> fseek(inptr, padding, SEEK_CUR);
>
> // then add it back (to demonstrate how)
> for (int k = 0; k < new_padding; k++)
> {
> fputc(0x00, outptr);
> }
>
>
> // close infile
> fclose(inptr);
>
> // close outfile
> fclose(outptr);
>
> // success
> return 0; }
Any other problems in the code I should be aware of?