Advertisement
macegr

Untitled

Dec 7th, 2012
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. /**
  2.  * Mirror Dither 512
  3.  *
  4.  * by Windell Oskay
  5.  *
  6.  * Based on Mirror 2 by Daniel Shiffman.
  7.  * and adapted to perform Atkinson Dithering.
  8.  *
  9.  * For more about Atkinson dithering, see: http://verlagmartinkoch.at/software/dither/index.html
  10.  *
  11.  * Sized at 512 x 342, with rounded corners, like an original Macintosh screen.
  12.  */
  13.  
  14. import processing.video.*;
  15.  
  16. // Number of columns and rows in our system
  17. int cols = 512;
  18. int rows = 342;
  19.  
  20. int rawVideoRows = round(cols*.75);
  21.  
  22.  
  23. int borderWidth = 45;
  24.  
  25. // Variable for capture device
  26. Capture video;
  27.  
  28. int mainwidth  = cols + 2 * borderWidth;
  29. int mainheight = rows + 2 * borderWidth;
  30.  
  31. int[] GrayArray;
  32. int GrayArrayLength;
  33.  
  34. boolean vcaptured = false;
  35.  
  36. void setup() {
  37.  
  38.   frameRate(30);
  39.  
  40.   //size(mainwidth, mainheight, P2D);  // Faster
  41.   size(mainwidth, mainheight, JAVA2D);  // More accurate, in general
  42.  
  43.  
  44.   colorMode(RGB);
  45.  
  46.   // Uses the default video input, see the reference if this causes an error
  47.   video = new Capture(this, cols, rawVideoRows);
  48.  
  49.   video.start();
  50.   noSmooth();
  51.   background(0);
  52. }
  53.  
  54.  
  55. void draw() {
  56.  
  57.   float brightTot;
  58.   int pixelCt;
  59.   color c2;
  60.   int idx = 0;
  61.  
  62.   if (vcaptured) {
  63.     //video.read();
  64.     vcaptured = false;
  65.     video.loadPixels();
  66.  
  67.  
  68.  
  69.     GrayArrayLength = cols * rawVideoRows;
  70.     int[] GrayArray = new int[GrayArrayLength];
  71.  
  72.     for (int n = 0; n < GrayArrayLength; n++)
  73.     {
  74.       GrayArray[n] = 0;
  75.     }
  76.  
  77.     // Black background:
  78.     background(0);
  79.  
  80.     // White rectangle, rounded corners:
  81.     fill(255);
  82.     noStroke();
  83.     rect(borderWidth, borderWidth, cols, rows, 7);   // Last digit is rounded corners
  84.  
  85.     noFill();
  86.     stroke(0);
  87.     strokeWeight(1);
  88.  
  89.  
  90.     loadPixels();
  91.  
  92.     int vOffset = floor ((rawVideoRows - rows) / 2);
  93.     int lastRow = (rawVideoRows - vOffset);
  94.     int yBorderTot = borderWidth - vOffset;
  95.  
  96.     // Begin loop for columns
  97.     for (int i = 0; i < cols;i++) {
  98.       // Begin loop for rows
  99.       for (int j = vOffset; j < lastRow; j++) {
  100.  
  101.  
  102.         // Where are we, pixel-wise?
  103.         int x = i;
  104.         int y = j;
  105.  
  106.         int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
  107.  
  108.         pixelCt = 0;
  109.         brightTot = 0;
  110.  
  111.         float brightTemp;
  112.  
  113.         c2 = video.pixels[loc];
  114.         brightTemp = brightness(c2);
  115.  
  116.         // Brightness correction curve:
  117.         brightTemp =  sqrt(255) * sqrt (brightTemp);
  118.  
  119.         if (brightTemp > 255)
  120.           brightTemp = 255;
  121.  
  122.         if (brightTemp < 0)
  123.           brightTemp = 0;
  124.  
  125.         int darkness = 255 - floor(brightTemp);
  126.  
  127.         idx = (j)*cols + (i);        
  128.  
  129.         darkness += GrayArray[idx];
  130.  
  131.         if ( darkness >= 128) {
  132.  
  133.           //          rect(x + borderWidth, y + borderWidth - vOffset, 1, 1);  // If using P2D
  134.           pixels[x+y*mainwidth+borderWidth+mainwidth*(borderWidth-vOffset)] = color(0,0,0);  // For use with JAVA2D only
  135.  
  136.           darkness -= 128;
  137.         }
  138.  
  139.         int darkn8 = round(darkness / 8);
  140.  
  141.         // Atkinson dithering algorithm:  http://verlagmartinkoch.at/software/dither/index.html          
  142.         // Distribute error as follows:
  143.         //     [ ]  1/8  1/8
  144.         //1/8  1/8  1/8
  145.         //     1/8
  146.  
  147.           if ((idx + 1) < GrayArrayLength)
  148.           GrayArray[idx + 1] += darkn8;
  149.         if ((idx + 2) < GrayArrayLength)
  150.           GrayArray[idx + 2] += darkn8;
  151.         if ((idx + cols - 1) < GrayArrayLength)
  152.           GrayArray[idx + cols - 1] += darkn8;
  153.         if ((idx + cols) < GrayArrayLength)
  154.           GrayArray[idx + cols] += darkn8;
  155.         if ((idx + cols + 1) < GrayArrayLength)
  156.           GrayArray[idx + cols + 1 ] += darkn8;
  157.         if ((idx + 2 * cols) < GrayArrayLength)
  158.           GrayArray[idx + 2 * cols] += darkn8;
  159.       }
  160.     }
  161.    
  162.     updatePixels();
  163.    
  164.   }
  165.  
  166. }
  167.  
  168.  
  169. void captureEvent(Capture c) {
  170.   video.read();
  171.   vcaptured = true;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement