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_
   Topics & Contributions
   Information Visualization
(Moderators: forkinsocket, REAS)
   translating image into points/rects
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: translating image into points/rects  (Read 1699 times)
_C


translating image into points/rects
« on: Oct 18th, 2004, 1:18pm »

wrote a little code to get an imported image  
with transparency build by some p5 forms.  
 
useful eg if you want to map something onto
a mercator view of the earth ...
 
Code:

 
// filter for building images through grafic (p5) forms
// christian schaefer cser@gmx.de 10/2004
 
Pos positions;
Vector points;
BImage img;
int pixel[][];
int pixelLength[][];
int res=10; // resolution  
float scale=1; // scale
float offsetX, offsetY; // sets image to middle of screen when mouse is there
 
 
void setup() {
  size(800, 500);  // when exporting the applet, its better to have ints for the size of it
  background(99);
  img = loadImage("welt_gray_gross.gif"); // this needs to be an image with transparent background
 
  pixel = new int[img.width][img.height];
  for(int i=0; i<img.height; i++) {
    for(int j=0; j<img.width; j++) {
 pixel[j][i] = img.pixels[i*img.width+j];
    }
  }
 
  points = new Vector();  
  getPoints();  
 
}
 
void loop() {
  clear();
  drawPoints();
   
}
 
void getPoints() {
  for(int i=0; i<img.width; i+=res) {
    for(int j=0; j<img.height; j+=res) {
 if(alpha(pixel[i][j])>200) {
   positions = new Pos();
   positions.posx = i-img.width/2; // since the imagemode is to the upper left corner i do this here
   positions.posy = j-img.height/2;
   points.add(positions);  
 
 }
    }
  }
 
}
 
void drawPoints() {
  offset();
  translate(offsetX, offsetY, 0);
  scale(scale);
  for(Iterator i=points.iterator(); i.hasNext();) { // going throught the vector and draw the points stored in it
    Pos temp = (Pos)i.next();
    ellipse(temp.posx, temp.posy, res, res);
  }
}
 
class Pos {  // very simple class to store two points in  
  int posx;
  int posy;
 
}
 
void keyPressed() {
 switch(key) {
  case RIGHT: scale+=0.2; break;
  case LEFT : scale-=0.2; break;
 
 }
}
 
void offset() {
 offsetX+=((mouseX)*0.08-offsetX*0.08)*5;
 offsetY+=((mouseY)*0.08-offsetY*0.08)*5;  
 
}
 
 
Pages: 1 

« Previous topic | Next topic »