How do I process an explicit image mask when extracting data from PDF?

Q: I having difficulty decoding the base image when explicit masking
is present in a PDF like the PDF created by your ElelemntBuilder

After the Image element is read and I use Image2RGB to get the image
data, it seems to be the mask image not the base image.

How do I get the base image data?

Below is a clippit of my image reading code which I just pulled from
your ElementReaderAdvTest sample.

void ProcessElements::Image(Element image)
  bool image_mask = image.IsImageMask();
  int width = image.GetImageWidth();
  int height = image.GetImageHeight();
  int out_data_sz = width * height * 3;
  int bpp = image.GetBitsPerComponent();

  Image2RGB img_conv(image); // Extract and convert image to RGB 8-bpc
  FilterReader reader(img_conv);

  // A buffer used to keep image data.
  unsigned char* image_data_out = new unsigned char[out_data_sz];
A: Extracting data from an image element will return the base image.

If the base image has a color-key mask you could use Image2RGBA
instead of Image2RGB. In this case the color-key information will be
stored in the extra alpha channel. For explicit masking image
conversion filters (Image2RGBA or Image2RGB) will not work because the
resolution of the explicit mask may be different from the base image
(e.g. you may have a very low-resolution base image with a very high-
rez explicit mask). To process explicit masks you could extract them
from a base image using image.GetMask() method and use another
Image2RGB [or even better GetImageData() - since image mask is always
1BPC] to extract the data. For example:

// C# pseudocode:
Obj mask = image.GetMask();
if (mask != 0) {
   Image explicit_mask(mask);
   Filter stm=mask.GetImageData(); // in C++: AutoPtr<Filter> stm
   FilterReader reader(img_conv); // in C++: *img_conv