简体   繁体   English

如何在Java中将索引图像转换为RGB图像?

[英]How to convert an indexed image to a RGB image in Java?

I have a color indexed TIFF image (8-bits) and I want to convert it to a RGB 24-bits image (not indexed). 我有一个彩色索引的TIFF图像(8位),我想将其转换为RGB 24位图像(未索引)。 What would be the way to do that? 这样做的方式是什么?

I'm using JMagick. 我正在使用JMagick。 In a weird way, it works fine for indexed 8-bits images that are grayscale when i use: 以一种怪异的方式,当我使用灰度级索引的8位图像时,它工作得很好:

image.transformRgbImage(info.getColorspace());

even if the image, though not indexed any more, is still 8-bits after that, which is lucky as it is grayscale and should actually be 8-bits. 即使该图像不再索引,但此后仍为8位,这是幸运的,因为它是灰度图像,实际上应该是8位。 The weird stuff is that the transformRgbImage() performs that although I'd rather expect it to convert the image to a 24-bits one. 奇怪的是,transformRgbImage()会执行该操作,尽管我希望它可以将图像转换为24位图像。 Anyway... 无论如何...

The same way doesn't work for a color indexed 8-bits image. 相同的方法不适用于彩色索引的8位图像。 I just don't know how to use the JMagick API to achieve that goal. 我只是不知道如何使用JMagick API来实现该目标。 I tried setting: 我尝试设置:

image.setDepth(24);

or: 要么:

info.setDepth(24);

but both result in an EXCEPTION_ACCESS_VIOLATION. 但两者都会导致EXCEPTION_ACCESS_VIOLATION。 When I set: 当我设定时:

info.setDepth(32);

no exception is raised, 1) but the image is 32-bits, which shouldn't be, and 2) it is all black (1 unique color). 不会引发异常,1)但是图像是32位(不应该这样),并且2)全部为黑色(1种唯一颜色)。 Why does the setDepth(24) raises such an exception?? 为什么setDepth(24)会引发这样的异常? How should I do? 我应该怎么做?

Thanks in advance for your help. 在此先感谢您的帮助。

I dont know about jmagick, but generally once you created an image object its properties are fixed (size and color model). 我不了解jmagick,但是通常一旦创建了图像对象,其属性就会固定(大小和颜色模型)。

You don't change an images properties, you create a new image with the desired target properties and paint your original image into the new image. 您无需更改图像属性,而是使用所需的目标属性创建图像,然后将原始图像绘制到新图像中。 In plain core java you would simply do it like this: 在普通核心Java中,您可以像这样简单地进行操作:

public BufferedImage toRGB(Image i) {
    BufferedImage rgb = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
    rgb.createGraphics().drawImage(i, 0, 0, null);
    return rgb;
}

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

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