First of all you're right that the native iOS (and Mac OS) frameworks do support JBIG2 images embedded in PDF data streams—actually it's part of Core Graphics.
The public API to read images in iOS is ImageIO. It extends Core Graphics by adding generic image file reading and writing functions. It creates CGImage
objects that can be used in CGContext
s to decompress and render. Sadly it is not able to read jbig2 image files.
On the other hand a PDF containing JBIG2 images can be rendered. That seems to be possible by Core Graphics adding custom filters to CGImage which are used only when rendering PDFs:
> cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/System/Library/Frameworks
> nm -arch armv7 ./CoreGraphics.framework/CoreGraphics | grep jbig2
000f4f6c t _jbig2_create_state
00081e68 t _jbig2_filter_finalize
00081e44 t _jbig2_filter_refill
00081e24 t _jbig2_filter_rewind
000f500c t _jbig2_read_bytes
000f4fc0 t _jbig2_release_state
000f5064 t _jbig2_rewind
0013b78c b _jbig2_vtable
00081d9c t _pdf_source_create_jbig2_filter
001247f0 s _pdf_source_create_jbig2_filter.callbacks
Displaying a PDF in Preview while running Instruments reveals the library where JBIG2 support is implemented:
Here's the actual library:
> nm -arch armv7 ./CoreGraphics.framework/Resources/libJBIG2.dylib | c++filt
...
00001f68 unsigned short JBIG2Bitmap::JBIG2Bitmap(unsigned int, JBIG2Bitmap*)
00007adc unsigned short JBIG2Stream::readGenericBitmap(int, int, int, int, int, int, JBIG2Bitmap*, int*, int*, int)
...
This library seems to include some xpdf-3 code but is mostly Apple's private implementation. There are no headers for this library, so it's to be considered private, especially on iOS.
That leaves us with only one option of how to use iOS native JBIG2 decompression: You have to wrap JBIG2 files into a minimal PDF. I don't think that the runtime overhead is relevant.
Addition to illustrate comment: Code to create an image from a PDF. This assumes that the PDF consists of one page that contains the JBIG2 image borderless in 72 dpi.
// create PDF document
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path]);
// get the first page
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
// create a bitmap context
CGSize size = CGPDFPageGetBoxRect(page, kCGPDFMediaBox).size;
UIGraphicsBeginImageContextWithOptions(size, YES, 1);
// flip the context
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, mediaBox.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
// draw the page into the bitmap context
CGContextDrawPDFPage(UIGraphicsGetCurrentContext(), page);
CGPDFDocumentRelease(document);
// get the image from the context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// save image as PNG file
[UIImagePNGRepresentation(image) writeToFile:somePath atomically:YES];