Image Masking: Now with Vector Paths

In my last article I ended on a small tease:

At a lull in between, I took a small break to put together a quick DLE program to demonstrate how one masks an Image with a vector path, which I’ll discuss in a follow-up article.

Since the solution isn’t as exciting as you might think, I’m going to discuss the other ways that you can mask an image before revealing the solution and discussing it, because there are lots of ways to mask an image in PDF, so it’s a bit more interesting a topic than you might think.

If you search the PDF Specification (as I did just now) you will find that there are essentially two sections that discuss Masking: Section 8.9.6 Masked Images and section 11.6.5 Specifying Soft Masks, and neither discusses masking with vector paths.

Soft Masks are part of the chapter on transparency and come in two flavors: Images and dictionaries.  If you are familiar with RGBA images, an image soft mask is essentially the Alpha (transparency) channel separated from the RGB image and made to stand-alone within the parent image XObject.  Which is essentially what DLE does with PNG files that have RGBA images.  SoftMask dictionaries, on the other hand, are part of the extended graphic state, and while they contain a Transparency Group Form XObject, which could contain a vector path, it’s not the mechanism that comes to mind as the ideal way to mask an image with a vector path.

Masked images come in three flavors; Stencil Masking, Explicit Masking, and Colour Key Masking. Explicit Masking is like an image soft mask except that the alpha channel pixel can only be all on or all off, so there’s no degree of transparency.  Colour key masking is a bit like how transparency works in GIF files, where one of the indexed colors is designated as the transparency color, but colour key masking you can specify a range for each color component of the image; if all the components values fall within those ranges, then that pixel is masked (or not depending on the Decode parameter, but that’s the idea). Stencil Masking is like Explicit Masking, except that you have thrown away the primary image and are masking whatever is already in the background.

None of these variants allow for vector paths, so how do you mask with a vector path? Masking is the right concept, but the wrong term in this case because to mask an image with a vector path, you simply apply a clipping path to the image.
Or at least, that was the theory, and I needed some code to prove it.  I decided to clip this image:


with a 17 point star created with the following code:

The result was: VectorPath_After1
However, I was then challenged by my neighbor at the table to turn the clip inside out, or effectively Outside In(!).

Appending the star to the exist clip path, which contains a rectangle around the entire image, required a slight alteration to how I created the star:

And this time the result was:VectorPath_After2

Mission accomplished: An image masked with a vector path.

The full code is here.

Evaluate the Adobe PDF Library for free!

Share this post with your friends

Get instant access to the latest PDF news, tips and tricks!

Do you want monthly updates on the latest document technology trends?

By submitting the form, you agree to receive marketing emails from Datalogics. You may unsubscribe at any time. 

Like what you're reading?

Get Datalogics blogs sent right to your inbox!

By submitting the form, you agree to receive marketing emails from Datalogics. You may unsubscribe at any time.