c++ - An algorithm for iterating over a rectangular area inside a 1 dimensional array (bitmapping) -
this odd question had tough time writing title for.
i'm working pixels (bitmaps, more specifically) , can't figure out (simple) maths pragmatically accessing each array cell.
my canvas [n16 x 16] pixels, n 1 or greater.
here's photo of basic n = 2 canvas:
http://i.imgur.com/mabwqfj.png
what want magical algorithm run 0 495 without touching lighter grey area go 16 512 (which cell 511, bad) without touching dark grey area.
so, 0 15, skip 16 31 followed 32 47, etc.
and n = 3:
http://i.imgur.com/tqjmwl6.png
in case 0-735 skipping lighter grey areas, 16-751 skipping areas on each side , 32-767 skipping darker grey areas.
what tried:
here's extract code, it's useful , shows tried already. it's part figures out value 'idxpos'.
// let's length = 3 now. (int character = 0; character < length; ++character) { // in case you're wondering, grabs 16x16 characters ascii spritesheet charpos = (string[character] - ' ') * 16 * 16; // runs through spritesheet character map // huge 16x1520 bitmap. (int pixel = 0; pixel < 16 * 16; ++pixel) { // ignore this, me messing around pixel tinting r = (((charmap[charpos + pixel] >> 0) & 0xff) + 255 - u); g = (((charmap[charpos + pixel] >> 8) & 0xff) + 255 - v); b = (((charmap[charpos + pixel] >> 16) & 0xff) + 255 - w); newcolour = rgb(r, g, b); // part stuck on: idxpos = pixel + (character * 16 * 16); bitmap[idxpos] = charmap[charpos + j]; } }
you idea. sounds dead simple me can't figure out.
oh, , i'm not interested in magical library can handle bitmap stuff me, i'm not in position can use one.
if question properly, want visit them in order mentioned. here's code (given n):
for(int = 0; < n; i++) //which section going through { for(int row = 0; row < size; row++) //size = 16, better use on of constants { for(int col = 0; col < size; col++) { int pixelindex = size * (row * n) + col + size * i; /*the last 1 offset - moves index right far need. if need 2 coordinates (as in (x,y)) instead of 1 number, is: */ int x = row, y = col + size * i; dosomethingwithpixel(pixelindex); } } }
hope helps.
Comments
Post a Comment