Created
July 3, 2014 14:03
-
-
Save MSylvia/36c397576801731fc2bc to your computer and use it in GitHub Desktop.
Get/Set Pixels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean ZOOM = true; | |
void go() | |
{ | |
// 100x100 | |
int x = 30; | |
int y = 30; | |
int w = 10; | |
int h = 10; | |
// A 10x10 square with (12,30) the top left corner | |
color[] c = new color[w*h]; | |
// Set Green | |
c = FillColor(color(0,255,0), c); | |
SetPixels(0,0,w,h,c); | |
// Set Blue | |
c = FillColor(color(0,0,255), c); | |
SetPixels(10,0,w,h,c); | |
// Put half of each somewhere else | |
c = GetPixels(5,0,w,h); | |
SetPixels(x,y,w,h,c); | |
// Debug corners | |
pixelCache[0] = color(255,0,0); | |
pixelCache[49] = color(255,0,0); | |
} | |
color[] GetPixels(int x, int y, int w, int h) | |
{ | |
color[] c = new color[w*h]; | |
int s = x + (y * width); | |
int z = 0; | |
for (int i = s; i < s+w; i++) | |
for (int j = 0; j < h; j++) | |
{ | |
int loc = i + (j * width); | |
c[z] = pixelCache[loc]; | |
z++; | |
} | |
return c; | |
} | |
void SetPixels(int x, int y, int w, int h, color[] p) | |
{ | |
int s = x + (y * width); | |
int z = 0; | |
for (int i = s; i < s+w; i++) | |
for (int j = 0; j < h; j++) | |
{ | |
int loc = i + (j * width); | |
pixelCache[loc] = p[z]; | |
z++; | |
} | |
} | |
color[] FillColor(color c, color[] a) | |
{ | |
for(int i = 0; i < a.length; i++) | |
a[i] = c; | |
return a; | |
} | |
// --------------------------------------- | |
// DONT EDIT BELOW THIS LINE | |
// --------------------------------------- | |
PImage img; | |
int scaleWidth = 50; | |
int scaleHeight = 50; | |
color[] pixelCache; | |
// --------------------------------------- | |
void draw() | |
{ | |
begin(); | |
go(); | |
finish(); | |
} | |
// --------------------------------------- | |
void setup() { | |
size(200, 200); | |
noSmooth(); | |
img = createImage(width, height, RGB); | |
image(img, 0, 0); | |
} | |
// --------------------------------------- | |
void begin() { | |
loadPixels(); | |
pixelCache = pixels; | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
int loc = x + y * width; | |
if (x % 2 == 0) { // If we are an even column | |
pixelCache[loc] = color(255); | |
} else { // If we are an odd column | |
pixelCache[loc] = color(240); | |
} | |
} | |
} | |
} | |
// --------------------------------------- | |
void finish() { | |
pixels = pixelCache; | |
updatePixels(); | |
img.pixels = pixels; | |
img.updatePixels(); | |
if(ZOOM) | |
copy(img, 0, 0, scaleWidth, scaleHeight, 0, 0, width, height); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment