JAI:如何将4波段32位CMYK图像转换为PNG? [英] JAI: How to convert 4-band 32-bits CMYK image into PNG?

查看:369
本文介绍了JAI:如何将4波段32位CMYK图像转换为PNG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像转换为PNG格式,我的数据是由LZW压缩的4波段32位TIFF图像。现在,通过使用Java2D和JAI,我可以解压缩数据以表示CMYK空间中的颜色,并且可以使用与4波段32位格式相同的设置以tiff格式存储和查看数据。

I'm trying to convert a image into PNG format, the data I have is a 4-band 32-bits TIFF-like image compressed by LZW. By using Java2D and JAI now I have data uncompressed to represent colors in CMYK space and it can be exported and viewed when stored in tiff with the same settings as 4 band 32-bit format.

问题是,当我尝试转换为其他格式(如PNG)时会生成零大小的数据,所以我想问一下是否有人有类似的转换经验图片?我的一些代码粘贴如下,如果您发现任何错误也请更正,谢谢!!

The problem is when I try converting to other formats like PNG it produce zero-sized data, so I'd like to ask is there anyone have similar experience on converting such image? I have some of my code pasted as below for your reference, please also correct if you found any mistake, thanks!!

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图像,在课程中使用以下代码,

I finally solved this by converting CMYK to RGB so it can generate PNG image, the following code is used during the course,

// 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());

这篇关于JAI:如何将4波段32位CMYK图像转换为PNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