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
   Programs
(Moderators: fry, REAS)
   scale bitmap and copy back to screen
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: scale bitmap and copy back to screen  (Read 3319 times)
thomastraum


scale bitmap and copy back to screen
« on: Apr 3rd, 2005, 3:46pm »

Hi everybody,
 
I'm trying to scale a bitmap smaller or bigger and copy it back to screen. I came up with these two methods for scaling an image bigger or smaller.  
I'm quite new to processing and all the pixel code thats why I would like to ask you if thats the correct way and the fastest way to achieve what I want. I also wonder also if the scaling bigger and smaller couldnt be merged into one routine, or if there is any build in functions who is doing that anyway.
 
My aim is to connect the scalefactor to a sine or some sound input with values bigger and smaller than 1, so it would be helpful to merge the code in one shorter routine instead of always checking whether the value is <1>.
 
Also I was wondering in general how a itunes visualization scales their bitmap, they seem to get stretched more like wrapped over a sphere, ie they are the most stretched at the corners.
 
Any help much appreciated.
 
CODE:
 
BImage mySprite;
 
float mySprite_x, mySprite_y;
 
int WIDTH=320, HEIGHT= 240;
 
void setup() {
 
  size(WIDTH, HEIGHT);
  background(0);
  noStroke();
  fill(102);
 
  mySprite = loadImage("5.gif");
  
}
 
int cbuf=0;
 
void loop() {
  
  mySprite_x = mySprite_x + ( ( mouseX - mySprite_x ) * 0.1 );
  mySprite_y = mySprite_y + ( ( mouseY - mySprite_y ) * 0.1 );
  
  image(mySprite, mySprite_x, mySprite_y);
 
  /*
  // scale bigger  
  float scaleFactor = 1.1;
  BImage scaledImage = new BImage(int(WIDTH*scaleFactor), int( HEIGHT*scaleFactor) );
  BImage source = new BImage( WIDTH, HEIGHT );
  source.pixels = pixels;
  scaledImage.replicate(source,0,0,source.width-1,source.height-1,0,0,sca ledImage.width-1,scaledImage.height-1);
   int offset_x = ( scaledImage.width - source.width ) / 2;
  int offset_y = ( scaledImage.height - source.height ) / 2;
  for ( int i=0; i<HEIGHT;i++ ) {
    for ( int j=0; j<WIDTH;j++ ) {
      pixels[i*WIDTH + j] = scaledImage.pixels[( (offset_y + i) *scaledImage.width ) + j + offset_x ];
    }
  }
  
  */
  
  // scale smaller
  float scaleFactor = 0.9;
  BImage scaledImage = new BImage(int( WIDTH*scaleFactor ), int( HEIGHT*scaleFactor ) );
  BImage source = new BImage( WIDTH, HEIGHT );
  source.pixels = pixels;
  scaledImage.replicate(source,0,0,source.width-1,source.height-1,0,0,sca ledImage.width-1,scaledImage.height-1);
  int offset_x = ( source.width - scaledImage.width ) / 2;
  int offset_y = ( source.height - scaledImage.height ) / 2;
  for ( int i=0; i<scaledImage.height;i++ ) {
    for ( int j=0; j<scaledImage.width;j++ ) {
      pixels[( (offset_y + i) * WIDTH ) + j + offset_x] = scaledImage.pixels[i *scaledImage.width + j ];
    }
  }
  
}
 
« Last Edit: Apr 3rd, 2005, 4:59pm by thomastraum »  
eskimoblood

222550793222550793 WWW
Re: scale bitmap and copy back to screen
« Reply #1 on: Apr 4th, 2005, 12:11pm »

for spheric image transformation look at this:
http://incubator.quasimondo.com/processing/rubber_screen.php
 
thomastraum


Re: scale bitmap and copy back to screen
« Reply #2 on: Apr 4th, 2005, 10:04pm »

thanks eskimoblood, cool link.
 
I just post the scaling code again in a shorter version, because I really need to speed things up, its already running too slow. It must run in the end on 600px wide and Im sure there is some way for a performance boost. I'm just too much of a beginner and cant figure it out.
 
 
CODE:
 
BImage scaledImage = new BImage(int(WIDTH*scaleFactor), int( HEIGHT*scaleFactor) );
  source.pixels = pixels;
  scaledImage.replicate(source,0,0,source.width-1,source.height-1,0,0,scal edImage.width-1,scaledImage.height-1);
 
if ( scaleFactor >= 1 ) {  
    // scale bigger  
    int offset_x = ( scaledImage.width - source.width ) / 2;
    int offset_y = ( scaledImage.height - source.height ) / 2;
    for ( int i=0; i<HEIGHT;i++ ) {
 for ( int j=0; j<WIDTH;j++ ) {
   pixels[i*WIDTH + j] = scaledImage.pixels[( (offset_y + i) *scaledImage.width ) + j + offset_x ];
 }
    }
  } else {  
    // scale smaller
    int offset_x = ( source.width - scaledImage.width ) / 2;
    int offset_y = ( source.height - scaledImage.height ) / 2;
    for ( int i=0; i<scaledImage.height;i++ ) {
 for ( int j=0; j<scaledImage.width;j++ ) {
   pixels[( (offset_y + i) * WIDTH ) + j + offset_x] = scaledImage.pixels[i *scaledImage.width + j ];
 }
    }
  }
 
Mark Hill

WWW Email
Re: scale bitmap and copy back to screen
« Reply #3 on: Apr 5th, 2005, 9:20pm »

 Just in passing...The quickest way of scaling an image and dumping it to the screen is to:
 
  image(yourBImage,xpos, ypos, xsize, ysize);  
   
 
Of course yourBImage can be a BGraphics object with full drawing capabilities.
 
thomastraum


Re: scale bitmap and copy back to screen
« Reply #4 on: Apr 6th, 2005, 12:10am »

Yo thanks Mark,
 
but ....
I had weird results by just using :
 
image(img, x, y, width, height)
 
If I used imagereplicate before drawing it to stage it just worked fine. Now the only thing I dont understand is that after multiple copies of the screencontents back to the screen, white pixels seem to turn grey and finally black after a while.  
Anyone knows why this may happen?
 
I also did some simple FPS tests and actually, with my old code which uses the pixels[] I get around 9 fps with the Image() version just 4.  
 
So I guess I keep my old code for now.
 
Mark Hill

WWW Email
Re: scale bitmap and copy back to screen
« Reply #5 on: Apr 6th, 2005, 9:36am »

// Draw a textured quad to screen.  
 
   background(0);
   scale(random(0,1));
   beginShape(QUADS);  
   texture(gTexture);  
   vertex(0,   0,   0,    0, 0);  
   vertex(100, 0,   0,  200, 0);  
   vertex(100, 100, 0,  200, 200);  
   vertex(0,   100, 0,  0,   200);  
   endShape();  
 
check out vertex() in reference section for details on variables x,y,z,u and v.
 
I haven't done a speed test for this one, but I guess the translate and scale functions are not going to be as fast as dedicated code (could be wrong though).
 
« Last Edit: Apr 6th, 2005, 9:44am by Mark Hill »  
Pages: 1 

« Previous topic | Next topic »