title: C Resizing BMP File by Factor of N, what am I doing wrong?
body: <p>new to C.  Have been having troubles resizing a 24 bit uncompressed bitmap with my code.  I am trying to scale this image up by a factor of n, I feel that I am close to having it work properly but my outputted image is still not correct.</p>
  
  <p>I can post pictures of the input 24 bit uncompressed BMP (small.bmp) I am using for testing, the picture my program is outputting (resized.bmp), and what the correct image of small.bmp scaled by a factor of 4 SHOULD look like, if that would be helpful.  Just ask.</p>
  
  <p><strong><em>resize.c</em></strong></p>
  
  <pre><code>#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: ./resize scale infile outfile\n");
          return 1;
      }
  
      int n = atoi(argv[1]);
      // remember filenames
      char *infile = argv[2];
      char *outfile = argv[3];
  
      // 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 oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
      bi.biWidth = bi.biWidth * n;
      bi.biHeight = bi.biHeight * n;
  
      // determine padding for scanlines
      int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
  
  bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
  bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  // write outfile's BITMAPFILEHEADER
  fwrite(&amp;bf, sizeof(BITMAPFILEHEADER), 1, outptr);
  
      // write outfile's BITMAPINFOHEADER
      fwrite(&amp;bi, sizeof(BITMAPINFOHEADER), 1, outptr);
  
  
      // iterate over infile's scanlines
      for (int i = 0, biHeight = abs(oldHeight); i &lt; biHeight; i++)
      {
  
          // store scanline in an array pixel by pixel for vertical scaling.
          RGBTRIPLE scanline[bi.biWidth - 1];
  
          // iterate over pixels in scanline
          for (int j = 0; j &lt; bi.biWidth; j++)
          {
              //check if we've hit padding in original bmp.
              if (j % oldpadding == 0 &amp;&amp; j != 0) 
              {
                  //skip the padding.
                  fseek(inptr, oldpadding, SEEK_CUR);
              }
  
              // temporary storage
              RGBTRIPLE triple;
  
              fread(&amp;triple, sizeof(RGBTRIPLE), 1, inptr);
  
              for (int h = 0; h &lt; n; h++)
              {
                  //scale horizontally, save each scanline pixel to our array.
                  scanline[j] = triple;
              }
  
          }
  
          for (int x = 0; x &lt; n; x++)
          {
              //write scanlines n - 1 times.
              for (int y = 0; y &lt; bi.biWidth; y++)
              {
                      fwrite(&amp;scanline[y], sizeof(RGBTRIPLE), 1, outptr);
              }
  
              //write padding if any for current scanline.
              for (int z = 0; z &lt; padding; z++)
              {
                  fputc(0x00, outptr);
              }
          }
  
      }
  
      // close infile
      fclose(inptr);
  
      // close outfile
      fclose(outptr);
  
      // success
      return 0;
  }
  </code></pre>
  
  <p><strong><em>bmp.h</em></strong></p>
  
  <pre><code>/**
   * BMP-related data types based on Microsoft's own.
   */
  
  #include &lt;stdint.h&gt;
  
  /**
   * Common Data Types 
   *
   * The data types in this section are essentially aliases for C/C++ 
   * primitive data types.
   *
   * Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx.
   * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
   */
  typedef uint8_t  BYTE;
  typedef uint32_t DWORD;
  typedef int32_t  LONG;
  typedef uint16_t WORD;
  
  /**
   * BITMAPFILEHEADER
   *
   * The BITMAPFILEHEADER structure contains information about the type, size,
   * and layout of a file that contains a DIB [device-independent bitmap].
   *
   * Adapted from 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; 
  
  /**
   * BITMAPINFOHEADER
   *
   * The BITMAPINFOHEADER structure contains information about the 
   * dimensions and color format of a DIB [device-independent bitmap].
   *
   * Adapted from 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; 
  
  /**
   * RGBTRIPLE
   *
   * This structure describes a color consisting of relative intensities of
   * red, green, and blue.
   *
   * Adapted from 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>