Jump to content

How to create image from integer pixels stored in array

- - - - -

  • Please log in to reply
31 replies to this topic

#1
MrSaurabh

MrSaurabh

    Newbie

  • Members
  • PipPip
  • 16 posts
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

#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
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)?

#3
MrSaurabh

MrSaurabh

    Newbie

  • Members
  • PipPip
  • 16 posts
I need to process the pixels further, hence converting to integer. No the values are in range 0-255

#4
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
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?

#5
MrSaurabh

MrSaurabh

    Newbie

  • Members
  • PipPip
  • 16 posts
java - How do i create image from pixels stored as r, g, b each occupying different position in array? - Stack Overflow
here u can see the output

#6
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
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.

#7
MrSaurabh

MrSaurabh

    Newbie

  • Members
  • PipPip
  • 16 posts
@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
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
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.

#9
MrSaurabh

MrSaurabh

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#10
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

way to convert ints to bytes array
Use a loop and an assignment statement

#11
MrSaurabh

MrSaurabh

    Newbie

  • Members
  • PipPip
  • 16 posts
Could you please put the code

#12
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Something like this
begin loop
byteVar = (byte)intVar
end loop




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users