title: jpeg to bmp with 300x300 resolution outputs distorted grayscale image
body: <p>I am using libjpeg on a Ubuntu 14.04 environment to decompress my JPEG image and write it to a BMP file. However, if I have a colored JPEG image with 300x300 resolution(2550x4206 pixels), the output BMP file is in grayscale and image seems dull. Other JPEG images with resolution  200x200, 400x400 and 600x600 will output the correct BMP image. Need your advice on this. Please help. Thank you.</p>
  
  <p>link to JPEG image: <a href="https://drive.google.com/file/d/0B3ob0t07z5xEdmtVVWRicUQ5SGs/view?usp=sharing" rel="nofollow">https://drive.google.com/file/d/0B3ob0t07z5xEdmtVVWRicUQ5SGs/view?usp=sharing</a></p>
  
  <p>Preview for BMP is not available. Download and view the image in Ubuntu or a Linux environment system. I have no idea why it doesn't display the image in Google or even in Windows.</p>
  
  <p>link to output BMP image: <a href="https://drive.google.com/file/d/0B3ob0t07z5xEZTMycVRVX3Vscnc/view?usp=sharing" rel="nofollow">https://drive.google.com/file/d/0B3ob0t07z5xEZTMycVRVX3Vscnc/view?usp=sharing</a></p>
  
  <p>Code snippet writing decompressed JPEG image to BMP file:</p>
  
  <pre><code>struct jpeg_decompress_struct cinfo;
  unsigned int bytesPerRow = cinfo.output_width * cinfo.num_components;
  unsigned int colColor;
  FILE *bmpFile = NULL;
  
  while (cinfo.output_scanline &lt; cinfo.image_height) {
  
      JSAMPROW row_pointer[1];
      row_pointer[0] = raw_image
              + cinfo.output_scanline * bytesPerRow;
      jpeg_read_scanlines(&amp;cinfo, row_pointer, 1);
      for (colColor = 0; colColor &lt; cinfo.image_width; colColor++) {
  
          /* BMP scanlines should be aligned at 4-byte boundary */
  
      }
  
      /* write each row to bmp file */
      fwrite(row_pointer[0], 1, bytesPerRow, bmpFile);
  }
  </code></pre>
  
  <p>BMP FILE: (bit fields are set byte by byte)</p>
  
  <pre><code>typedef struct {
      unsigned int img_bits_per_pixel;
      unsigned int img_scansize;
      unsigned int img_width;
      unsigned int img_height;
  } Image_Information;
  
  Image_Information *image_info;
  image_info = (Image_Information *) malloc(sizeof(Image_Information));
  
  image_info-&gt;img_height = cinfo.image_height;
  image_info-&gt;img_width = cinfo.image_width;
  image_info-&gt;img_scansize = ((image_info-&gt;img_width * 24 + 31) &amp; ~31) / 8;
  
  BITMAPFILEHEADER:
  bfType = "BM";
  bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + image_info-&gt;img_scansize * image_info-&gt;img_height;
  bfReserved1 = 0;    
  bfReserved2 = 0;
  bfOffBits = 54;
  
  BITMAPINFOHEADER:
  biSize  = sizeof(BITMAPINFOHEADER); 
  biWidth = image_info-&gt;img_width;
  biHeight = image_info-&gt;img_heigh;
  biPlanes = 1;
  biBitCount = 24;
  biCompression = 0;
  biSizeImage = 0;
  biXPelsPerMeter = 0;
  biYPelsPerMeter = 0;
  biClrUsed = 0;
  biClrImportant = 0;
  </code></pre>