我想用ImageIO或ImageWrite写16位带符号的缓冲图像,但它会出现以下错误。
java.lang.IllegalArgumentException: Raster ShortInterleavedRaster: width = 512 height = 512 #numDataElements 1 is incompatible with ColorModel IndexColorModel: #pixelBits = 8 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@1ce0072 transparency = 2 transIndex = 0 has alpha = true isAlphaPre = false
代码:
BufferedImage buff = dcmParser.getImage();//gives 16 bit signed buffered image
ColorModel cm = buff.getColorModel();
IndexColorModel icm = (IndexColorModel) cm;
WritableRaster raster = icm.createCompatibleWritableRaster(buff.getWidth(), buff.getHeight());
//WritableRaster raster = buff.getRaster();
int pixel = raster.getSample(0, 0, 0);
int size = icm.getMapSize();
byte[] reds = new byte[size];
byte[] greens = new byte[size];
byte[] blues = new byte[size];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
buff = new BufferedImage(icm2, raster, false, null);