package image; import java.awt.*; import java.awt.image.*; import javax.swing.*; /** *

Title: ImageGrabber

*

Description: takes and image and coverts the image in to an array ov values

*

Copyright: Copyright (c) 2007

*

Company: Hanover College

* @author John H. Krantz, Ph.D. * @version 0.1 */ public class ImageGrabber extends JPanel { static final long serialVersionUID = 0; private Image pic, drawPic; private int [] pixels;// pixels from picture in integer values private double [][][] pixelArray; // pixels laid out in a // three dimensional array private int [] origPixels; // the starting point. private int picWidth,picHeight;// dimensions of picture // private int picSize; // overall size of the picture private boolean scaled = true; // trigger to scale the // image the first time it is draw and then use that data // for the rest of the time. private int bright = 0, oldBright = 0; // update objects public final static int NONE = -1; public final static int LAST = 17; private int angleDone = NONE; public ImageGrabber() { } public ImageGrabber(Image source){ setPic(source); } public void paint (Graphics g){ int width = getSize().width; int height = getSize().height; g.setColor(Color.darkGray); g.fillRect(0,0,width,height); MemoryImageSource source = new MemoryImageSource(picWidth, picHeight, pixels, 0, picWidth); source.setAnimated(true); source.setFullBufferUpdates(true); Image img = Toolkit.getDefaultToolkit().createImage(source); // draw the image if (img.getSource() != null){ if (! scaled){ double picRatio = 1.0; if ((double)(height)/(double)(picHeight) < (double)(width)/(double)(picWidth)){ picRatio = (double)(height)/(double)(picHeight); // only go up to the maximum of the picture size } else { picRatio = (double)(width)/(double)(picWidth); } if (picRatio > 1.0) picRatio = 1.0; if (picRatio != 0.0 && picRatio < 1.0) scaleImage(picRatio); scaled = true; source = new MemoryImageSource(picWidth, picHeight, pixels, 0, picWidth); source.setAnimated(true); source.setFullBufferUpdates(true); img = Toolkit.getDefaultToolkit().createImage(source); drawPic = img; } if (oldBright != bright) doBrightness(); g.drawImage(img,0,0,this); // now draw lines to indicate how far the processing has // proceeded Graphics2D g2d = (Graphics2D)g; g2d.setStroke(new BasicStroke(2.0f)); for (int i = 0; i <= angleDone; i ++){ g2d.setPaint(Color.green); g2d.translate(width/2, height/2); g2d.rotate((double)i*Math.PI/18.0); g2d.drawLine(0, -height/2, 0, height/2); g2d.rotate(-(double)i*Math.PI/18.0); g2d.translate(-width/2, -height/2); } } } // set methods public void setPic(Image source){ pic = source; picWidth = 0; picHeight = 0; while (picWidth <= 0 | picHeight <= 0) { picWidth = pic.getWidth(null); picHeight = pic.getHeight(null); if (picWidth > 0 & picHeight > 0) { pixels = new int[picWidth * picHeight]; PixelGrabber pg = new PixelGrabber(pic, 0, 0, picWidth, picHeight, pixels, 0, picWidth); try { pg.grabPixels(); } catch (InterruptedException e){}; } } origPixels = new int[pixels.length]; for (int i = 0; i < pixels.length; i ++) origPixels[i] = pixels[i]; scaled = false; repaint(); } public void setBrightness(int br){ bright = (br>= -255 && br<= 255 ? br : 0); repaint(); // present the new image } public void setAngleDone(int angle){ angleDone = angle > NONE & angle <= LAST ? angle : NONE; } private void doBrightness(){ /** * this method simple offsets all of the pixel values by a contstant value * based loosely on methods in ImageJ - thanks for all of their * great work. */ int [] lut = new int [256]; // look up table for offet of contrast // create the lut (lookup table) for (int i = 0; i < lut.length; i ++){ lut[i] = i+bright; if (lut[i]>255) lut[i] = 255; if (lut[i]<0) lut[i] = 0;// correct range } // create the updated image for (int i=0; i < pixels.length; i++) { int pixl = origPixels[i];// start with the original image int r = lut[(pixl&0xff0000)>>16]; int g = lut[(pixl&0xff00)>>8]; int b = lut[pixl&0xff]; pixels[i] = (pixl&0xff000000) + (r<<16) + (g<<8) + b; } oldBright = bright; }// end method doBrightness // get methods public Image getImage() { return pic; } public Image getScaledImage() { return drawPic; } public double [][][] getPixelArray(){ int x = pic.getWidth(null); int y = pic.getHeight(null); int z = 3; pixelArray = new double [x][y][z]; // get and arrange the image data for (int i = 0; i < x; i ++){ for (int j = 0; j < y; j ++){ int r = (origPixels[j*x+i]&0xff0000)>>16; int g = (origPixels[j*x+i]&0xff00)>>8; int b = origPixels[j*x+i]&0xff; pixelArray[i][j][0] = r; pixelArray[i][j][1] = g; pixelArray[i][j][2] = b; } } return pixelArray; } private void scaleImage(double scale){ int newWidth = (int)((double)picWidth*scale); int newHeight = (int)((double)picHeight*scale); int newPixels[] = new int[newWidth*newHeight]; double srcCenterX = picWidth/2.0; double srcCenterY = picHeight/2.0; double newCenterX = newWidth/2.0; double newCenterY = newHeight/2.0; double xs, ys; int index1, index2; for (int y=0; y<=newHeight-1; y++) { ys = (y-newCenterY)/scale + srcCenterY; index1 = picWidth*(int)ys; index2 = y*newWidth; for (int x=0; x<=newWidth-1; x++) { xs = (x-newCenterX)/scale + srcCenterX; newPixels[index2++] = pixels[index1+(int)xs]; } } // now reset the image picWidth = newWidth; picHeight = newHeight; pixels = newPixels; // origPixels = new int [picWidth * picHeight]; // for (int i = 0; i < pixels.length; i ++) // origPixels[i] = pixels[i]; } }