3 Replies Latest reply on Feb 22, 2010 2:58 PM by makc3d

# repositioning groups of pixels

Hi,

I'm getting used to Pixel Bender and looping through each pixel.

I'm trying to make rectangular selections of pixels top to bottom, then left and right

and re-arrange the selections left to right, then top to bottom. Imagine looping through

pixels(x,y) and moving rectangular regions(y,x)

Here is how I see it actionscript, where bd is the original bitmap I chop:

```function chopBitmap():void {

var rect:BitmapData = new BitmapData(bd.width,bd.height,false,0xCC009900);

for(var j:int = 0 ; j < rows; j++){

for(var i:int = 0 ; i < columns ; i++){

var x:Number = i*selectionWidth;

var y:Number = j*selectionHeight;

rect.copyPixels(bd,new Rectangle(x,y,selectionWidth,selectionHeight),new Point(y,x));

}

}

}
```

I've looked through the PixelBender reference and found region, which might be handy.

I hanven't managed to use it though.

I found this post by Ryan Taylor: http://www.boostworthy.com/blog/?p=265

Tried the region bit, it compiles, but I'm not sure how to use it.

Any hints on how I could accomplish making rectangular selections and move them arround ?

Thanks,

George

• ###### 1. Re: repositioning groups of pixels

For every pixel you need to work out where it would have come from in the source image. I don't think you will need regions for this. You just need to think about the problem in a pixel by pixel way.

I'm not sure I fully understand how you want to move your regions so my example might be a bit off, but hopfully you can understand and make it work for your needs.

For simplicity lets say our image is 100 pixels wide and only 1 pixel high. We split that into 10 10x1 regions and we want to shift each region 10 pixels to the right.

Pixel 0,0 will be pixel 90,0 from the source.

Pixel 10, 0 will br pixel 0,0 from the source image.

You just need to work out the sums to make this happen.

I hope this helps.

• ###### 2. Re: repositioning groups of pixels

Thank you Richard,

I am trying that. Doing a bit of guesswork at the moment.

Here is what I got so far:

```<languageVersion : 1.0;>

kernel GridTest
<   namespace : "gp";
vendor : "George Profenza";
version : 1;
>
{
parameter float imageWidth<
minValue:1.0;
defaultValue:256.0;
maxValue:512.0;
>;
parameter float imageHeight<
minValue:1.0;
defaultValue:256.0;
maxValue:512.0;
>;
parameter float selectionWidth<
minValue:2.0;
defaultValue:128.0;
maxValue:500.0;
>;
parameter float selectionHeight<
minValue:2.0;
defaultValue:128.0;
maxValue:500.0;
>;
parameter float chopRows<
minValue:2.0;
defaultValue:8.0;
maxValue:16.0;

>;
parameter float chopColumns<
minValue:2.0;
defaultValue:8.0;
maxValue:16.0;

>;
input image4 src;
output pixel4 dst;

void
evaluatePixel()
{
float2 coord = outCoord();
if(coord.x < selectionWidth && coord.y > selectionHeight){
coord.x = imageWidth * 0.5   + coord.x;
coord.y = (imageHeight + imageHeight * .5) - coord.y;
}
if(coord.x > selectionWidth && coord.y < selectionHeight){
coord.x = -(coord.x - (imageWidth + imageWidth * 0.5));
coord.y = -(coord.y - imageHeight * 0.5);
}
pixel4 px = sampleNearest(src,coord);
dst = px;
}
}
```

It should work ok with a 256X256 image.

This test, looks ok for a 2X2 chopped bitmap, and I'm off by a lot:

1. the image and selection dimensions(w,h) don't work that well togehter.

2. this is problably the more pressing issue. I should use mod() or something to set coord.x and y instead of using if/else conditionals. Still getting used to not using for loops.

Any hints/further directions ?

Thanks,

George

• ###### 3. Re: repositioning groups of pixels

you may be off because when your second if is evaluated, coord var is already changed