xwd is a program that allows you to read the data from any X window (and dumps it in a file).
If you download the XFree86 source code (the XFree86*src-2.tgz file will suffice) you will find the source code to 'xwd' in one of the subdirectories.
This should provide you with sufficient detail, depending on your goals.
Most of the function calls have man pages or you can reference them at
http://tronche.com/gui/x/xlib/function-index.html .