Thanks everyone for your suggestions. The original snippet has an image in the form of a picture control, which is what I would like to extract the mask from. The source for that image is in the WAD file, but isn't a standard graphics format.
I think the easiest option might be to maintain the mask data in a separate picture, and do two lots of picture processing (compositing textures, building map views, etc) on both the picture with mask, and the mask only version. Then when it comes to saving to PNG, the mask can be extracted from the image data of the mask only picture and inserted into the pixmap cluster.
I haven't updated the viewer with the above changes, but if anyone is interested the current viewer source code is here (LV2017), and can be used with the Doom demo WAD.