【问题标题】:iText binary transparency bugiText 二进制透明度错误
【发布时间】:2016-08-24 09:44:58
【问题描述】:

我遇到了以下问题,将透明的 java.awt.Image 嵌入到带有 iText 的 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;
    }
}

【问题讨论】:

    标签: itext


    【解决方案1】:

    这是我解决问题的建议:

    在 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;
            }                       
        }
        ...
    }
    

    【讨论】:

    • 您能在 GitHub 存储库上将其作为拉取请求提交吗?这是提交补丁的推荐位置。 github.com/itext/itextpdf
    • 说了这么多,解决方法不完整。
    • cmets (!) 中的建议正确地建议区分alpha == 0alpha == -1 情况,但它还没有检查transparentPixel 是否未被使用之前 i> transparentPixel 已确定。
    • 你同意吗? - 当然我们总是在transparentPixel初始化之前输入那个块。这只是强调了我的意思。在transparentPixel在第一个具有透明度的像素中确定之前,显然无法验证之前的像素具有不同的颜色。因此,设置transparentPixel 值之后的代码必须另外检查该值是否尚未在先前(非透明)像素中使用。否则,您自己的示例代码的简单变体将导致错误。
    • 我在您的提案中添加了缺失的支票。顺便说一句,使用 xor (^) 您可以轻松地将最后两个 else ifs 缩短为一个:else if (((pixels[j] &amp; 0xffffff) == transparentPixel) ^ (alpha == 0)) { shades = true; }
    猜你喜欢
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2012-03-28
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多