Advertisement
JackWitherell

gameboy camera code

Jan 16th, 2018
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. import processing.video.*;
  2.  
  3. Capture cam;
  4. PImage contrastLeft, contrastRight, barHorizontal, contrastWord, contrastControl, imgstr;
  5. PImage brightnessUp, brightnessDown, barVertical, brightnessWord, brightnessControl;
  6. PImage left;
  7. PImage numbers [];
  8. boolean newim;
  9. PVector gbscreen=new PVector(160,144);
  10. int scale=3;
  11. int picsLeft=30;
  12. int digMax=3;
  13. int brightness=6;
  14. int contrast=7;
  15.  
  16. int error[]=new int[120*112];
  17.  
  18. int dither(float amount, int values, int dither, int output, int index){
  19.   if (dither!=0) dither++;
  20.   int tiers=(output+(dither*(output-1)));
  21.   if(dither==0){
  22.     tiers=output;
  23.   }
  24.   float percentage=amount/values;//128/256=.50
  25.   float odf=1.0f/tiers;//four colors would make this .25
  26.   int sector=PApplet.parseInt(percentage/odf);
  27.   if(sector%(dither+1)==0){
  28.     return PApplet.parseInt(map(PApplet.parseFloat(sector)/(tiers-1),0,1,0,255));
  29.   }
  30.   else{
  31.     int higher;
  32.     int lower;
  33.     lower=higher=sector;
  34.    
  35.     while(lower%(dither+1)!=0){
  36.       lower--;
  37.     }
  38.     while(higher%(dither+1)!=0){
  39.       higher++;
  40.     }
  41.    
  42.     int first=higher-sector;
  43.     int second=sector-lower;
  44.     index=index+((index/width)*(tiers/2));
  45.     if(first>second){
  46.       if(index%first==0){
  47.         return PApplet.parseInt(map(PApplet.parseFloat(higher)/(tiers-1),0,1,0,255));
  48.       }
  49.       return PApplet.parseInt(map(PApplet.parseFloat(lower)/(tiers-1),0,1,0,255));
  50.     }
  51.     else{
  52.       if(index%second==0){
  53.         return PApplet.parseInt(map(PApplet.parseFloat(lower)/(tiers-1),0,1,0,255));
  54.       }
  55.       return PApplet.parseInt(map(PApplet.parseFloat(higher)/(tiers-1),0,1,0,255));
  56.     }
  57.   }
  58. }
  59.  
  60. int xy(int x, int y){
  61.   return x+(width*y);
  62. }
  63.  
  64. void greyScale(int _x, int _y, int _width, int _height){
  65.   for(int x=0; x<_width; x++){
  66.     for(int y=0; y<_height; y++){
  67.       color temp=pixels[((y+_y)*width)+x+_x];
  68.       float aver=((red(temp)+green(temp)+blue(temp))/3);
  69.       int val=(int(map(aver,0,255,0,4))*85);
  70.       int err=int(aver)-val;
  71.       color tempdither=pixels[xy(_x+x+1,_y+y)];
  72.       int newerr=int(err*(7/16.0));
  73.       if(contrast==6){
  74.         pixels[xy(_x+x+1,_y+y)]=  color(red(tempdither)+newerr,green(tempdither)+newerr,blue(tempdither)+newerr);
  75.         tempdither=pixels[xy(_x+x-1,_y+y+1)];
  76.         newerr=int(err*(3/16.0));
  77.         pixels[xy(_x+x-1,_y+y+1)]=color(red(tempdither)+newerr,green(tempdither)+newerr,blue(tempdither)+newerr);
  78.         tempdither=pixels[xy(_x+x,_y+y+1)];
  79.         newerr=int(err*(5/16.0));
  80.         pixels[xy(_x+x,_y+y+1)]=  color(red(tempdither)+newerr,green(tempdither)+newerr,blue(tempdither)+newerr);
  81.         tempdither=pixels[xy(_x+x+1,_y+y+1)];
  82.         newerr=int(err*(1/16.0));
  83.         pixels[xy(_x+x+1,_y+y+1)]=color(red(tempdither)+newerr,green(tempdither)+newerr,blue(tempdither)+newerr);
  84.       }
  85.       temp=color(val);
  86.       pixels[(xy(_x+x,_y+y))]=temp;
  87.      
  88.     }
  89.   }
  90. }
  91.  
  92. void scale(int scale){
  93.   for(int x=width-1; x>-1; x--){
  94.     for(int y=height-1; y>-1; y--){
  95.       pixels[x+(y*(width))]=pixels[int(x/scale)+int(int(y/scale)*width)];
  96.     }
  97.   }
  98. }
  99.  
  100. void keyPressed(){
  101.   if (keyCode==UP){
  102.     if (brightness!=0) brightness--;
  103.   }
  104.   else if(keyCode==DOWN){
  105.     if (brightness!=15) brightness++;
  106.   }
  107.   if(keyCode==LEFT){
  108.     if (contrast!=0) contrast--;
  109.   }
  110.   else if(keyCode==RIGHT){
  111.     if (contrast!=15) contrast++;
  112.   }
  113. }
  114.  
  115. void settings(){
  116.   int a=int(gbscreen.x*scale);
  117.   int b=int(gbscreen.y*scale);
  118.   size(a, b);
  119. }
  120.  
  121. void setup() {
  122.   numbers=new PImage[10];
  123.   for(int i=0;i<120*112;i++){
  124.     error[i]=0;
  125.   }
  126.   PImage num=loadImage("numbers.bmp");
  127.   for(int i=0; i<10; i++){
  128.     numbers[i]=num.get(i*6,0,6,12);
  129.   }
  130.  
  131.   PImage contrastbar=loadImage("contrast.bmp");
  132.   contrastLeft=contrastbar.get(0,0,15,11);
  133.   contrastRight=contrastbar.get(52,0,15,11);
  134.   barHorizontal=contrastbar.get(15,8,1,3);
  135.   contrastWord=contrastbar.get(18,7,32,6);
  136.   contrastControl=contrastbar.get(15,0,4,6);
  137.  
  138.   PImage brightnessbar=loadImage("brightness.bmp");
  139.   brightnessUp=brightnessbar.get(0,0,11,15);
  140.   brightnessDown=brightnessbar.get(0,57,11,15);
  141.   barVertical=brightnessbar.get(8,15,3,1);
  142.   brightnessWord=brightnessbar.get(6,17,6,38);
  143.   brightnessControl=brightnessbar.get(0,15,6,4);
  144.  
  145.   left=loadImage("left.bmp");
  146.  
  147.   String[] cameras = Capture.list();
  148.  
  149.   if (cameras.length == 0) {
  150.     println("There are no cameras available for capture.");
  151.     exit();
  152.   } else {
  153.     println("Available cameras:");
  154.     for (int i = 0; i < cameras.length; i++) {
  155.       println(cameras[i]);
  156.     }
  157.     imgstr=new PImage();
  158.     cam = new Capture(this, cameras[1]);
  159.     cam.start();    
  160.   }      
  161. }
  162.  
  163. void draw() {
  164.   background(0);
  165.   if (cam.available() == true) {
  166.     cam.read();
  167.     imgstr=cam;
  168.     newim=true;
  169.   }
  170.  
  171.   if(newim){
  172.     if(((imgstr.width*7)/8)>imgstr.height){
  173.       int newWidth=int((float(imgstr.height)/7)*8);
  174.       int cutoff=(imgstr.width-newWidth)/2;
  175.       imgstr=imgstr.get(cutoff,0,newWidth,imgstr.height);
  176.     }
  177.     else{
  178.       int newHeight=int((float(imgstr.width)/8)*7);
  179.       int cutoff=(imgstr.height-newHeight)/2;
  180.       imgstr=imgstr.get(0,cutoff,imgstr.width,newHeight);
  181.     }
  182.   }
  183.   image(imgstr, 16, 16, 128, 112);
  184.  
  185.  
  186.   image(contrastLeft,16,131);
  187.   image(contrastRight,129,131);
  188.   image(barHorizontal,31,139,30,3);
  189.   image(barHorizontal,96,139,33,3);
  190.   image(contrastWord,63,138);
  191.   image(contrastControl,31+(contrast*6.3),131);
  192.  
  193.   image(brightnessUp,147,16);
  194.   image(brightnessDown,147,113);
  195.   image(barVertical,155,31,3,17);
  196.   image(barVertical,155,88,3,25);
  197.   image(brightnessWord,153,49);
  198.   image(brightnessControl,147,31+(brightness*5.2));
  199.  
  200.   for(int i=0;i<digMax;i++){
  201.     image(numbers[int((picsLeft%(int(pow(10,i+1))))/pow(10,i))],113-(i*8),3);
  202.   }
  203.   image(left,124,5);
  204.  
  205.   loadPixels();
  206.   greyScale(16,16,128,112);
  207.   scale(scale);
  208.   updatePixels();
  209.  
  210.   newim=false;
  211.   // The following does the same, and is faster when just drawing the image
  212.   // without any additional resizing, transformations, or tint.
  213.   //set(0, 0, cam);
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement