title: Bitmap writing program doesn't produce a readable image despite following format specifications
body: <p>I'm making a bitmap writer in C as part of a larger project. I followed the windows .bmp header format specifications and checked the generated file in a hex editor to compare it with functional .bmp images, however all image programs I have on my computer cannot open it. Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;string.h&gt;
  
  #pragma pack(push, 1) /* remove padding from sructs */
  
  void generateBitmap(int width, int height, float dpi, const char* filename, pixel* imgData) {
      FILE* bitmap;
  
      struct fHeader {
          uint16_t type; 
          uint32_t size;
          uint16_t reserved1;
          uint16_t reserved2;
          uint32_t offset;
      }bmpFileHeader;
  
      struct iHeader {
          uint32_t headerSize;
          int32_t  width;
          int32_t  height;
          uint16_t planes;
          uint16_t bitCount;
          uint32_t compression;
          uint32_t imageSize; /* may be 0 if uncompressed */
          int32_t  xPPM;
          int32_t  yPPM;
          uint32_t colorEntriesUsed;
          uint32_t importantColors;
      }bmpImageHeader;
  
      int bytesPerPixel = 3; /* 24 bit color */
      uint32_t imgSize = width * height;
      uint32_t fileSize = sizeof(bmpFileHeader) + sizeof(bmpImageHeader) + (bytesPerPixel * width * height);
      int32_t ppm = dpi * 39;
  
      bmpFileHeader.type = 0x4D42;
      bmpFileHeader.size = fileSize;
      bmpFileHeader.reserved1 = 0;
      bmpFileHeader.reserved2 = 0;
      bmpFileHeader.offset = sizeof(bmpFileHeader) + sizeof(bmpImageHeader);
  
      bmpImageHeader.headerSize = sizeof(bmpImageHeader);
      bmpImageHeader.width = width;
      bmpImageHeader.height = height;
      bmpImageHeader.planes = 1;
      bmpImageHeader.bitCount = 8 * bytesPerPixel;
      bmpImageHeader.compression = 0;
      bmpImageHeader.imageSize = bytesPerPixel * height * width;
      bmpImageHeader.xPPM = ppm; /* typically set these to zero */
      bmpImageHeader.yPPM = ppm;
      bmpImageHeader.colorEntriesUsed = 0;
      bmpImageHeader.importantColors = 0;
  
      bitmap = fopen(filename, "wb");
      fwrite(&amp;bmpFileHeader, 1, sizeof(bmpFileHeader), bitmap);
      fwrite(&amp;bmpImageHeader, 1, sizeof(bmpImageHeader), bitmap);
  
      int i;
      for (i = 0; i &lt; (width * height); i++) {
          fwrite(&amp;imgData[i], 3, sizeof(char), bitmap);
      }
  
      fclose(bitmap);
  }
  
  int main(void) {
      pixel imData[4];
  
      int i;
  
      for(i = 0; i &lt; 4; i++) {
          imData[i].r = 32;
          imData[i].g = 64;
          imData[i].b = 32;
      }
  
      generateBitmap(2, 2, 0, "bmptest.bmp", imData);
  
      return 0;
  }
  </code></pre>
  
  <p>The end result should just be a monotone 2x2 image. Some examples I've found set some more header values, such as <code>imageSize</code> to zero, but other examples treated them as I have here. </p>