My task includes reading a .jpg image, accessing the pixels, processing them and displaying the final image
the code is as follows:
public static void main(String[] args) {
BufferedImage sourceImage = null;
try {
sourceImage = ImageIO.read(new File("IScSH.jpg"));
} catch (IOException e) { }
int type = sourceImage.getType();
int w = sourceImage.getWidth();
int h = sourceImage.getHeight();
byte[] pixels = null;
if (type == BufferedImage.TYPE_3BYTE_BGR) {
System.out.println("type.3byte.bgr");
pixels = (byte[]) sourceImage.getData().getDataElements(0, 0, w, h, null);
}
try {
BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, pixels);
ImageIO.write(edgesImage, "jpg", new File("nvRhP.jpg"));
} catch (IOException e) { }
}
the above code works fine, but if I write something as, before the try block begins,
for ( i = 0; i < data.length; i++) {
data[i] = pixels[offset++] & 0xff;
}
i.e convert the byte array to integer array and then display the image it gives wrong output. The problem is, as I have to further process these pixels I have to convert them in integer values but then the result is wrong.
Please help
How to create image from integer pixels stored in array
Started by MrSaurabh, Feb 12 2012 02:42 AM
31 replies to this topic
#1
Posted 12 February 2012 - 02:42 AM
|
|
|
#2
Posted 12 February 2012 - 04:16 AM
If the data in the byte array works, why copy that data to an int array?
Why is the result wrong if you are working with an int array instead of a byte array?
Does your process change the values of the ints so that any of them are greater than 255 (the max byte contents)?
Why is the result wrong if you are working with an int array instead of a byte array?
Does your process change the values of the ints so that any of them are greater than 255 (the max byte contents)?
#3
Posted 12 February 2012 - 06:18 AM
I need to process the pixels further, hence converting to integer. No the values are in range 0-255
#4
Posted 12 February 2012 - 06:31 AM
Can you explain why the results are wrong? Can you give some examples.
Why do you need to convert the byte array to an int array?
Why do you need to convert the byte array to an int array?
#5
Posted 12 February 2012 - 06:35 AM
#6
Posted 12 February 2012 - 06:44 AM
Why do you need to convert the byte array to an int array?
For testing: try working with a small simple image where you know each pixel's value when you start and what you want it to be when you're done. Then compare the results with the desired and if they are different, go into the code to see why.
For testing: try working with a small simple image where you know each pixel's value when you start and what you want it to be when you're done. Then compare the results with the desired and if they are different, go into the code to see why.
#7
Posted 12 February 2012 - 07:23 AM
@Norm: Thankyou Sir, but I guess the question isn't clear to you. My task is to compress a image losslessly using some algo(its called LOCO-I). Now before I take the pixels and apply the formula and compress image, what I intended to do is just display the original image as it is but reading from integer array. And I NEED INTEGER ARRAY coz calculations becomes easy as per my algo. which I am yet to implement. So could you plz help me with it?
#8
Posted 12 February 2012 - 07:30 AM
Please explain why the results are wrong. if you convert bytes to ints, process the ints and then convert the ints back to bytes, where in these steps is the process going wrong?
I have no idea what your processing of the ints does. I can help you with the converting bytes to ints and converting ints to bytes. The processing of the ints is up to you.
I have no idea what your processing of the ints does. I can help you with the converting bytes to ints and converting ints to bytes. The processing of the ints is up to you.
#9
Posted 12 February 2012 - 07:37 AM
that is what m not understanding, referring the code, may be there is some problem withimage_type
BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, data);
ImageIO.write(edgesImage, "jpg", new File("nvRhP.jpg"));
data[] is int array. Hope to figure it out. Could you plz tell me easy way to convert ints to bytes array again.
BufferedImage edgesImage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
edgesImage.getWritableTile(0, 0).setDataElements(0, 0, w, h, data);
ImageIO.write(edgesImage, "jpg", new File("nvRhP.jpg"));
data[] is int array. Hope to figure it out. Could you plz tell me easy way to convert ints to bytes array again.
#10
Posted 12 February 2012 - 07:44 AM
Quote
way to convert ints to bytes array
#11
Posted 12 February 2012 - 08:14 AM
Could you please put the code
#12
Posted 12 February 2012 - 08:14 AM
Something like this
begin loop
byteVar = (byte)intVar
end loop
begin loop
byteVar = (byte)intVar
end loop
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









