Skip to content

Commit b55f30c

Browse files
author
Patrick
committedMar 14, 2014
first commit
1 parent 5f06ed0 commit b55f30c

11 files changed

+324
-143
lines changed
 

‎README.md

-97
This file was deleted.

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
project_summary.md

‎project_code/ImageAnalyze.java

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import javax.imageio.ImageIO;
2+
import java.io.File;
3+
import java.awt.image.BufferedImage;
4+
import java.awt.image.PixelGrabber;
5+
import java.awt.Image;
6+
import java.awt.image.ImageObserver;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Collections;
10+
import java.util.Comparator;
11+
12+
public class ImageAnalyze {
13+
private BufferedImage bi;
14+
private ArrayList<PixelGroup> pixelGroups = new ArrayList<PixelGroup>();
15+
16+
private int threshold = 50;
17+
private Painter painter;
18+
19+
private int tilesAcross = 2, tilesDown = 2;
20+
21+
public static void main( String[] args ) {
22+
String fn = "image.png";
23+
int threshold = 50;
24+
if ( args.length > 0 ){
25+
fn = args[0];
26+
}
27+
if ( args.length > 1 ){
28+
threshold = Integer.parseInt( args[1] );
29+
}
30+
ImageAnalyze ia = new ImageAnalyze( fn, threshold );
31+
ia.analyze();
32+
}
33+
public ImageAnalyze ( String fn, int threshold ){
34+
this.threshold = threshold;
35+
try {
36+
this.bi = ImageIO.read( new File( fn ) );
37+
}
38+
catch ( Exception e ){
39+
e.printStackTrace();
40+
System.exit(1);
41+
}
42+
}
43+
public void analyze(){
44+
this.handlepixels( this.bi, 0, 0, this.bi.getWidth(), this.bi.getHeight() );
45+
}
46+
public void handlesinglepixel(PixelPrim pixel) {
47+
48+
for ( PixelGroup pg : this.pixelGroups ){
49+
if ( pg.isSimilar( pixel ) ){
50+
return;
51+
}
52+
}
53+
54+
this.pixelGroups.add( new PixelGroup( pixel, this.threshold ) );
55+
}
56+
57+
public void handlepixels(Image img, int x, int y, int w, int h) {
58+
int[] pixels = new int[w * h];
59+
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
60+
try {
61+
pg.grabPixels();
62+
} catch (InterruptedException e) {
63+
System.err.println("interrupted waiting for pixels!");
64+
return;
65+
}
66+
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
67+
System.err.println("image fetch aborted or errored");
68+
return;
69+
}
70+
for (int j = 0; j < h; j++) {
71+
for (int i = 0; i < w; i++) {
72+
handlesinglepixel(new PixelPrim( x+i, y+j, pixels[j * w + i] ));
73+
}
74+
}
75+
76+
BufferedImage off_Image = new BufferedImage(w*tilesAcross, h*tilesDown, BufferedImage.TYPE_INT_ARGB);
77+
78+
Collections.sort( this.pixelGroups, new CustomComparator() );
79+
80+
int sublist_topindex = 5;
81+
if ( this.pixelGroups.size() < 5 ){
82+
sublist_topindex = this.pixelGroups.size();
83+
}
84+
List<PixelGroup> pgBySize = this.pixelGroups.subList( 0, sublist_topindex );
85+
86+
PixelGroup lowestsD = null;
87+
for ( PixelGroup pgr : pgBySize ) {
88+
System.out.println( pgr );
89+
if ( lowestsD == null || pgr.standardDeviation() < lowestsD.standardDeviation() ){
90+
lowestsD = pgr;
91+
}
92+
}
93+
System.out.println( "======================================\n" + lowestsD + "\n\n=============================" );
94+
lowestsD.isBackground = true;
95+
96+
this.painter = new PainterDot( w, h );
97+
98+
int xOffset=0,yOffset=0;
99+
for ( int tid = 0; tid < ( tilesAcross * tilesDown ); tid++ ){
100+
this.painter.paintTile( off_Image, this.pixelGroups, xOffset * w, yOffset * h );
101+
xOffset++;
102+
if ( xOffset >= tilesAcross ){
103+
xOffset = 0;
104+
yOffset++;
105+
}
106+
}
107+
108+
try {
109+
// retrieve image
110+
File outputfile = new File("/tmp/patimageout.png");
111+
ImageIO.write(off_Image, "png", outputfile);
112+
} catch (Exception e) {
113+
}
114+
115+
116+
}
117+
118+
119+
public class CustomComparator implements Comparator<PixelGroup> {
120+
@Override
121+
public int compare(PixelGroup o1, PixelGroup o2) {
122+
return o2.pixels.size() - o1.pixels.size();
123+
}
124+
}
125+
}

‎project_code/Painter.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java.util.ArrayList;
2+
import java.awt.image.BufferedImage;
3+
public interface Painter {
4+
public void paintTile ( BufferedImage off_Image, ArrayList<PixelGroup> apgr, int xOffset, int yOffset );
5+
}

‎project_code/PainterDot.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.ArrayList;
2+
import java.awt.image.ImageObserver;
3+
import java.awt.image.PixelGrabber;
4+
import java.awt.image.BufferedImage;
5+
6+
public class PainterDot implements Painter {
7+
private int w,h;
8+
public PainterDot ( int w, int h ){
9+
this.w = w;
10+
this.h = h;
11+
}
12+
public void paintTile ( BufferedImage off_Image, ArrayList<PixelGroup> apgr, int xOffset, int yOffset ){
13+
14+
int radius = 20;
15+
int spacing = radius;
16+
17+
BufferedImage solidImage = new BufferedImage(this.w, this.h, BufferedImage.TYPE_INT_ARGB);
18+
Painter p = new PainterNormal( w, h );
19+
p.paintTile( solidImage, apgr, 0, 0 );
20+
21+
int[] pixels = new int[w * h];
22+
PixelGrabber pg = new PixelGrabber(solidImage, 0, 0, w, h, pixels, 0, w);
23+
try {
24+
pg.grabPixels();
25+
} catch (InterruptedException e) {
26+
System.err.println("interrupted waiting for pixels!");
27+
return;
28+
}
29+
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
30+
System.err.println("image fetch aborted or errored");
31+
return;
32+
}
33+
34+
// off_Image.getGraphics().drawImage( solidImage, xOffset, yOffset, null );
35+
36+
for ( int i = 0; i < w; i+= spacing ) {
37+
for ( int j = 0; j < h; j+= spacing ) {
38+
this.drawSpot( off_Image, i, j, pixels[j * w + i], radius, xOffset, yOffset );
39+
}
40+
}
41+
}
42+
private void drawSpot( BufferedImage bi, int sX, int sY, int pixel, int radius, int xOff, int yOff ){
43+
44+
for ( int x = (int)(sX - ( radius / 2 ) ); x <= (int)(sX + ( radius / 2 ) ); x++ ){
45+
if ( x < 0 || x > this.w ) continue;
46+
for ( int y = (int)(sY - ( radius / 2 ) ); y <= (int)(sY + ( radius / 2 ) ); y++ ){
47+
if ( y < 0 || y > this.h ) continue;
48+
double dist = this._dist( x, y, sX, sY );
49+
if ( dist < (( radius * 1.0 ) / 2) ){
50+
try {
51+
bi.setRGB( xOff+x, yOff+y, pixel );
52+
} catch ( Exception e ){}
53+
}
54+
}
55+
}
56+
57+
}
58+
private double _dist ( int x1, int y1, int x2, int y2 ){
59+
return Math.sqrt((
60+
Math.pow( x1 - x2, 2 ) +
61+
Math.pow( y1 - y2, 2 )
62+
));
63+
}
64+
}

‎project_code/PainterNormal.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.ArrayList;
2+
import java.awt.image.BufferedImage;
3+
4+
public class PainterNormal implements Painter {
5+
private int w,h;
6+
public PainterNormal ( int w, int h ){
7+
this.w = w;
8+
this.h = h;
9+
}
10+
public void paintTile ( BufferedImage off_Image, ArrayList<PixelGroup> apgr, int xOffset, int yOffset ){
11+
for ( PixelGroup pgr : apgr ){
12+
pgr.randomizeColor();
13+
14+
for ( PixelPrim pp : pgr.pixels ){
15+
int colour = pgr.intColour();
16+
off_Image.setRGB( xOffset+pp.x, yOffset+pp.y, colour );
17+
}
18+
}
19+
}
20+
}

