Array,new,delete->something wrong
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;;;
[571 byte] By [
Leite33] at [2007-11-20 1:36:09]

# 1 Re: Array,new,delete->something wrong
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
# 2 Re: Array,new,delete->something wrong
No this doesnt work. it brings me error. You cant put + on array like this
pin[(i*x)+j]
# 3 Re: Array,new,delete->something wrong
What error?
- petter
# 4 Re: Array,new,delete->something wrong
ok i tried pin[i+j]=.... but still on value 2 values are false...Helpppppppppp
# 5 Re: Array,new,delete->something wrong
teh error before with pin[(i*x)+j] was "illegal floating point"
# 6 Re: Array,new,delete->something wrong
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.
# 7 Re: Array,new,delete->something wrong
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
# 8 Re: Array,new,delete->something wrong
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 ....
# 9 Re: Array,new,delete->something wrong
i find why long sorry my mistake.. But i still dont get why pin[(i*x]+j]
# 10 Re: Array,new,delete->something wrong
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
# 11 Re: Array,new,delete->something wrong
Yes i know that.. but why you dont do pin[(i*y)+(j*x)]
# 12 Re: Array,new,delete->something wrong
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
# 13 Re: Array,new,delete->something wrong
ok
i think i got it... i took valyes like black=0 red=255 is it right?
# 14 Re: Array,new,delete->something wrong
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