0
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
  // check if users put in correct command-line arguments
if (argc != 4)
{
    fprintf (stderr, "Usage: ./resize infile outfile\n");
    return 1;
}

// check if the first argument(n) is positive and less than or equal to 100
int n = atoi(argv[1]); 
if (n <= 0 || n > 100)
{
    fprintf (stderr, "Usage: ./resize infle outfile\n");
    return 1;
}

// check if the second argument is the name of a BMP file
char *infile = argv[2];
char *outfile = argv[3];

  // open the files
  FILE *inptr = fopen(infile, "r");
  if (inptr == NULL)
  {
      fprintf (stderr, "File can not open: %s\n", infile);
      return 1;
  }

  FILE *outptr = fopen(outfile, "w");
  if (outptr == NULL)
  {
      fprintf (stderr, "File can not open: %s\n", outfile);
      fclose(inptr);
      return 1;
  }

  //read the headers of files
  BITMAPFILEHEADER bf;
  BITMAPFILEHEADER outBf;
  BITMAPINFOHEADER bi;
  BITMAPINFOHEADER outBi;

  fread (&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  fread (&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
  outBf = bf;
  outBi = bi;

  //check if the file are 24-bit BMP
  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 1;
  }

   //check the padding
int padding = (4 - ((bi.biWidth * sizeof(RGBTRIPLE)) % 4)) %4;

// update the headers
 // update the width and height of the image

 outBi.biWidth = bi.biWidth * n;
 outBi.biHeight = bi.biHeight * n;

 // update padding
 int newPadding = (4 - ((outBi.biWidth * sizeof(RGBTRIPLE)) % 4)) % 4;

 // update the image size 
 outBi.biSizeImage = ((sizeof(RGBTRIPLE) * outBi.biWidth) + newPadding) * abs(outBi.biHeight);

 // update file size
 outBf.bfSize = outBi.biSizeImage + sizeof(outBf) + sizeof(outBi);

//write the headers of the outfile
fwrite (&outBf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite (&outBi, sizeof(BITMAPINFOHEADER), 1, outptr);

// iterate the file by pixels
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
      //temperate storage
        RGBTRIPLE triple;
      // create an array for each pixel
        RGBTRIPLE pixels[outBi.biWidth];

    // iterate for the verticle
    for (int w = 0; w <= n; w++)
    {   
        if (w % n != 0)
        {
            fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
        }
        //iterate the row by pixels
        for (int j = 0, biWidth = bi.biWidth; j < biWidth; j++)
        {

            // read the RGB from the infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            if (n == 1)
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
            if (n > 1)
            {
                for (int p = 0; p < n; p++)
                {
                    pixels[p] = triple;
                }

                //write the RGB to the outfile
                fwrite(&pixels, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        for (int k = 0; k < newPadding; k++)
        {
            fputc(0x00, outptr);
        }
    }
    fseek(inptr, padding, SEEK_CUR);
}

//close the files
fclose(inptr);
fclose(outptr);

return 0;

}

2 Answers 2

0

I have figured out the problem with my code!! It's the problem with both cursor and paddings. Give my best wishes to everyone else who is working on this pset

0

There is a major error in your code.

You are not skipping over padding correctly. You add the new padding before skipping over the old one (you skip over the old one the line after, at fseek(inptr, padding, SEEK_CUR);). This causes some issues in your code. To fix this, simply put your fseek() before your for-loop. You also want to skip over the odd padding after your for-loop. The walkthrough for pset4 (look below for the link) might help you fix your padding errors.

If you want to review the walkthrough, see here. This has some pseudocode that you can look over, as well as some explanations for problems that I see for your code.

Hopefully this will push you in the right direction. If this answers your question, hit the green checkmark! If you still have errors, comment below.

2
  • Your first point makes no sense to me, sorry. Those are structs, and structs can be simply copied.
    – Blauelf
    Commented Feb 17, 2017 at 14:50
  • @Blauelf edited answer. Commented Feb 17, 2017 at 15:02

You must log in to answer this question.

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