‎project_code/PixelGroup.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.ArrayList;
2+
3+
public class PixelGroup {
4+
public ArrayList<PixelPrim> pixels = new ArrayList<PixelPrim>();
5+
long totalDistance = 0;
6+
public double red, green, blue, alpha;
7+
public boolean isBackground = false;
8+
9+
public double standardDeviation () {
10+
long variance = totalDistance / this.pixels.size();
11+
double sD = Math.pow( variance, 0.5 );
12+
return sD;
13+
}
14+
15+
private int threshold = 50;
16+
17+
public void randomizeColor() {
18+
this.red = (int)(Math.random() * 255);
19+
this.green = (int)(Math.random() * 255);
20+
this.blue = (int)(Math.random() * 255);
21+
this.alpha = 255;//(int)(Math.random() * 255);
22+
}
23+
24+
public int intColour () {
25+
int colour = (((int)this.alpha & 0xFF) << 24) |
26+
(((int)this.red & 0xFF) << 16) |
27+
(((int)this.green & 0xFF) << 8) |
28+
(((int)this.blue & 0xFF) << 0);
29+
return colour;
30+
}
31+
public PixelGroup ( PixelPrim pixel, int threshold ){
32+
this.threshold = threshold;
33+
this.red = pixel.red;
34+
this.green = pixel.green;
35+
this.blue = pixel.blue;
36+
this.alpha = pixel.alpha;
37+
this.pixels.add( pixel );
38+
}
39+
public boolean isSimilar( PixelPrim pixel ){
40+
double dist = this._dist( pixel );
41+
if ( dist < this.threshold ){
42+
this.addPixel( pixel, dist );
43+
return true;
44+
}
45+
return false;
46+
}
47+
public void addPixel ( PixelPrim pixel, double dist ){
48+
this.red = ( ( this.red * this.pixels.size() ) + pixel.red ) / ( this.pixels.size() + 1 );
49+
this.green = ( ( this.green * this.pixels.size() ) + pixel.green ) / ( this.pixels.size() + 1 );
50+
this.blue = ( ( this.blue * this.pixels.size() ) + pixel.blue ) / ( this.pixels.size() + 1 );
51+
this.alpha = ( ( this.alpha * this.pixels.size() ) + pixel.alpha ) / ( this.pixels.size() + 1 );
52+
this.pixels.add( pixel );
53+
this.totalDistance += (int)Math.pow(dist,2);
54+
}
55+
private int _dist( PixelPrim pixel ) {
56+
double distance = Math.pow((
57+
Math.pow(((pixel.red * 1.0) - this.red),2) +
58+
Math.pow(((pixel.green * 1.0) - this.green),2) +
59+
Math.pow(((pixel.blue * 1.0) - this.blue),2) +
60+
Math.pow(((pixel.alpha * 1.0) - this.alpha),2)),0.5);
61+
return (int) distance;
62+
}
63+
64+
public String toString() {
65+
int r = (int) this.red, g = (int) this.green, b = (int) this.blue, a = (int) this.alpha;
66+
return r + "-" + g + "-" + b + "-" + a + ": " + this.pixels.size() + " - " + this.standardDeviation();
67+
}
68+
69+
}
70+

‎project_code/PixelPrim.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class PixelPrim {
2+
public int red,green,blue,alpha,x,y;
3+
public PixelPrim( int x, int y, int pixel ) {
4+
this.x = x;
5+
this.y = y;
6+
7+
this.alpha = (pixel >> 24) & 0xff;
8+
this.red = (pixel >> 16) & 0xff;
9+
this.green = (pixel >> 8) & 0xff;
10+
this.blue = (pixel ) & 0xff;
11+
}
12+
public PixelPrim( int x, int y, int red, int green, int blue, int alpha ) {
13+
this.x = x;
14+
this.y = y;
15+
16+
this.alpha = alpha;
17+
this.red = red;
18+
this.green = green;
19+
this.blue = blue;
20+
}
21+
}

‎project_code/jarme

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
javac $1.java -d build/
4+
echo "Main-Class: ImageAnalyzer" > build/Manifest.txt
5+
cd build/
6+
7+
jar cfm PopArt.jar Manifest.txt *
8+
9+
mv PopArt.jar ../
10+
rm * -rf

‎project_code/run

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
javac $1.java
2+
java $1 $2 $3
3+
rm *.class

‎project_summary.md

+2-33
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,2 @@
1-
# Project Title
2-
Insert the name of your project
3-
4-
## Authors
5-
- Insert main author name, surname, github account
6-
- Insert other author(s) name, surname, github account (one per list element)
7-
8-
## Description
9-
Insert a description containing about 100 to 150 words, including your motivation and the meaning behind your idea and execution. The Judges will be keen to know how your idea pushes the boundaries of code and technology.
10-
11-
## Link to Prototype
12-
NOTE: If your project lives online you can add one or more links here. Make sure you have a stable version of your project running before linking it.
13-
14-
[Example Link](http://www.google.com "Example Link")
15-
16-
## Example Code
17-
NOTE: Wrap your code blocks or any code citation by using ``` like the example below.
18-
```
19-
function test() {
20-
console.log("Printing a test");
21-
}
22-
```
23-
## Links to External Libraries
24-
NOTE: You can also use this space to link to external libraries or Github repositories you used on your project.
25-
26-
[Example Link](http://www.google.com "Example Link")
27-
28-
## Images & Videos
29-
NOTE: For additional images you can either use a relative link to an image on this repo or an absolute link to an externally hosted image.
30-
31-
![Example Image](project_images/cover.jpg?raw=true "Example Image")
32-
33-
https://www.youtube.com/watch?v=30yGOxJJ2PQ
1+
## Pops and Spots ##
2+
Take your favourite image, consolidate then randomise the colours four times.

‎project_technologies.json

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
{
2-
"technologies": {
3-
"apis": [
4-
"insert apis"
5-
],
6-
"platforms": [
7-
"insert platforms"
8-
],
9-
"toolkits": [
10-
"insert toolkits"
11-
]
12-
},
2+
"technologies": {},
133
"themes": [
14-
"insert themes"
4+
"Image Manipulation"
155
]
16-
}
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.