0

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?

1

So the new code reads the input file headers, writes them out to the new file, and then changes several header values and stores them in individual values instead of the proper struct elements of the headers?????

That's two problems that have to be fixed. The new header values have to be calculated and stored in the proper elements in the header structs. THEN, the headers need to be written to the output files.

My suggestion would be to copy the original headers to new header structs for output and modify the output header structs.

Also, review the shorts and class lectures about structs - how to modify elements in them and how to copy them.

As always, there may be other issues laying in wait later.... or not. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

9
  • I need to ask another question related to this. If you want me to start a new topic tell me BITMAPFILEHEADER bf; How is this line syntactically correct? Can you explain this line? I understand what BITMAPFILEHEADER is. @Cliff B
    – user20282
    Feb 18 '18 at 6:31
  • Just confirmation. I don't have to change anything inside bmp.h file?
    – user20282
    Feb 18 '18 at 6:49
  • Correct. In fact, you can't change bmp.h because it doesn't upload to check50. Also, it's a standards file that is provided to the world with the definition of the bitmap headers. The line BITMAPFILEHEADER bf; is straightforward. It declares a struct var called bf, as type BITMAPFILEHEADER. To be clear, BITMAPFILEHEADER is the name of a struct type. You use it in declarations the same way as int, float, etc., except that it declares an entire structure, not just a single, simple variable.
    – Cliff B
    Feb 18 '18 at 7:17
  • In the videos do they discuss that ex: BITMAPFILEHEADER bf; is for every variable? I know they discussed single variable. @Cliff B
    – user20282
    Feb 19 '18 at 1:36
  • Also I just want to summarize your instructions. First modify the variables in the struct after the structs are read and before the variables are written. But this brings up a question. I don't understand how biHeight ,biWidth, and biSize gets its value? sorry for the ton of questions. i know I can use xxd.
    – user20282
    Feb 19 '18 at 1:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .