Exemple #1
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;
 }
 private ImageData getDownImageData() {
   ImageData imageData = getEmptyImageData();
   int numTransparentPixels = 0;
   for (int row = 0; row < height; ++row) {
     for (int col = numTransparentPixels; col < width / 2; ++col) {
       imageData.setPixel(col, row, 1);
       imageData.setPixel(width - col - 1, row, 1);
     }
     ++numTransparentPixels;
   }
   return imageData;
 }
  private static void setBrightness() {
    if (image.isDisposed()) {
      return;
    }

    ImageData imageData = image.getImageData();

    for (int x = 0; x < imageData.width; x++) {
      for (int y = 0; y < imageData.height; y++) {
        RGB rgb = imageData.palette.getRGB(imageData.getPixel(x, y));
        float[] hsb = rgb.getHSB();

        hsb[2] = hsb[2] + 0.1f;
        if (hsb[2] > 1.0f) {
          hsb[2] = 1.0f;
        }

        //				hsb[1] = hsb[1] - 0.1f;
        //				if (hsb[1] < 0.0f) {
        //					hsb[1] = 0.0f;
        //				}
        RGB newRGB = new RGB(hsb[0], hsb[1], hsb[2]);

        int pixel = imageData.palette.getPixel(newRGB);
        imageData.setPixel(x, y, pixel);
      }
    }

    image.dispose();
    image = new Image(Display.getDefault(), imageData);

    imageFigure.setImage(image);
  }
  private Image createTransparentBackground(int width, int height) {
    PaletteData paletteData =
        new PaletteData(new RGB[] {new RGB(204, 204, 204), new RGB(255, 255, 255)});
    System.out.println("createTransparent width,height (" + width + "," + height + ")");
    ImageData imageData = new ImageData(width, height, 8, paletteData);
    RGB grayRGB = new RGB(204, 204, 204);
    RGB whiteRGB = new RGB(255, 255, 255);
    RGB currentColor = null;
    int switchColorNum = 0;
    for (int x = 0; x < width; x++) {
      if (x != 0 && (x % 8) == 0) {
        switchColorNum = switchColorNum == 0 ? 1 : 0;
      }
      for (int y = 0; y < height; y++) {
        if (y < 8) {
          currentColor = switchColorNum == 0 ? grayRGB : whiteRGB;
        } else if ((y % 8) == 0) {
          currentColor = currentColor == grayRGB ? whiteRGB : grayRGB;
        }
        int pixel = paletteData.getPixel(currentColor);
        imageData.setPixel(x, y, pixel);
      }
    }

    return new Image(Display.getDefault(), imageData);
  }
Exemple #5
0
  @Override
  public Image process(Image image) {
    ImageData oldData = image.getImageData();
    ImageData newData = oldData;
    int[] pixelData = ImageUtil.getPixelData(oldData); // 保存了所有的像素值,大小是 width * height
    //		int[] rData = pixelData;
    //		int[] gData = pixelData;
    //		int[] bData = pixelData;
    //		int[][] rgbs = new int[3][pixelData.length];
    int[] rgb = new int[3];
    int width = oldData.width;
    int height = oldData.height;
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        rgb = ImageUtil.getRGBFromPixel(pixelData[i * width + j]);
        newData.setPixel(j, i, ImageUtil.getPixelFromRGB(oldData, new RGB(0, 0, rgb[2])));
      }
    }

    //		for (int i = 0; i < pixelData.length; i++) {
    //			rgb = ImageUtil.getRGBFromPixel(pixelData[i]);
    //			newData.setPixel(x, y, pixelValue) = ImageUtil.getPixelFromRGB(oldData,new
    // int[]{rgb[0],0,0});
    //			rData[i] = ImageUtil.getPixelFromRGB(oldData,new int[]{rgb[0],0,0});
    //			gData[i] = ImageUtil.getPixelFromRGB(oldData,new int[]{0,rgb[1],0});
    //			bData[i] = ImageUtil.getPixelFromRGB(oldData,new int[]{0,0,rgb[2]});
    //		}
    //		ImageUtil.calculateNewDataInRGB(rData, newData);

    Image newImage = new Image(Display.getDefault(), newData);
    Image newGreyImage = new Image(Display.getDefault(), newImage, SWT.IMAGE_GRAY);
    return newGreyImage;
  }
  /**
   * @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;
  }
 public void testModifyData() throws Exception {
   ImageData originalData = getImageData(Fixture.IMAGE1);
   Image image = ResourceFactory.findImage(originalData);
   cache.putImageData(image, originalData);
   ImageData copyData1 = cache.getImageData(image);
   assertNotSame(originalData, copyData1);
   assertEqualsImageData(originalData, copyData1);
   // modify original data
   originalData.setPixel(0, 0, 23);
   ImageData copyData2 = cache.getImageData(image);
   assertNotSame(copyData1, copyData2);
   assertEqualsImageData(copyData1, copyData2);
 }
Exemple #8
0
  public Image overlay(Image buttomImage, Image topImage) {

    ImageData topData = topImage.getImageData();
    ImageData bottomData = buttomImage.getImageData();

    int zeroX = (bottomData.width - topData.width) / 2;
    int zeroY = (bottomData.height - topData.height) / 2;

    for (int y = 0; y < topData.height; y++) {
      for (int x = 0; x < topData.width; x++) {
        bottomData.setPixel(zeroX + x, zeroY + y, topData.getPixel(x, y));
      }
    }

    return new Image(Display.getCurrent(), bottomData);
  }
  private Image buildContentAreaPreview() {
    if (mContentAreaImage != null) {
      mContentAreaImage.dispose();
    }

    Rectangle rect = mNinePatchedImage.getContentArea();

    int yLen = rect.y + rect.height;
    for (int y = rect.y; y < yLen; y++) {
      int xLen = rect.x + rect.width;
      for (int x = rect.x; x < xLen; x++) {
        mContentAreaImageData.setPixel(x, y, PADDING_COLOR);
        mContentAreaImageData.setAlpha(x, y, PADDING_COLOR_ALPHA);
      }
    }
    return new Image(AdtPlugin.getDisplay(), mContentAreaImageData);
  }
  /**
   * Extract image from bounds.
   *
   * @param baseImageData the base image data
   * @param r the entity bounds
   * @return the image data
   */
  public static ImageData extractImageFromBounds(ImageData baseImageData, Rectangle r) {
    final ImageData id =
        new ImageData(r.width, r.height, baseImageData.depth, baseImageData.palette);

    int ey = r.y;
    int ex = r.x;

    for (int i = 0; i < r.height; i++) {
      int py = ey + i;
      for (int j = 0; j < r.width; j++) {
        int px = ex + j;
        id.setPixel(j, i, baseImageData.getPixel(px, py));
        id.setAlpha(j, i, baseImageData.getAlpha(px, py));
      }
    }
    return id;
  }
  private Image createContentImage(Image image, ContentData data) {
    ImageData imageData = image.getImageData();
    ImageData imageData2 = new ImageData(imageData.width, imageData.height, 24, imageData.palette);

    int endX = data.getStartX() + data.getWidth();
    int startX = data.getStartX();
    int startY = data.getStartY();
    int endY = data.getStartY() + data.getHeight();
    int pixel = imageData.palette.getPixel(new RGB(255, 255, 0));
    for (int x = 0; x < imageData2.width; x++) {
      for (int y = 0; y < imageData2.height; y++) {

        if ((startX <= x && endX >= x) && (startY <= y && endY >= y)) {
          imageData2.setPixel(x, y, pixel);
          imageData2.setAlpha(x, y, 150);
        }
      }
    }
    return new Image(Display.getDefault(), imageData2);
  }
 public ImageData getImageData(BufferedImage bufferedImage) {
   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;
 }
  public static Image getPrintImage(Image image, Color bgColor) {
    Image newImage = (Image) iconCashe.get(image);
    if (newImage == null) {
      ImageData imageData = image.getImageData();
      ImageData imgData = (ImageData) imageData.clone();
      ImageData mask = imgData.getTransparencyMask();
      int bgPixelValue = imgData.palette.getPixel(bgColor.getRGB());

      for (int x = 0; x < mask.width; x++) {
        for (int y = 0; y < mask.height; y++) {
          int pixelValue = mask.getPixel(x, y);
          if (pixelValue == 0) {
            imgData.setPixel(x, y, bgPixelValue);
          }
        }
      }
      imgData.maskData = null;

      newImage = new Image(null, imgData);
      iconCashe.put(image, newImage);
    }

    return newImage;
  }
Exemple #14
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;
  }
  @Override
  public Image run(Image input, int imageType) {

    ImageData inData = input.getImageData(); // Die echten Bilddaten sind hier drin

    // RGB zu YUV Umwandlungsmatrix
    //		double[] linear_conversion = {
    //			0.2126, 0.7152, 0.0722,
    //			-0.09991, -0.33609, 0.436,
    //			0.615, -0.55861, -0.05639
    //		};
    double[] linear_conversion = {0.299, 0.587, 0.114, -0.147, -0.289, 0.436, 0.615, -0.515, -0.1};
    Matrix m = new Matrix(3, 3, linear_conversion);

    // YUV zu RGB Umwandlungmatrix (das Inverse)
    //		double[] inv_linear_conversion = {
    //		    1.0000,    0.0000,    1.28033,
    //		    1.0000,   -0.21482,   -0.38059,
    //		    1.0000,    2.12798,   -0.0005
    //		};
    double[] inv_linear_conversion = {
      1.0000, 0.0000, 1.1398, 1.0000, -0.3946, -0.5805, 1.0000, 2.0320, -0.0005
    };
    Matrix minv = new Matrix(3, 3, inv_linear_conversion);

    for (int v = 0; v < inData.height; v++) {
      for (int u = 0; u < inData.width; u++) {
        int pixel = inData.getPixel(u, v);

        RGB rgb = inData.palette.getRGB(pixel);

        // Variante mit Farbwerte U und V von YUV auf 0 setzen
        Vector r = new Vector(rgb.red, rgb.green, rgb.blue);
        double[] y = m.times(r).getV();
        y[2] = 0;
        y[1] = 0;
        r = minv.times(new Vector(y));
        rgb.red = (int) r.x(1);
        rgb.green = (int) r.x(2);
        rgb.blue = (int) r.x(3);

        if (rgb.red < 0) rgb.red = 0;
        else if (rgb.red > 255) rgb.red = 255;

        if (rgb.green < 0) rgb.green = 0;
        else if (rgb.green > 255) rgb.green = 255;

        if (rgb.blue < 0) rgb.blue = 0;
        else if (rgb.blue > 255) rgb.blue = 255;

        // Variante mit der Sättigung auf 0 gesetzt (schlechter)
        //				float[] hsb = inData.palette.getRGB(pixel).getHSB();
        //
        //				hsb[1] = 0; //Sättigung auf 0 setzen
        //
        //				Color c = new Color(Color.HSBtoRGB(hsb[0],hsb[1],hsb[2]));
        //				RGB rgb = new RGB(0,0,0);
        //				rgb.red = c.getRed();
        //				rgb.green = c.getGreen();
        //				rgb.blue = c.getBlue();

        inData.setPixel(u, v, inData.palette.getPixel(rgb));
      }
    }
    return new Image(input.getDevice(), inData);
  }
  private void hilbertWalk(ImageData id, InputStream pixData, int order, int x, int y, int dir)
      throws IOException {
    if (x >= id.width || y >= id.height) {
      return;
    } else if (order == 0) {
      try {
        int p = pixData.read();
        if (p >= 0) {
          // flip along x=y axis;  assume width == height
          id.setPixel(y, x, p);

          /* Skanky; use an otherwise-unused ImageData field
           * to keep track of the max x,y used. Note that x and y are inverted.
           */
          if (y > id.x) {
            id.x = y;
          }
          if (x > id.y) {
            id.y = x;
          }
        }
        // xxx just give up; don't bother walking the rest of the image
      } catch (IllegalArgumentException ex) {
        System.out.println(
            "bad pixels: order "
                + order
                + ", dir "
                + dir
                + ", w "
                + id.width
                + ", h "
                + id.height
                + ", x "
                + x
                + ", y "
                + y);
        throw ex;
      }
    } else {
      order--;
      int delta = 1 << order;
      int nextX = x + delta;
      int nextY = y + delta;

      switch (dir) {
        case HILBERT_DIR_E:
          hilbertWalk(id, pixData, order, x, y, HILBERT_DIR_N);
          hilbertWalk(id, pixData, order, x, nextY, HILBERT_DIR_E);
          hilbertWalk(id, pixData, order, nextX, nextY, HILBERT_DIR_E);
          hilbertWalk(id, pixData, order, nextX, y, HILBERT_DIR_S);
          break;
        case HILBERT_DIR_N:
          hilbertWalk(id, pixData, order, x, y, HILBERT_DIR_E);
          hilbertWalk(id, pixData, order, nextX, y, HILBERT_DIR_N);
          hilbertWalk(id, pixData, order, nextX, nextY, HILBERT_DIR_N);
          hilbertWalk(id, pixData, order, x, nextY, HILBERT_DIR_W);
          break;
        case HILBERT_DIR_S:
          hilbertWalk(id, pixData, order, nextX, nextY, HILBERT_DIR_W);
          hilbertWalk(id, pixData, order, x, nextY, HILBERT_DIR_S);
          hilbertWalk(id, pixData, order, x, y, HILBERT_DIR_S);
          hilbertWalk(id, pixData, order, nextX, y, HILBERT_DIR_E);
          break;
        case HILBERT_DIR_W:
          hilbertWalk(id, pixData, order, nextX, nextY, HILBERT_DIR_S);
          hilbertWalk(id, pixData, order, nextX, y, HILBERT_DIR_W);
          hilbertWalk(id, pixData, order, x, y, HILBERT_DIR_W);
          hilbertWalk(id, pixData, order, x, nextY, HILBERT_DIR_N);
          break;
        default:
          throw new RuntimeException("Unexpected Hilbert direction " + dir);
      }
    }
  }
 private int zOrderData(ImageData id, byte pixData[]) {
   int maxX = 0;
   for (int i = 0; i < pixData.length; i++) {
     /* Tread the pixData index as a z-order curve index and
      * decompose into Cartesian coordinates.
      */
     int x =
         (i & 1)
             | ((i >>> 2) & 1) << 1
             | ((i >>> 4) & 1) << 2
             | ((i >>> 6) & 1) << 3
             | ((i >>> 8) & 1) << 4
             | ((i >>> 10) & 1) << 5
             | ((i >>> 12) & 1) << 6
             | ((i >>> 14) & 1) << 7
             | ((i >>> 16) & 1) << 8
             | ((i >>> 18) & 1) << 9
             | ((i >>> 20) & 1) << 10
             | ((i >>> 22) & 1) << 11
             | ((i >>> 24) & 1) << 12
             | ((i >>> 26) & 1) << 13
             | ((i >>> 28) & 1) << 14
             | ((i >>> 30) & 1) << 15;
     int y =
         ((i >>> 1) & 1) << 0
             | ((i >>> 3) & 1) << 1
             | ((i >>> 5) & 1) << 2
             | ((i >>> 7) & 1) << 3
             | ((i >>> 9) & 1) << 4
             | ((i >>> 11) & 1) << 5
             | ((i >>> 13) & 1) << 6
             | ((i >>> 15) & 1) << 7
             | ((i >>> 17) & 1) << 8
             | ((i >>> 19) & 1) << 9
             | ((i >>> 21) & 1) << 10
             | ((i >>> 23) & 1) << 11
             | ((i >>> 25) & 1) << 12
             | ((i >>> 27) & 1) << 13
             | ((i >>> 29) & 1) << 14
             | ((i >>> 31) & 1) << 15;
     try {
       id.setPixel(x, y, pixData[i]);
       if (x > maxX) {
         maxX = x;
       }
     } catch (IllegalArgumentException ex) {
       System.out.println(
           "bad pixels: i "
               + i
               + ", w "
               + id.width
               + ", h "
               + id.height
               + ", x "
               + x
               + ", y "
               + y);
       throw ex;
     }
   }
   return maxX;
 }