Advertisement
GilesCartmel

Creative Coding w4_03_FourSeasons

Aug 29th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. /*
  2.  * Creative Coding
  3.  * Week 4, 03 - one pixel cinema
  4.  * by Indae Hwang and Jon McCormack
  5.  * Copyright (c) 2014 Monash University
  6.  *
  7.  * This simple sketch demonstrates how to read pixel values from an image
  8.  * It simulates a 10 pixel "scanner" that moves from the top to the bottom of the image
  9.  * reading the colour values for 10 equally spaced points, then displaying those colours
  10.  * as vertical bars on the left half of the screen.
  11.  *
  12.  *
  13.  * Spring Image: https://commons.wikimedia.org/wiki/File:Barn-wildflowers-spring-daffodil_-_West_Virginia_-_ForestWander.jpg
  14.  * Creative Commons Attribution: http://www.forestwander.com/
  15.  *
  16.  * Summer Image: https://commons.wikimedia.org/wiki/File:Gold_Coast_summer,_Burleigh_Heads_Beach.jpg
  17.  * Creative Commons
  18.  *
  19.  * Autumn Image: https://commons.wikimedia.org/wiki/File:Autumn_colours_(3030599812).jpg
  20.  * Creative Commons
  21.  *
  22.  * Winter Image: https://commons.wikimedia.org/wiki/File:Northwich_(5329668354).jpg
  23.  * Creative Commons
  24.  *
  25. */
  26.  
  27. PImage[] myImg = new PImage[4];
  28. color[] pixelColors;
  29. int[] scanLine = new int[4];  // vertical position
  30.  
  31. void setup() {
  32.   size(1040, 320); // allow for 2x2 pictures + 400 width for our artwork
  33.   // all images have been resized to 320x160
  34.   myImg[0] = loadImage("Spring.jpg");
  35.   myImg[1] = loadImage("Summer.jpg");
  36.   myImg[2] = loadImage("Autumn.jpg");
  37.   myImg[3] = loadImage("Winter.jpg");
  38.  
  39.   pixelColors = new color[10];
  40.   scanLine[0] = 0;
  41.   scanLine[1] = 0;
  42.   scanLine[2] = 0;
  43.   scanLine[3] = 0;
  44.   smooth(4);
  45.   rectMode(CORNER);
  46.   //frameRate(10);
  47. }
  48.  
  49. void draw() {
  50.   background(0);
  51.   drawSamples();
  52.   drawImages();
  53.   drawScanLines();
  54.   moveScanLines();
  55.   saveFrame("frames/#####.tga");
  56. }
  57.  
  58. void drawSamples() {
  59.   int xOffset;
  60.   int yOffset;
  61.   for (int i = 0; i < 4; i++) {
  62.     // read the colours for the current scanLine
  63.     for (int s=0; s<10; s++) {
  64.       pixelColors[s] = myImg[i].get((s+1)*myImg[i].width/11, scanLine[i]);
  65.     }
  66.     // determine offsets for drawing the samples
  67.     switch(i) {
  68.       case 0:
  69.         xOffset = 0;
  70.         yOffset = 0;
  71.         break;
  72.       case 1:
  73.         xOffset = 200;
  74.         yOffset = 0;
  75.         break;
  76.       case 2:
  77.         xOffset = 0;
  78.         yOffset = 160;
  79.         break;
  80.       case 3:
  81.         xOffset = 200;
  82.         yOffset = 160;
  83.         break;
  84.     default:
  85.       xOffset = 0;
  86.       yOffset = 0;
  87.     }
  88.     // draw the sampled pixels as verticle bars
  89.     for (int s=0; s<10; s++) {
  90.       noStroke();
  91.       fill(pixelColors[s]);
  92.       rect(xOffset+(s*19), yOffset, xOffset+((s+1)*19), yOffset+160);
  93.     }
  94.   }
  95. }
  96.  
  97. void drawImages() {
  98.   image(myImg[0], 400, 0);
  99.   image(myImg[1], 720, 0);
  100.   image(myImg[2], 400,160);
  101.   image(myImg[3], 720,160);
  102. }
  103.  
  104. void drawScanLines() {
  105.   // draw circles over where the "scanner" is currently reading pixel values
  106.   for (int i=0; i<10; i++) {
  107.     stroke(255, 0, 0);
  108.     noFill();
  109.     ellipse(400 + (i+1)*myImg[0].width/11, scanLine[0], 5, 5);
  110.     ellipse(720 + (i+1)*myImg[1].width/11, scanLine[1], 5, 5);
  111.     ellipse(400 + (i+1)*myImg[2].width/11, 160 + scanLine[2], 5, 5);
  112.     ellipse(720 + (i+1)*myImg[3].width/11, 160 + scanLine[3], 5, 5);
  113.   }
  114. }
  115.  
  116. void moveScanLines() {
  117.   for (int i = 0; i < 4; i++) {
  118.     // move the scanlines at different rates
  119.     if (frameCount%(i+2) == 0) {
  120.       scanLine[i] ++;
  121.     }
  122.     // reset scanlines if past the image height
  123.     if (scanLine[i] > myImg[i].height) {
  124.       scanLine[i] = 0;
  125.     }
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement