Thursday, 8 January 2009

First posting

Hi All,

Happy New Year to you all and welcome to my first posting on the blog.

Still waiting for the book to turn up - does it really exist?

Thank goodness for safari.

What follows is the code that I will be using as the focus for my chunk.

Will start the explanation in the not too distant future - watch this space.


void setup(){
background(255,255,255);
RasterImageProcessor alHambra = new RasterImageProcessor();
println(alHambra.getImageDimensions());
size(9*alHambra.getImageWidth(), 12*alHambra.getImageHeight());
String [] refelections = {RasterImageProcessor.NOREFLECT, RasterImageProcessor.XREFLECT, RasterImageProcessor.YREFLECT, RasterImageProcessor.XYREFLECT};
String [] rotations = {RasterImageProcessor.ROTATE0, RasterImageProcessor.ROTATE270, RasterImageProcessor.ROTATE90, RasterImageProcessor.ROTATE180};
alHambra.showImages(rotations, alHambra.showImages(refelections, 0, 5, 5)+100, 5, 45); // tweak last 2 arguments to alter spacing between each image
}

class RasterImageProcessor{
// private instance properties
private PImage image;
// public class properties
static final String YREFLECT = "YREFLECT";
static final String XREFLECT= "XREFLECT";
static final String XYREFLECT = "XYREFLECT";
static final String NOREFLECT = "NOREFLECT";
static final String ROTATE0 = "ROTATE0";
static final String ROTATE90 = "ROTATE90";
static final String ROTATE180 = "ROTATE180";
static final String ROTATE270 = "ROTATE270";

// (1) constructors
public RasterImageProcessor(){ // default constructor generates a fixed image
// based on Chapter 6 example to include random colours and line lengths
// used by diagonal lines
final int imageWidth = (int) random(81,120);
final int imageHeight = (int) random(60,80);
float slope = float(imageHeight)/float(imageWidth);
image = createImage(imageWidth, imageHeight, RGB);
color c = color(random(235,250), random(235,250), random(235, 250));
for (int i=0; i < imageWidth; i++) // set image background to a random colour
for (int j=0; j < imageHeight; j++)
image.set(i, j, c);
c = color(random(50,150),random(50,150),random(50,150)); // set lines on image to a random colour //horizontal line
int len=(int)(imageWidth/random(1,3));
for (int i=0; i < len; i++){
image.set(i, imageHeight/2, c);
}
//vertical line
len=(int)(imageWidth/random(1,3));
for (int i=0; i < len; i++){
image.set(imageWidth/2, i, c);
}
//diagonal line (TL-BR)
len=(int)(imageWidth/random(1,4));
for (int i=0; i < len; i++){
image.set(int(i), int(i*slope), c);
}
//diagonal line (BL-TR)
len=(int)(imageWidth/random(1,4));
for (int i=0; i < len; i++){
image.set(int(i), int(imageHeight-i*slope), c);
}
}
// (2) getters and setters
public PImage getImage(){
return image;
}
// (3) application methods
public int getImageWidth(){
return image.width;
}
public int getImageHeight(){
return image.height;
}
public int [] getImageDimensions(){
int [] result = {
getImageWidth(),getImageHeight() };
return result;
}
private PImage newImage(String s){
if (s == XREFLECT)
return xReflect();
else if (s == YREFLECT)
return yReflect();
else if (s == XYREFLECT)
return xYReflect();
else if (s == ROTATE0)
return image;
else if (s == ROTATE90)
return rotate90();
else if (s == ROTATE180)
return rotate180();
else if (s == ROTATE270)
return rotate270();
else
return image;
}
private PImage yReflect(){// copy to newImage after reflection about the Y-axis
PImage newImage = new PImage(getImageWidth(), getImageHeight());
for (int i=0; i < getImageWidth(); i++)
for (int j=0; j < getImageHeight(); j++)
newImage.set(getImageWidth()-i-1, j, image.get(i,j));
return newImage;
}
private PImage xReflect(){// copy to newImage after reflection about the X-axis
PImage newImage = new PImage(getImageWidth(), getImageHeight());
for (int i=0; i < getImageWidth(); i++)
for (int j=0; j < getImageHeight(); j++)
newImage.set(i, getImageHeight()-j-1, image.get(i,j));
return newImage;
}
private PImage xYReflect(){// copy to newImage after reflection about the X-axis and Y-axis
PImage newImage = new PImage(getImageWidth(), getImageHeight());
for (int i = 0; i < getImageWidth(); i++)
for (int j = 0; j < getImageHeight(); j++)
newImage.set(getImageWidth()-i-1, getImageHeight()-j-1, image.get(i,j));
return newImage;
}
private PImage rotate90(){// copy to newImage after rotation through 90 degrees
PImage newImage = new PImage(getImageHeight(), getImageWidth());
for (int i = 0; i < newImage.width; i++)
for (int j = 0; j < newImage.height; j++)
newImage.set(getImageHeight()-i-1, getImageWidth()-j-1, image.get(image.width-j-1, i));
return newImage;
}
private PImage rotate180(){// copy to newImage after rotation through 180 degrees
PImage newImage = new PImage(getImageWidth(), getImageHeight());
for (int i = 0; i < getImageWidth(); i++)
for (int j = 0; j < getImageHeight(); j++)
newImage.set(getImageWidth()-i-1, getImageHeight()-j-1, image.get(i,j));
return newImage;
}
private PImage rotate270(){ // copy to newImage after rotation through 270 degrees
PImage newImage = new PImage(getImageHeight(), getImageWidth());
for (int j = 0; j < getImageWidth(); j++)
for (int i = 0; i < getImageHeight(); i++)
newImage.set(i, j, image.get(getImageWidth()-j-1,i));
return newImage;
}
private int makeSelection(int i, int j){
int select, x = i%2, y = j%2;
select = (x*2)+y;
return select;
}
public int showImages (String [] list, int base, int xGap, int yGap){
final int repeat = 3;
for (int i=0; i < repeat*list.length; i++)
for (int j=0; j < list.length; j++)
image(newImage(list[makeSelection(i,j)]), i*(getImageWidth() + xGap), base + j*(getImageHeight() + yGap));
return repeat*(getImageHeight() + yGap);
}
}

2 comments:

Anonymous said...

Just signed in using my openid for the first time based on my chunk blog that is why you will know I have a vested interest in your contribution.

Antony said...

Hi, I've been trying to contact you about the Processing book that I'm going to attempt to publish, but I probably have an out-of-date email address for you. Could you contact me please? antonylees@gmail.com
Cheers :)