예제 #1
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;
 }
예제 #2
0
  /** 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));
        }
      }
    }
  }
예제 #3
0
 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);
 }
예제 #4
0
  public static void main(String[] args) {
    int x = 500, y = 80;
    DrawingKit dk = new DrawingKit("Daffodils", 800, 800);
    BufferedImage pict = dk.loadPicture("daffodils.jpg");

    // get pixel value at location (500, 80)
    int encodedPixelColor = pict.getRGB(x, y);
    Color pixelColor = new Color(encodedPixelColor);
    System.out.println(pixelColor);
    int red = pixelColor.getRed();
    int green = pixelColor.getGreen();
    int blue = pixelColor.getBlue();
    // change the color of the pixel to be pure red
    red = 255;
    green = 0;
    blue = 0;

    // update the pixel color in picture
    Color newPixelColor = new Color(red, green, blue);
    int newRgbvalue = newPixelColor.getRGB();
    pict.setRGB(x, y, newRgbvalue);
    // display the approximate location of the pixel
    dk.drawPicture(pict, 0, 0);
    BasicStroke s = new BasicStroke(3);
    dk.setStroke(s);
    Ellipse2D.Float e = new Ellipse2D.Float(x - 3, y - 3, 8, 8);
    dk.draw(e);
    dk.drawString("(600, 150)", x - 3, y - 5);
  }
 /**
  * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance
  * penalty of BufferedImage.getRGB unmanaging the image.
  *
  * @param image a BufferedImage object
  * @param x the left edge of the pixel block
  * @param y the right edge of the pixel block
  * @param width the width of the pixel arry
  * @param height the height of the pixel arry
  * @param pixels the array to hold the returned pixels. May be null.
  * @return the pixels
  * @see #setRGB
  */
 public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
   int type = image.getType();
   if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) {
     return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels);
   }
   return image.getRGB(x, y, width, height, pixels, 0, width);
 }
예제 #6
0
  public Color getColorAt(int x, int y) {
    int col = img.getRGB(x, y);
    int r = col >> 16 & 0xff;
    int g = col >> 8 & 0xff;
    int b = col & 0xff;

    return new Color(r, g, b);
  }
예제 #7
0
 public void drawTileNumC(int tileNum, int x, int y, Color c, Graphics g) {
   BufferedImage coloredTile = tiles[tileNum];
   for (int i = 0; i < this.tW; i++) {
     for (int j = 0; j < this.tH; j++) {
       Color originalColor = new Color(coloredTile.getRGB(i, j), true);
       Color nc = new Color(c.getRed(), c.getGreen(), c.getBlue(), originalColor.getAlpha());
       coloredTile.setRGB(i, j, nc.getRGB());
     }
   }
   g.drawImage(tiles[tileNum], x, y, null);
 }
  public static java.awt.Image ApplyAlphaMask(java.awt.Image original, java.awt.Image alphamask) {
    int width = original.getWidth(null), height = original.getHeight(null);
    if (width <= 0) width = 1;
    if (height <= 0) height = 1;
    BufferedImage resultImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    BufferedImage image = toBufferedImage(original);
    BufferedImage mask = toBufferedImage(alphamask);
    for (int y = 0; y < image.getHeight(null); y++) {
      for (int x = 0; x < image.getWidth(null); x++) {
        try {
          Color c = new Color(image.getRGB(x, y));
          Color maskC = new Color(mask.getRGB(x, y));
          Color maskedColor = new Color(c.getRed(), c.getGreen(), c.getBlue(), maskC.getRed());
          resultImg.setRGB(x, y, maskedColor.getRGB());
        } catch (Exception e) {

        }
      }
    }
    return toImage(resultImg);
  }
