有的图像是透明的,但是透明的不完全,就是有一些假的透明色。
public void toPureWhite(ImageData imageData) { int redShift = imageData.palette.redShift; int greenShift = imageData.palette.greenShift; int blueShift = imageData.palette.blueShift; int[] lineData = new int[imageData.width]; int r,g,b,pixelValue; for (int y = 0; y < imageData.height; y++) { // Analyze each pixel value in the line imageData.getPixels(0,y,imageData.width,lineData,0); for (int x=0; x<lineData.length; x++) { pixelValue = lineData[x]; r = pixelValue & redShift; g = (pixelValue & greenShift) >> 8; b = (pixelValue & blueShift) >> 16; if (r > 230 && g > 230 && b > 150) imageData.setPixel(x,y,0xFFFFFF); } } }
上面提供的将一些与白色相近的点,转化为真正的白色。
其实Palette有去RGB的方法,就是每次新建的RGB的对象,但是上面的方法应该不是太通用。
public RGB getRGB(int pixel) { if (isDirect) { int r = pixel & redMask; r = (redShift < 0) ? r >>> -redShift : r << redShift; int g = pixel & greenMask; g = (greenShift < 0) ? g >>> -greenShift : g << greenShift; int b = pixel & blueMask; b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; return new RGB(r, g, b); } else { if (pixel < 0 || pixel >= colors.length) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } return colors[pixel]; } }
这是PaletteData里面的方法,对直接图和索引图都是有效的。