iText二进制透明错误

iText二进制透明错误,itext,Itext,我遇到了以下问题:使用iText将透明的java.awt.Image嵌入pdf(使用2.1.7和5.5.9进行了尝试) 我想在这种特殊情况下,当将二进制透明图像从ARGB图像转换为iText图像时,iText无法正确处理它们 当提供的图像仅包含100%和0%的透明像素(即二进制透明),且所有像素均为黑色(不透明像素和透明像素-即图像的所有像素的颜色=黑色,但alpha值为0%或100%)时,透明像素颜色值在内部检测为黑色(我认为这是错误的),这将导致生成的pdf中出现不可见图像 测试用例: im

我遇到了以下问题:使用iText将透明的java.awt.Image嵌入pdf(使用2.1.7和5.5.9进行了尝试)

我想在这种特殊情况下,当将二进制透明图像从ARGB图像转换为iText图像时,iText无法正确处理它们

当提供的图像仅包含100%和0%的透明像素(即二进制透明),且所有像素均为黑色(不透明像素和透明像素-即图像的所有像素的颜色=黑色,但alpha值为0%或100%)时,透明像素颜色值在内部检测为黑色(我认为这是错误的),这将导致生成的pdf中出现不可见图像

测试用例:

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class BinaryTransparencyBug {


    private static Image bkgnd;


    public static void main(String[] args) throws Exception {

        bkgnd = Image.getInstance(new URL("http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/images/berlin2013.jpg"));
        bkgnd.scaleAbsolute(PageSize.A4);
        bkgnd.setAbsolutePosition(0, 0);


        Document document = new Document();
        File file = new File("target/binary_transparency_bug.pdf");
        FileOutputStream outputStream = new FileOutputStream(file);
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();


        addBackground(writer);
        document.add(new Paragraph("Binary transparency bug test case"));
        document.add(new Paragraph("OK: Visible image (opaque pixels are red, non opaque pixels are black)"));
        document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.red,false,null), null));
        document.newPage();

        addBackground(writer);
        document.add(new Paragraph("Suspected bug: invisible image (both opaque an non opaque pixels have the same color)"));
        document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,false,null), null));
        document.newPage();

        addBackground(writer);
        document.add(new Paragraph("Analysis: Aliasing makes the problem disappear, because this way the image is not binary transparent any more"));
        document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,true,null), null));
        document.newPage();

        addBackground(writer);
        document.add(new Paragraph("Analysis: Setting the color of the transparent pixels to anything but black makes the problem go away, too"));
        document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,false,Color.red), null));

        document.close();

    }

    private static void addBackground(PdfWriter writer)
            throws BadElementException, MalformedURLException, IOException, DocumentException {
        PdfContentByte canvas = writer.getDirectContentUnder();
        canvas.saveState();
        canvas.addImage(bkgnd);
        canvas.restoreState();
    }

    // Create an ARGB AWT Image that has only 100% transparent and 0%
    // transparent pixels.
    // All 100% opaque pixels have the provided color "color"
    // All transparent pixels have the Color "backgroundColor"
    public static BufferedImage createBinaryTransparentAWTImage(Color color, boolean alias, Color backgroundColor) {
        Dimension size = new Dimension(200, 200);
        BufferedImage awtimg = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = awtimg.createGraphics();

        if (backgroundColor!=null)
        {
            //Usually it doen't make much sense to set the color of transparent pixels...
            //but in this case it changes the behavior of com.itextpdf.text.Image.getInstance fundamentally!
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0f));       
            g2d.setColor(backgroundColor);
            g2d.fillRect(0, 0, size.width, size.height);
        }
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
        g2d.setColor(color);
        if (alias)
        {
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        }

        BasicStroke bs = new BasicStroke(2);
        g2d.setStroke(bs);
        for (int i = 0; i < 5; i++) {
            g2d.drawLine((size.width + 2) / 4 * i, 0, (size.width + 2) / 4 * i, size.height - 1);
            g2d.drawLine(0, (size.height + 2) / 4 * i, size.width - 1, (size.height + 2) / 4 * i);
        }
        return awtimg;
    }
}
导入java.awt.AlphaComposite;
导入java.awt.BasicStroke;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics2D;
导入java.awt.RenderingHints;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入com.itextpdf.text.BadElementException;
导入com.itextpdf.text.Document;
导入com.itextpdf.text.DocumentException;
导入com.itextpdf.text.Image;
导入com.itextpdf.text.PageSize;
导入com.itextpdf.text.paragration;
导入com.itextpdf.text.pdf.PdfContentByte;
导入com.itextpdf.text.pdf.PdfWriter;
公共类二进制透明bug{
私有静态图像bkgnd;
公共静态void main(字符串[]args)引发异常{
bkgnd=Image.getInstance(新URL(“http://gitlab.itextsupport.com/itext/sandbox/raw/master/resources/images/berlin2013.jpg"));
bkgnd.可缩放溶质(页面大小A4);
bkgnd.setAbsolutePosition(0,0);
文档=新文档();
File File=新文件(“target/binary_transparency_bug.pdf”);
FileOutputStream outputStream=新的FileOutputStream(文件);
PdfWriter writer=PdfWriter.getInstance(文档,outputStream);
document.open();
背景(作者);
添加(新段落(“二进制透明错误测试用例”);
添加(新段落(“确定:可见图像(不透明像素为红色,非不透明像素为黑色)”);
add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.red,false,null),null));
document.newPage();
背景(作者);
添加(新段落(“可疑缺陷:不可见图像(不透明和非不透明像素颜色相同)”);
add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,false,null),null));
document.newPage();
背景(作者);
添加(新的段落(“分析:别名使问题消失,因为这样图像不再是二进制透明的”);
add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,true,null),null));
document.newPage();
背景(作者);
添加(新段落(“分析:将透明像素的颜色设置为除黑色以外的任何颜色都会使问题消失”);
add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,false,Color.red),null));
document.close();
}
私有静态void addBackground(PdfWriter编写器)
抛出BadElementException、MalformedURLException、IOException、DocumentException{
PdfContentByte canvas=writer.getDirectContentUnder();
canvas.saveState();
canvas.addImage(bkgnd);
canvas.restoreState();
}
//创建只有100%透明和0%透明的ARGB AWT图像
//透明像素。
//所有100%不透明像素都具有提供的颜色“颜色”
//所有透明像素的颜色均为“backgroundColor”
公共静态缓冲区图像CreateBaryTransparentAwtImage(颜色、布尔别名、颜色背景色){
尺寸尺寸=新尺寸(200200);
BuffereImage awtimg=新的BuffereImage(size.width、size.height、BuffereImage.TYPE_INT_ARGB);
Graphics2D g2d=awtimg.createGraphics();
if(backgroundColor!=null)
{
//通常设置透明像素的颜色没有多大意义。。。
//但在本例中,它从根本上改变了com.itextpdf.text.Image.getInstance的行为!
setComposite(AlphaComposite.getInstance(AlphaComposite.SRC,0f));
g2d.setColor(背景色);
g2d.fillRect(0,0,size.width,size.height);
}
setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1f));
g2d.setColor(彩色);
如果(别名)
{
g2d.setRenderingHint(renderingHits.KEY\u抗锯齿,renderingHits.VALUE\u抗锯齿\u开启);
g2d.setRenderingHint(RenderingHits.KEY\u插值,RenderingHits.VALUE\u插值\u双线性);
}
基本行程bs=新的基本行程(2);
g2d.调整行程(bs);
对于(int i=0;i<5;i++){
g2d.绘图线((尺寸.宽度+2)/4*i,0,(尺寸.宽度+2)/4*i,尺寸.高度-1);
g2d.绘图线(0,(尺寸高度+2)/4*i,尺寸宽度-1,(尺寸高度+2)/4*i);
}
返回awtimg;
}
}

