Skip navigation
FrontAlex
Currently Being Moderated

Semi-transparent image

Jul 10, 2013 2:11 AM

Tags: #transparent #image #alpha #xobject #smask

Hi,

 

I'm working on a small and simple pdf generator (just for simple drawing purpose), and I'm trying to manage semi-transparent image (like png).

I've successfully managed non semi transparent image (so with no alpha layer) like jpeg, bmp,... or non semi-transparent png (or removing alpha layer from png to display them).

 

When generating a simple PDF (one page, with one picture drawn on it with a non transparent), I've obtain something like this:

 

%PDF-1.7

1 0 obj <</Type /Catalog /Pages 2 0 R>>

endobj

2 0 obj <</Type /Pages /Kids [4 0 R] /Count 1>>

endobj

3 0 obj

  <<

    /Title (Test)

    /Author (Me)

    /CreationDate(D:20130710104410+02'00')

    /ModDate(D:20130710104410+02'00')

  >>

endobj

4 0 obj <</Type /Page /Parent 2 0 R /MediaBox [0 0 595 841] /Resources 5 0 R /Contents 6 0 R>>

endobj

5 0 obj <<

/XObject <</Im1   7 0 R >>

>>

endobj

6 0 obj

<</Length 33>>

stream

q

149 0 0 66 50 725 cm

/Im1 Do

Q

endstream

endobj

7 0 obj <<

/Type /XObject

/Subtype /Image

/Width 149

/Height 66

/ColorSpace /DeviceRGB

/BitsPerComponent 8

/Filter /FlateDecode

/Length 338

>>stream

BINARY DATA STREAM

endstream

endobj

xref

0 8

0000000000 65535 f

0000000009 00000 n

0000000056 00000 n

0000000111 00000 n

0000000413 00000 n

0000000515 00000 n

0000000563 00000 n

0000000643 00000 n

trailer <</Size 8 /Root 1 0 R /Info 3 0 R>>

startxref

1147

%%EOF

 

 

According to the pdf specification, I'v tried to manage the alpha layer of image by using SMask like this:

Add a /SMask X o R (where X will be 8 in my example) to my XObject Image (7 0 obj in my previous example); and then generating the new 8 0 obj with only the alpha layer as data.

My SMask object has the same width/height of my Image (it's the alpha layer), and each pixel varying from 0 to 255 (alpha opacity value), extracted and encoded as the same way of my RGB image.

 

So, if I update my example, I have:

 

%PDF-1.7

1 0 obj <</Type /Catalog /Pages 2 0 R>>

endobj

2 0 obj <</Type /Pages /Kids [4 0 R] /Count 1>>

endobj

3 0 obj

  <<

    /Title (Test)

    /Author (Me)

    /CreationDate(D:20130710104410+02'00')

    /ModDate(D:20130710104410+02'00')

  >>

endobj

4 0 obj <</Type /Page /Parent 2 0 R /MediaBox [0 0 595 841] /Resources 5 0 R /Contents 6 0 R>>

endobj

5 0 obj <<

/XObject <</Im1   7 0 R >>

>>

endobj

6 0 obj

<</Length 33>>

stream

q

149 0 0 66 50 725 cm

/Im1 Do

Q

endstream

endobj

7 0 obj <<

/Type /XObject

/Subtype /Image

/Width 149

/Height 66

/ColorSpace /DeviceRGB

/BitsPerComponent 8

/SMask 8 0 R

/Filter /FlateDecode

/Length 338

>>stream

BINARY DATA STREAM

endstream

endobj

8 0 obj <<

/Type /XObject

/Subtype /Image

/Width 149

/Height 66

/ColorSpace /DeviceGRAY

/BitsPerComponent 8

/Filter /FlateDecode

/Length 1092

>>stream

BINARY DATA STREAM

endstream

endobj

xref

0 9

0000000000 65535 f

0000000009 00000 n

0000000056 00000 n

0000000111 00000 n

0000000413 00000 n

0000000515 00000 n

0000000563 00000 n

0000000643 00000 n

0000001160 00000 n

trailer <</Size 9 /Root 1 0 R /Info 3 0 R>>

startxref

2420

%%EOF

 

 

 

Using SMask is the right way to manager an alpha layer, or not?

I've tried to generate a similar pdf from word with a pdf printer driver, but it seems not generating a SMask tag and object.

Must the SMask object (my 8 0 obj) must be reference to the pdf resources (5 0 obj)?

 

In fact, my pdf is not displayed in Adobe Pdf Reader (last version, an error is popup saying that there is an error into my pdf) but it displayed correctly with another pdf reader (PDF Architect).

 

Regards,

 

Alex

 
Replies
  • Currently Being Moderated
    Jul 10, 2013 3:49 AM   in reply to FrontAlex

    1. There are of course many ways to do transparency, but where transparency is to be varied using raster data, an SMask is the usual way. For the SMask to apply to a single image, it can be in the image dictionary. Otherwise, it can be in the ExtGState and apply to all painting operations including images.

     

    2. When you generate a PDF you will often find transparency is flattened, and it always will be on print-to-PDF models, so you need to look at true transparency such as that generated by InDesign.

     

    3. If you have a PDF with errors, please share it. (Your web site, acrobat.com, file sharing site). Pasting PDFs as text rarely shows what the true error is, and prevents those of use with useful tools from using them. For example we cannot tell whether your xrefs are correct, or whether DeviceGRAY is a typo in the original code or just in your transcription.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 10, 2013 4:53 AM   in reply to Test Screen Name

    #3 - DeviceGray

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points