class OutputBuffer – the code

//  output with variable magnification of the screen == increased resolution
//  magnification=ratio of output pixels to screen pixels
//
// uses unitLength==ratio of number of screen pixels to one unit of length
// origin of coordinates lies at center of screen if offsetX=offsetY=0;
//  positive offsetX moves origin to the right
//  positive offsetY moves origin to the top 
//              (both in terms of unitlength, independent of magnification)

class OutputBuffer{
  float magnification;                      // relative scale to screen
  PGraphics thisImage;
  int thisWidth, thisHeight;
  float unitLength,offsetX,offsetY;
  float thisScale;
  float centerX,centerY;
 
  OutputBuffer(float mp){   // call this to create a buffer with given magnification of screen
    magnification(mp);
    unitLength=width/2;
    offsetX=0;
    offsetY=0;
  }
  
  void magnification(float mp){  //  this sets the magnification, can be changed later
    magnification=mp;
    thisWidth=int(width*magnification);
    thisHeight=int(height*magnification);
    thisImage=createGraphics(thisWidth,thisHeight);
  }
  
  void setUnitLength(float up){
    unitLength=up;
  }
  
  void setOffset(float ox,float oy){
    offsetX=ox;
    offsetY=oy;
  }

  //--------------------------------------------------------------------------------------
  //   begin and end drawing or using pixels, show it
  //  set up transformations and scales
  
  void beginImage(){
    centerX=thisWidth/2+offsetX*unitLength*magnification;   //index of pixel at x=0
    centerY=thisHeight/2-offsetY*unitLength*magnification;
    thisImage.beginDraw();
    thisImage.translate(centerX,centerY);
    thisScale=1./(unitLength*magnification);
    thisImage.scale(unitLength*magnification,-unitLength*magnification);
    trueStrokeWeight(1);
  }
  
  //  to get correct thickness of lines independent of resolution
  void trueStrokeWeight(float weight){
     thisImage.strokeWeight(weight/unitLength);
  } 
     
  void endImage(){
    thisImage.endDraw();
  }
  
  void beginPixels(){
    beginImage();
    thisImage.loadPixels();
  }
  
  void endPixels(){
    thisImage.updatePixels();
    endImage();
  }
  
  void show(){
    image(thisImage,0,0,width,height);  //  show without distortion
  }
  //
  //---------------------------------------------------------------------------------   
  //  calculate coordinates from indizes to pixels
    
  Vector getVector(float i,float j){
    return new Vector((i-centerX)*thisScale,(j-centerY)*thisScale);
  }
        
  //-----------------------------------------------------------------------------------------
  //  save the image
  //  this is for processing2
  // use: "saveImage();" to save the image

  void saveImage(){
    startOutput(this);
  }

  void saveAt(String filePath){
    thisImage.save(filePath);
  }
}
//------------------------------------------------------------------------------------
//  for saving an image===outputImage
void startOutput(OutputBuffer buffer){
    activeOutputBuffer=buffer;                     // declare in main tab: ImageBuffer outputBuffer
    selectOutput(" where do you want to save the image ?","saveImageCallback"); 
}

void saveImageCallback(File selection) {
  if (selection!=null) {                            
    String filePath=selection.getAbsolutePath();
    String[] filePathPieces=split(filePath,".");          
    if(filePathPieces.length==1) filePath=filePathPieces[0]+".png";
    println("output filepath is: "+filePath);
    activeOutputBuffer.saveAt(filePath);
  }
}


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