Example #1
0
 /**
  * @param pixels - Byte Array with Pixels
  * @param w - Image Width (columns)
  * @param h - Image Heigth (row)
  */
 PanelImage(byte[] pixels, int w, int h, boolean quad) {
   if (quad) {
     bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
     bi.setRGB(0, 0, w, h, byteArrayToIntArrayQuad(pixels), 0, w);
   } else {
     bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
     bi.setRGB(0, 0, w, h, byteArrayToIntArray(pixels, w, h), 0, w);
   }
   this.setPreferredSize(new Dimension(w, h));
   this.setVisible(true);
 }
Example #2
0
  public void saveFrametoPNG(String filename) {
    final int frameWidth = _canvas.getWidth();
    final int frameHeight = _canvas.getHeight();
    final ByteBuffer pixelsRGB = Direct.newByteBuffer(frameWidth * frameHeight * 3);
    _canvas.runWithContext(
        new Runnable() {
          public void run() {
            // glPushAttrib(GL_PIXEL_MODE_BIT);
            glReadBuffer(GL_BACK);
            glPixelStorei(GL_PACK_ALIGNMENT, 1);
            glReadPixels(0, 0, frameWidth, frameHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelsRGB);
            // glPopAttrib();
          }
        });
    int[] pixelInts = new int[frameWidth * frameHeight];
    int p = frameWidth * frameHeight * 3;
    int q; // Index into ByteBuffer
    int i = 0; // Index into target int[]
    int w3 = frameWidth * 3; // Number of bytes in each row
    for (int row = 0; row < frameHeight; row++) {
      p -= w3;
      q = p;
      for (int col = 0; col < frameWidth; col++) {
        int iR = pixelsRGB.get(q++);
        int iG = pixelsRGB.get(q++);
        int iB = pixelsRGB.get(q++);
        pixelInts[i++] =
            0xFF000000 | ((iR & 0x000000FF) << 16) | ((iG & 0x000000FF) << 8) | (iB & 0x000000FF);
      }
    }

    // Create a new BufferedImage from the pixeldata.
    BufferedImage bufferedImage =
        new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_ARGB);
    bufferedImage.setRGB(0, 0, frameWidth, frameHeight, pixelInts, 0, frameWidth);

    try {
      javax.imageio.ImageIO.write(bufferedImage, "PNG", new File(filename));
    } catch (IOException e) {
      System.out.println("Error: ImageIO.write.");
      e.printStackTrace();
    }

    /* End code taken from: http://www.felixgers.de/teaching/jogl/imagingProg.html */

    /*
     final BufferedImage image = new BufferedImage(
     this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
     Graphics gr = image.getGraphics();
     this.printAll(gr);
     gr.dispose();
    try {
    	ImageIO.write(image, "PNG", new File(filename));
      } catch (IOException e) {
           System.out.println( "Error: ImageIO.write." );
           e.printStackTrace();
      }
    */
  }
  /** 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 #4
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);
 }
  /**
   * 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;
      }
    }
  }
 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);
 }
  public static void main(String[] args) {
    // read filename and N 2 parameters
    String fileName = args[0];
    N = Integer.parseInt(args[1]);

    // output two images, one original image at left, the other result image at right
    BufferedImage imgOriginal =
        new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
    BufferedImage img = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);

    try {
      File file = new File(fileName);
      InputStream is = new FileInputStream(file);

      long len = file.length();
      byte[] bytes = new byte[(int) len];

      int offset = 0;
      int numRead = 0;
      while (offset < bytes.length
          && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
      }

      int ind = 0;
      for (int y = 0; y < IMAGE_HEIGHT; y++) {
        for (int x = 0; x < IMAGE_WIDTH; x++) {
          // for reading .raw image to show as a rgb image
          byte r = bytes[ind];
          byte g = bytes[ind];
          byte b = bytes[ind];

          // set pixel for display original image
          int pixOriginal = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
          imgOriginal.setRGB(x, y, pixOriginal);
          ind++;
        }
      }
      is.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    int[] vectorSpace = calculateVectorSpace(imgOriginal);

    // quantization
    for (int y = 0; y < IMAGE_HEIGHT; y++) {
      for (int x = 0; x < IMAGE_WIDTH; x++) {
        int clusterId = vectorSpace[IMAGE_WIDTH * y + x];
        img.setRGB(x, y, clusters[clusterId].getPixel());
      }
    }

    // Use a panel and label to display the image
    JPanel panel = new JPanel();
    panel.add(new JLabel(new ImageIcon(imgOriginal)));
    panel.add(new JLabel(new ImageIcon(img)));

    JFrame frame = new JFrame("Display images");

    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
Example #8
0
  public void write() {

    int navg = 8;
    //		int nshift=3;
    bookend = 8;

    numPgs = pdffile.getNumPages();

    files = new File[numPgs];

    int[] pixelsi = null;
    long[] sum = null;
    long[][] hist = null;
    BufferedImage bimage = null;

    //		BufferedImage simage = null;
    //	    float data[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f,
    //	            0.0625f, 0.125f, 0.0625f };
    //	    Kernel kernel = new Kernel(3, 3, data);
    //	    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);

    done(0);
    for (int i = 0; i < numPgs; i++) {
      if (i > MAXPAGE) break;
      PDFPage page = getPage(i);
      if (i == 0) {
        w = (int) page.getBBox().getWidth();
        h = (int) page.getBBox().getHeight();
        rect = new Rectangle(0, 0, w, h);
        // w /= 2; h /=2;
        bimage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
        // simage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
        pixelsi = new int[h * w];
        sum = new long[h * w];
        hist = new long[navg][h * w];
        for (int j = 0; j < h * w; j++) {
          sum[j] = 0;
        }
        page1.countDown();
      }

      // generate page image
      Image image = pdffile.getPage(i).getImage(w, h, rect, null, true, true);
      // force complete loading
      image = new ImageIcon(image).getImage();
      // Copy image to buffered image
      Graphics g = bimage.createGraphics();
      // Paint the image onto the buffered image
      g.drawImage(image, 0, 0, null);
      g.dispose();

      // extract pixels into array
      bimage.getRGB(0, 0, w, h, pixelsi, 0, w);
      // Accumulate rolling averages
      int im = i % navg;
      int ii = i - navg / 2; // middle of averaging range
      for (int j = 0; j < h * w; j++) {
        int p = pixelsi[j], q = 0;

        // Expand packed 8x3 pixel to 16x3
        long r = 0, r2 = 0;
        r |= (p & 0xff);
        r <<= 16;
        p >>= 8;
        r |= (p & 0xff);
        r <<= 16;
        p >>= 8;
        r |= (p & 0xff);
        r = ~r;
        sum[j] += r; // rolling sum
        if (i >= navg) { // we have enough to average
          sum[j] -= hist[im][j]; // roll off the old
          hist[im][j] = r;
          r2 = (3 * sum[j] / navg + r) / 4;
          r = ~r2;
        } else {
          hist[im][j] = r;
        }

        // Repack averaged pixel
        q |= (r & 0xff);
        q <<= 8;
        r >>= 16;
        q |= (r & 0xff);
        q <<= 8;
        r >>= 16;
        q |= (r & 0xff);

        //	Average over number of images frames with a non-background pixel in this location.
        //  Note that we sum in complement space, so background is zero.
        //				if(i>=navg)
        //				nfg[j] -= fghist[im][j];
        //			nfg[j] += (fghist[im][j] = (q==-1) ? 0 : 1);
        //
        //				// If all pixels in history were background, this is easy...
        //				if(nfg[j]==0)
        //					q = -1;
        //
        //				else {
        //
        //					if(i>=navg) sum[k]-=hist[im][k];
        //					hist[im][k] = ~(p&0xff);
        //					sum[k] += hist[im][k];
        //					if(i>=navg)
        //						q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2);
        //					else if(i>=navg/2)
        //						q += ~(sum[k]/nfg[j] + hist[i-navg/2][k])>>1;
        //					else
        //						q += ~(sum[k]/nfg[j]);
        //					k++; q<<=8; p>>=8;
        //
        //					if(i>=navg) sum[k]-=hist[im][k];
        //					hist[im][k] = ~(p&0xff);
        //					sum[k] += hist[im][k];
        //					if(i>=navg)
        //						q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2);
        //				else if(i>=navg/2)
        //				q += ~((sum[k]/nfg[j] + hist[i-navg/2][k])>>1);
        //			else
        //				q += ~(sum[k]/nfg[j]);
        //			k++; q<<=8; p>>=8;
        //
        //					if(i>=navg) sum[k]-=hist[im][k];
        //					hist[im][k] = ~(p&0xff);
        //					sum[k] += hist[im][k];
        //					if(i>=navg)
        //						q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2);
        //					else if(i>=navg/2)
        //						q += ~((sum[k]/nfg[j] + hist[i-navg/2][k])>>1);
        //					else
        //						q += ~(sum[k]/nfg[j]);
        //					k++;
        //				}

        pixelsi[j] = q;
      }

      bimage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
      bimage.setRGB(0, 0, w, h, pixelsi, 0, w);

      // save it as a file
      if (i >= navg) {
        try {
          ImageIO.write(bimage, "png", imageFile(0, ii + 1));
        } catch (Exception e) {
          throw new RuntimeException(e.getMessage());
        }
      }

      done(ii + 1);
      // System.err.println("Page " + i + " " + (System.currentTimeMillis()-t0)/1000.);

      if (terminated) {
        System.err.println("Prematurely terminated");
        for (File f : files) f.delete();
        tmpdir.delete();
        break;
      }
    }
  }
  public void run() {
    while (true) {
      BALL = null;
      ball_t ballLCM = new ball_t();

      synchronized (depthMonitor) {
        try {
          depthMonitor.wait();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      ballLCM.nanoTime = depthStream.latestTimestamp;
      ArrayList<Statistics> blobs;
      depthStream.getReadLock().lock();
      try {
        blobs = finder.analyze2(depthStream.getValidImageArray());
      } finally {
        depthStream.getReadLock().unlock();
      }
      Statistics ball = null;
      Statistics robot = null;
      int minSize = pg.gi("blobThresh");

      // find robot and ball by blob size
      Collections.sort(blobs, ComparatorFactory.getStatisticsCompareSize());
      // find robot and ball by y pixel
      // Collections.sort(blobs, ComparatorFactory.getStatisticsCompareYPix());
      //			if (tracking) {
      //				System.out.println("num blobs: " + blobs.size());
      //				System.out.println("biggest blob size: " + blobs.get(0).N);
      //			}
      //			for (Statistics blob : blobs) {
      //				if (blob.N > 10) {
      //					System.out.println("blob size: " + blob.N);
      //				}
      //				else {
      //					break;
      //				}
      //			}
      if (blobs.size() == 1) {
        Statistics first = blobs.get(0);
        if (first.N > minSize) {
          ball = first;
        }
      } else if (blobs.size() >= 2) {
        Statistics first = blobs.get(0);
        Statistics second = blobs.get(1);
        if (first.N > minSize) {
          ball = first;
        }
        if (second.N > minSize) {
          robot = first;
          ball = second;
        }
      }

      // System.out.println("balls points " + depthStream.trajectory.size());

      // if not tracking keep kv.depthStream.trajectory to just one index
      if (!tracking) {
        depthStream.trajectory.clear();
      }

      if (ball != null) {
        depthStream.trajectory.add(ball);

        Point depthPix = ball.center();
        Point depthCoord = new Point();
        depthCoord.x = depthPix.x - KinectVideo.C_X;
        depthCoord.y = KinectVideo.C_Y - depthPix.y;

        // System.out.println("avg depth " + ball.Uz());

        // get depth from average depth of blob
        double realDepth = raw_depth_to_meters(ball.Uz());
        Point3D coord = depthStream.getWorldCoords(depthCoord, realDepth);
        if (depthPix != null) {
          for (int y = depthPix.y - 3; y < depthPix.y + 3; y++) {
            for (int x = depthPix.x - 3; x < depthPix.x + 3; x++) {
              try {
                depthImg.setRGB(x, y, 0xFFFF0000);
              } catch (Exception e) {
                // System.out.println(x + " " + y);
              }
              ;
            }
          }
          // if (tracking) {
          // 	//save image
          // 	depthStream.getReadLock().lock();
          // 	try {
          // 		File imgFile = new File("image" + ballNum++ + ".png");
          // 		try {
          // 			ImageIO.write(depthImg, "png", imgFile);
          // 		}
          // 		catch(Exception e) {
          // 			System.out.println("can't save img");
          // 		}
          // 	}
          // 	finally {
          // 		depthStream.getReadLock().unlock();
          // 	}
          // }

          if (tracking) {
            ballLCM.x = coord.x;
            ballLCM.y = coord.y;
            ballLCM.z = coord.z;
            // if(tracking)
            System.out.println("updating new ball (" + System.currentTimeMillis() + ")");
            //						if (ballLCM.x > CatchController.TARGET_MAX_X)
            lcm.publish("6_BALL", ballLCM);
            //						else
            //							System.out.println("ball past target zone");
          }
        }
      }
    }
  }