예제 #9
0
  /**
   * do damage to character
   *
   * @param Dmg Amount of Damage
   */
  public void Damage(int Dmg, int Type) {
    // If character is already dead then dont do damage
    if (ISDEAD) return;

    // Do damage
    if (Type == 1) {
      // DAMAGE FROM PHYSICAL ATTACK
      if (STATS.ARMOR > Dmg) return;
      HEALTH = HEALTH - Dmg + STATS.ARMOR;
    } else if (Type == 2) {
      // DAMAGE FROM MAGIC ATTACK
      if (STATS.MAGIC_RESIST > Dmg) return;
      HEALTH = HEALTH - Dmg + STATS.MAGIC_RESIST;
    }
    // If an Npc then run and hide
    if (NAI != null) {
      NAI.State = "alarmed";
    }

    // Death condition
    if (HEALTH <= 0) {
      // If player is dead
      if (this.CLASS.equals("Player")) {

        HEALTH = 0;

        // Quit game
        JOptionPane.showMessageDialog(null, "You are dead");
        X = 100;
        Y = 100;
        Opify(50);
        HEALTH = (int) MAX_HEALTH;

      } else {
        // If other character
        // set Death stats
        ISDEAD = true;
        HEALTH = 0;
        // display death
        CLASS = Cl + " (Dead)";

        // Rot effect
        for (int i = 0; i < imgCHARAC.getWidth(); i++) {
          for (int j = 0; j < imgCHARAC.getHeight(); j++) {
            imgCHARAC.setRGB(i, j, imgCHARAC.getRGB(i, j) * (int) Math.pow(3, 3));
          }
        }

        // Make inventory open to looting
        INVENTORY.OPEN_INVEN = true;
      }
    }
  }
예제 #10
0
  public Color getColorAt(double x, double y) {
    int col = img.getRGB((int) x, (int) y);
    double r = col >> 16 & 0xff;
    double g = col >> 8 & 0xff;
    double b = col & 0xff;

    int col_r = img.getRGB((int) x + 1, (int) y);
    double rr = col_r >> 16 & 0xff;
    double gr = col_r >> 8 & 0xff;
    double br = col_r & 0xff;

    double fact = x - Math.floor(x);

    double rf = r + (rr - r) * fact;
    double gf = g + (gr - g) * fact;
    double bf = b + (br - b) * fact;

    col = img.getRGB((int) x, (int) y + 1);
    r = col >> 16 & 0xff;
    g = col >> 8 & 0xff;
    b = col & 0xff;

    col_r = img.getRGB((int) x + 1, (int) y + 1);
    rr = col_r >> 16 & 0xff;
    gr = col_r >> 8 & 0xff;
    br = col_r & 0xff;

    double rf2 = r + (rr - r) * fact;
    double gf2 = g + (gr - g) * fact;
    double bf2 = b + (br - b) * fact;

    fact = y - Math.floor(y);

    double rff = rf + (rf2 - rf) * fact;
    double gff = gf + (gf2 - gf) * fact;
    double bff = bf + (bf2 - bf) * fact;

    return new Color((int) rff, (int) gff, (int) bff);
  }
예제 #11
0
  /*
   * Method takes a parameter of a BufferedImage
   * and returns a 2d array
   */
  public static int[][] readIntoArray(BufferedImage x) {
    int width = x.getWidth();
    int height = x.getHeight();

    // initialize the 2d array to the size of the picture
    int[][] imagePixels = new int[width][height];

    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        imagePixels[i][j] = x.getRGB(i, j); // store RGB value in array
        //     printPixelARGB(x.getRGB(i,j));
      }
    }
    return imagePixels;
  }
