Пример #1
0
  /**
   * Returns a scaled instance of the tile image. Using a MediaTracker instance, this function waits
   * until the scaling operation is done.
   *
   * <p>Internally it caches the scaled image in order to optimize the common case, where the same
   * scale is requested as the last time.
   *
   * @param zoom the requested zoom level
   * @return Image
   */
  public Image getScaledImage(double zoom) {
    if (zoom == 1.0) {
      return getImage();
    } else if (zoom == myZoom && scaledImage != null) {
      return scaledImage;
    } else {
      Image img = getImage();
      Rectangle bounds = img.getBounds();
      if (img != null) {
        ImageData imageData = img.getImageData();
        if (tileset != null && tileset.getTransparentColor() != null) {
          imageData.transparentPixel = imageData.palette.getPixel(tileset.getTransparentColor());
        }
        scaledImage =
            new Image(
                Display.getDefault(),
                imageData.scaledTo(
                    (int) Math.round(bounds.width * zoom), (int) Math.round(bounds.height * zoom)));

        //                MediaTracker mediaTracker = new MediaTracker(new Canvas());
        //                mediaTracker.addImage(scaledImage, 0);
        //                try {
        //                    mediaTracker.waitForID(0);
        //                }
        //                catch (InterruptedException ie) {
        //                    System.err.println(ie);
        //                }
        //                mediaTracker.removeImage(scaledImage);
        myZoom = zoom;
        return scaledImage;
      }
    }

    return null;
  }
Пример #2
0
  private ImageData getEmptyImageData() {
    final RGB whiteRGB = new RGB(255, 255, 255); // The transparancy color
    final RGB blackRGB = new RGB(0, 0, 0);

    PaletteData palette = new PaletteData(new RGB[] {whiteRGB, blackRGB});
    ImageData imageData = new ImageData(width, height, 1, palette);
    int whitePixel = imageData.palette.getPixel(whiteRGB);
    imageData.transparentPixel = whitePixel;
    return imageData;
  }
Пример #3
0
 static ImageData convertToSWT(BufferedImage bufferedImage) {
   if (bufferedImage.getColorModel() instanceof DirectColorModel) {
     DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
     PaletteData palette =
         new PaletteData(
             colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
     ImageData data =
         new ImageData(
             bufferedImage.getWidth(),
             bufferedImage.getHeight(),
             colorModel.getPixelSize(),
             palette);
     WritableRaster raster = bufferedImage.getRaster();
     int[] pixelArray = new int[3];
     for (int y = 0; y < data.height; y++) {
       for (int x = 0; x < data.width; x++) {
         raster.getPixel(x, y, pixelArray);
         int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
         data.setPixel(x, y, pixel);
       }
     }
     return data;
   } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
     IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
     int size = colorModel.getMapSize();
     byte[] reds = new byte[size];
     byte[] greens = new byte[size];
     byte[] blues = new byte[size];
     colorModel.getReds(reds);
     colorModel.getGreens(greens);
     colorModel.getBlues(blues);
     RGB[] rgbs = new RGB[size];
     for (int i = 0; i < rgbs.length; i++) {
       rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
     }
     PaletteData palette = new PaletteData(rgbs);
     ImageData data =
         new ImageData(
             bufferedImage.getWidth(),
             bufferedImage.getHeight(),
             colorModel.getPixelSize(),
             palette);
     data.transparentPixel = colorModel.getTransparentPixel();
     WritableRaster raster = bufferedImage.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;
   }
   return null;
 }
Пример #4
0
  /**
   * Returns a corner image for the specified position and color.
   *
   * <p>The returned image is cached and may not be disposed.
   *
   * @param position the wanted position of the image
   * @param rgb the wanted color
   * @return the image
   */
  public static Image getCornerImage(DecorationPosition position, RGB rgb) {
    final String key = "CORNER_IMAGE:" + position + ":" + rgb; // $NON-NLS-1$ //$NON-NLS-2$
    final ImageRegistry ir = JFaceResources.getImageRegistry();
    Image image = ir.get(key);
    if (image != null) return image;

    final Display device = Display.getDefault();
    final Color color = new Color(device, rgb);

    image = new Image(device, 5, 5);
    final GC gc = new GC(image);
    gc.setBackground(color);

    switch (position) {
      case TOP_LEFT:
        gc.fillPolygon(new int[] {0, 0, 4, 0, 0, 4});
        break;
      case CENTER_LEFT:
        gc.fillPolygon(new int[] {0, 0, 4, 2, 0, 4});
        break;
      case BOTTOM_LEFT:
        gc.fillPolygon(new int[] {0, 0, 4, 4, 0, 4});
        break;
      case TOP_RIGHT:
        gc.fillPolygon(new int[] {4, 0, 0, 0, 4, 4});
        break;
      case CENTER_RIGHT:
        gc.fillPolygon(new int[] {4, 0, 2, 0, 4, 4});
        break;
      case BOTTOM_RIGHT:
        gc.fillPolygon(new int[] {4, 0, 4, 0, 4, 4});
        break;
    }

    gc.dispose();
    color.dispose();

    /*
     * Set the transparent color
     */
    final ImageData ideaData = image.getImageData();
    final int whitePixel = ideaData.palette.getPixel(new RGB(255, 255, 255));
    ideaData.transparentPixel = whitePixel;

    ir.put(key, image);

    return image;
  }
Пример #5
0
  public static ImageData createImageData(RenderedImage image) {
    ImageData swtdata = null;
    int width = image.getWidth();
    int height = image.getHeight();
    PaletteData palette;
    int depth;

    depth = 24;
    palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
    swtdata = new ImageData(width, height, depth, palette);
    swtdata.transparentPixel = TRANSPARENT;

    byte blueT = (byte) ((TRANSPARENT) & 0xFF);
    byte greenT = (byte) ((TRANSPARENT >> 8) & 0xFF);
    byte redT = (byte) ((TRANSPARENT >> 16) & 0xFF);
    // System.out.println("red="+redT+"blue"+blueT+"green"+greenT);
    // System.out.println("Transparent"+TRANSPARENT);

    // awtImage2.getRGB();
    Raster raster = image.getData();
    int[] awtdata =
        raster.getPixels(
            0, 0, width, height, new int[width * height * 3]); // raster.getNumBands()]);
    int step = swtdata.depth / 8;

    byte[] data = swtdata.data;
    int baseindex = 0;
    // System.out.println( "AWT size:" + awtdata.length );
    for (int y = 0; y < height; y++) {
      int idx = ((0 + y) * swtdata.bytesPerLine) + (0 * step);

      for (int x = 0; x < width; x++) {
        baseindex = (x + (y * width)) * 4;

        if (awtdata[baseindex + 3] == 0) {
          data[idx++] = blueT;
          data[idx++] = greenT;
          data[idx++] = redT;
        } else {
          data[idx++] = (byte) awtdata[baseindex];
          data[idx++] = (byte) awtdata[baseindex + 1];
          data[idx++] = (byte) awtdata[baseindex + 2];
        }
      }
    }
    return swtdata;
  }
Пример #6
0
  private static Image getFlagRect(Image oFlag) {
    // Color background = GUIPI.getColor(GUIPI.windowColor);

    int size = Math.max(oFlag.getBounds().height, oFlag.getBounds().width);
    //	Rectangle rect = new Rectangle(0,0,size,size);
    Image i = new Image(oFlag.getDevice(), size, size);
    GC gc = new GC(i);
    gc.setBackground(oFlag.getDevice().getSystemColor(SWT.COLOR_WHITE));
    gc.fillRectangle(0, 0, size, size);
    gc.drawImage(
        oFlag, (size - oFlag.getBounds().width) / 2, (size - oFlag.getBounds().height) / 2);
    gc.dispose();

    ImageData id = i.getImageData();
    id.transparentPixel = id.palette.getPixel(new RGB(255, 255, 255));
    i.dispose();
    return new Image(oFlag.getDevice(), id);
  }
Пример #7
0
  /**
   * Transform a java2d bufferedimage to a swt image.
   *
   * @param bufferedImage the image to trasform.
   * @param width the image width.
   * @param height the image height.
   * @return swt image.
   */
  private ImageData awtToSwt(BufferedImage bufferedImage, int width, int height) {
    final int[] awtPixels = new int[width * height];
    ImageData swtImageData = new ImageData(width, height, 24, PALETTE_DATA);
    swtImageData.transparentPixel = TRANSPARENT_COLOR;
    int step = swtImageData.depth / 8;
    final byte[] data = swtImageData.data;
    bufferedImage.getRGB(0, 0, width, height, awtPixels, 0, width);
    for (int i = 0; i < height; i++) {
      int idx = (0 + i) * swtImageData.bytesPerLine + 0 * step;
      for (int j = 0; j < width; j++) {
        int rgb = awtPixels[j + i * width];
        for (int k = swtImageData.depth - 8; k >= 0; k -= 8) {
          data[idx++] = (byte) ((rgb >> k) & 0xFF);
        }
      }
    }

    return swtImageData;
  }
  /**
   * @param height The height of drag handle to generate.
   * @param backgroundColor background color to generate drag handle.
   * @param embossed if <code>true</code> generated embossed drag handle, otherwise generated
   *     engraved one.
   * @return Drag handle {@link ImageData}.
   */
  public ImageData create(int height, HSB backgroundColor, boolean embossed) {

    ImageData source = null;

    if (embossed) {
      source = SharedImages.getImageDescriptor(SharedImages.HANDLE_EMBOSSED).getImageData();
    } else {
      source = SharedImages.getImageDescriptor(SharedImages.HANDLE).getImageData();
    }

    ImageData result = new ImageData(source.width, height, source.depth, source.palette);

    int offset = (result.height - source.height) / 2;

    for (int x = 0; x < result.width; x++) {
      for (int y = 0; y < result.height; y++) {
        if (y >= offset && y < offset + source.height) {
          result.setPixel(x, y, source.getPixel(x, y - offset));
        } else {
          result.setPixel(x, y, source.transparentPixel);
        }
      }
    }
    result.transparentPixel = source.transparentPixel;

    List<RGB> newRGBs = new ArrayList<RGB>();
    for (RGB each : result.palette.colors) {
      try {
        HSB hsb = backgroundColor.getCopy();
        hsb.brightness = new HSB(each).brightness;
        hsb = hsb.mixWith(backgroundColor, 0.7f);
        newRGBs.add(hsb.toRGB());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    result.palette.colors = newRGBs.toArray(new RGB[newRGBs.size()]);

    return result;
  }
  private static void initializeRegistry() {
    // Taken from org.eclipse.ui.internal.WorkbenchImages
    Display d = Display.getCurrent();

    Image viewMenu = new Image(d, 11, 16);
    Image viewMenuMask = new Image(d, 11, 16);

    GC gc = new GC(viewMenu);
    GC maskgc = new GC(viewMenuMask);
    drawViewMenu(gc, maskgc);
    gc.dispose();
    maskgc.dispose();

    ImageData data = viewMenu.getImageData();
    data.transparentPixel = data.getPixel(0, 0);

    Image vm2 = new Image(d, viewMenu.getImageData(), viewMenuMask.getImageData());
    viewMenu.dispose();
    viewMenuMask.dispose();

    getImageRegistry().put(EDITOR_MENU, vm2);
    getImageDescriptorRegistry().put(EDITOR_MENU, ImageDescriptor.createFromImage(vm2));
  }
Пример #10
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;
  }
Пример #11
0
 protected org.eclipse.swt.graphics.Image createSWTImage(ImageData myImageTemplate) {
   _myImageTemplate.transparentPixel = _myImageTemplate.palette.getPixel(new RGB(255, 255, 255));
   org.eclipse.swt.graphics.Image image =
       new org.eclipse.swt.graphics.Image(Display.getCurrent(), _myImageTemplate);
   return image;
 }