gpt4 book ai didi

java - JAI TIFF 编解码器创建工件

转载 作者:行者123 更新时间:2023-11-30 11:17:15 31 4
gpt4 key购买 nike

我一直在使用 JAI 来写入和读取 TIFF 图像,但我最近在编码灰度图像时遇到了一个问题,我在每个 strip 的末尾得到 7 个黑色 (0) 像素 (8每条线数):

7-black-pixels-artifact on each 8th line

只有在使用下面的 SSCE 将 DEFLATE 压缩设置为 deflate level 0 时,我才能重现它。任何其他值似乎都有效。

有时我也看到图像的 strip 向左移动了 30 像素左右。虽然我无法稳定地重现该问题,但如果您知道一些关于在 JAI 中编码 TIFF 的隐藏技巧,它也可能会解决这个问题。

private static byte[] genImage(int width, int height) {
byte[] pix = new byte[width * height];
Arrays.fill(pix, (byte)0xcf);
for (int j = 0; j < height; j++) {
Arrays.fill(pix, j*width + width/3, j*width + 2*width/3, (byte)0x7f);
}
return pix;
}

public static void main(String[] args) throws Exception {
int width = 256;
int height = 256;
byte[] pix = genImage(width, height);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
System.arraycopy(pix, 0, ((DataBufferByte)img.getRaster().getDataBuffer()).getData(), 0, width * height);
TIFFEncodeParam comp = new TIFFEncodeParam();
comp.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
comp.setDeflateLevel(0); // Changing 0 to values 1-9 "solves" the problem

FileOutputStream fos = new FileOutputStream("bugjai.tiff");
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", fos, comp);
encoder.encode(img);
fos.close();
}

编辑:

我进一步调查检查了 8x8 图像上的文件内容。我将字节内容压缩为 0 级(使用 java.util.zip.Deflater)并得到额外的 11 个字节。我在 strip 的末尾插入了这些字节并修补了 header 以反射(reflect)新的长度(值 4B 而不是 40 在位置 75h) ,!不再是 7 个像素。

Diff OK/NOK

所以 JAI 中似乎存在编码错误,“忘记”在放气时放置/计算字节数。然而,TIFF6 specification在第 15 页的开头说:

The Value is expected to begin on a word boundary; the corresponding Value Offset will thus be an even number

让我感到困惑的是:“开始”指的是文件中的位置? “修补”值从 40 变为 4B,这不是偶数。 JAI 会不会以某种方式计算错误并四舍五入?

附录:

当“压缩”数据大于原始数据时,似乎会出现问题:在这里,当压缩数据为 75 字节长时,原始数据为 64 字节长。 JAI 可能假设图像的最大尺寸不能超过原始数据的尺寸。

最佳答案

因此,在问题中进行一些额外测试(参见编辑和附录)后,JAI 中似乎存在错误,将最大字节数限制为 width * heigth。我猜他们假设没有压缩会增加图像的大小......除非你用 0 因子(我理解为“存储”)放气,在那里你得到像素,加上标题和尾部。由于 JAI 限制了写入的字节数,因此未正确写入尾符(如果有的话),并且数据未完全解码。

教训是:至少使用 1 的放气系数(在 the javadoc 中也标记为 BEST_SPEED/MIN_COMPRESSION)。

关于java - JAI TIFF 编解码器创建工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24475778/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com