예제 #12
0
  /**
   * Loads PNG files and returns the result as an int[][]. The only PNG formats permitted are those
   * with up to 256 grays (including simple black and white) or indexed colors from an up to
   * 256-sized color table. Each integer value represents the gray level or the color table index
   * value of the pixel. The Y dimension is not flipped.
   */
  public static int[][] loadPNGFile(InputStream str) throws IOException {
    // read the bytes into a byte array
    BufferedInputStream stream = new BufferedInputStream(str);
    ArrayList list = new ArrayList();
    int count = 0;
    while (true) {
      byte[] buffer = new byte[16384 * 16];
      int len = stream.read(buffer);
      if (len <= 0) // all done
      break;
      else if (len < buffer.length) {
        byte[] buf2 = new byte[len];
        System.arraycopy(buffer, 0, buf2, 0, len);
        buffer = buf2;
      }
      count += len;
      list.add(buffer);
    }
    byte[] data = new byte[count];
    int cur = 0;
    for (int i = 0; i < list.size(); i++) {
      byte[] b = (byte[]) (list.get(i));
      System.arraycopy(b, 0, data, cur, b.length);
      cur += b.length;
    }

    // Next convert the byte array to a buffered image
    BufferedImage image = ((ToolkitImage) (new ImageIcon(data).getImage())).getBufferedImage();

    // Is the color model something we can use?
    int type = image.getType();
    if (type == BufferedImage.TYPE_BYTE_BINARY || type == BufferedImage.TYPE_BYTE_GRAY) {
      int w = image.getWidth();
      int h = image.getHeight();
      int[][] result = new int[w][h];
      // obviously this could be done more efficiently
      for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++) result[i][j] = (image.getRGB(i, j) & 0xFF);
      return result;
    } else if (type == BufferedImage.TYPE_BYTE_INDEXED) {
      Raster raster = image.getRaster();
      if (raster.getTransferType() != DataBuffer.TYPE_BYTE) // uh oh
      throw new IOException("Input Stream must contain an image with byte data if indexed.");
      byte[] pixel = new byte[1];
      int w = image.getWidth();
      int h = image.getHeight();
      int[][] result = new int[w][h];
      // obviously this could be done more efficiently
      for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++) {
          result[i][j] = ((byte[]) (raster.getDataElements(i, j, pixel)))[0];
          if (result[i][j] < 0) result[i][j] += 256;
        }
      return result;
    }
    // else if (type == TYPE_USHORT_GRAY)   // at present we don't handle shorts
    //    {
    //    }
    else
      throw new IOException(
          "Input Stream must contain a binary, byte-sized grayscale, or byte-sized indexed color scheme: "
              + image);
  }
