FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Loading image
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Loading image  (Read 412 times)
inadaze


Loading image
« on: Oct 30th, 2004, 7:01pm »

Hey,
I am trying to practice writing classes and then objects.  I created a class called Picture and then instantiated it but it doesn't load.  There is no error though so I don't know where to start looking.  Here's the code:
 
//Picture Container
class Picture  {
 
  //properties
  int Xpos;
  int Ypos;
  int PicLength;
  int PicWidth;
  String FileName;
  boolean blackAndWhite;
 
  //Constructor
  Picture(int Xpos,int Ypos,int PicLength,int PicWidth,String FileName){
    this.Xpos = Xpos;
    this.Ypos = Ypos;
    this.PicLength = PicLength;
    this.PicWidth = PicWidth;
    this.FileName = FileName;
 
  }
 
  //Methods
  void draw()  {
    BImage Palm;
    Palm = loadImage(this.FileName);
    image(Palm,this.Xpos,this.Ypos);
 
  }
}
//====================================================================== ==
void setup()  {
  background(0);
  size(500,500);
  ellipseMode(CENTER_DIAMETER);
  rectMode(CENTER_DIAMETER);
  imageMode(CENTER_DIAMETER);
}
 
void loop()  {
  Picture myPalmPicture = new Picture(250,250,500,500,"myPalm.jpg");
  myPalmPicture.draw();
 
}
 
The image is in the data folder.
Any help would be useful.
Thanks
Jay
 
fjen

WWW
Re: Loading image
« Reply #1 on: Oct 30th, 2004, 10:20pm »

hmm. works for me. which version of processing are you using? which system are you on?
 
you should load the image before using it in loop. file-access is pretty slow (compared to memory access), so it might be that the image can't be loaded fast enough (especially over the net), or the loop hangs until it's loaded.
i propose you create _one_ instance of Picture in setup(), have it load it's image once in it's constructor and then draw that image in Picture.draw().
another thing: try to keep the creation of instances (Picture and BImage in your case) out of the loop. For every instance you create there has a certain amount of memory to be allocated, the class has to be found, the constructor is called, ..., all that slows down the performance.
 
/F
 
inadaze


Re: Loading image
« Reply #2 on: Oct 30th, 2004, 11:15pm »

okay, I tried it with another image and it worked fine?
The image that I was originally trying to load said it was a jpg but when I opened it in photoshop it was a tiff.
 
Thanks and sorry about the stupid question...
Jay
 
katapulp


Re: Loading image
« Reply #3 on: Nov 22nd, 2004, 6:18pm »

on Oct 30th, 2004, 10:20pm, fjen wrote:
you should load the image before using it in loop. file-access is pretty slow (compared to memory access), so it might be that the image can't be loaded fast enough (especially over the net), or the loop hangs until it's loaded.
i propose you create _one_ instance of Picture in setup(), have it load it's image once in it's constructor and then draw that image in Picture.draw().
another thing: try to keep the creation of instances (Picture and BImage in your case) out of the loop. For every instance you create there has a certain amount of memory to be allocated, the class has to be found, the constructor is called, ..., all that slows down the performance.

 
I've the same problem, adding an image (1Ko gif :s) slow down all the sketch when it's online...  
 
I didn't understand very well your solution, do you have an exemple somewhere
 
Thx a lot...
Katapulp
 
fjen

WWW
Re: Loading image
« Reply #4 on: Nov 22nd, 2004, 9:42pm »

from what you wrote your problem seems to be somewhere else. can you post you code?
 
i was just trying to explain, that it is better not to create instances of your object inside the loop every time ... try creating them (when you use "new") in setup once and then reuse them in the loop ...  
 
not so good: Code:

void setup()
{
  size(100,100);
}
 
void loop()
{
  SomeObject myObject = new SomeObject();
  // new object _every_ cycle
  myObject.doSomething();
}

 
better: Code:

SomeObject myObject;
void setup()
{
  size(100,100);
  SomeObject myObject = new SomeObject();
}
void loop()
{
  myObject.doSomething();
}

 
( note this is not always the case. sometimes you want to create the object inside loop )
 
/F
« Last Edit: Nov 22nd, 2004, 9:44pm by fjen »  
katapulp


Re: Loading image
« Reply #5 on: Nov 23rd, 2004, 9:57pm »

Have a look here:
http://www.regarde.org/processing/lag/
(click on the screen to start, then, try to drag&drop the pixel boy)
 
On my computer, when i run the sketch online (local run works fine) it's too laggy :s
 
Edit: other problem > when i put few texts my cpu get busy (local) i don't understand why... maybe because they are in the loop... i don't know any other way to  declare bfont. After so many hours i'm still a big noob :/
« Last Edit: Nov 23rd, 2004, 10:11pm by katapulp »  
fry


WWW
Re: Loading image
« Reply #6 on: Nov 23rd, 2004, 11:28pm »

you're loading 30 copies of the image each time you try to draw to the screen. you just need to load that .gif once, for instance:
 
BImage image;
 
void setup() {
  // other setup stuff here...
 
  // at the end of setup()
  caca = loadImage("caca.gif");
}
 
and then in your class:
 
void invokCaca()
{
  image(caca, cacax, cacay);
}
 
since the .gif is just one file, you only need to load it once, in addition, you don't want to use loadImage() over and over again inside of loop, especially if it's not changing.  
 
processing applets in revision 69 and lower will use as  much cpu as they can, revisions 70 and later (not yet publicly available) fix this situation. it will work fine though, once you move things around.
 
katapulp


Re: Loading image
« Reply #7 on: Nov 24th, 2004, 10:05am »

Understood. Thank you!
 
Pages: 1 

« Previous topic | Next topic »