reading a image into a int array

how can i do this

i know how to read the image into a byte array

but not int array

[101 byte] By [aaa801a] at [2007-11-27 11:54:30]
# 1

Uh, just taking a wild guess -- you create an array of int and repeat whatever you did before? ;)

If that's not what you wanted to do, you'll have to be a bit more specific in your request.

Time_Agentessa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 2

> Uh, just taking a wild guess -- you create an array

> of int and repeat whatever you did before? ;)

>

> If that's not what you wanted to do, you'll have to

> be a bit more specific in your request.

images can be read as stream of bytes .they cant be stored into an integer array.

srinivassa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 3

> how can i do this

> i know how to read the image into a byte array

> but not int array

need to understand basics of computing first ..!

srinivassa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 4

Aha, thanks! Learning something new every day, it seems :)

Time_Agentessa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 5

> how can i do this

> i know how to read the image into a byte array

> but not int array

try searching the forums, this question has been asked and code has been posted, i'm sure more than a few times

SoulTech2012a at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 6

> > Uh, just taking a wild guess -- you create an

> array

> > of int and repeat whatever you did before? ;)

> >

> > If that's not what you wanted to do, you'll have

> to

> > be a bit more specific in your request.

>

> images can be read as stream of bytes .they cant be

> stored into an integer array.

An array of int seems to be more correct other than using an array of Integer objects. Anyway you only need an array of int if your image is bigger than 4 byte, these because an int in java has 4 byte.

MeTitus

Me_Titusa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 7

Four bytes in an int?! Why wasn't I informed!?

ParvatiDevia at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 8

> Four bytes in an int?! Why wasn't I informed!?

You can inform yourself right here.

http://mindprod.com/jgloss/conversion.html

MeTitus

Me_Titusa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 9

I am not entirelly sure why you need to do this, but here is something that might help you.

byte[] byteImage = new byte[8];

int[] intImage = new int[byteImage.length/4];

byteImage[0] = 1;

byteImage[1] = 2;

byteImage[2] = 3;

byteImage[3] = 4;

byteImage[4] = 5;

byteImage[5] = 6;

byteImage[6] = 7;

byteImage[7] = 8;

String strTmp = "";

for(int iter=0;iter<byteImage.length;iter++)

{

strTmp += byteImage[iter];

strTmp += byteImage[++iter];

strTmp += byteImage[++iter];

strTmp += byteImage[++iter];

intImage[(iter/4)] = Integer.parseInt(strTmp);

strTmp = "";

}

MeTitus>

Me_Titusa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 10

What if:

byteImage[0] = (byte)255;

...

byteImage[7] = (byte)255;

ParvatiDevia at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 11

What?

Basically a byte can hold 256 characters, in digits this can go from 0 to 255.

255 = 111111111

0= 00000000

MeTitus

Me_Titusa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 12

> What if:

> > byteImage[0] = (byte)255;

> ...

> byteImage[7] = (byte)255;

>

And you don't need to cast.

byteImage[0] = 255;

byteImage[7] = 255;

MeTitus

Me_Titusa at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 13

> Basically a byte can hold 256 characters

What do you mean, 256 characters. Like a string of length 256?

ParvatiDevia at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 14

> And you don't need to cast.

>

> byteImage[0] = 255;

>

> byteImage[7] = 255;

But with this example:

public class Konfused {

public static void main(String[] args) {

byte[] byteImage = new byte[8];

byteImage[0] = 255; //error on this line

}

}

I am getting the compile-time error: possible loss of precision

ParvatiDevia at 2007-7-29 18:56:17 > top of Java-index,Java Essentials,Java Programming...
# 15

A byte can hold the ASCII characters from 0 to 255 if note unsegned

or -127 to 128 if I am not mistaken cause the last byte value would hold the sign value.

MeTitus

Me_Titusa at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 16

> A byte can hold the ASCII characters from 0 to 255 if note unsegned

How do you make a byte unsigned in Java?

ParvatiDevia at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 17

> or -127 to 128 if I am not mistaken

Still getting the error "loss of precision"

byte b = 128;

ParvatiDevia at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 18

To get this easier, you can have 256 different combinations in a byte, if you have 2 then its 256*256. Some byte values have an ASCII correspondent other dont, for eg, chinese characters need to be represented by e byte UTF16, 2*8 bit, because they don't have an correspondent in the ASCII table. Don't think about int as number or strings as letters, they are all bytes anyway. But constraints were added to int, long... primitive types which say that they can only hold bytes which have an numeric match in the ASCII table.

Hope this helps.

MeTitus

Me_Titusa at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 19

byte b = (byte) 128;

cotton.ma at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 20

> > or -127 to 128 if I am not mistaken

>

> Still getting the error "loss of precision"

> > byte b = 128;

>

Just shift 8 bits. You are not going to loose any precision anyway.

<< 8

MeTitus

Me_Titusa at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 21

>. But constraints were added to

> int, long... primitive types which say that they can

> only hold bytes which have an numeric match in the

> ASCII table.

What in the ?

cotton.ma at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 22

Do you really need to drag ASCII into the description of datatype byte? Is that the only way to define a byte? How does this related to the original question -- placing image data into an int[]? What is the relationship of ASCII tables to image data?

ParvatiDevia at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 23

> Just shift 8 bits. You are not going to loose any

> precision anyway.

>

> << 8

>

> MeTitus

When I shift 8 bits:

byte b = 1 << 8;

I still get the error "loss of precision".

ParvatiDevia at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 24

> byte b = (byte) 128;

In java a byte is sign, so like I said before it uses the last position of the byte to

represent the sign.

It seems you need to cast anyway.

byte a = (byte)127 -- valid

byte c = (byte)256 --not valid

MeTitus

Me_Titusa at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 25

> > A byte can hold the ASCII characters from 0 to 255

> if note unsegned

>

> How do you make a byte unsigned in Java?

byte a = -127;

MeTitus

Me_Titusa at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 26

> >

> byte c = (byte)256 --not valid

>

>

>

You know that compiles right?

And could you explain what the hell you meant when you started taking about constraints on primitives and ASCII tables?

cotton.ma at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 27

That's for your explanation. But your code for converting 4 bytes into an int still isn't working:

public class Konfused {

public static void main(String[] args) {

byte[] byteImage = new byte[8];

int[] intImage = new int[byteImage.length/4];

byteImage[0] = (byte)255;

byteImage[1] = (byte)255;

byteImage[2] = (byte)255;

byteImage[3] = (byte)255;

byteImage[4] = (byte)255;

byteImage[5] = (byte)255;

byteImage[6] = (byte)255;

byteImage[7] = (byte)255;

String strTmp = "";

for(int iter=0;iter < byteImage.length;iter++) {

strTmp += byteImage[iter];

strTmp += byteImage[++iter];

strTmp += byteImage[++iter];

strTmp += byteImage[++iter];

intImage[(iter/4)] = Integer.parseInt(strTmp);

strTmp = "";

}

}

}

When I run this, I get the error "java.lang.NumberFormatException: For input string: "-1-1-1-1".

The error is thrown by parseInt.

ParvatiDevia at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 28

> Basically a byte can hold 256 characters, in digits

> this can go from 0 to 255.

> 255 = 111111111

> 0= 00000000

> MeTitus

That is true for the make believe world of unsigned types which

java doesnt have (except for char).

In the signed world the order of the values is

0 -> 127 then -128 -> -1

so the conversion is:

byte b = (number > 127) ? (number - 256) : number;

public class ByteOperations{

public static void main(String[] args){

byte b;

b = convertUnsigned(0);

b = convertUnsigned(127);

b = convertUnsigned(128);

b = convertUnsigned(255);

}

public static void display(byte b){

int num = Integer.parseInt("FF", 16) & (int)b;

System.out.println("Byte " + b + ": " + Integer.toBinaryString(num));

}

public static byte convertUnsigned(int num){

byte b = (num > 127) ? (byte)(num - 256) : (byte)num;

display(b);

return b;

}

}

TuringPesta at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 29

> > Just shift 8 bits. You are not going to loose any

> > precision anyway.

> >

> > << 8

> >

> > MeTitus

>

> When I shift 8 bits:

> > byte b = 1 << 8;

>

> I still get the error "loss of precision".

Why are you shifting there, makes no sense. Eg:

byte[] aa = new byte[2];

int aInt = 300; // 2 bytes

aa[0] = (byte)aInt;

aInt = aInt >> 8;

aa[1] = (byte)aInt;

MeTitus

Me_Titusa at 2007-7-29 18:56:21 > top of Java-index,Java Essentials,Java Programming...
# 30

TP: thanks for clarifying things. Once you see it, it's so simple.

ParvatiDevia at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 31

> > > >

>

> > byte c = (byte)256 --not valid

> >

> >

> >

>

> You know that compiles right?

>

> And could you explain what the hell you meant when

> you started taking about constraints on primitives

> and ASCII tables?

I didnt explain myself right. I wasn't saying that constrainst were added to the ascii table, but that primitive types such as short, int, long... can only have the byte values that have a digit match in the ascii table.

MeTitus

Me_Titusa at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 32

> TP: thanks for clarifying things. Once you see it,

> it's so simple.

Good we could help you, and just to finalize.

the maximum number an int can if, is givem by this:

255 255 255 255 = 4 (byte * 255)

MeTitus

Me_Titusa at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 33

> the maximum number an int can if, is givem by this:

>

> 255 255 255 255 = 4 (byte * 255)

or rather:

-1 -1 -1 -1

TuringPesta at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 34

-4?

BigDaddyLoveHandlesa at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 35

> -4?

o_O

MeTitus

Me_Titusa at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 36

> > -4?

>

> o_O

>

>

> MeTitus

Reply #27 -- your code isn't working.

BigDaddyLoveHandlesa at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 37

Titus' code doesnt work because he's trying to use Integer.parseInt

with a makeshift string of negatives, lol.

Integer.parseInt("-1-1-1-1", 16);

If you are going the String way (which is a HORRIBLE idea) at least

do it right. Output the integer as a Hex String and then do String

processing to assure that the String is padded with a 0 in front and

is exactly 2 characters.

TuringPesta at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 38

> > > -4?

> >

> > o_O

> >

> >

> > MeTitus

>

> Reply #27 -- your code isn't working.

lol, I did that with notepad, in my sisters computer couldn't get right at first...

It is not going to work if you have even numbers... I'll fix that later..

But anyway the code was just to help the op understand about bits and bytes ;)

MeTItus

Me_Titusa at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...
# 39

> Titus' code doesnt work because he's trying to use

> Integer.parseInt

> with a makeshift string of negatives, lol.

>

> Integer.parseInt("-1-1-1-1", 16);

>

> If you are going the String way (which is a HORRIBLE

Thats why we have people like you that help the community improving ;)

We can't be all like you...

How would you do that then? Trying to improve here ;)

MeTitus

Me_Titusa at 2007-7-29 18:56:26 > top of Java-index,Java Essentials,Java Programming...