0

I have been busy for a while.

I modified the code but it still doesn't work. I am not sure where to put

the new headerfiles.

> > write outfile's BITMAPFILEHEADER  
    // I Tried placing new_new_bfsize here
> > 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.

Are the modifications to the code better?

The program won't compile new_bfsize is unused. Should I call "new_bfsize" just "bi.bfsize"?

I think I have managed to solve a few problems for resize. If new_biWidth equals 0 the code won't work. Can new_biWidth equal 0?

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; }
0

No, new_biWidth can't equal 0 (unless there's no image at all.)

The bigger question is this. How can the two headers be written out before the values that need to be changed in the headers are actually changed????

Hint: Start with small.bmp and a scaling factor of 1. Then use xxd and peek to see if you're getting the correct header. Then work on the image. Once everything works at n=1, change to n=2 and get the headers right. THEN, work on the image.

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

6
  • I am very confused by xxd. I don't see xxd mentioned anywhere in the course. i tried googling it but am very confused. It is probably been asked a million times but could you explain xxd very simply? Start with small.bmp and a scaling factor of 1. I assume "Start with small.bmp" and a scaling factor of 1. you want me to type $ ./resize 1 small.bmp large.bmp,?
    – user20282
    Feb 6 '18 at 4:23
  • xxd is a tool that displays the actual raw hexadecimal data contained in a file. Look at the man page or type xxd -h for the flags that it uses. With the right flags, you can post as much or as little data as you want, you can start where you want in the file, you can group the data almost any way you want, etc.
    – Cliff B
    Feb 6 '18 at 4:26
  • well, I might use ./resize 1 small.bmp s1.bmp so that I don't overwrite large.bmp
    – Cliff B
    Feb 6 '18 at 4:28
  • I have modified the code and asked some new question. Could you answer the new questions and take a look at the new code? @Cliff B
    – user20282
    Feb 15 '18 at 23:57
  • New issues and new code deserve a new question. Changing this confuses anyone else that reads it in the future. Could you please un-edit and open a new question? If it's still the same problem, that's a different story. Thanks.
    – Cliff B
    Feb 16 '18 at 2:47

You must log in to answer this question.

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