title: How to interpret the field 'data' of an XImage
body: <p>I am trying to understand how the data obtained from <code>XGetImage</code> is disposed in memory:</p>
  
  <pre><code>XImage img = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap);
  </code></pre>
  
  <p>Now suppose I want to decompose each pixel value in red, blue, green channels. How can I do this in a portable way? The following is an example, but it depends on a particular configuration of the XServer and does not work in every case:</p>
  
  <pre><code>for (int x = 0; x &lt; width; x++)
      for (int y = 0; y &lt; height; y++) {
          unsigned long pixel = XGetPixel(img, x, y);   
          unsigned char blue = pixel &amp; blue_mask;
          unsigned char green = (pixel &amp; green_mask) &gt;&gt; 8;
          unsigned char red = (pixel &amp; red_mask) &gt;&gt; 16;
          //...    
      }
  </code></pre>
  
  <p>In the above example I am assuming a particular order of the <code>RGB</code> channels in <code>pixel</code> and also that pixels are 24bit-depth: in facts, I have <code>img-&gt;depth=24</code> and <code>img-&gt;bits_per_pixels=32</code> (the screen is also 24-bit depth). But this is not a generic case. </p>
  
  <p>As a second step I want to get rid of XGetPixel and use or describe img->data directly. The first thing I need to know is if there is anything in <code>Xlib</code> which exactly gives me all the informations I need to interpret how the image is built starting from the <code>img-&gt;data</code> field, which are:</p>
  
  <ol>
  <li>the order of R,G,B channels in each pixel;</li>
  <li>the number of bits for each pixels;</li>
  <li>the numbbe of bits for each channel;</li>
  <li>if possible, a corresponding <a href="http://www.fourcc.org/rgb.php" rel="nofollow">FOURCC</a></li>
  </ol>