• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Surprisingly Complex Images Generated From Very Simple Code

 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was doing the equivalent of a hello world graphics program in java and so I wrote a simple program that loops through all pixels in an image and uses some simple math to decide what color to display. I was very surprised to see the complexity of the images that were created and displayed to the screen. Give it a go. You simply need to compile the code below and run. Some sample calls follow:


java -classpath . Paints
java -classpath . Paints 0 43
java -classpath . Paints 0 58
java -classpath . Paints 1
java -classpath . Paints 1 23

The code follows. It should work as is. I compiled in 1.5 though it may work in older versions too. It is worth the effort!


[ June 12, 2008: Message edited by: steve souza ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're into this kind of thing you might have a look at the Expression plugin I wrote for the ImageJ Java image processing application.

It uses formulas to create new images, or alter existing ones. With just 3 formulas (one each for the red, green and blue color components) surprisingly complex images can be created. Here are some examples.

Feedback and questions are welcome.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's the basic gist of the code I posted. Dynamically changing color based on some formula. The images it generates are complex like yours are though they all look different too. Yes, it is very interesting that such simple code can create such complex, interesting images. I studied the code for a while and still really don't have much of an idea how the complex images are created.

You say your images were generated with 'one liners'. Can you give me a couple examples?
 
Author
Posts: 587
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool program. I uploaded some of the images to my site so people can get a general idea of the images (hope you don't mind). I couldn't cut and paste your code from the Javaranch '' tags format, but I managed to piece it together... it's below (no tags) for anyone who wants to cut and paste it.

http://gliesian.com/souza_images/image_1_10.GIF
http://gliesian.com/souza_images/image_1_23.GIF
http://gliesian.com/souza_images/image_2_13.GIF

NOTE: If anyone produces a really cool image with the app, give me the numbers and I'll run the app with them and upload the image.

Souza's code:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** Program that loops through every pixel in a bufferimage and uses various logic to see how to color the pixel. * The simple algorithm has suprisingly complex results. * * * * java -classpath . Paints 0 * java -classpath . Paints 0 10 * java -classpath . Paints 0 43 * java -classpath . Paints 0 58 * java -classpath . Paints 0 37 * java -classpath . Paints 0 13 * java -classpath . Paints 0 97 * java -classpath . Paints 0 123 * java -classpath . Paints 1 * java -classpath . Paints 1 3 * java -classpath . Paints 1 5 * java -classpath . Paints 1 10 * java -classpath . Paints 1 23 * java -classpath . Paints 1 25 * java -classpath . Paints 1 35 * * @author stevesouza - liquilight software * * Feel free to alter and play with this program. * */
public class Paints extends JPanel {

private String[] args;

public Paints(String[] args) {
this.args = args;
}
static final int WIDTH = 1000, HEIGHT = 1000;

public String getName() {
return "Paints";
}

public int getWidth() {
return WIDTH;
}

public int getHeight() {
return HEIGHT;
}
private static final int NOT_PROVIDED = Integer.MAX_VALUE;

public void paint(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
BufferedImage tile = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
int modValue;
if (args == null || args.length <= 1) {
modValue = NOT_PROVIDED;
} else {
modValue = Integer.valueOf(args[1]).intValue();
}
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
if (args == null || args.length == 0 || "0".equals(args[0])) {
if (modValue == NOT_PROVIDED) {
tile.setRGB(i, j, (int) Math.tan(j * i));
} else {
tile.setRGB(i, j, (int) Math.tan(j * i % modValue));
}
} else if ("1".equals(args[0])) {
if (modValue == NOT_PROVIDED) {
tile.setRGB(i, j, i * j);
} else {
if (i * i * j * j * 3 % modValue == 0) {
tile.setRGB(i, j, Color.red.getRGB());
} else {
tile.setRGB(i, j, Color.black.getRGB());
}
}
} else if ("2".equals(args[0])) {
tile.setRGB(i, j, Color.red.getRGB());
}
}
}
Graphics2D g2d = tile.createGraphics();
g.drawImage(tile, 0, 0, WIDTH, HEIGHT, 0, 0, WIDTH, HEIGHT, null);
}

public static void main(String[] args) {
System.out.println("Sample usage to display surpisingly interesting images: java -classpath . Paints 0 57");
System.out.println("java -classpath . Paints 0 57: 0 is or no args base display on tangents. 2nd value if provided is used to create a variety of images by modding with this number");
System.out.println("java -classpath . Paints 1 21: 1 will base display on multiplication. 2nd value if provided is used to create a variety of images by modding with this number");
System.out.println("java -classpath . Paints 2: 2 simply colors the screen red, to show that no trickery is creating the diverse images");
System.out.println();
System.out.println("Try any of the following as a starting point");
System.out.println("java -classpath . Paints 0");
System.out.println("java -classpath . Paints 0 10");
System.out.println("java -classpath . Paints 0 43");
System.out.println("java -classpath . Paints 0 58");
System.out.println("java -classpath . Paints 0 37");
System.out.println("java -classpath . Paints 0 13");
System.out.println("java -classpath . Paints 0 97");
System.out.println("java -classpath . Paints 0 123");
System.out.println();
System.out.println("java -classpath . Paints 1");
System.out.println("java -classpath . Paints 1 3");
System.out.println("java -classpath . Paints 1 5");
System.out.println("java -classpath . Paints 1 10");
System.out.println("java -classpath . Paints 1 23");
System.out.println("java -classpath . Paints 1 25");
System.out.println("java -classpath . Paints 1 35");
System.out.println();
System.out.println("Feel free to alter and play with this program");
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setContentPane(new Paints(args));
f.setSize(WIDTH, HEIGHT);
f.setVisible(true);
}
}
[ June 12, 2008: Message edited by: Robert Liguori ]
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's great. I'm glad you posted them. The images for these are interesting:
java -classpath . Paints 0
java -classpath . Paints 1
[ June 12, 2008: Message edited by: steve souza ]
 
