Skip to content

Commit

Permalink
Matrix from Strings
Browse files Browse the repository at this point in the history
Standard coloring modes e.g Triangle Area, Curvature and Stripe Averages
Catmull-Rom Spline interpolation
Custom Color Palettes
Bugfixes, performance improvements
Removal of redundant code
Proper zoom support
  • Loading branch information
tamchow committed Dec 3, 2015
1 parent 9c1561f commit f380ca8
Show file tree
Hide file tree
Showing 11 changed files with 464 additions and 404 deletions.
317 changes: 183 additions & 134 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified out/artifacts/FractalGenerator_jar/FractalGenerator.jar
Binary file not shown.
204 changes: 59 additions & 145 deletions src/in/tamchow/fractal/FractalGenerator.java

Large diffs are not rendered by default.

80 changes: 34 additions & 46 deletions src/in/tamchow/fractal/Main.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,48 @@
package in.tamchow.fractal;

import in.tamchow.fractal.config.ConfigReader;
import in.tamchow.fractal.config.color.ColorMode;
import in.tamchow.fractal.config.color.ColorConfig;
import in.tamchow.fractal.config.color.Colors;
import in.tamchow.fractal.config.fractalconfig.FractalConfig;
import in.tamchow.fractal.math.complex.Complex;
import in.tamchow.fractal.platform_tools.ImageConverter;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
* Main class, handles CMDLINE input.
*/
public class Main {
public static void main(String[] args) {
String func = "( z ^ 3 ) + ( d * z ) + c", variableCode = "z", poly = "{1,z,3};+;{d,z,1};+;{c,z,0}";
String[][] consts = {{"c", "-0.8,+0.156i"}, {"d", "-0.7198,+0.911i"}};
int resx = 401, resy = 401, zoom = 10, zoompow = 0, baseprec = 150, colmode = ColorMode.COLOR_NEWTON_1, numcol = 32, coldens = 125, fracmode = FractalGenerator.MODE_NEWTON, iter = 8;
double bound = 2.0, escrad = 2.0, tolerance = 1e-1;
String func = "( z ^ 2 ) + c", variableCode = "z", poly = "{1,z,3};+;{d,z,1};+;{c,z,0}";
String[][] consts = {{"c", "-0.1,+0.651i"}, {"d", "-0.7198,+0.911i"}};
int resx = 801, resy = 801, zoom = 10, zoompow = 0, baseprec = 100, fracmode = FractalGenerator.MODE_JULIA, iter = 8;
double escrad = 2.0, tolerance = 1e-1;
ColorConfig cfg = new ColorConfig(Colors.CALCULATIONS.TRIANGLE_AREA_INEQUALITY, 8, 32);
Complex constant = null;
func = poly;
boolean fromFile = false;
//func = poly;
boolean def = (args.length == 0);
FractalConfig fccfg = new FractalConfig(0, 0, 0);
if (args.length > 1) {
func = args[0];
consts[0][0] = args[1].substring(0, args[1].indexOf(':'));
consts[0][1] = args[1].substring(args[1].indexOf(':') + 1, args[1].length());
variableCode = args[2];
resx = Integer.valueOf(args[3]);
resy = Integer.valueOf(args[4]);
zoom = Integer.valueOf(args[5]);
zoompow = Integer.valueOf(args[6]);
baseprec = Integer.valueOf(args[7]);
colmode = Integer.valueOf(args[8]);
numcol = Integer.valueOf(args[9]);
coldens = Integer.valueOf(args[10]);
fracmode = Integer.valueOf(args[11]);
bound = Double.valueOf(args[12]);
iter = Integer.valueOf(args[13]);
escrad = Double.valueOf(args[14]);
if (args.length >= 16) {
tolerance = Double.valueOf(args[15]);
}
if (args.length == 17) {
constant = new Complex(args[16]);
}
fromFile = false;
} else if (args.length == 1) {
if (!def) {
try {
fccfg = ConfigReader.getFractalConfigFromFile(new File(args[0]));
fromFile = true;
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
long inittime = System.currentTimeMillis();
FractalGenerator jgen;
if (!fromFile) {
jgen = new FractalGenerator(resx, resy, zoom, zoompow, baseprec, colmode, numcol, coldens, fracmode, bound, func, consts, variableCode, tolerance);
if (def) {
jgen = new FractalGenerator(resx, resy, zoom, zoompow, baseprec, fracmode, func, consts, variableCode, tolerance, cfg);
} else {
jgen = new FractalGenerator(fccfg.getParams()[0]);
}
long starttime = System.currentTimeMillis();
System.out.println("Initiating fractal took:" + (starttime - inittime) + "ms");
if (!fromFile) {
if (def) {
if (constant != null) {
jgen.generate(iter, escrad, constant);
} else {
Expand All @@ -73,34 +51,44 @@ public static void main(String[] args) {
} else {
jgen.generate(fccfg.getParams()[0]);
}

long gentime = System.currentTimeMillis();
System.out.println("Generating fractal took:" + ((double) (gentime - starttime) / 60000) + "mins");
File pic = new File("D:/Fractal.jpg");
File postpic = new File("D:/Fractal_processed.jpg");
try {
ImageIO.write(ImageConverter.toImage(jgen.getArgand()), "jpg", pic);
ImageIO.write(ImageConverter.toImage(jgen.getArgand().getColorAveraged()), "jpg", postpic);
//ImageIO.write(ImageConverter.toImage(jgen.getArgand().getColorAveraged()), "jpg", postpic);
} catch (Exception e) {
e.printStackTrace();
}
long endtime = System.currentTimeMillis();
System.out.println("Writing image took:" + (endtime - gentime) + "ms");
/*ImageData[] imgs={new ImageData("D:/Fractal.png")};
System.exit(0);
/*ImageData[] imgs={new ImageData("D:/Fractal.jpg")};
int[] trans={-1};
int wait=1;
ImageConfig ic=new ImageConfig(5,40,imgs,trans,wait);
ImageDisplay.show(ic,"Images");*/
/*Random random=new Random();
jgen.zoom(jgen.toCooordinates(jgen.boundary_points.get(random.nextInt(jgen.boundary_points.size())))[0],jgen.toCooordinates(jgen.boundary_points.get(random.nextInt(jgen.boundary_points.size())))[1],2);
Random random = new Random();
starttime = System.currentTimeMillis();
int[] coords = jgen.toCooordinates(jgen.getArgand_map()[random.nextInt(resx)][random.nextInt(resy)]);
jgen.zoom(coords[0], coords[1], 1);
//jgen.zoom(jgen.getCenter_x(), jgen.getCenter_y(), 1);
jgen.generate(128);
System.out.println(jgen.boundary_points.get(0));
if (def) {
if (constant != null) {
jgen.generate(iter, escrad, constant);
} else {
jgen.generate(iter, escrad);
}
} else {
jgen.generate(fccfg.getParams()[0]);
}
endtime = System.currentTimeMillis();
System.out.println("Generating zoomed fractal took:" + ((double) (endtime - starttime) / 60000) + "mins");
try {
ImageIO.write(Image_ImageData.toImage(jgen.getArgand()), "png", zoompic);
ImageIO.write(ImageConverter.toImage(jgen.getArgand()), "jpg", postpic);
} catch (Exception e) {
e.printStackTrace();
}*/
}
}
}
115 changes: 92 additions & 23 deletions src/in/tamchow/fractal/config/color/ColorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,79 @@
* Holds colour configuration for custom palettes
*/
public class ColorConfig {
public int basecolor, step, color_density, num_colors;
public int basecolor, step, color_density, num_colors, mode, palette_type;
public int[] palette;
boolean colors_corrected;

public ColorConfig(int color_density, int num_colors, int basecolor) {
initColorConfig(color_density, num_colors, basecolor);
public ColorConfig(int mode, int color_density, int num_colors, int basecolor) {
initColorConfig(mode, color_density, num_colors, basecolor);
}

public ColorConfig(int mode, int color_density, int num_colors, int basecolor, int step) {
initColorConfig(mode, color_density, num_colors, basecolor, step);
}
public ColorConfig(int[] palette) {
setPalette(palette, false);
}

public ColorConfig(int mode, int color_density, int num_colors) {
initColorConfig(mode, num_colors);
setColor_density(color_density);
}

public ColorConfig() {
palette = null;
setPalette_type(Colors.PALETTE.RANDOM);
initColorConfig(0, 0, 0x0, 0);
}

public ColorConfig(ColorConfig old) {
initColorConfig(old.getMode(), old.getColor_density(), old.getNum_colors(), old.getBasecolor(), old.getStep());
setPalette(old.getPalette(), false);
colors_corrected = old.colors_corrected;
}

public int createIndex(double val, double min, double max) {
return (int) Math.abs(((((val - min) / (max - min))) * color_density) % num_colors);
}

public int getColor(int index) {
return palette[index];
}

public int splineInterpolated(int index, double bias) {
if ((!colors_corrected) && num_colors < 4) {
num_colors = 4;
setBasecolor(basecolor);
setColor_density(color_density);
setMode(mode);
setNum_colors(num_colors);
setStep(step);
if (palette_type == Colors.PALETTE.RANDOM) {
initRandomPalette(num_colors, true);
} else {
initGradientPalette();
}
colors_corrected = true;
}
double h0 = 0.5 * ((bias * bias) * (bias - 1)),
h1 = 0.5 * (bias * (1 + 4 * bias - 3 * (bias * bias))),
h2 = 0.5 * (2 - 5 * (bias * bias) + 3 * (bias * bias * bias)),
h3 = 0.5 * (bias * (2 * bias - (bias * bias) - 1));
int i1 = ((index - 1) < 0) ? num_colors - 1 : index - 1, i2 = ((index - 2) < 0) ? num_colors - 2 : index - 2, i3 = ((index - 3) < 0) ? num_colors - 3 : index - 3;
double color = (h0 * palette[index] + h1 * palette[i1] + h2 * palette[i2] + h3 * palette[i3]);
color = (color < 0) ? -color : color;
return (int) color;
}

public int getPalette_type() {
return palette_type;
}

public void setPalette_type(int palette_type) {
this.palette_type = palette_type;
}

public int getBasecolor() {
return basecolor;
}
Expand Down Expand Up @@ -54,6 +111,7 @@ public void setPalette(int[] palette, boolean preserve) {
}

public void initRandomPalette(int num_colors, boolean preserve) {
setPalette_type(Colors.PALETTE.RANDOM);
if (!preserve) {
palette = new int[num_colors];
for (int pidx = 0; pidx < num_colors; pidx++) {
Expand All @@ -71,6 +129,7 @@ public void initRandomPalette(int num_colors, boolean preserve) {
}

public void initGradientPalette() {
setPalette_type(Colors.PALETTE.CUSTOM);
palette = new int[num_colors];
int baseidx = num_colors / 2;
for (int i = 0; i < baseidx; i++) {
Expand All @@ -81,31 +140,32 @@ public void initGradientPalette() {
}
}

public void initGrayScalePalette(int max) {
palette = new int[max];
for (int i = 0; i < palette.length; i++) {
palette[i] = i << 16 | i << 8 | i;
}
}

private void initColorConfig(int color_density, int num_colors, int basecolor, int step) {
private void initColorConfig(int mode, int color_density, int num_colors, int basecolor, int step) {
colors_corrected = false;
setColor_density(color_density);
setNum_colors(num_colors);
setBasecolor(basecolor);
setStep(step);
initGradientPalette();
setMode(mode);
}

private void initColorConfig(int color_density, int num_colors, int basecolor) {

private void initColorConfig(int mode, int color_density, int num_colors, int basecolor) {
colors_corrected = false;
setColor_density(color_density);
setNum_colors(num_colors);
setBasecolor(basecolor);
calcStep();
initGradientPalette();
setMode(mode);
}

private void initColorConfig(int num_colors) {
private void initColorConfig(int mode, int num_colors) {
colors_corrected = false;
setNum_colors(num_colors);
initRandomPalette(num_colors, false);
setMode(mode);
}

private void calcStep() {
Expand All @@ -128,31 +188,40 @@ public void setNum_colors(int num_colors) {
this.num_colors = num_colors;
}

public int getMode() {
return mode;
}

public void setMode(int mode) {
this.mode = mode;
}

public void colorsFromString(String[] colors) {
if (colors[0].startsWith("0x")) {
mode = Integer.parseInt(colors[0]);
if (colors[1].startsWith("0x")) {
int[] palette = new int[colors.length];
for (int i = 0; i < colors.length; i++) {
for (int i = 1; i < colors.length; i++) {
palette[i] = Integer.parseInt(colors[i], 16);
}
setPalette(palette, false);
} else {
switch (colors.length) {
case 1:
initColorConfig(Integer.parseInt(colors[0]));
break;
case 3:
initColorConfig(Integer.parseInt(colors[0]), Integer.parseInt(colors[1]), Integer.parseInt(colors[2], 16));
case 2:
initColorConfig(mode, Integer.parseInt(colors[1]));
break;
case 4:
initColorConfig(Integer.parseInt(colors[0]), Integer.parseInt(colors[1]), Integer.parseInt(colors[3], 16), Integer.parseInt(colors[2]));
initColorConfig(mode, Integer.parseInt(colors[1]), Integer.parseInt(colors[2]), Integer.parseInt(colors[3], 16));
break;
case 5:
initColorConfig(mode, Integer.parseInt(colors[1]), Integer.parseInt(colors[2]), Integer.parseInt(colors[4], 16), Integer.parseInt(colors[3]));
break;
default:
throw new IllegalArgumentException("Unsupoorted Input");
throw new IllegalArgumentException("Unsupported Input");
}
}
}

public boolean noCustomPalette() {
return palette.length == 0 || palette == null;
return (palette_type == Colors.PALETTE.RANDOM) || (palette.length == 0) || (palette == null);
}
}
8 changes: 0 additions & 8 deletions src/in/tamchow/fractal/config/color/ColorMode.java

This file was deleted.

14 changes: 14 additions & 0 deletions src/in/tamchow/fractal/config/color/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package in.tamchow.fractal.config.color;

/**
* A few constant values for coloring modes
*/
public final class Colors {
public static final class CALCULATIONS {
public static final int COLOR_DIVIDE = 0, COLOR_MULTIPLY = 1, COLOR_HIGH_CONTRAST = 2, COLOR_GRAYSCALE = 3, COLOR_NEWTON_1 = 4, COLOR_NEWTON_2 = 5, TRIANGLE_AREA_INEQUALITY = 6, CURVATURE_AVERAGE = 7, STRIPE_AVERAGE = 8;
}

public static final class PALETTE {
public static final int RANDOM = 0, CUSTOM = 1;
}
}
Loading

0 comments on commit f380ca8

Please sign in to comment.