[ 
https://issues.apache.org/jira/browse/PDFBOX-513?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12755129#action_12755129
 ] 

Yonas Jongkind commented on PDFBOX-513:
---------------------------------------

Hi Daniel,

We were building the image pragmatically using a buffered image and then 
storing it into the PDF. JPEG does not as you point out support transparency, 
but buffered images do.

How PDF's with JPEG's and transparency work is that the PDF spec stores 2 
images. One is the alpha mask, and the other is the image.  We tried to get it 
to work with TIFF, but that is so poorly supported (in both Java and PDFBox) 
that we gave up.

This code is in essence splitting out those two images and storing them in the 
PDF.

This code only affects the constructor which checks to see if there is 
transparency and if there is records the extra image (the mask) in the PDF. I 
think the constructor code should be:

    /**
     * Construct from a buffered image.
     *
     * @param doc The document to create the image as part of.
     * @param bi The image to convert to a jpeg
     * @throws IOException If there is an error processing the jpeg data.
     */
    public PDJpeg( PDDocument doc, BufferedImage bi ) throws IOException
    {
        super( new PDStream( doc ), "jpg" );
        BufferedImage alpha = null;
        if (bi.getColorModel().hasAlpha()){
        
                alpha = new BufferedImage(bi.getWidth(), bi.getHeight(), 
BufferedImage.TYPE_BYTE_GRAY);
                Graphics2D g = alpha.createGraphics();
                g.setColor(Color.BLACK);
                g.drawRect(0, 0, bi.getWidth(), bi.getHeight());
                g.setColor(Color.WHITE);
                g.drawImage(bi, 0, 0, null);
                    for(int y = 0; y < alpha.getHeight(); y++){
                        for(int x = 0; x < alpha.getWidth(); x++){
                                Color color = new Color(alpha.getRGB(x, y));
                                if(color.getRed() != 0 && color.getGreen() != 0 
&& color.getBlue() != 0){
                                        alpha.setRGB(x, y, 
(Color.white).getRGB());
                                }
                        }
                    }
                
                BufferedImage image = new BufferedImage(bi.getWidth(), 
bi.getHeight(), BufferedImage.TYPE_USHORT_565_RGB);
                g = image.createGraphics();
                g.drawImage(bi, 0, 0, null);
                bi = image;
        }
        
        java.io.OutputStream os = getCOSStream().createFilteredStream();
        try
        {
            ImageIO.write(bi,"jpeg",os);

            COSDictionary dic = getCOSStream();
            dic.setItem( COSName.FILTER, COSName.DCT_DECODE );
            dic.setItem( COSName.SUBTYPE, COSName.IMAGE);
            dic.setItem( COSName.TYPE, COSName.getPDFName( "XObject" ) );
                PDXObjectImage alphaPdImage = null;
            if(alpha != null){
                alphaPdImage = new PDJpeg(doc, alpha);
                dic.setItem("SMask", alphaPdImage);
            }
            setBitsPerComponent( 8 );
            if (bi.getColorModel().getNumComponents() == 3) {
                setColorSpace( PDDeviceRGB.INSTANCE );
            } else if (bi.getColorModel().getNumComponents() == 1) {
                setColorSpace( new PDDeviceGray() );
            } else throw new IllegalStateException();
            setHeight( bi.getHeight() );
            setWidth( bi.getWidth() );
        }
        finally
        {
            os.close();
        }
    }




Code we use to create a sample with transparency is:

BufferedImage bufferedImage = new BufferedImage(100,100, 
BufferedImage.TYPE_INT_ARGB);                              
Graphics2D g2 = bufferedImage.createGraphics();
g2.setBackground(new Color(255,255,255,255));
g2.setColor(Color.red);
g2.drawString("HELLO", 20,20);


Sorry about getImageIndexThingy().... That was for debugging/testing and is not 
used.



> PDJpeg does not support transparency/alpha
> ------------------------------------------
>
>                 Key: PDFBOX-513
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-513
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 0.8.0-incubator
>         Environment: Any
>            Reporter: Yonas Jongkind
>         Attachments: PDJpeg.java
>
>
> The code does not handle this feature. Patch attached.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to