Robert James Liguori
Author
Posts: 587
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here they are (just 0) and (just 1).




[ June 12, 2008: Message edited by: Robert Liguori ]
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are all pretty interesting:

System.out.println("Try any of the following as a starting point");
System.out.println("java -classpath . Paints 0");
System.out.println("java -classpath . Paints 0 10");
System.out.println("java -classpath . Paints 0 43");
System.out.println("java -classpath . Paints 0 58");
System.out.println("java -classpath . Paints 0 37");
System.out.println("java -classpath . Paints 0 13");
System.out.println("java -classpath . Paints 0 97");
System.out.println("java -classpath . Paints 0 123");
System.out.println();
System.out.println("java -classpath . Paints 1");
System.out.println("java -classpath . Paints 1 3");
System.out.println("java -classpath . Paints 1 5");
System.out.println("java -classpath . Paints 1 10");
System.out.println("java -classpath . Paints 1 23");
System.out.println("java -classpath . Paints 1 25");
System.out.println("java -classpath . Paints 1 35");
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by steve souza:
You say your images were generated with 'one liners'. Can you give me a couple examples?



Sure. For the examples on that page, the following formulas were used. If there 3 formulas, then they are used for calculating the red, green and blue color components; if there's just a single formula then it's used for all 3 colors, resulting in a greyscale image.

The Mandelbrot formulas are not truely one-liners. I defined a function that calculates the iteration depth for a given point - once you have that, it becomes a one-liner :-)

To make the formulas more expressive, a number of variable are predefined:
x and y - coordinates of the pixel, w and h - width and height of the image, d - distance from center of image, a - angle when measured from center of image
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. My program works solely based on your x, y concept. For example I have a loop that paints all pixels based on the following formula:

image.setRGB(i, j, (int) Math.tan(i*j));

i and j are analagous to your x, and y. I am guessing your code could generate the same image. What is ImageJ used for in general? Do you have other images in your gallery?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by steve souza:
What is ImageJ used for in general? Do you have other images in your gallery?


ImageJ originates in the (bio)medical/scientific community (the American NIH, to be precise), but I use it for general image-processing tasks. It understands a lot of file formats, and there are tons of plugins for all imaginable tasks, including my little collection.

I don't have more images to show, but the plugin ships with a lot more predefined formulas that are ready to run. Plus, the plugin lets you define more functions (like the Mandelbrot function) in Java syntax, thus allowing an almost unlimited amount of creativity.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are some more images created by the tangent formula. There are 3 different images each consisting of 100 different 100x100 images.




[ September 02, 2008: Message edited by: steve souza ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic