private static double compareImage(String image1Path, String imageTrgtPath) {
   try {
     BufferedImage image1 = ImageIO.read(new File(image1Path));
     BufferedImage imageTrgt = ImageIO.read(new File(imageTrgtPath));
     int[] img1RGB =
         image1.getRGB(0, 0, image1.getWidth(), image1.getHeight(), null, 0, image1.getWidth());
     int[] imgTrgtRGB =
         imageTrgt.getRGB(
             0, 0, imageTrgt.getWidth(), imageTrgt.getHeight(), null, 0, imageTrgt.getWidth());
     int nSize = img1RGB.length < imgTrgtRGB.length ? img1RGB.length : imgTrgtRGB.length;
     if (nSize == 0) {
       return 0;
     }
     double nSum = 0.0;
     for (int i = 0; i < nSize; i++) {
       nSum =
           nSum
               + (1
                   - Math.abs((double) (img1RGB[i] - imgTrgtRGB[i]))
                       / Math.abs(img1RGB[i] < imgTrgtRGB[i] ? img1RGB[i] : imgTrgtRGB[i]));
     }
     return nSum / nSize;
   } catch (IOException e) {
     e.printStackTrace();
     return 0;
   }
 }
Example #2
0
 static BufferedImage addMagnitudes(BufferedImage img1, BufferedImage img2) {
   BufferedImage dst = new BufferedImage(img1.getWidth(), img1.getHeight(), img1.getType());
   for (int y = 0; y < img1.getHeight(); ++y) {
     for (int x = 0; x < img1.getWidth(); ++x) {
       int rgb1 = img1.getRGB(x, y);
       int rgb2 = img2.getRGB(x, y);
       int r1 = (rgb1 & 0x00FF0000) >> 16;
       int r2 = (rgb2 & 0x00FF0000) >> 16;
       int r = (int) (Math.sqrt(r1 * r1 + r2 * r2) / Math.sqrt(2.0));
       if (r > 255) r = 255;
       if (r < 0) r = 0;
       int g1 = (rgb1 & 0x0000FF00) >> 8;
       int g2 = (rgb2 & 0x0000FF00) >> 8;
       int g = (int) (Math.sqrt(g1 * g1 + g2 * g2) / Math.sqrt(2.0));
       if (g > 255) g = 255;
       if (g < 0) g = 0;
       int b1 = rgb1 & 0x000000FF;
       int b2 = rgb2 & 0x000000FF;
       int b = (int) (Math.sqrt(b1 * b1 + b2 * b2) / Math.sqrt(2.0));
       if (b > 255) b = 255;
       if (b < 0) b = 0;
       int rgb = b + (g << 8) + (r << 16);
       dst.setRGB(x, y, rgb);
     }
   }
   return dst;
 }
Example #3
0
 public static BufferedImage outline(BufferedImage img, Color col) {
   Coord sz = imgsz(img).add(2, 2);
   BufferedImage ol = TexI.mkbuf(sz);
   for (int y = 0; y < sz.y; y++) {
     for (int x = 0; x < sz.x; x++) {
       boolean t;
       if ((y == 0) || (x == 0) || (y == sz.y - 1) || (x == sz.x - 1)) {
         t = true;
       } else {
         int cl = img.getRGB(x - 1, y - 1);
         t = Utils.rgbm.getAlpha(cl) < 250;
       }
       if (!t) continue;
       if (((x > 1)
               && (y > 0)
               && (y < sz.y - 1)
               && (Utils.rgbm.getAlpha(img.getRGB(x - 2, y - 1)) >= 250))
           || ((x > 0)
               && (y > 1)
               && (x < sz.x - 1)
               && (Utils.rgbm.getAlpha(img.getRGB(x - 1, y - 2)) >= 250))
           || ((x < sz.x - 2)
               && (y > 0)
               && (y < sz.y - 1)
               && (Utils.rgbm.getAlpha(img.getRGB(x, y - 1)) >= 250))
           || ((x > 0)
               && (y < sz.y - 2)
               && (x < sz.x - 1)
               && (Utils.rgbm.getAlpha(img.getRGB(x - 1, y)) >= 250)))
         ol.setRGB(x, y, col.getRGB());
     }
   }
   return (ol);
 }
Example #4
0
 // Need to allow for minimal rounding error, so allow each component
 // to differ by 1.
 void compare(BufferedImage bi0, BufferedImage bi1) {
   int wid = bi0.getWidth();
   int hgt = bi0.getHeight();
   for (int x = 0; x < wid; x++) {
     for (int y = 0; y < hgt; y++) {
       int rgb0 = bi0.getRGB(x, y);
       int rgb1 = bi1.getRGB(x, y);
       if (rgb0 == rgb1) continue;
       int r0 = (rgb0 & 0xff0000) >> 16;
       int r1 = (rgb1 & 0xff0000) >> 16;
       int rdiff = r0 - r1;
       if (rdiff < 0) rdiff = -rdiff;
       int g0 = (rgb0 & 0x00ff00) >> 8;
       int g1 = (rgb1 & 0x00ff00) >> 8;
       int gdiff = g0 - g1;
       if (gdiff < 0) gdiff = -gdiff;
       int b0 = (rgb0 & 0x0000ff);
       int b1 = (rgb1 & 0x0000ff);
       int bdiff = b0 - b1;
       if (bdiff < 0) bdiff = -bdiff;
       if (rdiff > 1 || gdiff > 1 || bdiff > 1) {
         throw new RuntimeException(
             "Images differ at x="
                 + x
                 + " y="
                 + y
                 + " "
                 + Integer.toHexString(rgb0)
                 + " vs "
                 + Integer.toHexString(rgb1));
       }
     }
   }
 }
 /**
  * Test that point features are rendered at the expected image coordinates when the map is
  * rotated. StreamingRenderer
  *
  * @throws Exception
  */
 @Test
 public void testRotatedTransform() throws Exception {
   // If we rotate the world rectangle + 90 degrees around (0,0), we get the screen rectangle
   final Rectangle screen = new Rectangle(0, 0, 100, 50);
   final Envelope world = new Envelope(0, 50, 0, -100);
   final AffineTransform worldToScreen =
       AffineTransform.getRotateInstance(Math.toRadians(90), 0, 0);
   DefaultFeatureCollection fc = new DefaultFeatureCollection();
   fc.add(createPoint(0, 0));
   fc.add(createPoint(world.getMaxX(), world.getMinY()));
   MapContext mapContext = new DefaultMapContext(DefaultGeographicCRS.WGS84);
   mapContext.addLayer((FeatureCollection) fc, createPointStyle());
   BufferedImage image =
       new BufferedImage(screen.width, screen.height, BufferedImage.TYPE_4BYTE_ABGR);
   final StreamingRenderer sr = new StreamingRenderer();
   sr.setContext(mapContext);
   sr.paint(image.createGraphics(), screen, worldToScreen);
   assertTrue("Pixel should be drawn at 0,0 ", image.getRGB(0, 0) != 0);
   assertTrue(
       "Pixel should not be drawn in image centre ",
       image.getRGB(screen.width / 2, screen.height / 2) == 0);
   assertTrue(
       "Pixel should be drawn at image max corner ",
       image.getRGB(screen.width - 1, screen.height - 1) != 0);
 }
  public static int[] decodeImage(BufferedImage img) {
    /* finds the hidden values in the text file */

    int[] a_b, a, b;
    a = new int[8];
    b = new int[8];
    a_b = new int[2];
    int i;

    for (i = 0; i < a.length; i++) {
      int p = img.getRGB(i, i);
      int[] bin = convertToBinary(p, 32);
      a[i] = bin[0];
    }

    for (; i < (a.length + b.length); i++) {
      int p = img.getRGB(i, i);
      int[] bin = convertToBinary(p, 32);
      b[i - b.length] = bin[0];
    }

    a_b[0] = convertToDigit(a, 8);
    a_b[1] = convertToDigit(b, 8);

    return a_b;
  }
Example #7
0
  /**
   * Verifies that the specified images match.
   *
   * @param expected the expected image to check against
   * @param actual the actual image
   */
  private static void assertEqual(BufferedImage expected, BufferedImage actual) {

    int w = expected.getWidth();
    int h = expected.getHeight();

    Assert.assertEquals("width", w, actual.getWidth());
    Assert.assertEquals("height", h, actual.getHeight());

    int[] expectedPixels = new int[w * h];
    expected.getRGB(0, 0, w, h, expectedPixels, 0, w);

    int[] actualPixels = new int[w * h];
    actual.getRGB(0, 0, w, h, actualPixels, 0, w);

    for (int i = 0; i < expectedPixels.length; i++) {
      int expectedPixel = expectedPixels[i];
      int actualPixel = actualPixels[i];
      if (expectedPixel != actualPixel) {
        int x = i % w;
        int y = i / w;
        throw new ComparisonFailure(
            "pixel at " + x + ", " + y, toHexString(expectedPixel), toHexString(actualPixel));
      }
    }
  }
  private static ArrayList<Integer> CornerCheck(BufferedImage img, int x, int y) {
    ArrayList<Integer> corner = new ArrayList<Integer>();
    int base = 0;
    int up = 0;
    int down = 0;
    int left = 0;
    int right = 0;
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        base = base + img.getRGB(x + j, y + i);
        up = up + img.getRGB(x + j, y + 1 + i);
        down = down + img.getRGB(x + j, y - 1 + i);
        left = left + img.getRGB(x - 1 + j, y + i);
        right = right + img.getRGB(x + 1 + j, y + i);
      }
    }

    if (((base != up) && (base != down) && (up != down))
        && ((base != left) && (base != right) && (left != right))) {
      // System.out.println("HIT!2");
      // System.out.println("x:"+x+" y:"+y);
      // System.out.println("base:"+base+" up:"+up+" down:"+down);
      // System.out.println("base:"+base+" left:"+left+" right:"+right);
      corner.add(x);
      corner.add(y);
    } else {
      corner.add(-1);
      corner.add(-1);
    }
    return corner;
  }
  public static BufferedImage doFilter(BufferedImage img) {
    BufferedImage img2 =
        new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    // img.getSubimage(0, 0, img.getWidth(), img.getHeight());
    for (int i = 0; i < img2.getWidth(); i++) {
      for (int j = 0; j < img2.getHeight(); j++) {
        img2.setRGB(i, j, img.getRGB(i, j));
        // img2.setRGB(i, j, Color.BLACK.getRGB());
      }
    }
    int a = 32;
    for (int j = 0; j < img2.getHeight(); j++) {
      for (int i = 0; i < img2.getWidth(); i++) {
        int c = img.getRGB(i, j);
        Color c2 = new Color(c);
        Error er = new Error();

        er.r = (int) (a * matrix[i % 2][j % 2]);
        er.g = er.r;
        er.b = er.r;
        c2 = addToPixel(c2, er, 1);
        Color c3 = getClosestPixel(c2);
        setPixel(img2, i, j, c3);

        // oldpixel := pixel[x][y] + threshold_map_4x4[x mod 4][y mod 4]
        // newpixel := find_closest_palette_color(oldpixel)
        // pixel[x][y] := newpixel
      }
    }
    return img2;
  }
Example #10
0
 public BufferedImage loadImage(File file) throws IOException {
   BufferedImage ret = ImageIO.read(file);
   for (int i = 0; i < ret.getWidth(); i++)
     for (int j = 0; j < ret.getHeight(); j++)
       if (ret.getRGB(i, j) != 0xFFFFFF) ret.setRGB(i, j, ret.getRGB(i, j) | 0xFF000000);
   return ret;
 }
Example #11
0
 public void run() {
   BufferedImage img = panel.getImage();
   Graphics2D g2 = img.createGraphics();
   int anzahl = 255;
   int st = 6;
   int r = st / 2;
   int x1, y1;
   int c;
   Random random = new Random();
   MyColor myColor = MyColor.getInstance();
   for (int y = 0; y < img.getHeight(); y += st) {
     for (Integer x = 0; x < img.getWidth(); x += st) {
       int color = img.getRGB(x, y);
       if (color != RED && color != WHITE) {
         c = 0;
         for (int i = 0; i < anzahl; i++) {
           x1 = x;
           y1 = y;
           color = BLACK;
           while (color != RED && color != WHITE) {
             x1 += random.nextBoolean() ? 5 : -5;
             y1 += random.nextBoolean() ? 5 : -5;
             color = img.getRGB(x1, y1);
           }
           if (color == RED) {
             c += 1;
           }
         }
         System.out.println("Point(" + x + "," + y + ")  c=" + c);
         g2.setColor(myColor.getColorAt(c));
         g2.fillOval(x - r, y - r, st, st);
       }
     }
   }
 }
  @Override
  public BufferedImage processImage(BufferedImage image) {

    originalImage = image;

    width = originalImage.getWidth();
    height = originalImage.getHeight();

    filteredImage = new BufferedImage(width, height, originalImage.getType());

    kernel = createKernel();

    int white = 255;
    int black = 0;

    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        int color = new Color(originalImage.getRGB(i, j)).getRed();
        if (color == black) {
          convolve(i, j);
        } else {
          int alpha = new Color(originalImage.getRGB(i, j)).getAlpha();
          int rgb = ImageUtilities.colorToRGB(alpha, white, white, white);
          filteredImage.setRGB(i, j, rgb);
        }
      }
    }
    return filteredImage;
  }
  // Implementation of http://scale2x.sourceforge.net/algorithm.html
  public static BufferedImage scale2x(BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();
    BufferedImage tmp = new BufferedImage(w * 2, h * 2, 2);

    for (int x = 0; x < h; ++x) {
      int x2 = x * 2;
      for (int y = 0; y < w; ++y) {
        int y2 = y * 2;
        int E = image.getRGB(y, x);
        int D = (x == 0 ? E : image.getRGB(y, x - 1));
        int B = (y == 0 ? E : image.getRGB(y - 1, x));
        int H = (y >= w - 1 ? E : image.getRGB(y + 1, x));
        int F = (x >= h - 1 ? E : image.getRGB(y, x + 1));

        int e0, e1, e2, e3;

        if (B != H && D != F) {
          e0 = D == B ? D : E;
          e1 = B == F ? F : E;
          e2 = D == H ? D : E;
          e3 = H == F ? F : E;
        } else {
          e0 = e1 = e2 = e3 = E;
        }

        tmp.setRGB(y2, x2, e0);
        tmp.setRGB(y2 + 1, x2, e1);
        tmp.setRGB(y2, x2 + 1, e2);
        tmp.setRGB(y2 + 1, x2 + 1, e3);
      }
    }

    return tmp;
  }
  public BufferedImage filter(BufferedImage i) {
    BufferedImage result =
        new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_RGB);

    for (int y = 0; y < i.getHeight(); y++) {
      for (int x = 0; x < i.getWidth(); x++) {
        if (y >= getMinY() && y <= getMaxY() && x >= getMinX() && x <= getMaxX()) {
          int pixel = i.getRGB(x, y);

          int redAmount = (pixel >> 16) & 0xff;
          int greenAmount = (pixel >> 8) & 0xff;
          int blueAmount = (pixel >> 0) & 0xff;

          int sum = redAmount + greenAmount + blueAmount;
          sum = sum / 3;

          redAmount = sum;
          greenAmount = sum;
          blueAmount = sum;

          int newPixel = (redAmount << 16) | (greenAmount << 8) | blueAmount;
          result.setRGB(x, y, newPixel);
        } else result.setRGB(x, y, i.getRGB(x, y));
      }
    }
    return result;
  }
Example #15
0
  private static TileOverride setupOutline(
      int tileNum, BufferedImage terrain, BufferedImage template) {
    switch (tileNum) {
      case TILE_NUM_STILL_LAVA: // still lava
      case TILE_NUM_FLOWING_LAVA: // flowing lava
      case TILE_NUM_STILL_WATER: // still water
      case TILE_NUM_FLOWING_WATER: // flowing water
      case TILE_NUM_FIRE_E_W: // fire east-west
      case TILE_NUM_FIRE_N_S: // fire north-south
      case TILE_NUM_PORTAL: // portal
        return null;

      default:
        break;
    }

    int tileSize = terrain.getWidth() / 16;
    int tileX = (tileNum % 16) * tileSize;
    int tileY = (tileNum / 16) * tileSize;
    BufferedImage newImage =
        new BufferedImage(template.getWidth(), template.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < template.getWidth(); x++) {
      for (int y = 0; y < template.getHeight(); y++) {
        int rgb = template.getRGB(x, y);
        if ((rgb & 0xff000000) == 0) {
          rgb = terrain.getRGB(tileX + (x % tileSize), tileY + (y % tileSize));
        }
        newImage.setRGB(x, y, rgb);
      }
    }

    return TileOverride.create(newImage);
  }
  @Override
  public void setup() {
    super.setup();
    int sWidth = overrideData.getWidth();
    int sHeight = overrideData.getHeight();

    pixels = new int[tileSizeSquare];
    if (tileSizeBase == sWidth && tileSizeBase == sHeight) {
      overrideData.getRGB(0, 0, sWidth, sHeight, pixels, 0, sWidth);
    } else {
      BufferedImage tmp = new BufferedImage(tileSizeBase, tileSizeBase, 6);
      Graphics2D gfx = tmp.createGraphics();
      gfx.drawImage(
          overrideData,
          0,
          0,
          tileSizeBase,
          tileSizeBase,
          0,
          0,
          sWidth,
          sHeight,
          (ImageObserver) null);
      tmp.getRGB(0, 0, tileSizeBase, tileSizeBase, pixels, 0, tileSizeBase);
      gfx.dispose();
    }

    update();
  }
  private void calculatePercentWhitePixels() {
    // calculate percent white pixels in upper left
    int numWhitePixels = 0;
    int totalPixels = 0;
    int runningTotalWhitePixels = 0;
    int runningTotalPixels = 0;

    for (int i = leftBoundary; i < leftBoundary + getWidth() / 2; i++) {
      for (int j = topBoundary; j < topBoundary + getHeight() / 2; j++) {
        totalPixels++;
        if (isPixelWhite(new Color(img.getRGB(i, j)))) numWhitePixels++;
      }
    }
    runningTotalWhitePixels += numWhitePixels;
    runningTotalPixels += totalPixels;
    percentWhitePixelsUpperRight = (1.0 * numWhitePixels / totalPixels);

    // calculate percent white pixels in upper right
    numWhitePixels = 0;
    totalPixels = 0;
    for (int i = leftBoundary + getWidth() / 2; i <= rightBoundary; i++) {
      for (int j = topBoundary; j < topBoundary + getHeight() / 2; j++) {
        totalPixels++;
        if (isPixelWhite(new Color(img.getRGB(i, j)))) numWhitePixels++;
      }
    }
    runningTotalWhitePixels += numWhitePixels;
    runningTotalPixels += totalPixels;
    percentWhitePixelsUpperLeft = (1.0 * numWhitePixels / totalPixels);

    // calculate percent white pixels in lower left
    numWhitePixels = 0;
    totalPixels = 0;
    for (int i = leftBoundary; i < leftBoundary + getWidth() / 2; i++) {
      for (int j = topBoundary + getHeight() / 2; j <= bottomBoundary; j++) {
        totalPixels++;
        if (isPixelWhite(new Color(img.getRGB(i, j)))) numWhitePixels++;
      }
    }
    runningTotalWhitePixels += numWhitePixels;
    runningTotalPixels += totalPixels;
    percentWhitePixelsLowerLeft = (1.0 * numWhitePixels / totalPixels);

    // calculate percent white pixels in lower right
    numWhitePixels = 0;
    totalPixels = 0;
    for (int i = leftBoundary + getWidth() / 2; i <= rightBoundary; i++) {
      for (int j = topBoundary + getWidth() / 2; j <= bottomBoundary; j++) {
        totalPixels++;
        if (isPixelWhite(new Color(img.getRGB(i, j)))) numWhitePixels++;
      }
    }
    runningTotalWhitePixels += numWhitePixels;
    runningTotalPixels += totalPixels;
    percentWhitePixelsLowerRight = (1.0 * numWhitePixels / totalPixels);

    // calculate total percent white pixels
    percentWhitePixelsTotal = (1.0 * runningTotalWhitePixels / runningTotalPixels);
  }
Example #18
0
  public void getImage() throws IOException {

    files.clear();
    allBlackObjects.clear();
    fieldObjectPixels.clear();
    points = null;
    next = null;

    File gameImage1 = new File("d://liga/szene1.png");
    File output = new File("d://liga/out.png");

    /* Color Image*/
    BufferedImage bi = ImageIO.read(gameImage1);
    BufferedImage colorImage =
        new BufferedImage(bi.getWidth(null), bi.getHeight(null), BufferedImage.TYPE_BYTE_BINARY);
    // colorImage = new Contrast(image).contrast(0.3);

    Graphics2D gColor = (Graphics2D) colorImage.getGraphics();
    gColor.drawImage(bi, 0, 0, null);
    // Save Color Img as output.png
    String outputPath = "d://liga/output.png";
    File outputFile = new File(outputPath);
    ImageIO.write(colorImage, "png", outputFile);
    System.out.println("test");

    /* Get image resolution */
    width = bi.getWidth();
    height = bi.getHeight();

    /* GEHE JEDEN PIXEL DURCH */
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        // Test every Pixel if Colored or not
        /* WENN AKTUELLE PIXELFARBE -1 < 0 IST */
        /** SUCHE GRÜN * */
        Color tempCol = new Color(colorImage.getRGB(x, y));
        if (tempCol.getGreen() > 100) {
          // System.out.println(tempCol.getGreen());
          if ((colorImage.getRGB(x, y)) < -1) {
            Point tempPoint = new Point(x, y);
            tempPoint.setColor(colorImage.getRGB(x, y));
            fieldObjectPixels.add(tempPoint);
          }
        }
      }
    }
    if (fieldObjectPixels.size() > 0) {
      // Go on
      createFieldObject();
    } else {
      System.out.println("No colored FieldObjects found.");
    }

    gColor.dispose();
    ImageIO.write(colorImage, "png", output);
  }
 /** Check image equality* */
 public static void assertImageEquals(String msg, BufferedImage ref, BufferedImage res) {
   for (int x = 0; x < res.getWidth(); x++) {
     for (int y = 0; y < res.getHeight(); y++) {
       assertThat(
           String.format(msg + " (%d,%d)", x, y),
           new Color(res.getRGB(x, y), true),
           is(new Color(ref.getRGB(x, y), true)));
     }
   }
 }
Example #20
0
 public double getError(BufferedImage modified, int width, int height) {
   double error = 0.0;
   double total_pixels = width * height;
   for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
       error += computeDifference(original.getRGB(x, y), modified.getRGB(x, y));
     }
   }
   return error / total_pixels;
 }
  /** Prototype give player a leveled up look */
  public void Opify(double Fac) {

    for (int i = 0; i < imgCHARAC.getWidth(); i++) {
      for (int j = 0; j < imgCHARAC.getHeight(); j++) {
        if (imgCHARAC.getRGB(i, j) != imgCHARAC.getRGB(2, 2)) {
          imgCHARAC.setRGB(i, j, (int) Math.round(imgCHARAC.getRGB(i, j) * Fac));
        }
      }
    }
  }
Example #22
0
 private static boolean characterImagesEqual(BufferedImage ci1, BufferedImage ci2) {
   if (ci1 == ci2) return true;
   if ((ci1 == null) || (ci2 == null)) return false;
   if ((ci1.getWidth() != ci2.getWidth()) || (ci1.getHeight() != ci2.getHeight())) return false;
   for (int x = 0; x < ci1.getWidth(); x++)
     for (int y = 0; y < ci1.getHeight(); y++) {
       if (ci1.getRGB(x, y) != ci2.getRGB(x, y)) return false;
     }
   return true;
 }
Example #23
0
  public static BufferedImage scale2x(BufferedImage bufferedimage) {
    int j2 = bufferedimage.getWidth();
    int k2 = bufferedimage.getHeight();
    BufferedImage bufferedimage1 = new BufferedImage(j2 * 2, k2 * 2, 2);
    for (int l2 = 0; l2 < k2; l2++) {
      for (int i3 = 0; i3 < j2; i3++) {
        int i = bufferedimage.getRGB(i3, l2);
        int j1;
        if (l2 == 0) {
          j1 = i;
        } else {
          j1 = bufferedimage.getRGB(i3, l2 - 1);
        }
        int k1;
        if (i3 == 0) {
          k1 = i;
        } else {
          k1 = bufferedimage.getRGB(i3 - 1, l2);
        }
        int l1;
        if (i3 >= j2 - 1) {
          l1 = i;
        } else {
          l1 = bufferedimage.getRGB(i3 + 1, l2);
        }
        int i2;
        if (l2 >= k2 - 1) {
          i2 = i;
        } else {
          i2 = bufferedimage.getRGB(i3, l2 + 1);
        }
        int j;
        int k;
        int l;
        int i1;
        if (j1 != i2 && k1 != l1) {
          j = k1 != j1 ? i : k1;
          k = j1 != l1 ? i : l1;
          l = k1 != i2 ? i : k1;
          i1 = i2 != l1 ? i : l1;
        } else {
          j = i;
          k = i;
          l = i;
          i1 = i;
        }
        bufferedimage1.setRGB(i3 * 2, l2 * 2, j);
        bufferedimage1.setRGB(i3 * 2 + 1, l2 * 2, k);
        bufferedimage1.setRGB(i3 * 2, l2 * 2 + 1, l);
        bufferedimage1.setRGB(i3 * 2 + 1, l2 * 2 + 1, i1);
      }
    }

    return bufferedimage1;
  }
Example #24
0
 public boolean contains(int x, int y) {
     try {
         BufferedImage bi = (BufferedImage) this.currAnimation.getImage();
         if(this.animations.size()>1) {
         	return bi.getRGB(x + refPixelX, y + refPixelY) != 0;
         }
         return bi.getRGB(x, y)!= 0;
     } catch (Exception e) {
     }
     return false;
 }
Example #25
0
 public BufferedImage colorTile(BufferedImage tile) {
   if (tile != null) {
     for (int y = 0; y < tile.getHeight(); y++) {
       for (int x = 0; x < tile.getWidth(); x++) {
         int value = new Color(tile.getRGB(x, y), true).getAlpha();
         Color color = new Color(gradient.getRGB(value, 0), true);
         tile.setRGB(x, y, color.getRGB());
       }
     }
   }
   return tile;
 }
Example #26
0
  /**
   * Creates a <code>BufferedImage</code> from an <code>Image</code>. This method can function on a
   * completely headless system. This especially includes Linux and Unix systems that do not have
   * the X11 libraries installed, which are required for the AWT subsystem to operate. The resulting
   * image will be smoothly scaled using bilinear filtering.
   *
   * @param source The image to convert
   * @param w The desired image width
   * @param h The desired image height
   * @return The converted image
   * @param type int
   */
  public static BufferedImage createHeadlessSmoothBufferedImage(
      BufferedImage source, int type, int width, int height) {

    LOG.trace("createHeadlessSmoothBufferedImage");
    if (type == ImageUtils.IMAGE_PNG && hasAlpha(source)) {
      type = BufferedImage.TYPE_INT_ARGB;
    } else {
      type = BufferedImage.TYPE_INT_RGB;
    }

    BufferedImage dest = new BufferedImage(width, height, type);

    int sourcex;
    int sourcey;

    double scalex = (double) width / source.getWidth();
    double scaley = (double) height / source.getHeight();

    int x1;
    int y1;

    double xdiff;
    double ydiff;

    int rgb;
    int rgb1;
    int rgb2;

    for (int y = 0; y < height; y++) {
      sourcey = y * source.getHeight() / dest.getHeight();
      ydiff = scale(y, scaley) - sourcey;

      for (int x = 0; x < width; x++) {
        sourcex = x * source.getWidth() / dest.getWidth();
        xdiff = scale(x, scalex) - sourcex;

        x1 = Math.min(source.getWidth() - 1, sourcex + 1);
        y1 = Math.min(source.getHeight() - 1, sourcey + 1);

        rgb1 =
            getRGBInterpolation(source.getRGB(sourcex, sourcey), source.getRGB(x1, sourcey), xdiff);
        rgb2 = getRGBInterpolation(source.getRGB(sourcex, y1), source.getRGB(x1, y1), xdiff);

        rgb = getRGBInterpolation(rgb1, rgb2, ydiff);

        dest.setRGB(x, y, rgb);
      }
    }

    return dest;
  }
 public BufferedImage createCelshading() {
   BufferedImage returnImage = createImage(camera, 3);
   BufferedImage edgeImage = EdgeDetection.sobelOperation(createImage(camera, 1));
   for (int i = 0; i < returnImage.getWidth(); i++) {
     for (int j = 0; j < returnImage.getHeight(); j++) {
       if (edgeImage.getRGB(i, j) == -16777216) {
         returnImage.setRGB(i, j, edgeImage.getRGB(i, j));
       } else {
         returnImage.setRGB(i, j, returnImage.getRGB(i, j));
       }
     }
   }
   return returnImage;
 }
