title: C misreading resolution of .bmp file
body: <p>I have a project that involves reading a .bmp file into a C program, putting a mask on it, and printing the version with the mask back to a different file. The part I'm having an issue with seems to be the actual process of reading in the file. The first big red flag I'm seeing is that it keeps reading in the wrong resolution. I've searched quite a bit and seen a few scripts to read in a .bmp file as answers to various questions here but using the logic from those scripts hasn't helped. </p>
  
  <p>The primary issue seems to be that rather than reading in the proper dimensions of 200 x 300 on the example image given by my professor, it reads in 13107200 x 65536. However, if I were to include the part of the code that prints to a different file, you would see that the output file has the appropriate resolution. This tells me that I am likely reading in the information properly but not storing it in the way that I think I am.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct HEADER {
      unsigned short int Type;        // Magic indentifier
      unsigned int Size;              // File size in bytes
      unsigned short int Reserved1, Reserved2;
      unsigned int Offset;            // Offset to data (in B)
  } Header;                           // -- 14 Bytes
  
  struct INFOHEADER {
      unsigned int Size;              // Header size in bytes
      int Width, Height;              // Width / height of image
      unsigned short int Planes;      // Number of colour planes
      unsigned short int Bits;        // Bits per pixel
      unsigned int Compression;       // Compression type
      unsigned int ImageSize;         // Image size in bytes
      int xResolution, yResolution;   // Pixels per meter
      unsigned int Colors;            // Number of colors
      unsigned int ImportantColors;   // Important colors
  } InfoHeader;                       // -- 40 Bytes
  
  struct PIXEL {
      unsigned char Red, Green, Blue; // Intensity of Red, Green, and Blue
  };                                  // -- 3 Bytes
  
  int getFileSize(FILE *input);
  int getHexVal(FILE *input);
  struct HEADER *getHeader(FILE *input);
  struct INFOHEADER *getInfoHeader(FILE *input);
  struct PIXEL *getPixel(FILE *input, struct PIXEL *loc);
  struct HEADER *printHeader(FILE *output);
  struct INFOHEADER *printInfoHeader(FILE *output);
  struct PIXEL *printPixel(FILE *output, struct PIXEL *loc);
  
  int main(int argc, char const *argv[]) {
      if (argc == 3) {
           if (!strcmp(argv[1], argv[2])) {
              printf("The input and output file must be different. Please try again.\n");
              return 1;
          }
  
          // char Matrix[3][3] =
          // { {  0, -1,  0 },
          //   { -1,  4, -1 },
          //   {  0, -1,  0 }
          // };
  
          FILE *input = fopen(argv[1], "rb");
          if (!input) return 1;
          int i, j;
          // getHeader(input);
          fread(&amp;Header, sizeof(struct HEADER), 1, input);
          if (Header.Type != 0x4D42) {
              printf("The specified input file was not a bitmap. Please try again.");
              fclose(input);
              return 1;
          }
          // getInfoHeader(input);
          fread(&amp;InfoHeader, sizeof(struct INFOHEADER), 1, input);
          fseek(input, Header.Offset, SEEK_SET);
          struct PIXEL arr[InfoHeader.Width][InfoHeader.Height];
  
          printf("%d %d\n", InfoHeader.Width, InfoHeader.Height);
          for (i = 0; i &lt; InfoHeader.Width; i++) {
              for (j = 0; j &lt; InfoHeader.Height; j++) {
                  getPixel(input, arr[i] + j);
                  printf("%d %d %d\n", arr[i][j].Red, arr[i][j].Green, arr[i][j].Blue);
              }
          }
          fclose(input);
      }
  }
  </code></pre>