How do I extract a monochrome image embedded in PDF as 8bpp grayscale?

Q: We are converting embedded PDF images to a format that does not
understand 1 bpp images. In order to have the transparency come
through, we need to provide a color table (easily obtained from the
gstate) and a byte[] representing the pixel data. Each byte in the new
file must be an index into the color table, so in this case each 8
bits must be 0000 0000 or 0000 0001. I am getting the byte array:

                    pdftron.PDF.Image image = new
                    int out_data_sz = image.GetImageDataSize();
                    Filter img_conv = image.GetImageData();
                    FilterReader reader = new FilterReader(img_conv);
                    byte[] image_data_out = new byte[out_data_sz];

This yields a byte[] of a 1bpp file such as 0101 0100, which needs to
be converted to a byte[] representing a 8bpp file. I was assuming that
this would produce a byte array where each bit represents a single
pixel. However, when I expand each bit to a byte, it produces an
incorrect image. I convert it as follows:

            byte[] _out = new byte[bytes.Length * 8];
            int outPtr = 0;
            for (int x = 0; x < bytes.Length; x++)
                _out[outPtr++] = Convert.ToByte((bytes[x] & 128) >>
                _out[outPtr++] = Convert.ToByte((bytes[x] & 64) >> 6);
                _out[outPtr++] = Convert.ToByte((bytes[x] & 32) >> 5);
                _out[outPtr++] = Convert.ToByte((bytes[x] & 16) >> 4);
                _out[outPtr++] = Convert.ToByte((bytes[x] & 8) >> 3);
                _out[outPtr++] = Convert.ToByte((bytes[x] & 4) >> 2);
                _out[outPtr++] = Convert.ToByte((bytes[x] & 2) >> 1);
                _out[outPtr++] = Convert.ToByte((bytes[x] & 1));

I pass in the byte[] image_data_out as returned by GetImageData(). I
have stepped through my code, and it is producing what is expected. In
fact, I have stepped through this part of my code with an entire image
and it produced an output as expected, with 1 byte in the new file
representing 1 bit in the original. . The data that I am getting from
getImageData does not seem to accurately represent these 1bpp images
as I had expected, yet if I just use the original image and save it as
a jpeg, it is correct. Am I misinterpreting how this image data is
produced? Is there a better way using PDFnet to convert a 1bpp indexed
image to a 8bpp indexed image? (I realize that this is inefficient,
but the technology we are converting to does not understand 1bpp
indexed files, so our hands are tied.)
A: It is hard to say what could be the problem without taking a look
at the file. For example, an image may have a ‘Decode’, ‘Mask’, and
‘ColorSpace’ parameters that may affect how the image looks on the

Another, possibly simpler way to convert monochrome image to grayscale
would be using pdftron::PDF::Image::GetBitmap() utility method.
Basically this method would return a System.Drawing.Bitmap which you
could then draw on another System.Drawing.Bitmap which is using a 8bpp
pixel format. For more information of this topic you may want to refer
to MSDN and other resources related to GDI+.