示例#1
0
  public Image fade(Image image, int alpha) {
    ImageData imageData = image.getImageData();

    byte[] aplhas = new byte[imageData.height * imageData.width];

    for (int i = 0; i < aplhas.length; i++) {
      aplhas[i] = (byte) alpha;
    }

    imageData.alphaData = aplhas;
    return new Image(Display.getCurrent(), imageData);
  }
  private static Image takeScreenshot(final Display display, final Shell parent) {
    /* Take the screen shot */
    GC gc = new GC(parent);
    Image image = new Image(display, parent.getClientArea());
    gc.copyArea(image, 0, 0);
    GC gcImage = new GC(image);
    gcImage.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
    gcImage.setAdvanced(true);
    Region region = parent.getRegion();

    data = image.getImageData();
    if (region != null) {
      int height = image.getBounds().height;
      int width = image.getBounds().width;
      byte[] alphaData = new byte[height * width];
      int currentPosition = 0;
      for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
          if (!region.contains(j, i)) {
            gcImage.drawPoint(j, i);
            alphaData[currentPosition] = 0;
          } else {
            alphaData[currentPosition] = (byte) 255;
          }
          currentPosition++;
        }
      }
      data.alphaData = alphaData;
    }

    image.dispose();
    gcImage.dispose();
    gc.dispose();

    return new Image(display, data);
  }
示例#3
0
  public ImageData image(BufferedImage image) {
    if (image.getColorModel() instanceof DirectColorModel) {
      DirectColorModel cmodel = (DirectColorModel) image.getColorModel();
      PaletteData palette =
          new PaletteData(cmodel.getRedMask(), cmodel.getGreenMask(), cmodel.getBlueMask());
      ImageData data =
          new ImageData(image.getWidth(), image.getHeight(), cmodel.getPixelSize(), palette);
      for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
          int rgb = image.getRGB(x, y);
          int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
          data.setPixel(x, y, pixel);
          if (cmodel.hasAlpha()) data.setAlpha(x, y, (rgb >> 24) & 0xFF);
        }
      }
      return data;

    } else if (image.getColorModel() instanceof IndexColorModel) {
      IndexColorModel cmodel = (IndexColorModel) image.getColorModel();
      int size = cmodel.getMapSize();
      byte[] reds = new byte[size];
      byte[] greens = new byte[size];
      byte[] blues = new byte[size];
      cmodel.getReds(reds);
      cmodel.getGreens(greens);
      cmodel.getBlues(blues);
      RGB[] rgbs = new RGB[size];
      for (int ii = 0; ii < rgbs.length; ii++) {
        rgbs[ii] = new RGB(reds[ii] & 0xFF, greens[ii] & 0xFF, blues[ii] & 0xFF);
      }
      PaletteData palette = new PaletteData(rgbs);
      ImageData data =
          new ImageData(image.getWidth(), image.getHeight(), cmodel.getPixelSize(), palette);
      data.transparentPixel = cmodel.getTransparentPixel();
      WritableRaster raster = image.getRaster();
      int[] pixelArray = new int[1];
      for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
          raster.getPixel(x, y, pixelArray);
          data.setPixel(x, y, pixelArray[0]);
        }
      }
      return data;
    } else if (image.getColorModel() instanceof ComponentColorModel) {
      ComponentColorModel cmodel = (ComponentColorModel) image.getColorModel();
      PaletteData palette = new PaletteData(0x0000FF, 0x00FF00, 0xFF0000); // BGR
      ImageData data = new ImageData(image.getWidth(), image.getHeight(), 24, palette);
      if (cmodel.hasAlpha()) data.alphaData = new byte[image.getWidth() * image.getHeight()];
      WritableRaster raster = image.getRaster();
      int[] pixelArray = new int[4];
      for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
          raster.getPixel(x, y, pixelArray);
          data.setPixel(x, y, (pixelArray[2] << 16) | (pixelArray[1] << 8) | (pixelArray[0]));
          if (data.alphaData != null) data.alphaData[y * data.width + x] = (byte) pixelArray[3];
        }
      }
      return data;
    }
    return null;
  }