Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Clyde on February 03, 2009

Title: Indexing
Post by: Clyde on February 03, 2009
I dont know if this is the correct terminology, but I refer to it as Indexing ( sometimes referencing ) which I use in conjuction with a pallete which has a range of 255. Anyhows, hopefully you'll get what im jibber jabbing about below.

Today, I tinkered in the IDE, and am now using a system of one dimensional arrays in the form of pointers.

What I have is a colour pallette which is [ 255*1 ]
And an ScreenBuffer of [ 640*480 ]

I accsess them by [ x+y*Width ]

For the method of pallette indexing I previously used, the screen info would be stored in a 2 dimensional arrays eg, Screen( 640,480 ) and the pallette as a single Pal( 256 ). Id then use the following to extract the corresponding colour out of the pallete with:

Colour=Pal( Screen(x,y) )

What I would like to know is how I would achieve this using the new method of one dimensional arrays?

I have tried this method but it is wrong someplace :(


Val=Screen[ x+y*ScreenWidth ]
Col=Pal[ Value ]
Title: Re: Indexing
Post by: Shockwave on February 03, 2009
You are nearly there..


Do this instead;



Val=Screen( x+(y*ScreenWidth) )
Col=Pal(Val )



Or better still something like..



dim as uinteger ptr p1
dim as integer l

p1=@screen(y*screenwidth)

for l=0 to width

        Col=Pal(*p1 )
        p1+=1

next




Title: Re: Indexing
Post by: Clyde on February 04, 2009
Cheers and thanks alot dude :)
Title: Re: Indexing
Post by: Clyde on February 11, 2009
My second indexing problem:

Hope that you get the jist of things, its quite hard to explain. So I'll try and make it sense.

I have an array that is: pattern( (ScreenWidth*3)+(ScreenHeight*3) ) which is a huge pattern made up by anding the colour by 255. So that it is in the range 0-255, much like a pallete.

And I have an image full of colour information or better still a texture, which is
Image(256*256).

And I am really stuck on how to compute that into 1 dimensional array indexing.

Before in my old method of a Dim Array( Width, Height ) I'd use the following inside a For X and For Y loop. ( This is technique used from a bin2bas image file )

pixel=colourimage( Pattern(Pos1)+Pattern(Pos2)+Inc and 255,   Pattern(Pos4)+Pattern(Pos5)+Inc and 255 )
Col=(imagered(pixel,0) Shl 16) Or (imagegrn(pixel,0) Shl 8 )  Or imageblu(pixel,0)


What I have tried for a day or so to do isnt right somehow:

This is what I came up with:
Value=Image( ( (Pos1)+(Pos2)+Inc And 255 )+( (Pos4)+(Pos5)+Inc And 255 )*ImageWidth )
Col=Pattern( (Value) * (PatternWidth) )

if anyone could sort me out much appreciated! :D

Cheers and bless ya,
Clyde.

Title: Re: Indexing
Post by: Shockwave on February 11, 2009
Post your code please.
Title: Re: Indexing
Post by: Clyde on February 11, 2009
I will pursue this some more. And let you know if im still having headaches with it.

Thanks,
Clyde.
Title: Re: Indexing
Post by: Jim on February 11, 2009
You have code like

a+b+c AND 255

You need

(a+b+c) AND 255
and
((a+b+c) AND 255)*ImageWidth

Jim
Title: Re: Indexing
Post by: Clyde on February 11, 2009
Thanks Jim for the tip, appreciate it :)
Title: Re: Indexing
Post by: Clyde on October 08, 2009
This uses palette indexing, but i am not sure if i have this correct adapting it into C++. Please dont show me optimizations for the time being, I'd just like to know and see if I have it correct, especially in the pixel colour working out section as I dont know if you'd use plus; ive tried this though it compiles, nothing shows up.

both screen_buffer->pixels[], pal->pixels[], and buffer->pixels are unsigned integer pointers; just for quickness i havent put the creation into this, just as Mum says its bed time.

AA / Smooth Image Routine; with little example on palette indexing.

int x,y;
unsigned int col;

for ( y=1; y<buffer->height-2; y++)
{
            for (x=1; x<buffer->wwidth-2; x++)
{
                col=(( buffer->pixels[ (x+1)+(y)*buffer->wwidth ]+
                                                      buffer->pixels[ (x-1)+(y)*buffer->wwidth ]+
                                                      buffer->pixels[ (x)+(y+1)*buffer->wwidth ]+
                                                      buffer->pixels[ (x)+(y-1)*buffer->wwidth ] ) / 5 );

if (col<0xFF000000) col=0xFF000000;
screen_buffer->pixels[x+y*wwidth]=pal->pixels[ col &255 ];

           }
     }
}


Cheers,
Clyde.
Title: Re: Indexing
Post by: Jim on October 09, 2009
The buffer->pixels are all rgb colours.  Adding them together is going to look a bit funny - if you wanted that to give you an average colour then they'd have to split in to separate r,g,b bits, then do the add, the divide, and then convert back to an integer.

Say you have two colours, colour0 and colour1, then this is how to get the average colour

unsigned int r0 = (colour0>>16)&0xff;
unsigned int g0 = (colour0>>8)&0xff;
unsigned int b0 = colour0&0xff;

unsigned int r1 = (colour1>>16)&0xff;
unsigned int g1 = (colour1>>8)&0xff;
unsigned int b1 = colour1&0xff;

unsigned int r = (r0+r1)/2;
unsigned int g = (g0+g1)/2;
unsigned int b = (b0+b1)/2;

unsigned int averagecolour = (r<<16)|(g<<8)|b;

You can extend that to blend more colours, eg. (r0+r1+r2)/3  or (r0+r1+r2+r3)/4 etc.

But your real problem is that once you have got this rgb, you are trying to use it to look up a palette entry.
First of all this:
if (col<0xFF000000) col=0xFF000000;
is always going to set col to 0xff000000, because all rgb colours with no alpha are less than 0xff000000.
Then this:
col&255
is always going to be 0, because 0xff00000 & 255 is always 0.
So, I guess your palette colour 0 is black and pal->pixels[0] is black and that's what you see.

You could easily see this in the VS2008 debugger - you should try to learn to use it for single stepping through the code and watching the numbers change.

If you want to write the average colour into the screen, you can't use the palette, you'll need to use the calculation above to blend the colours, and do

screen_buffer->pixels[x+y*wwidth] = averagecolour;


Quick question:
Is buffer->pixels palette indexes or rgb colours?  You didn't show it.  Above I assumed they were rgb colours.  But, if you're adding together palette indexes that's probably not going to work either - same problem, they always end up as 0 because of the if and the &.  Then you might be able to use the resultant averaged palette index to look up a colour, but that's very unlikely to be what you really want - you'd have to have arranged the palette colours in a very special way.

Jim
Title: Re: Indexing
Post by: Clyde on October 09, 2009
I've been using this method in Freebasic for a few years now. onlything i've altered is to include the alpha ( shl or shr 24 )

palette_buffer->pixels[ 255 ];//  argb colours 0-255
buffer->pixels[ width*height ];// are the same - argb.

I generally do is in the process that plots where a colour, ie the blue channel, is to have the buffer have an integer value, thats then combined in the aa, blur, fire routine; thats used in

col  =src->pixels[ xpos+ypos*wwidth]
argb=pal->pixels[col and 255]

so im guessing that the use of operator / numeric signs are different to that of Blitz or Freebasic.
Title: Re: Indexing
Post by: Jim on October 09, 2009
Quoteso im guessing that the use of operator / numeric signs are different to that of Blitz or Freebasic.
I don't know what you mean.

And if FB is & in C
Or in FB is | in C
Shr in FB is >> in C
Shl in FB is << in C

This:

if (col<0xFF000000) col=0xFF000000;
pixel = [col & 255];

is the main problem in your code, it's making everything black.

Jim

Title: Re: Indexing
Post by: rain_storm on October 10, 2009
Sorry for my ignorance but what exactly does this do?

    pixel = [col & 255];

is that the same as

    LPVOID temp = col & 255;
    pixel = &temp;

or is it just the same as

    pixel = 0xFF000000 & 0x000000FF;

Title: Re: Indexing
Post by: Clyde on October 10, 2009
pixel=pal->argb[ col &255 ]
is used to get a range of colours made up in a palette from 0-255.