예제 #13
0
  public erosion(String nombre) {

    imageName = nombre;

    // BufferedImage image2;
    String aux;

    // int ancho=image.getWidth();

    // BufferedImage image = ImageIO.read( new File( imageName ) );
    try {
      image = ImageIO.read(new File(imageName));
    } catch (IOException e) {
      System.out.println("image missing");
    }

    try {
      image2 = ImageIO.read(new File(imageName));
    } catch (IOException e) {
      System.out.println("image missing");
    }

    int k;
    int a;
    int b;
    int i;
    int j;

    int rojo;
    int verde;
    int azul;

    int promedio;
    try {
      int ancho, alto;
      alto = image.getHeight();
      ancho = image.getWidth();
      for (i = 0; i < ancho; i++) {
        for (j = 0; j < alto; j++) {
          k = image.getRGB(i, j);
          k = 0xFFFFFF + k;

          rojo = k / 0x10000;

          k = k % 0x10000;
          verde = k / 0x100;

          k = k % 0x100;
          azul = k;

          if (rojo < 0) rojo = 255 + rojo;
          if (verde < 0) verde = 255 + verde;
          if (azul < 0) azul = 255 + azul;

          promedio = azul + verde + rojo;
          promedio = promedio / 3;
          /*if(promedio<0)
          promedio=promedio+128;*/

          rojo = promedio;
          verde = promedio;
          azul = promedio;
          // printf("")

          if (promedio >= 128) promedio = 255;
          else promedio = 0;
          k = promedio + promedio * 0x100 + promedio * 0x10000;

          this.image.setRGB(i, j, k);
        }
      }
    } catch (Exception e) {
      System.out.printf("n");
    }
    //
    try {
      int ancho, alto;
      alto = image2.getHeight();
      ancho = image2.getWidth();
      for (i = 0; i < ancho; i++) {
        for (j = 0; j < alto; j++) {
          k = image2.getRGB(i, j);
          k = 0xFFFFFF + k;

          rojo = k / 0x10000;

          k = k % 0x10000;
          verde = k / 0x100;

          k = k % 0x100;
          azul = k;

          if (rojo < 0) rojo = 255 + rojo;
          if (verde < 0) verde = 255 + verde;
          if (azul < 0) azul = 255 + azul;

          promedio = azul + verde + rojo;
          promedio = promedio / 3;
          /*if(promedio<0)
          promedio=promedio+128;*/

          rojo = promedio;
          verde = promedio;
          azul = promedio;
          // printf("")

          k = promedio + promedio * 0x100 + promedio * 0x10000;

          this.image2.setRGB(i, j, k);
        }
      }
    } catch (Exception e) {
      System.out.printf("n");
    }

    //
    try {
      int ancho, alto;
      alto = image.getHeight();
      ancho = image.getWidth();
      for (i = 0; i < ancho; i++) {
        for (j = 0; j < alto; j++) {
          k = image.getRGB(i, j);
          k = 0xFFFFFF + k;

          rojo = k / 0x10000;

          k = k % 0x10000;
          verde = k / 0x100;

          k = k % 0x100;
          azul = k;

          if (rojo < 0) rojo = 255 + rojo;
          if (verde < 0) verde = 255 + verde;
          if (azul < 0) azul = 255 + azul;

          /*promedio=azul+verde+rojo;
          promedio=promedio/3;
          /*if(promedio<0)
           promedio=promedio+128;*/

          if (i < ancho - 2) {
            if (rojo >= 128) {
              k = image.getRGB(i + 1, j);
              k = 0xFFFFFF + k;

              rojo = k / 0x10000;

              k = k % 0x10000;
              verde = k / 0x100;

              k = k % 0x100;
              azul = k;

              if (rojo < 0) rojo = 255 + rojo;
              if (verde < 0) verde = 255 + verde;
              if (azul < 0) azul = 255 + azul;

              if (rojo >= 128) {
                k = image.getRGB(i + 2, j);
                k = 0xFFFFFF + k;

                rojo = k / 0x10000;

                k = k % 0x10000;
                verde = k / 0x100;

                k = k % 0x100;
                azul = k;

                if (rojo < 0) rojo = 255 + rojo;
                if (verde < 0) verde = 255 + verde;
                if (azul < 0) azul = 255 + azul;
                if (rojo < 128) {
                  k = 0;
                  this.image2.setRGB(i, j, k);
                  this.image2.setRGB(i + 1, j, k);
                  this.image2.setRGB(i + 2, j, k);
                }
              }

            } else {
              k = rojo + rojo * 0x100 + rojo * 0x10000;
              this.image2.setRGB(i, j, k);
            }
          }
        }
      }
    } catch (Exception e) {
      System.out.printf("n");
    }
    //

    /*else if(l==4)
    posterizacion();*/

    // Toolkit tool = Toolkit.getDefaultToolkit();
    // image = tool.getImage(imageName);

    // dialogo.setLocationRelativeTo(f);

  }
예제 #14
0
  private IFD writeCMYKImage(ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param)
      throws IOException {
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(width)); // 256
      ifd.add(new DEFactory.ImageLengthDE(height)); // 257

      DEFactory.BitsPerSampleDE bpss = new DEFactory.BitsPerSampleDE(4);
      bpss.setBitsPerSample(0, 8); // cyan
      bpss.setBitsPerSample(1, 8); // magneta
      bpss.setBitsPerSample(2, 8); // yellow
      bpss.setBitsPerSample(3, 8); // key (black)
      ifd.add(bpss); // 258

      ifd.add(new DEFactory.CompressionDE(NOCOMPRESSION)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(CMYK)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per strip
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }

      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.SamplesPerPixelDE(4)); // 277
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279

      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      int index = 0;
      for (int y = 0; y < height; y += maxrps) {
        /*
        Assume rgb image.

        Each strip: evaluate c m y k colour
                    save in byte array
                    write to image file
        */

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        for (int i = 0; i < maxrps; i++) {
          if ((y + i) == height) {
            break;
          } // last strip might have less rows
          for (int x = 0; x < width; x++) {
            int c = image.getRGB(x, y + i);

            int R = (c >> 16) & 0x00FF;
            int G = (c >> 8) & 0x00FF;
            int B = (c) & 0x00FF;

            if ((R == 255) && (G == 255) && (B == 255)) {
              baos.write(0);
              baos.write(0);
              baos.write(0);
              baos.write(0);
            } else {
              double C = 1.0 - R / 255.0;
              double M = 1.0 - G / 255.0;
              double Y = 1.0 - B / 255.0;

              double K = C;
              if (M < K) {
                K = M;
              }
              if (Y < K) {
                K = Y;
              }

              C = ((C - K) / (1.0 - K)) * 255.0;
              M = ((M - K) / (1.0 - K)) * 255.0;
              Y = ((Y - K) / (1.0 - K)) * 255.0;
              K *= 255.0;

              baos.write((int) C);
              baos.write((int) M);
              baos.write((int) Y);
              baos.write((int) K);
            }
          }
        }
        baos.close();

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream

        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeCMYKImage:\n\t" + e.getMessage());
    }
  }
예제 #15
0
  private IFD writeBModHufImage(
      ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param) throws IOException {
    try {
      int width = image.getWidth();
      int height = image.getHeight();

      IFD ifd = new IFD(); // entries need to be in tag order !

      ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file
      ifd.add(new DEFactory.ImageWidthDE(width)); // 256
      ifd.add(new DEFactory.ImageLengthDE(height)); // 257
      ifd.add(new DEFactory.CompressionDE(CCITTGROUP3MODHUFFMAN)); // 259
      ifd.add(new DEFactory.PhotometricInterpretationDE(WhiteIsZero)); // 262

      int maxrps, maxstripes; // max RowsPerStrip
      if ((1 << 13) <= width) {
        maxrps = 1;
        maxstripes = height; // one row per stripe
      } else {
        maxrps = (1 << 13) / width;
        maxstripes = (height + maxrps - 1) / maxrps;
      }

      DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes);
      ifd.add(offsets); // 273
      ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278
      DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes);
      ifd.add(counts); // 279

      if (param == null) {
        ifd.add(new DEFactory.XResolutionDE(72.0)); // 282
        ifd.add(new DEFactory.YResolutionDE(72.0)); // 283
      } else {
        ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282
        ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283
      }
      ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296

      int index = 0;
      for (int y = 0; y < height; y += maxrps) {
        /*
        Assume bilevel image (black/white[=-1])

        Each strip: count run length
                    encode into modified hufman codes
                    swap bits
                    save in byte array
                    write to image file
        */

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BitSwapOutputStream bsos = new BitSwapOutputStream(baos);
        ModHuffmanOutputStream mhos = new ModHuffmanOutputStream(bsos);

        RLEOutputStream rlos =
            new RLEOutputStream(mhos, 3); // rgb = 3 bytes per sample code word (not needed here)

        for (int i = 0; i < maxrps; i++) {
          if ((y + i) == height) {
            break;
          } // last strip might have less rows
          rlos.setStartCodeWord(-1); // white run first
          for (int x = 0; x < width; x++) {
            rlos.write(image.getRGB(x, y + i));
          }
          rlos.flush(); // write padding after ever image row
        }
        rlos.close();

        byte[] data = baos.toByteArray();
        counts.setCount(index, data.length); // update ifd strip counter array
        offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array
        out.write(data); // write to image stream

        index++;
      }
      return ifd;
    } catch (Exception e) {
      e.printStackTrace();
      throw new IOException(getClass().getName() + ".writeBModHufImage:\n\t" + e.getMessage());
    }
  }