Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Image generating code hangs up

$
0
0

Hi all, i have the following code which seems to me alright:

import processing.pdf.*;
import processing.serial.*;


Serial myPort;  // Create object from Serial class

int layers=24;

int switches;

byte[] temp = new byte[4];


PImage[] source = new PImage[layers+1];      // Source image
PImage destination;  // Destination image

void setup() {
  size(732, 732);

  myPort = new Serial(this, "/dev/tty.usbmodem621", 9600);


for(int n=0;n<layers+1;n++){
  source[n] = loadImage("layer"+n+".jpg");
}
  // The destination image is created as a blank image the same size as the source.
  destination = createImage(source[0].width, source[0].height, RGB);


  set();
}

void draw() {
  background(255);

  if ( myPort.available() > 0) {  // If data is available,

    switches = Integer.parseInt(myPort.readString());
    println(switches);
    if(switches==-9294){
    beginRecord(PDF,"output.pdf");
    destination=source[24];
    image(destination,0,0);
    set();
    print();
    }else{
        loop();
    beginRecord(PDF,"output.pdf");
    display();
    textSize(26);
    fill(0);
    text(switches,width-150,height-30);
    print();
    }
  }
  /*switches=55;*/
  display();

}

void set(){

destination.loadPixels();
  for (int x = 0; x < destination.width; x++) {
    for (int y = 0; y < destination.height; y++ ) {
      int loc = x + y*source[0].width;
      // Test the brightness against the threshold
        destination.pixels[loc]  = color(255);  // White
    }
  }

}

void createimg(int n){

float threshold = 127;

  // We are going to look at both image's pixels
  source[n].loadPixels();
  destination.loadPixels();

  for (int x = 0; x < source[n].width; x++) {
    for (int y = 0; y < source[n].height; y++ ) {
      int loc = x + y*source[n].width;
      // Test the brightness against the threshold
      if (brightness(source[n].pixels[loc]) < threshold) {
        destination.pixels[loc]  = color(0);
      }
    }
  }

  // We changed the pixels in destination
  destination.updatePixels();

}

void display(){

  // Display the destination
  image(destination,0,0);
  set();
}

void loop(){

  for(int i = 0; i < layers; i++)
  {
    if(isBitOn(i, switches) && (switches != 0))
    {
    createimg(i);
    }
  }
}

boolean isBitOn(int bit, long value)
{
  long mask = 1;
  mask = mask << bit;
  if((value & mask)>0){
    return true;
  }else{
    return false;
  }
}

void print(){
endRecord();
saveFrame("img/test/bild-######.png");
open("printpdf.app");
}

It works for a couple of time, then hangs up. Does anybody find a problem? Thanks for the help!


Viewing all articles
Browse latest Browse all 2896

Trending Articles