DrawImage with IndexColorModel with transparency

流过昼夜 提交于 2019-12-23 17:21:45

问题


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

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