User Tools

Site Tools


cs276:scaleup

This is an old revision of the document!


/*
 *  ScaleUp
 *
 *  Scale an image up by a factor of two.
 * 
 *  Created 2 Oct 2007 by spc
 */

PImage pi, canvas;

void setup() {
  pi = loadImage("rose.jpg");
  println("width: " + pi.width +"height:" + pi.height);
  canvas = loadImage("7inX95in.jpg");
  size(canvas.width, canvas.height);
  noLoop();
}

void draw() {
  copy(pi, canvas, 0, pi.height*2);
  scaleUpImage(pi, canvas);
  image(canvas, 0, 0);
}

// copy(source, dest, xoffset, yoffset)
void copy (PImage src, PImage dest, int xoff, int yoff) {
  src.loadPixels();
  dest.loadPixels();
  for (int row = 0, drow = yoff; row < src.height; drow++, row++) {
    for (int col = 0, dcol = xoff; col < src.width; dcol++, col++) {
      color c = src.get((int) col, (int) row);
      dest.set((int) dcol, (int) drow, c);
    }
  }
  dest.updatePixels();
}

// scaleUpImage(source, dest)
// This works by writing every pixel of the source twice
// into the array of the destination; again, be careful that 
// the sizes of the pixel arrays are compatible

void scaleUpImage(PImage source, PImage dest) {
  source.loadPixels();
  dest.loadPixels();
  for (float row = 0, drow = 0; row < source.height; drow++, row += 0.5) {
    for (float col = 0, dcol = 0; col < source.width; dcol++, col += 0.5) {
      color c = source.get((int) col, (int) row);
      dest.set((int) dcol, (int) drow, c);
    }
  }
  dest.updatePixels();
}
cs276/scaleup.1191527565.txt.gz · Last modified: 2007/10/04 14:52 by scarl