I have written two classes, one the GrabPixels class which takes an image and extracts the pixels from it. The FFT class then tries to call grabPixel. The problem I get is that img in this line
grabPix = new GrabPixels(img);
is null. I'm not too sure why. Could someone help.
GrabPixels
package finalproject;
import java.awt.Image;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
*
*/
public class GrabPixels {
static public BufferedImage img;
static public RunProgram run;
static public int[] pixels;
static private int width;
static private int height;
public GrabPixels(){
}
public GrabPixels(BufferedImage img) {
this.img = img;
try {
img = ImageIO.read(new File("E:\\face1.jpg"));
} catch (IOException ex) {
Logger.getLogger(GrabPixels.class.getName()).log(Level.SEVERE, null, ex);
}
}
public int[] getPixels() {
if (img != null) {
width = img.getWidth();
System.out.println("Width = " + width);
height = img.getHeight();
System.out.println("Height = " + height);
pixels = new int[width * height];
}
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
try {
pg.grabPixels();
for (int i = 0; i < pixels.length; i++) {
int pix = pixels[i];
pixels[i] = pix * 2;
System.out.println(pix);
i++;
}
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return null;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return null;
}
return pixels;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setWidth(int width) {
GrabPixels.width = width;
}
public void setHeight(int height) {
GrabPixels.height = height;
}
}
FFT
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package finalproject;
/**
*
* @author Joana Gana
*/
import java.awt.image.BufferedImage;
import java.lang.Math.*;
/**
* The FFT class contains methods to apply the 2D FFT to a
* TwoDArray.
*
*/
public class FFT {
/**
* Object instance of GrabPixels.
*/
private static GrabPixels grabPix;
/**
* Data structure to hold the input to the algorithm.
*/
public static TwoDArray input;
/**
* Data structure to hold the intermediate results of the algorithm.
* After applying the 1D FFT to the columns but before the rows.
*/
public static TwoDArray intermediate;
/**
* Data structure to hold the ouput of the algorithm.
*/
public static TwoDArray output;
/**
* Data structure to hold the 1D pixels of the image.
*/
private static int[] pix;
/**
* Width of the array.
*/
private static int width;
/**
* Height of the array.
*/
private static int height;
/**
* Default no argument constructor.
*/
public FFT() {
}
/**
* Constructor to set up an FFT object and then automatically
* apply the FFT algorithm.
*
* @param pixels int array containing the image data.
* @param w The width of the image in pixels.
* @param h The height of the image in pixels.
*/
public FFT(int[] pixel, int w, int h) {
input = new TwoDArray(pixel, w, h);
System.out.println(input);
intermediate = new TwoDArray(pixel, w, h);
output = new TwoDArray(pixel, w, h);
transform();
}
/**
* Method to recursively apply the 1D FFT to a ComplexNumber array.
*
* @param x A ComplexNumber array containing a row or a column of
* image data.
* @return A ComplexNumber array containing the result of the 1D FFT.
*/
static ComplexNumber[] recursiveFFT(ComplexNumber[] x) {
ComplexNumber z1, z2, z3, z4, tmp, cTwo;
int n = x.length;
int m = n / 2;
ComplexNumber[] result = new ComplexNumber[n];
ComplexNumber[] even = new ComplexNumber[m];
ComplexNumber[] odd = new ComplexNumber[m];
ComplexNumber[] sum = new ComplexNumber[m];
ComplexNumber[] diff = new ComplexNumber[m];
cTwo = new ComplexNumber(2, 0);
if (n == 1) {
result[0] = x[0];
} else {
z1 = new ComplexNumber(0.0, -2 * (Math.PI) / n);
tmp = ComplexNumber.cExp(z1);
z1 = new ComplexNumber(1.0, 0.0);
for (int i = 0; i < m; ++i) {
z3 = ComplexNumber.cSum(x[i], x[i + m]);
sum[i] = ComplexNumber.cDiv(z3, cTwo);
z3 = ComplexNumber.cDif(x[i], x[i + m]);
z4 = ComplexNumber.cMult(z3, z1);
diff[i] = ComplexNumber.cDiv(z4, cTwo);
z2 = ComplexNumber.cMult(z1, tmp);
z1 = new ComplexNumber(z2);
}
even = recursiveFFT(sum);
odd = recursiveFFT(diff);
for (int i = 0; i < m; ++i) {
result[i * 2] = new ComplexNumber(even[i]);
result[i * 2 + 1] = new ComplexNumber(odd[i]);
}
}
//System.out.println(result);
return result;
}
/**
* Method to apply the 2D FFT by applying the recursive 1D FFT to the
* columns and then the rows of image data.
*/
void transform() {
for (int i = 0; i < input.size; ++i) {
intermediate.putColumn(i, recursiveFFT(input.getColumn(i)));
}
for (int i = 0; i < intermediate.size; ++i) {
output.putRow(i, recursiveFFT(intermediate.getRow(i)));
}
for (int j = 0; j < output.values.length; ++j) {
for (int i = 0; i < output.values[0].length; ++i) {
intermediate.values[i][j] = output.values[i][j];
input.values[i][j] = output.values[i][j];
}
}
}
public static void main(String[] args) {
FFT fft = new FFT(pix, width, height);
grabPix = new GrabPixels();
BufferedImage img = GrabPixels.img;
grabPix = new GrabPixels(img);
if (grabPix != null) {
//System.out.println("does it work?");
pix = grabPix.getPixels();
width = grabPix.getWidth();
//System.out.println("yes!!!!");
height = grabPix.getHeight();
input = new TwoDArray(pix = grabPix.getPixels(), width = grabPix.getWidth(), height = grabPix.getHeight());
intermediate = new TwoDArray(pix = grabPix.getPixels(), width = grabPix.getWidth(), height = grabPix.getHeight());
output = new TwoDArray(pix = grabPix.getPixels(), width = grabPix.getWidth(), height = grabPix.getHeight());
fft.transform();
//System.out.println(output);
String print = "";
for (int i = 0; i < input.size; i++) {
for (int j = 0; j < input.size; j++) {
print = print + input.values[i][j];
}
print = print + "\n";
}
}
}
}
Thanks in advance!