Skip to main content

ColorQuantizerDescriptor's create method returning null images systematically

1 reply [Last post]
aurelienporte
Offline
Joined: 2012-10-21
Points: 0

Hello!
I'm having some troubles with JAI API to quantize avatars in 255 colors. I would like to use ColorQuantizerDescriptor.create method with median-cut algorithm but the method always returns an empty image. I'm not sure what I'm doing wrong.
I've check many times the doc about this method so I'm kind of convince of the arguments I use: before calling the method, I'm converting the source image in RGB (3 bands) and I checked the validity of the rendering hints, others are basic arguments.
Here is my method:

private BufferedImage convertToIndexedColor(BufferedImage source) {
if (source == null) {
return source;
}
RenderingHints rh;
if (((Graphics2D) source.getGraphics()) == null) {
rh = null;
}
else {
rh = ((Graphics2D) source.getGraphics()).getRenderingHints();
}
int numBands = source.getSampleModel().getNumBands();
BufferedImage rgbImg = source;
if ( numBands == 4 ){
BufferedImage argbImg = switchFullTransparentPixToUniqueRGBColor(source);
rgbImg = convertARGBToRGBImage(argbImg);
}
else if (numBands != 3) {
assert false;
}

RenderedOp medianCutQuantizerOp = ColorQuantizerDescriptor.create(rgbImg, ColorQuantizerDescriptor.MEDIANCUT, 255, 32768, null, 1, 1, rh);//1 color for full transparency
assert medianCutQuantizerOp.getColorModel() instanceof IndexColorModel;
BufferedImage indexImg = medianCutQuantizerOp.getAsBufferedImage(null, medianCutQuantizerOp.getColorModel());
return indexImg;
}

1) Did anyone already had the same issue? Or would you have any idea of what I'm doing wrong?
2) Besides, I'm keeping 1 color for full transparency (that explains the 255 instead of 256 in the create function arguments) but how can I add this specific color to my palette after the quantization. I still haven't found a way of modifying the index table, even after some research...
Thanks a lot!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aurelienporte
Offline
Joined: 2012-10-21
Points: 0

Ok so I found this sentence in the doc:

"The supported source image data type is implementation-dependent. For example, the Sun implementation will support only the byte type."

I first thought that it was the origin of my problem. But even if I add the following lines of code before calling ColorQuantizer.create method, it still outputs a renderedOp with only null attributes...

BufferedImage testByteImg = new BufferedImage(rgbImg.getWidth(), rgbImg.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D testG = testByteImg.createGraphics();
testG.drawImage(rgbImg, 0, 0, rgbImg.getWidth(), rgbImg.getHeight(), null);
int testDataType = testByteImg.getSampleModel().getDataType();

I know that what I'm doing here doesn't have any sense because I'm loosing the colors before even calling the ColorQuantizerDescriptor.create method but that's only suppose to be a test. And even the test is unsuccessful!

Any ideas?