You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Yonas Jongkind (JIRA)" <ji...@apache.org> on 2009/09/06 09:12:57 UTC

[jira] Created: (PDFBOX-513) PDJpeg does not support transparency/alpha

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


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.


[jira] Commented: (PDFBOX-513) PDJpeg does not support transparency/alpha

Posted by "Yonas Jongkind (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/PDFBOX-513?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12751836#action_12751836 ] 

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

And a partial diff as well... Source refomatting made the full diff useless, but it's an easy bit of code to copy from the attached source:

84,85c98
<         if (image != null) 
<         {
---
>         if (image != null) {
103a117,139
>         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;
>         }





> 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.


[jira] Commented: (PDFBOX-513) PDJpeg does not support transparency/alpha

Posted by "Andreas Lehmkühler (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/PDFBOX-513?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12751921#action_12751921 ] 

Andreas Lehmkühler commented on PDFBOX-513:
-------------------------------------------

Thanks for the contribution. Do you have a sample jpg you can provide to do some testing?

> 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.


[jira] Updated: (PDFBOX-513) PDJpeg does not support transparency/alpha

Posted by "Yonas Jongkind (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/PDFBOX-513?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Yonas Jongkind updated PDFBOX-513:
----------------------------------

    Attachment: PDJpeg.java

Version that does support transparency.

> 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.


[jira] Commented: (PDFBOX-513) PDJpeg does not support transparency/alpha

Posted by "Yonas Jongkind (JIRA)" <ji...@apache.org>.
    [ 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.


[jira] Commented: (PDFBOX-513) PDJpeg does not support transparency/alpha

Posted by "Daniel Wilson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/PDFBOX-513?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12753848#action_12753848 ] 

Daniel Wilson commented on PDFBOX-513:
--------------------------------------

Yonas,

That sample would certainly help.

Also a bit of clarification ... certainly the format changes have made a full diff difficult.  But I see several areas of difference between the trunk version and yours.

First is the 2nd constructor ... is that where the code you've pasted above belongs?

Then there's the  public byte[] getImageIndexThingy() at the end of the file.  Is that needed?

Finally ... and maybe I'm missing something ... but is support for JPG transparency really needed ... as JPG files do not support transparency?

Thanks!

Daniel

> 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.