开发者

How to create a Raster from a pixel float array in Java?

开发者 https://www.devze.com 2023-01-22 03:17 出处:网络
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 m开

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 m开发者_开发百科issing 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消