问题
Here’s my problem : I would like to apply transformations (translate + rotate + clip) on a BufferedImage based on an IndexColorModel with transparency ( index 0 is my transparent pixel, index 1 is black, index 2 is white, and so on…)
The source image (ie, before transformations) is instantiated as this and then, valuated by reading data from a file:
// Color Model
IndexColorModel cm;
cm = new IndexColorModel(7, length, colors[0], colors[1], colors[2], 0);
// Source image.
BufferedImage source;
source = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, cm);
Then, the new image is created like that :
AffineTransform tr = new AffineTransform();
// Valuating AffineTransform […]
IndexColorModel cm2 = (IndexColorModel ) source.getColorModel();
BufferedImage result = new BufferedImage( newWidth, newHeight, BufferedImage.TYPE_BYTE_INDEXED, cm2);
Graphics2D graphics = (Graphics2D) result.getGraphics();
Polygon polygon = new Polygon (xList, yList, points.length) ;
graphics.setClip(polygon);
graphics.drawImage(source, tr, null);
The BufferedImage result transformations are ok, but there is a problem with colors : the transparent pixels are transparent (ok) but the black pixels seem to be trasparent now too. As my IndexColorMap was badly read. By using TYPE_INT_ARGB, it seems to be ok, but not possible for me because the images are very large (memory limitation).
I made another test with AffineTransformOp like that :
AffineTransformOp op = new AffineTransform(tr, AffineTransformOp.TYPE_NEAREST_NEIGBOR);
op.filter(source, result);
The display is ok (black is black ;) ), but I don't know how to apply the clipping operation. However, it makes me think that the IndexColorModel is ok.
What am I doing wrong ?... What's the problem with drawing image with an IndexColorModel with transparency ? Could you help me to find a solution to this problem, respecting my memory constraints ?...
来源:https://stackoverflow.com/questions/43628358/drawimage-with-indexcolormodel-with-transparency