public static BufferedImage encodeImage(String filename, int[] primes) {
    /* encodes a and b in the image using a sequence.
     * We are assuming that the image would be big enough
     * to hold the 16 bits
     */

    BufferedImage img, newimg = null;
    int[] a = convertToBinary(primes[0], 8);
    int[] b = convertToBinary(primes[1], 8);
    int[] a_b = copyBits(a, b); // copy all bits into one array

    try {
      img = ImageIO.read(new File(imagePath + filename));
      for (int i = 0; i < a_b.length; i++) {
        int p = img.getRGB(i, i);
        int[] bin = convertToBinary(p, 32);
        bin[0] = a_b[i];
        int d = convertToDigit(bin, 32);
        img.setRGB(i, i, d);
      }
      ImageIO.write(img, "png", new File(imagePath + "new_" + filename));
      newimg = ImageIO.read(new File(imagePath + "new_" + filename));
    } catch (IOException e) {
      System.out.println("ERROR WRITING IMAGE...\n" + e.toString());
      System.exit(1);
    }

    return newimg;
  }
Beispiel #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;
 }
Beispiel #3
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();
      }
    */
  }
Beispiel #4
0
 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
   _goodness = in.readFloat();
   int w = in.readInt();
   int h = in.readInt();
   _i = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
   for (int y = 0; y < h; y++) {
     int[] row = (int[]) in.readObject();
     _i.setRGB(0, y, w, 1, row, 0, 0);
   }
 }
Beispiel #5
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);
 }
Beispiel #6
0
  /*
   * USE AS A GUIDE
   *Method clears red values from jpg
   */
  public static BufferedImage depleteRed(int arr[][], BufferedImage x) {
    // local variables
    int arrWidth = arr.length;
    int arrHeight = arr[0].length;

    for (int i = 0; i < arrWidth; i++) {
      for (int j = 0; j < arrHeight; j++) {
        arr[i][j] = arr[i][j] & 0xFF00FFFF; // uses bitwise operator to turn off all red pixels
        x.setRGB(i, j, arr[i][j]);
      }
    }

    return x;
  }
Beispiel #7
0
 public static void main(String[] args) throws Exception {
   Robot robot = new Robot(args[0]);
   BufferedImage oimg = robot.cam.capture(true);
   // BufferedImage img = maslab.camera.ImageUtil.convertImage(oimg, BufferedImage.TYPE_INT_RGB);
   BufferedImage img =
       new BufferedImage(oimg.getWidth(), oimg.getHeight(), BufferedImage.TYPE_INT_RGB);
   for (int y = 0; y < oimg.getHeight(); ++y) {
     for (int x = 0; x < oimg.getWidth(); ++x) {
       img.setRGB(x, y, oimg.getRGB(x, y));
     }
   }
   BufferedImage blurred = gaussianBlur(img);
   ImageIO.write(blurred, "bmp", new File("gaussian.bmp"));
   BufferedImage xe = sobelX(blurred);
   ImageIO.write(xe, "bmp", new File("sobelx.bmp"));
   BufferedImage ye = sobelY(blurred);
   ImageIO.write(ye, "bmp", new File("sobely.bmp"));
   BufferedImage edges = addMagnitudes(xe, ye);
   BufferedImage dst = edges;
   ImageIO.write(dst, "bmp", new File("edges.bmp"));
 }
  // Takes a PImage and compresses it into a JPEG byte stream
  // Adapted from Dan Shiffman's UDP Sender code
  public byte[] compressImage(PImage img) {
    // We need a buffered image to do the JPG encoding
    BufferedImage bimg = new BufferedImage(img.width, img.height, BufferedImage.TYPE_INT_RGB);

    img.loadPixels();
    bimg.setRGB(0, 0, img.width, img.height, img.pixels, 0, img.width);

    // Need these output streams to get image as bytes for UDP communication
    ByteArrayOutputStream baStream = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baStream);

    // Turn the BufferedImage into a JPG and put it in the BufferedOutputStream
    // Requires try/catch
    try {
      ImageIO.write(bimg, "jpg", bos);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Get the byte array, which we will send out via UDP!
    return baStream.toByteArray();
  }
  public GroundTexture(int size, Ground grnd) {

    System.out.println("Calculating Ground Texture...");

    img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);

    double r, g, b;
    Random rnd = new Random();

    System.out.print("Initializing...");

    for (int x = 0; x < size; x++) {
      for (int y = 0; y < size; y++) {
        double slope = 0;
        for (int nx = x - 1; nx < x + 1; nx++)
          for (int ny = y - 1; ny < y + 1; ny++)
            if ((nx >= 0) && (nx < size))
              if ((ny >= 0) && (ny < size)) slope += Math.abs(grnd.topo[nx][ny] - grnd.topo[x][y]);
        if (slope < 1) slope = 1;
        g = 5d + 80d / (grnd.topo[x][y] / 2000d + 1d) + rnd.nextDouble() * 30d / (slope);
        r =
            g
                * (1.17
                    + (-.3 + (rnd.nextDouble() * .3))
                        / (grnd.topo[x][y] / 200d + 1d)
                        / (slope / 5 + 1));
        b =
            r
                * (.7
                    + (-.3 + (rnd.nextDouble() * .2))
                        / (grnd.topo[x][y] / 200d + 1d)
                        / (slope / 5 + 1));

        img.setRGB(x, y, colorRGB((int) r, (int) g, (int) b));
      }
    }

    /**/

    //		save("bodentextur2","png");

    System.out.print("                \rRandom Growth");

    for (int i = 0; i < size * size * 8; i++) {
      if (i % (size * size / 2) == 0) System.out.print(".");
      int x = 3 + rnd.nextInt(size - 6);
      int y = 3 + rnd.nextInt(size - 6);
      int nx = x - 1 + rnd.nextInt(3);
      int ny = y - 1 + rnd.nextInt(3);
      while ((nx < 0) || (nx >= size) || (ny < 0) || (ny >= size)) {
        nx = x - 1 + rnd.nextInt(3);
        ny = y - 1 + rnd.nextInt(3);
      }
      Color dis = getColorAt(x, y);
      Color col = getColorAt(nx, ny);
      if (grnd.topo[nx][ny] >= 4.5)
        if (col.getGreen() / col.getRed() > dis.getGreen() / dis.getRed())
          if (Math.abs(grnd.topo[x][y] - grnd.topo[nx][ny]) < .65) {
            int c = colorRGB(col.getRed(), col.getGreen(), col.getBlue());
            // img.setRGB(x,y, colorRGB(col.getRed(), col.getGreen(), col.getBlue()));
            // img.setRGB(x-1+rnd.nextInt(3),y-1+rnd.nextInt(3), colorRGB(col.getRed(),
            // col.getGreen(), col.getBlue()));
            for (nx = x - 1 - rnd.nextInt(3); nx < 1 + x + rnd.nextInt(3); nx++)
              for (ny = y - 1 - rnd.nextInt(3); ny < y + 1 + rnd.nextInt(3); ny++) {
                img.setRGB(nx, ny, c);
                grnd.topo[nx][ny] += 1.8;
              }
          }
    }

    System.out.print("                 \rAntialiasing...");
    /*		for (int x = 0; x < size; x++){
    	for (int y = 0; y < size; y++){
    		double sumr = 0;
    		double sumg = 0;
    		double sumb = 0;
    		double div = 0;
    		for (int nx=x-1;nx<x+1;nx++)
    			for (int ny=y-1;ny<y+1;ny++)
    				if ((nx>=0) && (nx < size)) if ((ny>=0) && (ny < size)) {
    					Color col = getColorAt(nx,ny);
    					sumr+=col.getRed();
    					sumg+=col.getGreen();
    					sumb+=col.getBlue();
    					div++;
    				}
    		r=sumr/div;
    		g=sumg/div;
    		b=sumb/div;
    		img.setRGB(x,y, colorRGB((int)r, (int)g, (int)b) );

    	}
    }*/

    System.out.print("        \r");
    //		save("bodentextur3","png");
    save("bodentextur", "png");
  }
Beispiel #10
0
 public void setRow(int[] row, int y) {
   _i.setRGB(0, y, row.length, 1, row, 0, 0);
 }
Beispiel #11
0
 public void set(int x, int y, int v) {
   x = boundx(x);
   y = boundy(y);
   _i.setRGB(x, y, v);
 }
  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);
  }
Beispiel #13
0
 /**
  * Create RGBA-8 image from ints (each int = AABBGGRR). why isn't there a built in method to do
  * this?
  */
 public static BufferedImage createRGBA8Image(int[] pix, int width, int height) {
   BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   img.setRGB(0, 0, width, height, pix, 0, width);
   return img;
 }
Beispiel #14
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;
      }
    }
  }