16-bit images in PDF output
📌 Overview
This article explains how to maintain 16-bit images in the PDF output.
🖼️IDOMImage
In Mako, the IDOMImage class represents an image. Images can be loaded from a file:
const auto mako = IJawsMako::create();
IJawsMako::enableAllFeatures(mako);
...
U8String inputFile = "MyImage.tif";
const auto image = IDOMTIFFImage::create(mako, IInputStream::createFromFile(mako, inputFile));
var mako = IJawsMako.create();
IJawsMako.enableAllFeatures(mako);
...
string inputFile = "MyImage.tif";
var image = IDOMTIFFImage.create(mako, IInputStream.createFromFile(mako, inputFile));
Images are also created by rendering a PDF page. In this example, to a 16 bits-per-sample, CMYK image:
// Create an input
IPDFInputPtr input = IPDFInput::create(mako);
// Get the assembly from the input
String inputFile = "MyFile.pdf";
IDocumentAssemblyPtr assembly = input->open(inputFile);
// Get the document from the assembly
IDocumentPtr document = assembly->getDocument();
// Get first page
IDOMFixedPagePtr fixedPage = document->getPage(0)->getContent();
// Render the page 16bpp, cmyk
IJawsRendererPtr renderer = IJawsRenderer::create(mako);
IDOMImage composite = renderer->render(
fixedPage,
renderResolution,
16,
IDOMColorSpaceDeviceCMYK::create(mako)
);
// Write a TIFF
IDOMTIFFImage::encode(mako, composite, IOutputStream::createToFile(mako, "render.tif"));
// Create a PDF input
using var input = IPDFInput.create(mako);
// Get the assembly from the input
string inputFile = "MyFile.pdf";
using var assembly = input.open(inputFile);
// Get the document from the assembly
using var document = assembly.getDocument();
// Get the first page
using var fixedPage = document.getPage(0).getContent();
// Render the page to a 16bpp CMYK image
using var renderer = IJawsRenderer.create(mako);
using var colorSpace = IDOMColorSpaceDeviceCMYK.create(mako);
using var composite = renderer.render(
fixedPage,
renderResolution,
16,
IDOMColorSpaceDeviceCMYK.create(mako)
);
// Write to a TIFF file
using var outputStream = IOutputStream.createToFile(mako, "render.tif");
IDOMTIFFImage.encode(mako, composite, outputStream);
📗 Instructions
Currently, Mako encodes IDOMImages to 8 bits when writing PDF output, even though the IDOMImage may be 16-bit. This limitation can be circumvented by converting to an IDOMPDFImage. An IDOMPDFImage represents the native format for images in PDF. When Mako writes a page that contains such images to a PDF file, they are passed directly into the output without further processing, thereby preserving 16-bit images. This means that compression of the image must be handled when creating an IDOMPDFImage, but this is easily achieved with a stream creator that supports compression. This function shows it in action.
🪜 Steps
Get image particulars: Get the frame, color space, width, height, bps and
rawBytesproperties.Compress the image: Use a “flate writer”, created using
IOutputStream::createToFlateCompressed()to compress the image.Create the new image: Use
IDOMPDFImage::create()to make the new image which will preserve 16-bit encoding.
⌨️ Sample Code
☑️ Conclusion
Handling 16-bit images in PDF output with Mako involves converting IDOMImages to IDOMPDFImages to preserve their bit depth. By following the outlined steps, users can ensure that their images maintain their quality and integrity when included in PDF files. The provided sample code demonstrates how to achieve this using flate compression, ensuring efficient and effective image handling.
📚 Additional Resources
If you need additional help, see our API documentation for detailed information on class/method usage, or raise a support ticket via our customer portal.