Click to See Complete Forum and Search --> : Array,new,delete->something wrong


Leite33
September 23rd, 2006, 07:26 AM
Hi. I made something to get pixel values from pic. The code is below but there is something wrong:

x=Picture Height;
y=Picture Width;

int i,j;
int *pin=new int[x*y];

for(i=0;i<y;i++)
for(j=0;j<x;j++)
pin[i*j]=Image1->Canvas->GetPixel[i][j];

So when i use malloc operator and array like pin[i][j] i get the correct values but when i use new,delete and array like pin[i*j] i take wrong values. Why;
I am new in arrays and i have to use new and delete operators and not malloc and free. Can anyone help;;;

wildfrog
September 23rd, 2006, 08:40 AM
This is typically what I would do:

for(i=0;i<y;i++) // top to bottom
for(j=0;j<x;j++) // left to right
pin[(i * x) + j]=Image1->Canvas->GetPixel[i][j];

- petter

Leite33
September 23rd, 2006, 03:06 PM
No this doesnt work. it brings me error. You cant put + on array like this
pin[(i*x)+j]

wildfrog
September 23rd, 2006, 03:13 PM
What error?

- petter

Leite33
September 23rd, 2006, 03:14 PM
ok i tried pin[i+j]=..... but still on value 2 values are false....Helpppppppppp

Leite33
September 23rd, 2006, 03:15 PM
teh error before with pin[(i*x)+j] was "illegal floating point"

Zaccheus
September 23rd, 2006, 05:53 PM
This is typically what I would do:

for(i=0;i<y;i++) // top to bottom
for(j=0;j<x;j++) // left to right
pin[(i * x) + j]=Image1->Canvas->GetPixel[i][j];

- petter
Correct.

greg_dolley
September 23rd, 2006, 09:08 PM
teh error before with pin[(i*x)+j] was "illegal floating point"

That's because one of your variables are probably floating point (maybe the "x"?). You can't index by floats, only whole numbers. Try casting the whole expression as a long, like this:


pin[(long)((i*x)+j)]


Greg Dolley

Leite33
September 24th, 2006, 12:47 AM
Greg you are amazing thanks.But still i dont get :

1.why i did pin[(i*x)+j)] and not lets say pin[(i*j)] or something like that

2. why i used long, the x and y are integer are y=width of pic and x=height of pic
i and j i think are still integer, are the pixel values of pic like black=0 red=255 .....

Leite33
September 24th, 2006, 12:53 AM
i find why long sorry my mistake.. But i still dont get why pin[(i*x]+j]

greg_dolley
September 25th, 2006, 05:09 AM
i find why long sorry my mistake.. But i still dont get why pin[(i*x]+j]

You defined "x" in your original post as the picture height, not width. The expression "(i*height)+j" is an incorrect equation. It needs to be "(i*width)+j", or in your code: pin[(i*y)+j].

Btw, typically when programming anything with cartesian coordinates, you use "x" to represent anything going along the horizontal axis, and "y" to represent anything going along the vertical axis.

Greg Dolley

Leite33
September 25th, 2006, 07:00 AM
Yes i know that.. but why you dont do pin[(i*y)+(j*x)]

wildfrog
September 25th, 2006, 07:46 AM
Yes i know that.. but why you dont do pin[(i*y)+(j*x)]Because the image is stored in a one-dimentional array where the first pixel in a line is located just after the last pixel in the previous line.
Image look like this:

0123456789
0123456789
0123456789

Array looks like this:

012345678901234567890123456789


You want that pixel at [3, 1] (the red one). P = (Y*W) + X = (1 * 10) + 3 = 13. Now, count from 0, left to right, top to bottom until you reach 13.

- petter

Leite33
September 25th, 2006, 09:37 AM
ok
i think i got it... i took valyes like black=0 red=255 is it right?

Leite33
September 25th, 2006, 09:41 AM
i made pin[(i*width)+j] but i took wrong values
the correct is [(i*height)+j] probably depends what you put for height an width