source
swapPixelsTest.pde
/**
* 任意の座標の色情報を入れ替える
*/
int COLORS[] = {
color(255, 0, 0),
color(0, 255, 0),
color(0, 0, 255),
color(255, 255, 0),
color(255, 0, 255),
color(0, 255, 255),
color(255, 255, 255),
};
// 取得元の開始座標
PVector swapSourcePositions[] = {
new PVector(120, 0),
new PVector(320, 0),
};
// 入れ替え先の開始座標
PVector swapDestPositions[] = {
new PVector(240, 0),
new PVector(540, 0),
};
// 入れ替える縦横サイズ
PVector swapSize = new PVector(10, 350);
void settings() {
size(700, 350);
}
void setup() {
background(0);
noFill();
stroke(0);
// 色付きのバーを描画
drawColorfulRectBars();
// 任意の座標のpixelsを入れ替える
doSwapPixels();
}
void draw() {
}
void drawColorfulRectBars() {
for (int i=0;i<COLORS.length;i++) {
fill(COLORS[i]);
rect(i * 100, 0, (i + 1) * 100, height);
fill(255);
text("[" + (i * 100) + ", 10]", i * 100 + 5, 15);
}
}
void doSwapPixels() {
int[] sourcePixels = new int[int(swapSize.x * swapSize.y)];
int[] destPixels = new int[int(swapSize.x * swapSize.y)];
loadPixels();
for (int i=0;i<swapSourcePositions.length;i++) {
for (int incY = 0;incY < int(swapSize.y); incY++) {
int sourceY = int(swapSourcePositions[i].y) + incY;
int destY = int(swapDestPositions[i].y) + incY;
for (int incX = 0;incX < int(swapSize.x); incX++) {
int sourceX= int(swapSourcePositions[i].x) + incX;
int destX = int(swapDestPositions[i].x) + incX;
sourcePixels[i] = pixels[sourceY * width + sourceX];
destPixels[i] = pixels[destY * width + destX];
pixels[destY * width + destX] = sourcePixels[i];
pixels[sourceY * width + sourceX] = destPixels[i];
//print(String.format("[%03d, %03d] -> [%03d, %03d] /// ", sourceX, sourceY, destX, destY));
}
//println();
}
}
updatePixels();
}