JAI:如何将4波段32位CMYK图像转换为PNG



我正在尝试将图像转换为png格式,我拥有的数据是一个由LZW压缩的4频32位TIFF的图像。通过使用Java2d和Jai现在,我有了未压缩的数据来表示CMYK空间中的颜色,并且在将其存储在TIFF中的情况下,可以将其导出和查看,其设置与4个频段32位格式相同。

问题是,当我尝试转换为PNG等其他格式时,它会产生零尺寸的数据,所以我想问是否有人在转换此类图像方面有类似的经验?我的一些代码粘贴在下面供您参考,也请纠正,如果您发现任何错误,谢谢!

int bands = 4;
int w = sizeParam.getHorizonPts();
int h = sizeParam.getVerticalPts();
ColorModel cm = new ComponentColorModel(new CMYKColorSpace(), new int[]{8,8,8,8},
                false, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
// Create WritableRaster with four bands
WritableRaster r = RasterFactory.createBandedRaster(
                DataBuffer.TYPE_FLOAT, w, h, bands, null);
for (int i = 0; i < bandStreams.length; i++) {
        int x, y;
        x = y = 0;
        byte[] uncomp = new byte[w * h];
        decoder.decode(bandStreams[i], uncomp, h);
        for (int pos = 0; pos < uncomp.length; pos++) {
                r.setSample(x++, y, i, (float) (uncomp[pos] & 0xff) / 255);
                if (x >= w) {
                        x = 0;
                        y++;
                }
        }
}
// Create TiledImage
TiledImage tiledImage = new TiledImage(0, 0, w, h, 0, 0,
                RasterFactory.createBandedSampleModel(DataBuffer.TYPE_FLOAT, w,
                                h, bands), cm);
tiledImage.setData(r);
JAI.create("filestore", tiledImage, "test.tif", "TIFF");

我最终通过将CMYK转换为RGB来解决此问题,以便它可以生成PNG图像,在课程中使用以下代码,

// Create target image with RGB color.
BufferedImage result = new BufferedImage(w, h,
            BufferedImage.TYPE_INT_RGB);
// Convert pixels from YMCK to RGB.
ColorConvertOp cmykToRgb = new ColorConvertOp(new CMYKColorSpace(),
            result.getColorModel().getColorSpace(), null);
cmykToRgb.filter(r, result.getRaster());

最新更新