Reading and writing images (again !)

Frankly, I have difficulties to organize and structure code. Thus this post. Looking at my posts on kaleidoscopes I realized that I need code to read images in addition to writing. Then I first wrote a class InputBuffer similarly to the class OutputBuffer I already posted. But this duplicated much of the code related to the transformation of coordinates. I think that the class OutputBuffer is not well made because it has code to write an image and other code to define its coordinates. I would better create code to read and write images and separate code to define their coordinates.

Here I am discussing the new code for writing and reading images. You will find it in the next post “readWriteImages – the code”. With

readImage();

you start reading image. This you will use usually in the setup() routine. In the draw routine you can then use

thisImage=getImage();

where “thisImage” has to be declared as a PImage. If “thisImage” is not equal to null, then it will contain the input image and you can manipulate the image. To save an image you use

write(thisImage);

but you could too save a PGraphics with

write(graphics);

and the screen image you can save with

write();

Note that while the computer is writing an image the boolean variable “writing” equals true. This tells the program that it should not modify the image.
This sample program shows how to use the code:

PImage thisImage;
PGraphics graphics;


void setup(){
  size(600,600);
  graphics=createGraphics(400,600);
  readImage();
}

void draw(){
  thisImage=getImage();
  if(thisImage!=null){
    noLoop();
    graphics.beginDraw();
    graphics.background(color(0,255,0));
    graphics.ellipse(300,400,200,200);   
    graphics.endDraw();
    image(graphics,10,10);    
    image(thisImage,0,0);
  }
}

void keyPressed(){
//  write();
//  write(thisImage);
  write(graphics);
}
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