views:

44

answers:

2

I'm trying to take a BufferedImage, apply a Fourier transform (using jtransforms), and write the data back to the BufferedImage. But I'm stuck creating a new Raster to set the results back, am I missing something here?

BufferedImage bitmap;
float [] bitfloat = null;

bitmap = ImageIO.read(new File("filename"));
FloatDCT_2D dct = new FloatDCT_2D(bitmap.getWidth(),bitmap.getHeight());

bitfloat = bitmap.getData().getPixels(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitfloat);
dct.forward(bitfloat, false);

But I'm stumped trying to finish off this line, what should I give the createRaster function? The javadocs for createRaster make little sense to me:

bitmap.setData(Raster.createRaster(`arg1`, `arg2`, `arg3`));

I'm starting to wonder if a float array is even necessary, but there aren't many examples of jtransforms out there.

A: 

I'm doing Google searches for FloatDCT_2D to see what package/library it's in, and it looks like there are several references to various sources, such as "edu.emory.mathcs.jtransforms.dct.FloatDCT_2D". Without knowing what custom library you're using, it's really hard to give you any advice on how to perform the transform.

My guess is in general, that you should read the input data from the original raster, perform the transform on the original data, then write the output to a new raster.

However, your last statement all on it's own looks odd... Raster.createRaster() looks like you're calling a static method with no parameters on a class you've never referenced in the code you've posted. How is that generating data for your bitmap??? Even in pseudo code, you would need to take the results of your transform and build the resultant raster.

Bal
Yes, that is what I'm trying to do. Raster.createRaster() takes three arguments, but I'm completely lost as to what to give it, the javadocs for createRaster are not useful at all.
Steve H
+1  A: 

Don't create a new Raster. Use WritableRaster.setPixels(int,int,int,int,float[]) to write the array back to the image.

final int w = bitmap.getWidth();
final int h = bitmap.getHeight();

final WritableRaster wr = bitmap.getData();
bitfloat = wr.getPixels(0, 0, w, h, bitfloat);

// do processing here

wr.setPixels(0, 0, w, h, bitfloat);    

Note also that if you're planning to display this image, you should really copy it to a screen-compatible type; ImageIO seldom returns those.

uckelman
Fantastic, I knew I was missing something obvious. Do remind me to award you the bounty if I forget (I have to wait 24h)
Steve H