Example #28
0
  // Maps a region of Color c1 to Color c2.
  public static void floodFill(BufferedImage sprite, int x_start, int y_start, Color color_2) {
    // Initial Color.
    int c1 = sprite.getRGB(x_start, y_start);

    // Processed Color.
    int c2 = color_2.getRGB();

    Stack<Integer> S = new List<Integer>();

    S.push(x_start);
    S.push(y_start);

    while (!S.isEmpty()) {
      int y = S.pop();
      int x = S.pop();

      int c;

      // Utilize the JAVA array bounds checks for edge cases.
      try {
        c = sprite.getRGB(x, y);
      } catch (Exception e) {
        continue;
      }

      // Base Case.
      if (c != c1) {
        continue;
      }

      // Convert the pixel to the second color.
      sprite.setRGB(x, y, c2);

      // Up
      S.push(x);
      S.push(y + 1);

      // Down
      S.push(x);
      S.push(y - 1);

      // Left
      S.push(x - 1);
      S.push(y);

      // Right
      S.push(x + 1);
      S.push(y);
    }
  }
 public void overlay() {
   for (int y = 0; y < height; ++y) {
     for (int x = 0; x < width; ++x) {
       // System.out.println(nonMax.getRGB(x,y));
       if (nonMax.getRGB(x, y) != -1) {
         int rgb = img.getRGB(x, y);
         Color color = new Color(rgb);
         Color res = new Color(255, color.getGreen(), color.getBlue());
         img.setRGB(x, y, res.getRGB());
       }
     }
   }
   ImageIcon icon1 = new ImageIcon(img);
   lbl1.setIcon(icon1);
 }
Example #30
0
  public static void flipHorizontal(BufferedImage bi) {

    int xSize = bi.getWidth();
    int ySize = bi.getHeight();

    // Temp image, to store pixels as we reverse everything
    BufferedImage newBi = new BufferedImage(xSize, ySize, 3);

    /**
     * INSERT TWO LOOPS HERE: - FIRST LOOP MOVES DATA INTO A SECOND, TEMPORARY IMAGE WITH PIXELS
     * FLIPPED HORIZONTALLY - SECOND LOOP MOVES DATA BACK INTO ORIGINAL IMAGE
     *
     * <p>Note: Due to a limitation in Greenfoot, you can get the backing BufferedImage from a
     * GreenfootImage, but you cannot set or create a GreenfootImage based on a BufferedImage. So,
     * you have to use a temporary BufferedImage (as declared for you, above) and then copy it,
     * pixel by pixel, back to the original image. Changes to bi in this method will affect the
     * GreenfootImage automatically.
     */
    for (int x = 0; x < xSize; x++) {
      for (int y = 0; y < ySize; y++) {
        int rgb = bi.getRGB(xSize - x - 1, y);

        int[] rgbValues = unpackPixel(rgb);
        int alpha = rgbValues[0];
        int red = rgbValues[1];
        int green = rgbValues[2];
        int blue = rgbValues[3];

        int newColour = packagePixel(red, green, blue, alpha);
        newBi.setRGB(x, y, newColour);
      }
    }

    for (int x = 0; x < xSize; x++) {
      for (int y = 0; y < ySize; y++) {
        int rgb = newBi.getRGB(x, y);

        int[] rgbValues = unpackPixel(rgb);
        int alpha = rgbValues[0];
        int red = rgbValues[1];
        int green = rgbValues[2];
        int blue = rgbValues[3];

        int newColour = packagePixel(red, green, blue, alpha);
        bi.setRGB(x, y, newColour);
      }
    }
  }