title: Difficulties converting 8-bit greyscale .BMP images to 1-bit .BMP Images
body: <p>I'm looking for a way to convert a BMP image of 256 grey levels to a 1-bit BMP image. The conversion rule is that the bit should be 0 for grey levels from 0 - 127, and 1 from 128 - 255. So far I have attempted to make a program in C. The problem is even though I changed the bits per pixel in the information header to 1 BBP, the output is a black image (I'm using Lena as the input file btw) as if the colour depth is still 8-bit (while in Properties it says 1-bit).</p>
  <p>I tried to open the BMP file in a hex editor and saw that the pixel data had been changed to 0 and 1 as expected and the headers of the input and output files were the same except the colour depth. Am I missing anything? What changes do I need to make for the image to show what is expected?
  <a href="https://i.stack.imgur.com/NKBFc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NKBFc.png" alt="link" /></a></p>
  <p>Here is my program:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  
  #pragma pack(1)     //avoid padding in a struct    
  
  typedef uint8_t  BYTE;
  typedef uint32_t DWORD;
  typedef int32_t  LONG;
  typedef uint16_t WORD;
  
  typedef struct 
  {
      WORD Signature;
      DWORD fSize;
      WORD fReserved_1;
      WORD fReserved_2;
      DWORD fOffset;
  } BITMAP_HEADER;
  
  typedef struct 
  {
      DWORD Size;
      LONG  Width;
      LONG  Height;
      WORD  Planes;
      WORD  BitsPerPixel;
      DWORD Compression;
      DWORD imgSize;
      LONG  ResX;
      LONG  ResY;
      DWORD Color;
      DWORD ImpColor;
  } INFO_HEADER;
  
  typedef struct
  {
      BYTE rgbtBlue;    //since I'm only dealing with 8-bit colour depth 
                        //so only one value is needed I guess?
      // BYTE rgbtGreen;
      // BYTE rgbtRed;
  } RGBTRIPLE;
  
  void BinaryConvert(DWORD height, DWORD width, RGBTRIPLE img[height][width]);
  
  int main()
  {
      FILE* fp, *cp;
      fp = fopen(&quot;Resources/test.bmp&quot;, &quot;rb&quot;);     //bmp is binary file, therefore use &quot;rb&quot; permission
      cp = fopen(&quot;Resources/output.bmp&quot;, &quot;wb&quot;);
  
      BITMAP_HEADER fHeader;
      INFO_HEADER   fInfo;
      
      //read headers
      fread(&amp;fHeader, sizeof(BITMAP_HEADER), 1, fp);
      fread(&amp;fInfo, sizeof(INFO_HEADER), 1, fp);
  
      int width = fInfo.Width;
      int height = fInfo.Height;
  
      int padding = (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4;
      
      RGBTRIPLE (*image)[width] = calloc(height, width * sizeof(RGBTRIPLE));
  
      //read pixel data
      for (int i = 0; i &lt; height; i++)
      {
          fread(image[i], sizeof(RGBTRIPLE), width, fp);
          fseek(fp, padding,  SEEK_CUR);      //skip over padding
      }
  
      //coonvert grey levels into 0 and 1
      BinaryConvert(height, width, image);
  
      //modify file header
      fInfo.noBitsPerPixel = 1;
  
      //write headers into output file
      fseek(cp, 0, SEEK_SET);
      fwrite(&amp;fHeader, sizeof(BITMAP_HEADER), 1, cp);
      fwrite(&amp;fInfo, sizeof(INFO_HEADER), 1, cp);
    
      //write pixel data into output file
      for (int i = 0; i &lt; height; i++)
      {
         fwrite(image[i], sizeof(RGBTRIPLE), width, cp);
         for (int k = 0; k &lt; padding; k++)
         {
             fputc(0x00, cp);
         }
      }
  
      fclose(cp);
      fclose(fp);   
  }
     
  void BinaryConvert(DWORD height, DWORD width, RGBTRIPLE img[height][width])
  {
      for (int i = 0; i &lt; height; i++)
      {
          for (int j = 0; j &lt; width; j++)
          {
              if (img[i][j].rgbtBlue &lt;= 127 &amp;&amp; img[i][j].rgbtBlue &gt;=0)
              {
                  img[i][j].rgbtBlue = 0;
              }
              else if (img[i][j].rgbtBlue &lt;= 255 &amp;&amp; img[i][j].rgbtBlue &gt;= 128)
                  img[i][j].rgbtBlue = 1;
          }
      }
  }
  </code></pre>
  <p><a href="https://imgur.com/a/RIvgBI0" rel="nofollow noreferrer">Input file (lena.bmp). (257 KB)</a></p>
  <p><a href="https://imgur.com/a/wTPGMrL" rel="nofollow noreferrer">Result from program (257 KB)</a></p>