以下是我的解决方案:

在Image.getInstance中(AWTImage、color、forcebw): 在forceBW=false和color=null的情况下:

for (int j = 0; j < size; j++) {
    byte alpha = smask[j] = (byte) (pixels[j] >> 24 & 0xff);
    /* bugfix by Chris Nokleberg */
    if (!shades) {
        if (alpha != 0 && alpha != -1) { 
            //as soon as there is any pixel with alpha not 0% or 100%
            //switch to smask
            shades = true;
        } else if (transparency == null) {
            //in binary transparency mode, determine the transparentPixel Color to be the 
            //value of the first Pixel we find with 100% transparency
            if (alpha == 0) {
                transparentPixel = pixels[j] & 0xffffff;
                transparency = new int[6];
                transparency[0] = transparency[1] = transparentPixel >> 16 & 0xff;
                transparency[2] = transparency[3] = transparentPixel >> 8 & 0xff;
                transparency[4] = transparency[5] = transparentPixel & 0xff;
                // vvv--- added by mkl
                // Check whether this value for transparent pixels
                // has already been used for a non-transparent one
                // before this position
                for (int jj = 0; jj < j; jj++)
                {
                    if ((pixels[jj] & 0xffffff) == transparentPixel)
                    {
                        // found a prior use of the transparentPixel color
                        // and, therefore, cannot make use of this color
                        // for transparency; we could still use an image
                        // mask but for simplicity let's use a soft mask
                        // which already is implemented here
                        shades = true;
                        break;
                    }
                }
                // ^^^--- added by mkl
            }
        } else if (((pixels[j] & 0xffffff) != transparentPixel) && (alpha==0)) {
            //TB: The above if seems incorrect to me. (EDIT: it was if ((pixels[j] & 0xffffff) != transparentPixel)
            //As soon as we find any pixel that has differnt color from  
            //transparentPixel-Color and alpha 0% or 100% 
            //switch of binary transparency mode.
            //IMHO this should only be done if alpha==0!
            //so the if clause should be 
            //((pixels[j] & 0xffffff) != transparentPixel) && (alpha==0)
            shades = true;
        }
        //TB: Proposed fix:
        else if ((pixels[j] & 0xffffff) == transparentPixel && alpha!=0) {
            //switch of binary transparency mode, if we find any pixel with the transparentPixel-Color,
            //but which is not transparent 
            shades = true;
        }                       
    }
    ...
}
for(int j=0;j>24&0xff);
/*克里斯·诺克伯格的错误修正*/
如果(!阴影){
如果(alpha!=0&&alpha!=-1){
//一旦出现alpha不是0%或100%的像素
//切换到smask
阴影=真实;
}else if(透明度==null){
//在“二进制透明度”模式下,确定要显示的透明度像素颜色