Bitcrushing Live Video Input to the NES Colour Palette: P5 Code

06Feb11

Processing source code. Draws NES colour-palletized webcam input to the screen. Read more about the NES colour palette here. Many thanks to The Almighty Guru site for the NES Hacker wiki entry with the RGB values list.

//HendersonSix NES Colorizer 1.0
//Thanks to the Almighty Guru's NES Hacker Wiki for the RGB List

import processing.video.*;

String[] palette;  //string array to load the palette.txt file
PVector[] colors = new PVector[64];  //256 colors in the palette file
float d;  //variable to store the distance between the NES color and the sampled color

Capture video;

int res=4;  //sets the increment value in the for loops
float mindist = 256;  //set minimum dist to maximum
color nes;  //the color eventually drawn to the screen

void setup(){

  size(640, 480);

  video = new Capture(this, width, height);

  palette = loadStrings("NesPalette.txt");  //text file with RGB values

    //divide each line from the .txt file into R, G, and B values
  for(int i=0; i<palette.length; i++){
    String[] cols = split(palette[i], ',');
    if(cols.length == 3){
      colors[i] = new PVector(int(cols[0]), int(cols[1]), int(cols[2]));
    }
  }
  noStroke();
}

void draw(){

  if(video.available()){
    video.read();

    //loop through the video input
    for(int x=0; x<video.width; x+=res){
      for(int y=0; y<video.height; y+=res){

        color currColor = video.get(x, y);
        //isolate the R, G, and B channels
        int rval = (currColor >> 16) & 0xFF;
        int gval = (currColor >> 8 ) & 0xFF;
        int bval = currColor & 0xFF;
        //store them in a pvector
        PVector curr = new PVector(rval, gval, bval);

        mindist = 256;  //reset minimum distance variable to maximum

        //get the distance between the current sampled color and the nes color palette using the brute force method
        for(int i=0; i<palette.length; i++){
          d = colors[i].dist(curr);
          if(d < mindist){
            mindist = d;  //if there is a new lowest distance, set mindist accordingly
            nes = color(colors[i].x, colors[i].y, colors[i].z);
          }
        }
        fill(nes);  //fill rect with the color from the nes palette
        rect(x, y, res, res);
      }
    }
  }
}

void keyPressed(){
  if(key==32){
    save(frameCount+".jpg");
    println("saved! "+frameCount);
  }
}
About these ads


One Response to “Bitcrushing Live Video Input to the NES Colour Palette: P5 Code”


  1. 1 I’ve been 8bitted « People's Foreign Exchange

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: