We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › PImage syntax/location
Page Index Toggle Pages: 1
PImage syntax/location (Read 598 times)
PImage syntax/location
Nov 10th, 2006, 11:44pm
 
I am trying to set up a PImage variable "img" that can be redefined at different points in the code.  I have a few questions, because I've been getting errors all along the way:

Where should the PImage be *declared*?  
ex:
PImage img;

Where can it be *defined*?
ex:
img = loadImage("a.jpg");


If img is declared, shouldn't it be able to be redefined with another loadImage() somewhere else in the code?  Does anyone know the rules on this?

For example, why does this work:

PImage b;
b = loadImage("a.jpg");
image(b, 0, 0);

But this doesn't:

void setup() {
 size(256,192);
}

PImage b;
b = loadImage("a.jpg");

void draw(){
image(b,0,0);
}

I am working with several images in a program and I would prefer to use only one PImage that can be redefined (if possible), rather than several individual PImages.

Any help is much appreciated!
Re: PImage syntax/location
Reply #1 - Nov 11th, 2006, 12:57am
 
try this:
Code:

PImage b;

void setup() {
size(256,192);
}

void draw(){
b = loadImage("a.jpg");
image(b,0,0);
}

You could also loadImage in the setup()
Re: PImage syntax/location
Reply #2 - Nov 11th, 2006, 11:41am
 
If you only have one image to load it is better to do this in the setup() method. Otherwise is the image is loading in every draw call and that will lower the speed of your app.
Re: PImage syntax/location
Reply #3 - Nov 11th, 2006, 7:36pm
 
Thanks for the replies. I tried those options and they both work as expected. I was trying avoid placing the loadImage in void draw to avoid re-drawing the image each frame. But my problem is that I am loading images that are being rewritten/abstracted as a representation of binary code (ones and zeros being imported from a binary generator in pd via OSC). The image appears digit by digit, line by line as the binary numbers down the screen. This part of the code works with single images but I need to call up several images in sequence for this project. When the image has been completely drawn on the screen, a new image needs to be loaded and drawn over the previous image line by line. To test it I just set it up with two images that will switch back and forth.

I tried to do the following outside of void draw :

void changePic() {
 if(currentpic==0) {currentpic = 1; img = loadImage("b.jpg");}
 if(currentpic==1) {currentpic = 0; img = loadImage("a.jpg");}
}
Re: PImage syntax/location
Reply #4 - Nov 11th, 2006, 11:41pm
 
Here is the code:

import oscP5.*;
import netP5.*;

/**
* oscP5sendreceive by andreas schlegel
* example shows how to send and receive osc messages.
*/
//import oscP5.*;

int posx = 0;
int posy = 0;
int numdivx = 256;
int numdivy = 192;
boolean drawn = false;

PImage img;


int currentpic = 0;
 
OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {

 PFont fontA = loadFont("Verdana-6.vlw");
 textFont(fontA, 6);
 size(1024,768);
 //frameRate(800);
 background(255);
 noStroke();
 fill(0);
 /* start oscP5, listening for incoming messages at port 12000 */
 oscP5 = new OscP5(this,12000);
 
 /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
  * an ip address and a port number. myRemoteLocation is used as parameter in
  * oscP5.send() when sending osc packets to another computer, device,
  * application. usage see below. for testing purposes the listening port
  * and the port of the remote location address are the same, hence you will
  * send messages back to this sketch.
  */
 myRemoteLocation = new NetAddress("127.0.0.1",12000);
}





// The font must be located in the sketch's
// "data" directory to load successfully
//fonty = loadFont("Verdana-12.vlw");

void draw() {
 
  }


void changePic() {
 if(currentpic==0) {currentpic = 1; img = loadImage("b.jpg");}
 if(currentpic==1) {currentpic = 0; img = loadImage("a.jpg");}
}


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
 fill(255,5);
 rect(0,0,width,height);

 
 
 
 int msgval = theOscMessage.get(0).intValue();
 if(msgval==1){
 fill(255);
 rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
 fill(img.get(posx,posy));
 pushMatrix();
 translate(0,height/numdivy);
 text("1",width/numdivx*posx,height/numdivy*posy);
 popMatrix();
 } else{
   fill(255);
   rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
   fill(img.get(posx,posy));
   pushMatrix();
   translate(0, height/numdivy);
   text("0",width/numdivx*posx,height/numdivy*posy);
   popMatrix();
 }
 
 posx+=1;
 if(posx==numdivx){
   if(posy==numdivy-1) {
     drawn = true;
     changePic();
   }
 }
 if(posx>numdivx) {posx = 0; posy+=1;}
 if(posy>numdivy-1) {posy = 0;}
 /* print the address pattern and the typetag of the received OscMessage */
 //print("### received an osc message.");
 //print(" addrpattern: "+theOscMessage.addrpattern());
 //println(" typetag: "+theOscMessage.typetag());
}


Re: PImage syntax/location
Reply #5 - Nov 15th, 2006, 12:19am
 
Kevin McCoy and I got it to work. We brought the binary generator into processing, removed the data from pd coming via OSC all together and wrote a new "void imageNumber" for each image we need to call. It is easy to run through a sequence. If any one knows why a PImage variable cant be re-designated as we had tried to do in our first try please let me know.

thanks, working code follows (for some reason I cant to figure out how to post code as "code" with my browser so I have to paste it as it appears below):


int posx = 0;
int posy = 0;
int numdivx = 256;
int numdivy = 192;

int imageLink = 0;

int r;
String binseq;
int refPoint = 0;
boolean parsed = true;
char message;

PImage a;
PImage b;

//int currentpic = 0;
 

void setup() {
 //define PImages
a = loadImage("a.jpg");
b = loadImage("b.jpg");
//
 PFont fontA = loadFont("Verdana-6.vlw");
 textFont(fontA, 6);
 size(1024,768);
 frameRate(800);
 background(255);
 noStroke();
 fill(0);

}





// The font must be located in the sketch's
// "data" directory to load successfully
//fonty = loadFont("Verdana-12.vlw");

void draw() {
 
  if (parsed==false) {
    message = binseq.charAt(refPoint);
    refPoint+=1;
    //println(refPoint);
    if(refPoint>7) {refPoint = 0; parsed = true;}
    imageChooser();
   
  }
  if (parsed==true) {
     r = int(random(128, 256));
     binseq = binary(r);
     //println(binseq);
     parsed = false;
  }
}


void imageChooser() {
 if(imageLink==0) {imageOne();}
 if(imageLink==1) {imageTwo();}
}


void imageOne(){

 //fill(255,5);
 //rect(0,0,width,height);
 int msgval = message;
 if(msgval=='1'){
 fill(255);
 rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
 fill(a.get(posx,posy),30);
 rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
 fill(a.get(posx,posy));
 pushMatrix();
 translate(0,height/numdivy);
 text("1",width/numdivx*posx,height/numdivy*posy);
 popMatrix();
 } else{
   fill(255);
   rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
   fill(a.get(posx,posy),30);
 rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
   fill(a.get(posx,posy));
   pushMatrix();
   translate(0, height/numdivy);
   text("0",width/numdivx*posx,height/numdivy*posy);
   popMatrix();
 }
 
 posx+=1;
 if(posx==numdivx){
   if(posy==numdivy-1) {
     imageLink = 1;
   }
 }
 if(posx>numdivx) {posx = 0; posy+=1;}
 if(posy>numdivy-1) {posy = 0;}
}

void imageTwo(){
 //fill(255,5);
 //rect(0,0,width,height);
 int msgval = message;
 if(msgval=='1'){
 fill(255);
 rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
 fill(a.get(posx,posy),30);
 rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
 fill(b.get(posx,posy));
 pushMatrix();
 translate(0,height/numdivy);
 text("1",width/numdivx*posx,height/numdivy*posy);
 popMatrix();
 } else{
   fill(255);
   rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
   fill(a.get(posx,posy),30);
 rect(width/numdivx*posx,height/numdivy*posy,width/numdivx,height/numdivy);
   fill(b.get(posx,posy));
   pushMatrix();
   translate(0, height/numdivy);
   text("0",width/numdivx*posx,height/numdivy*posy);
   popMatrix();
 }
 
 posx+=1;
 if(posx==numdivx){
   if(posy==numdivy-1) {
     imageLink = 0;
   }
 }
 if(posx>numdivx) {posx = 0; posy+=1;}
 if(posy>numdivy-1) {posy = 0;}
}


Page Index Toggle Pages: 1