private static byte getBitsAsByte(byte[] data, int offset, int length) {
        int offsetBytes = offset / 8;
        int indexBits = offset % 8;
        int rVal = 0;

        // start at data[offsetBytes]...  spill into next byte as needed.
        for (int i = length; --i >= 0;) {
            byte b = data[offsetBytes];
            int test = indexBits == 7 ? 1 : 2 << (6 - indexBits);
            if ((b & test) != 0) {
                if (i == 0) {
                    rVal++;
                } else {
                    rVal += (2 << i - 1);
                }
            }
            indexBits++;
            if (indexBits == 8) {
                indexBits = 0;
                offsetBytes++;
            }
        }

        return (byte) rVal;
    }
<code>loadImage</code> is a manual image loader which is entirely
independent of AWT. OUT: RGB888 or RGBA8888 Image object


@param in
           InputStream of an uncompressed 24b RGB or 32b RGBA TGA
@param flip
           Flip the image vertically
@return <code>Image</code> object that contains the
        image, either as a RGB888 or RGBA8888
@throws java.io.IOException if an I/O error occurs
jme3-core/src/plugins/java/com/jme3/texture/plugins/TGALoader.java