I need to write an image to a .png with a 255-color indexed color model. I know usually do it like this:
BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_INDEXED,model);
but that doesn't work with models with 255 colors (as opposed to 256 colors). I'm fairly sure it is the BufferedImage creation that is the problem, as when I call model.getMapSize(), it returns the correct size.

The extra color added to the image's index is 15,15,15. I am drawing the BufferedImage by drawing a regular (BufferedImage.TYPE_INT_RGB) BufferedImage, pixel by pixel, then transposing it onto the indexed BufferedImage by drawing on it with a Graphics2D object.

Should I be something other than a BufferedImage to write the image, or should I be using a different constructor for BufferedImage, or am I doing something else wrong?