简体   繁体   English

Java AffineTransformOp的奇怪行为

[英]Strange behaviour of java AffineTransformOp

I can correctly use AffineTransformOp.filter on a png/jpeg image. 我可以在png / jpeg图像上正确使用AffineTransformOp.filter。 But when I subclass the AffineTransformOp it throws an exception! 但是,当我将AffineTransformOp子类化时,它将引发异常! The following minimal example shows this: 以下最小示例显示了这一点:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.io.*;

public class Strange  extends JPanel {


  public static void main(String args[]) throws Exception {
      BufferedImage from = (BufferedImage) ImageIO.read(new File(args[0]));
      MyTrans at = new MyTrans();
      AffineTransformOp at2 = new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_BILINEAR);
      System.err.println("Bad = "+at.getTransform());
      System.err.println("Good = "+at2.getTransform());
      BufferedImage bi1 = at.filter(from,null);//1
      BufferedImage bi2 = at2.filter(from,null);//2
  }
}

class MyTrans extends AffineTransformOp {

    public MyTrans() {
        super(new AffineTransform(), TYPE_BILINEAR);
    }

}

The line marked with 1 throws an ImagingOpException:Unable to transform src image . 标记为1的行抛出ImagingOpException:Unable to transform src image But I comment out that line, then the next line (which does exactly the same thing) runs smoothly. 但是我注释掉该行,然后下一行(功能完全相同)顺利运行。 The transform here is just the identity transform. 这里的转换只是身份转换。 I am using java 8 on Ubuntu 16.04. 我在Ubuntu 16.04上使用Java 8。 Any png or jpeg image causes thiso SO I am not attaching the image. 任何png或jpeg图像都会导致此,所以我没有附加图像。

AffineTransformOp.filter(src, dst) internally calls ImagingLib.filter(this, src, dst) from package sun.awt.image.ImagingLib , on line 284. AffineTransformOp.filter(src,dst)在行284上从包sun.awt.image.ImagingLib内部调用ImagingLib.filter(this,src,dst)。

 ...
   if (ImagingLib.filter(this, src, dst) == null) {
        throw new ImagingOpException ("Unable to transform src image");
    }
 ...

The behavior we are observing is related on how ImagingLib.filter() is implemented. 我们观察到的行为与ImagingLib.filter()的实现方式有关。 This method checks if the first parameter corresponds to any of these classes: 此方法检查第一个参数是否对应于以下任何类:

  • LookupOp LookupOp
  • AffineTransformOp AffineTransformOp
  • ConvolveOp 的ConvolveOp

When ImagingLib.filter is called the first parameter passed to the function is this . 调用ImagingLib.filter时,传递给该函数的第一个参数是this In the sample code you wrote: at.class is equal to "MyTrans" and at2.class is equal to "AffineTransformOp". 在您编写的示例代码中: at.class等于“ MyTrans”, at2.class等于“ AffineTransformOp”。

Take a look at the code below, it belongs to ImagingLib.filter() source code. 看一下下面的代码,它属于ImagingLib.filter()源代码。

 public static BufferedImage filter(BufferedImageOp var0, BufferedImage var1, BufferedImage var2) {

  ...
        BufferedImage var3 = null;
        switch(getNativeOpIndex(var0.getClass())) {
        case 0:
            LookupTable var4 = ((LookupOp)var0).getTable();
            if (var4.getOffset() != 0) {
                return null;
            }

            if (var4 instanceof ByteLookupTable) {
                ByteLookupTable var9 = (ByteLookupTable)var4;
                if (lookupByteBI(var1, var2, var9.getTable()) > 0) {
                    var3 = var2;
                }
            }
            break;
        case 1:
            AffineTransformOp var5 = (AffineTransformOp)var0;
            double[] var6 = new double[6];
            AffineTransform var7 = var5.getTransform();
            var5.getTransform().getMatrix(var6);
            if (transformBI(var1, var2, var6, var5.getInterpolationType()) > 0) {
                var3 = var2;
            }
            break;
        case 2:
            ConvolveOp var8 = (ConvolveOp)var0;
            if (convolveBI(var1, var2, var8.getKernel(), var8.getEdgeCondition()) > 0) {
                var3 = var2;
            }
        }

        if (var3 != null) {
            SunWritableRaster.markDirty(var3);
        }

        return var3;
    }
}

So, when getNativeOpIndex(var0.getClass()) is invoked, because "MyTrans" is not the class type expected, it returns -1 therefore it does not mach any case on the switch and null is returned. 因此,当调用getNativeOpIndex(var0.getClass())时 ,由于“ MyTrans”不是预期的类类型,因此它返回-1,因此在交换机上不存在任何情况,并且返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM