1 11 package org.eclipse.jface.resource; 12 13 import org.eclipse.swt.graphics.ImageData; 14 import org.eclipse.swt.graphics.PaletteData; 15 import org.eclipse.swt.graphics.Point; 16 import org.eclipse.swt.graphics.RGB; 17 18 30 public abstract class CompositeImageDescriptor extends ImageDescriptor { 31 32 35 private ImageData imageData; 36 37 40 protected CompositeImageDescriptor() { 41 } 42 43 56 protected abstract void drawCompositeImage(int width, int height); 57 58 73 final protected void drawImage(ImageData src, int ox, int oy) { 74 ImageData dst = imageData; 75 PaletteData srcPalette = src.palette; 76 ImageData srcMask = null; 77 int alphaMask = 0, alphaShift = 0; 78 if (src.maskData != null) { 79 srcMask = src.getTransparencyMask (); 80 if (src.depth == 32) { 81 alphaMask = ~(srcPalette.redMask | srcPalette.greenMask | srcPalette.blueMask); 82 while (alphaMask != 0 && ((alphaMask >>> alphaShift) & 1) == 0) alphaShift++; 83 } 84 } 85 for (int srcY = 0, dstY = srcY + oy; srcY < src.height; srcY++, dstY++) { 86 for (int srcX = 0, dstX = srcX + ox; srcX < src.width; srcX++, dstX++) { 87 if (!(0 <= dstX && dstX < dst.width && 0 <= dstY && dstY < dst.height)) continue; 88 int srcPixel = src.getPixel(srcX, srcY); 89 int srcAlpha = 255; 90 if (src.maskData != null) { 91 if (src.depth == 32) { 92 srcAlpha = (srcPixel & alphaMask) >>> alphaShift; 93 if (srcAlpha == 0) { 94 srcAlpha = srcMask.getPixel(srcX, srcY) != 0 ? 255 : 0; 95 } 96 } else { 97 if (srcMask.getPixel(srcX, srcY) == 0) srcAlpha = 0; 98 } 99 } else if (src.transparentPixel != -1) { 100 if (src.transparentPixel == srcPixel) srcAlpha = 0; 101 } else if (src.alpha != -1) { 102 srcAlpha = src.alpha; 103 } else if (src.alphaData != null) { 104 srcAlpha = src.getAlpha(srcX, srcY); 105 } 106 if (srcAlpha == 0) continue; 107 int srcRed, srcGreen, srcBlue; 108 if (srcPalette.isDirect) { 109 srcRed = srcPixel & srcPalette.redMask; 110 srcRed = (srcPalette.redShift < 0) ? srcRed >>> -srcPalette.redShift : srcRed << srcPalette.redShift; 111 srcGreen = srcPixel & srcPalette.greenMask; 112 srcGreen = (srcPalette.greenShift < 0) ? srcGreen >>> -srcPalette.greenShift : srcGreen << srcPalette.greenShift; 113 srcBlue = srcPixel & srcPalette.blueMask; 114 srcBlue = (srcPalette.blueShift < 0) ? srcBlue >>> -srcPalette.blueShift : srcBlue << srcPalette.blueShift; 115 } else { 116 RGB rgb = srcPalette.getRGB(srcPixel); 117 srcRed = rgb.red; 118 srcGreen = rgb.green; 119 srcBlue = rgb.blue; 120 } 121 int dstRed, dstGreen, dstBlue, dstAlpha; 122 if (srcAlpha == 255) { 123 dstRed = srcRed; 124 dstGreen = srcGreen; 125 dstBlue= srcBlue; 126 dstAlpha = srcAlpha; 127 } else { 128 int dstPixel = dst.getPixel(dstX, dstY); 129 dstAlpha = dst.getAlpha(dstX, dstY); 130 dstRed = (dstPixel & 0xFF) >>> 0; 131 dstGreen = (dstPixel & 0xFF00) >>> 8; 132 dstBlue = (dstPixel & 0xFF0000) >>> 16; 133 dstRed += (srcRed - dstRed) * srcAlpha / 255; 134 dstGreen += (srcGreen - dstGreen) * srcAlpha / 255; 135 dstBlue += (srcBlue - dstBlue) * srcAlpha / 255; 136 dstAlpha += (srcAlpha - dstAlpha) * srcAlpha / 255; 137 } 138 dst.setPixel(dstX, dstY, ((dstRed & 0xFF) << 0) | ((dstGreen & 0xFF) << 8) | ((dstBlue & 0xFF) << 16)); 139 dst.setAlpha(dstX, dstY, dstAlpha); 140 } 141 } 142 } 143 144 147 public ImageData getImageData() { 148 Point size = getSize(); 149 150 151 imageData = new ImageData(size.x, size.y, 24, new PaletteData(0xFF, 0xFF00, 0xFF0000)); 152 imageData.alphaData = new byte[imageData.width * imageData.height]; 153 154 drawCompositeImage(size.x, size.y); 155 156 157 boolean transparency = false; 158 byte[] alphaData = imageData.alphaData; 159 for (int i = 0; i < alphaData.length; i++) { 160 int alpha = alphaData[i] & 0xFF; 161 if (!(alpha == 0 || alpha == 255)) { 162 163 return imageData; 164 } 165 if (!transparency && alpha == 0) transparency = true; 166 } 167 if (transparency) { 168 169 PaletteData palette = new PaletteData(new RGB[]{new RGB(0, 0, 0), new RGB(255, 255, 255)}); 170 ImageData mask = new ImageData(imageData.width, imageData.height, 1, palette); 171 for (int y = 0; y < mask.height; y++) { 172 for (int x = 0; x < mask.width; x++) { 173 mask.setPixel(x, y, imageData.getAlpha(x, y) == 255 ? 1 : 0); 174 } 175 } 176 } else { 177 178 imageData.alphaData = null; 179 } 180 return imageData; 181 } 182 183 184 191 protected int getTransparentPixel() { 192 return 0; 193 } 194 195 203 protected abstract Point getSize(); 204 205 209 protected void setImageData(ImageData imageData) { 210 this.imageData = imageData; 211 } 212 } 213 | Popular Tags |