Hi, I´ve got a doubt, whit arrays and setpixel command.

Previous topic - Next topic

mentalthink

Hi, I´ve got a doubt, whit arrays and setpixel command.

Well, I explain a little what I want to do:

1-I have a picture, and I want copy part of this image to an array (I know what have a new command sprite2mem but I want to do whit arrays, for post-processing)

2-ok I copied the pixels from a image to array, whit 2 simple fors and an array of 2 dimensions, seems like this:

for j=0 to 100
  for i=0 to 100
   arrayC[j]=getpixel(j,i)
next
next

3-Ok I have my colors in an array 2 dimensions.

4- Now the trouble... I put the data in the location :Ex array[10][10], and it´s have the value of 8 or 9 digits, but I want know the RGB values of this point array[10][10], how I can desglose the 2 dimensions array for obtaint the Red,Gree,Blue values individually.

Thanks and Best Regards,
Iván J

Moru

Getpixel gets you a bunch of bytes, each byte contains one of the rgb components and one is empty. I'm uncertain of the correct order though.

Something like this:
RR00GGBB or similar. Paint a picture so you know the colors and then extract the different bytes from the integer and you should see what is what.

Let us know if you need more hints on how to do it :-)

Edit: Just read up on sprite2mem and there you have the values already.

mentalthink

Hi Moru, thanks in advance for your support and help.

Well I look how you say the sprite2mem command but is very new for me, I don´t understand very well, but give some ideas for extract the RGB, but whitout succes.

I try to get the 2 values of array int this case I make the array$(in strings) and after whit command Mid$, I try to divide in parts for extrat the rgb, now I´m thinking and I think this is wrong, because not are 3 parts else 4, and I think this values I have to translate to Hex, for it´s works  :S, I think this don´t works too, but don´t arrive to much far.

Well Moru, if you can give a hand, I go to sleep very happy  =D

Best Regards, and thanks again for your support, and ofcourse, for your time.

Iván Jimenez.

PS: Don´t worry for reply (if you desire ) very quickly, I have too much code still, for finish the application, only stay making probes, for not have frights, more forward.

Moru

no problem, what you need to do is think base 16, not 10 :-)

You need to single out the components with math since it's an integer.

If you divide the number by 256 (0x100 hex), you will move the next byte to the lower section of the integer. Then you need to remove the upper part of the variable by bAND(0xFF, variable%)

Code (glbasic) Select
//To single out the second component:
variable% = 0xFF007755
green% = variable% / 0x100
green% = bAND(0xFF, green%)


This should get you the value of green, 0x77 (119 decimal)

Moebius

Alternatively, you can use the 'shift right' function - 'ASR()'.  From what I know, it should be slightly faster than dividing by 256, unless there is optimisation done by the compiler.

Example:
Code (glbasic) Select
// The same as Moru's example, but using ASR
variable% = 0xFF007755
green% = bAND(0xFF, ASR(variable%, 8)) // Shift 'variable' across a byte (8 bits), and then 'AND' it with 256 so the byte value for green is left
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Moebius

Quote from: Ocean on 2010-Sep-11
Should, could & would lead you nowhere.  Write a few lines of code to measure & validate your assumptions on your target platform(s).  To measure is to create facts, these really beat assumptions.

I umm... was busy...  I had to do an assignment....  :whistle:
Anyway, I have been bothered to do a test in order to protect the remains of my programming integrity.  The times are almost exactly the same after putting each one through 2 billion loops on my PC  :P    Anyway, I just thought it looks neater to shift the variable across a byte rather than divide by 256, even though they both do the same...
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Moru

Yes I preffer the shift too, it is more natural for this sort of problem and easier to understand. But that command didn't exist when I last had use for this :-)


mentalthink

Hi, Moru, Serpent & Ocean, thanks for reply.

Well I go to explain what I made, don´t works, I think I´m stay making some wrong.

I use this function, was made hemlos in another part of forum for change from decimal to hex:

Hemlos´s Code
Code (glbasic) Select


FUNCTION ASC2HEX$: a2hv
LOCAL Hex16$,count,a1$,a2$,a2h$
Hex16$="0123456789ABCDEF"
count=-1
FOR i=0 TO 15
FOR ii=0 TO 15
INC count, 1
IF count=a2hv
a1$=MID$(Hex16$,i,1)
a2$=MID$(Hex16$,ii,1)
a2h$=a1$+""+a2$
ENDIF
NEXT
NEXT
RETURN a2h$
ENDFUNCTION



1- I get the pixel what I want whit GetPixel(mx,my) and put into array[0][0]

2-now in this array[0][0]= have a number like this 6554345

3- well I put this number to hex whit the Hemlos´s Function and divide by 0x100

Well it´s all I made.

Best Regards, ad thanks againg for your time and help.

Moru

You don't have to convert to hex, that is just a way to show the number to us humans. What you really want to do is directly extract the rgb-values from the color.


mentalthink

ok, Moru Thanks again for your support.

Best Regards,
Iván J.