Write to 16 bit BufferedImage TYPE_USHORT_GRAY

蹲街弑〆低调 提交于 2019-12-30 07:11:33

问题


I'm trying to write 16 bit grayscale imagedata to a png using BufferedImage.TYPE_USHORT_GRAY. Normally I write to an image like so:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

then:

image.setRGB(x,y,Color.getRGB);

to set the pixels, and finally:

ImageIO.write(image, "png", new File(path + ".png"));

to write to a png image.

But now I have this as image:

BufferedImage imageGray = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);

How do I go about saving pixels to that format? Using setRGB() with a 16 bit integer doesn't seem to work, when I open the saved png file I see a lot of banding happening.

I tried saving a simple gradient from 0 to 65535 and then using the setRGB() on the grayscale image, and checked the results in Photoshop. I can see the image consists of smaller gradients every 256 rows. I'm guessing either setRGB() or imageIO doesn't work as I would like it to.

Are there workarounds for this? Does imageIO even support the BufferedImage.TYPE_USHORT_GRAY format? Or can it only save 8 bit data? And if it can save 16bit data, how would I go about saving pixel data, preferably in a way like setRGB() works (so for a certain x,y coordinate)?


回答1:


As pst already commented below my question:

Try using the Raster directly?

Accessing the Raster directly solved the problem.




回答2:


From BufferedImage you can read

public static final int TYPE_USHORT_GRAY

Represents an unsigned short grayscale image, non-indexed). This image has a ComponentColorModel with a CS_GRAY ColorSpace.

So try instantiating your own ColorSpace with the CS_GRAY type (ColorSpace.getInstance(ColorSpace.CS_GRAY) should do it I suppose). This object has a method called fromRGB which you should be able to use...




回答3:


You probably need to widen the signed 16bit shorts to ints and remove the sign:

int ushort = (int)(shortData[x][y]) & 0xFFFF;


来源:https://stackoverflow.com/questions/4116858/write-to-16-bit-bufferedimage-type-ushort-gray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!