Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Creative Coding
- * Week 4, 03 - one pixel cinema
- * by Indae Hwang and Jon McCormack
- * Copyright (c) 2014 Monash University
- *
- * This simple sketch demonstrates how to read pixel values from an image
- * It simulates a 10 pixel "scanner" that moves from the top to the bottom of the image
- * reading the colour values for 10 equally spaced points, then displaying those colours
- * as vertical bars on the left half of the screen.
- *
- *
- * Spring Image: https://commons.wikimedia.org/wiki/File:Barn-wildflowers-spring-daffodil_-_West_Virginia_-_ForestWander.jpg
- * Creative Commons Attribution: http://www.forestwander.com/
- *
- * Summer Image: https://commons.wikimedia.org/wiki/File:Gold_Coast_summer,_Burleigh_Heads_Beach.jpg
- * Creative Commons
- *
- * Autumn Image: https://commons.wikimedia.org/wiki/File:Autumn_colours_(3030599812).jpg
- * Creative Commons
- *
- * Winter Image: https://commons.wikimedia.org/wiki/File:Northwich_(5329668354).jpg
- * Creative Commons
- *
- */
- PImage[] myImg = new PImage[4];
- color[] pixelColors;
- int[] scanLine = new int[4]; // vertical position
- void setup() {
- size(1040, 320); // allow for 2x2 pictures + 400 width for our artwork
- // all images have been resized to 320x160
- myImg[0] = loadImage("Spring.jpg");
- myImg[1] = loadImage("Summer.jpg");
- myImg[2] = loadImage("Autumn.jpg");
- myImg[3] = loadImage("Winter.jpg");
- pixelColors = new color[10];
- scanLine[0] = 0;
- scanLine[1] = 0;
- scanLine[2] = 0;
- scanLine[3] = 0;
- smooth(4);
- rectMode(CORNER);
- //frameRate(10);
- }
- void draw() {
- background(0);
- drawSamples();
- drawImages();
- drawScanLines();
- moveScanLines();
- saveFrame("frames/#####.tga");
- }
- void drawSamples() {
- int xOffset;
- int yOffset;
- for (int i = 0; i < 4; i++) {
- // read the colours for the current scanLine
- for (int s=0; s<10; s++) {
- pixelColors[s] = myImg[i].get((s+1)*myImg[i].width/11, scanLine[i]);
- }
- // determine offsets for drawing the samples
- switch(i) {
- case 0:
- xOffset = 0;
- yOffset = 0;
- break;
- case 1:
- xOffset = 200;
- yOffset = 0;
- break;
- case 2:
- xOffset = 0;
- yOffset = 160;
- break;
- case 3:
- xOffset = 200;
- yOffset = 160;
- break;
- default:
- xOffset = 0;
- yOffset = 0;
- }
- // draw the sampled pixels as verticle bars
- for (int s=0; s<10; s++) {
- noStroke();
- fill(pixelColors[s]);
- rect(xOffset+(s*19), yOffset, xOffset+((s+1)*19), yOffset+160);
- }
- }
- }
- void drawImages() {
- image(myImg[0], 400, 0);
- image(myImg[1], 720, 0);
- image(myImg[2], 400,160);
- image(myImg[3], 720,160);
- }
- void drawScanLines() {
- // draw circles over where the "scanner" is currently reading pixel values
- for (int i=0; i<10; i++) {
- stroke(255, 0, 0);
- noFill();
- ellipse(400 + (i+1)*myImg[0].width/11, scanLine[0], 5, 5);
- ellipse(720 + (i+1)*myImg[1].width/11, scanLine[1], 5, 5);
- ellipse(400 + (i+1)*myImg[2].width/11, 160 + scanLine[2], 5, 5);
- ellipse(720 + (i+1)*myImg[3].width/11, 160 + scanLine[3], 5, 5);
- }
- }
- void moveScanLines() {
- for (int i = 0; i < 4; i++) {
- // move the scanlines at different rates
- if (frameCount%(i+2) == 0) {
- scanLine[i] ++;
- }
- // reset scanlines if past the image height
- if (scanLine[i] > myImg[i].height) {
- scanLine[i] = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement