title: Why I need the pixel format to decompress images with libjpeg turbo
body: <p><strong>Background story:</strong> Right now I am working on an image encode and decode service. I will attach the service to a sensor (ex. camera) like to compress the image, send it over the network to receive it at the end with a client service, where the image is finaly decompressed and used. For this whole compression/decompression stuff I decided to use libJPEG Turbo, because it is the fastest library that I found.</p>
  
  <p><strong>Problem:</strong> In my mind a JPEG file is self describing and it should be possible to read it with every tool. But why I need for the compression and decompression with libJPEG Turbo additionally the pixel format (GRAY, RGB, BGR, RGBX, ...)? How can I extract/decompress the pixel format from the compressed JPEG (Byte Array)?</p>
  
  <p><strong>My code - compression (c++):</strong></p>
  
  <pre><code>tjhandle _jpegCompressor = tjInitCompress();
  tjCompress2(_jpegCompressor, buffer, width, 0, height, TJPF_RGB, &amp;_compressedImage, &amp;_jpegSize, TJSAMP_444, JPEG_QUALITY, TJFLAG_FASTDCT);
  tjDestroy(_jpegCompressor);
  </code></pre>
  
  <p><strong>My code - decompression (c++):</strong></p>
  
  <pre><code>int width, height;
  tjDecompressHeader(_jpegDecompressor, _compressedImage, _jpegSize, &amp;width, &amp;height);
  size_t size = width*height*channels;
  unsigned char* buffer = new unsigned char[size];
  tjDecompress2(_jpegDecompressor, _compressedImage, _jpegSize, buffer, width, 0, height, TJPF_RGB, TJFLAG_FASTDCT);
  tjDestroy(_jpegDecompressor);
  </code></pre>
  
  <p><strong>Tests:</strong>
  I tested it out and the only importance is that I use for compression and decompression the same pixel format. If I use both times TJPF_BGR (or TJPF_RGB) it works, but if I use for decompression a different pixel format the channels are flipped. </p>
  
  <p>Interestingly If I use the tjDecompress function (not tjDecompress2) I need to compress with TJ_RGB and can decompress with TJ_RGB or TJ_BGR to get a right result... If I start to compress with TJ_BGR the color is flipped at the end. What do I missunderstand?</p>
  
  <pre><code>tjDecompress(_jpegDecompressor, _compressedImage, _jpegSize, buffer, width, 0, height, 3, TJFLAG_FASTDCT);
  </code></pre>