readWriteImages – the code

// reading and writing images, graphics and the screen for processing2
// I don't use objects because the callback-routines cannot be object-methods

PImage _Image;                            // beware of dublicate definitions!
PGraphics _Graphics;
boolean reading=false,writing=false;

//  read the PImage input
  
void readImage(){
  reading=true;
  selectInput("Please choose INPUT-image ", "readImageCallback"); 
}
  
void readImageCallback(File selection) { 
  if (selection==null) exit();
  String filePath=selection.getAbsolutePath();
  println(" loading image from "+filePath);
  _Image=loadImage(filePath);
  println(" input image finished");
  reading=false;
}

PImage getImage(){                          // returns null if image not yet read
  if (!reading) return _Image;
  PImage nothing=null;
  return nothing;
}
//-----------------------------------------------------------------------

String getFilePath(File selection){
  String filePath=selection.getAbsolutePath();
  String[] filePathPieces=split(filePath,".");          
  if(filePathPieces.length==1) filePath=filePathPieces[0]+".png";
  println("output filepath is: "+filePath);
  return filePath;
}

// write the PImage output

void write(PImage output){
  writing=true;
  _Image=output;
  selectOutput(" where do you want to save the image ?","writeImageCallback"); 
}

void writeImageCallback(File selection) {
  if (selection!=null) {                            
    String filePath=getFilePath(selection);
    _Image.save(filePath);
    println("output finished");
    writing=false;
  }
}

// write the PGraphics

void write(PGraphics output){
  writing=true;
  _Graphics=output;
  selectOutput(" where do you want to save the image ?","writeGraphicsCallback"); 
}

void writeGraphicsCallback(File selection) {
  if (selection!=null) {                            
    String filePath=getFilePath(selection);
    _Graphics.save(filePath);
    println("output finished");
    writing=false;
  }
}

// write the screen

void write(){
  writing=true;
  selectOutput(" where do you want to save the image ?","writeScreenCallback"); 
}

void writeScreenCallback(File selection) {
  if (selection!=null) {                            
    String filePath=getFilePath(selection);
    save(filePath);
    println("output finished");
    writing=false;
  }
}


This entry was posted in programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s