/**
  * Returns the sum of the red, green, and blue components of all the pixels of an image within a
  * region.
  *
  * <p>If the region extends outside the bounds of the image, each pixel outside the bounds of the
  * image is considered to have a value of zero.
  *
  * @param image the image to sum. This must not be null.
  * @param region The region of the image to sum pixels from. This must not be null.
  * @return The sum of components
  */
 private static long brightnessSum(Image image, Rectangle region) {
   final PixelReader reader = image.getPixelReader();
   long sum = 0;
   for (int y = region.y; y < region.y + region.height + 1; y++) {
     for (int x = region.x; x < region.x + region.width + 1; x++) {
       if (x >= 0 && x < image.getWidth() && y >= 0 && y < image.getHeight()) {
         int argb = reader.getArgb(x, y);
         // Sum the red, green, and blue components to get a value
         // between 0 and 765
         long brightness = ((argb >> 16) & 0xFF) + ((argb >> 8) & 0xFF) + (argb & 0xFF);
         sum += brightness;
       }
     }
   }
   return sum;
 }
Beispiel #2
0
 private static void setRGBMatrix() {
   int height = (int) image.getHeight();
   int width = (int) image.getWidth();
   imgR = new double[width][height];
   imgG = new double[width][height];
   imgB = new double[width][height];
   PixelReader reader = image.getPixelReader();
   for (int readY = 0; readY < height; readY++) {
     for (int readX = 0; readX < width; readX++) {
       Color color = reader.getColor(readX, readY);
       imgG[readX][readY] = color.getGreen();
       imgB[readX][readY] = color.getBlue();
       imgR[readX][readY] = color.getRed();
     }
   }
 }
  /**
   * Do a low level pixel perfect hit test.
   *
   * @param x The x coordinate to test, relative to the origin of this animation's tile
   * @param y The y coordinate to test, relative to the origin of this animation's tile
   * @param frame The frame to hit test on
   * @param angle The angle of the camera
   * @param direction The direction the sprite is facing
   * @return true if the hit test passes.
   */
  public boolean hitTest(int x, int y0, int frame, CameraAngle angle, FacingDirection direction) {
    int y = y0 + h - ((int) GlobalConstants.TILEH);
    if (x < 0 || x >= w || y < 0 || y >= h) return false;

    int rotation = direction.transform(angle);
    int xt = (int) ((double) (x + (frame * GlobalConstants.TILEW)) / sf);
    int yt = (int) ((double) (y + (rotation * h)) / sf);

    return hitTester.getColor(xt, yt).isOpaque();
  }