title: How to resize a bitmap (BMP) in C - my code is not working
body: <p>I've been trying to fix this code. Everything seems to be working fine based on the printf tests I've added but the new image isn't resizing horizontally. </p>
  
  <p>This is for an online course I'm taking so the coding may look elementary but we may not have covered more advanced solutions yet. </p>
  
  <p>I've pasted both programs needed to execute the program and also included the pics. </p>
  
  <p>Execute the program using this argument or similar <code>./copy 2 staff.bmp large.bmp</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/OLUzw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OLUzw.png" alt="image to resize"></a> <a href="https://i.stack.imgur.com/AXuKq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AXuKq.png" alt="image after resize"></a></p>
  
  <pre><code>// Copies a BMP file
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "bmp.h"
  
  int main(int argc, char *argv[])
  {
      // ensure proper usage
      if (argc != 4)
      {
          fprintf(stderr, "Usage: copy infile outfile\n");
          return 1;
      }
  
  
      // remember filenames
      int num = atoi(argv[1]);
      char *infile = argv[2];
      char *outfile = argv[3];
  
      if (num &lt; 1 || num &gt; 100)
      {
          fprintf(stderr, "Resize only 1-100. Try again.\n");
          return 4;
      }
  
      // open input file
      FILE *inptr = fopen(infile, "r");
      if (inptr == NULL)
      {
          fprintf(stderr, "Could not open %s.\n", infile);
          return 2;
      }
  
      // open output file
      FILE *outptr = fopen(outfile, "w");
      if (outptr == NULL)
      {
          fclose(inptr);
          fprintf(stderr, "Could not create %s.\n", outfile);
          return 3;
      }
  
      // read infile's BITMAPFILEHEADER
      BITMAPFILEHEADER bf;
      fread(&amp;bf, sizeof(BITMAPFILEHEADER), 1, inptr);
  
      // read infile's BITMAPINFOHEADER
      BITMAPINFOHEADER bi;
      fread(&amp;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;
      }
  
      int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
      printf("padding%i ", padding);
  
      BITMAPINFOHEADER newbi = bi;
      BITMAPFILEHEADER newbf = bf;
  
      newbi.biWidth = bi.biWidth * num;
      printf("newbiwidth%i ", newbi.biWidth);
  
      newbi.biHeight = bi.biHeight * num;
      printf("newbiheight%i ", newbi.biHeight);
  
      // determine padding for scanlines
      int newpadding = (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
      printf("newpadding%i ", newpadding);
  
      newbi.biSizeImage = (newbi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(newbi.biHeight);
      printf("newbisizeimage%i ", newbi.biSizeImage);
  
      newbf.bfSize = newbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
      printf("newbfsize%i ", newbf.bfSize);
  
      // write outfile's BITMAPFILEHEADER
      fwrite(&amp;newbf, sizeof(BITMAPFILEHEADER), 1, outptr);
  
      // write outfile's BITMAPINFOHEADER
      fwrite(&amp;newbi, sizeof(BITMAPINFOHEADER), 1, outptr);
  
  
  
      // temporary storage
      RGBTRIPLE triple;
      RGBTRIPLE *newtriple = malloc(num * sizeof(RGBTRIPLE) + 1);
      RGBTRIPLE *sline = malloc(newbi.biWidth * sizeof(RGBTRIPLE));
  
      // read RGB triple from infile
      for (int i = 0, biheight = abs(bi.biHeight); i &lt; biheight; i++)
      {
          printf("H%i  ", i);
          for (int j = 0; j &lt; bi.biWidth; j++)
          {
              printf("W%i  ", j);
              fread(&amp;triple, sizeof(RGBTRIPLE), 1, inptr);
  
              //store triple times resize number
              for (int m = 0; m &lt; num; m++)
              {
                  newtriple[m] = triple;
                  printf("T%i  ", m);
              }
  
                      //store new triple as new scanline
                      sline[j] = *newtriple;
                      printf("S%i  ", j);
          }
  
              //write new scanline to file
              for (int k = 0; k &lt; num; k++)
              {
                  printf("F%i  ", k);
                  fwrite(sline, newbi.biWidth * 3, 1, outptr);
  
                  // add padding if any
                  for (int h = 0; h &lt; newpadding; h++)
                  {
                      fputc(0x00, outptr);
                  }
  
                  // skip over padding, if any
                  fseek(inptr, padding, SEEK_CUR);
              }
  
      }
      free(newtriple);
      free(sline);
  
      // close infile
      fclose(inptr);
  
      // close outfile
      fclose(outptr);
  
  
      // success
      return 0;
  }
  
  // BMP-related data types based on Microsoft's own
  
  #include &lt;stdint.h&gt;
  
  // aliases for C/C++ primitive data types
  // https://msdn.microsoft.com/en-us/library/cc230309.aspx
  typedef uint8_t  BYTE;
  typedef uint32_t DWORD;
  typedef int32_t  LONG;
  typedef uint16_t WORD;
  
  // information about the type, size, and layout of a file
  // https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx
  typedef struct
  {
      WORD bfType;
      DWORD bfSize;
      WORD bfReserved1;
      WORD bfReserved2;
      DWORD bfOffBits;
  } __attribute__((__packed__))
  BITMAPFILEHEADER;
  
  // information about the dimensions and color format
  // https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx
  typedef struct
  {
      DWORD biSize;
      LONG biWidth;
      LONG biHeight;
      WORD biPlanes;
      WORD biBitCount;
      DWORD biCompression;
      DWORD biSizeImage;
      LONG biXPelsPerMeter;
      LONG biYPelsPerMeter;
      DWORD biClrUsed;
      DWORD biClrImportant;
  } __attribute__((__packed__))
  BITMAPINFOHEADER;
  
  // relative intensities of red, green, and blue
  // https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx
  typedef struct
  {
      BYTE rgbtBlue;
      BYTE rgbtGreen;
      BYTE rgbtRed;
  } __attribute__((__packed__))
  RGBTRIPLE;
  </code></pre>