Source code: com/arranger/jarl/test/FracTest.java
1 package com.arranger.jarl.test;
2
3 import com.arranger.jarl.test.frac.ComplexRectangle;
4 import com.arranger.jarl.test.frac.Drawing;
5 import com.arranger.jarl.test.frac.FractalCalculator;
6 import com.arranger.jarl.test.frac.MandelbrotCalculator;
7 import com.arranger.jarl.util.IOUtil;
8 import com.arranger.jarl.base.GradientManager;
9 import com.jhlabs.image.Gradient;
10 import junit.framework.TestCase;
11
12 import java.awt.*;
13 import java.awt.image.BufferedImage;
14 import java.io.File;
15
16 public class FracTest extends TestCase {
17
18 protected int m_imageHeight = 360;
19 protected int m_imageWidth = 640;
20 protected Color[] m_color;
21 //protected static double zoomFactor = 0;
22 protected static int numColors = 512;
23
24 private final int ORIGIANL_ITERATIONS = 33;
25 private final ComplexRectangle ORIGINAL_RECT = new ComplexRectangle(-2.5, 1.5, -2.0, 2.0);
26
27 private final int INITIAL_ITERATIONS =
28 ORIGIANL_ITERATIONS;
29 //105;
30 //170;
31 //238;
32
33
34
35 private final ComplexRectangle INITIAL_RECT =
36 ORIGINAL_RECT;
37 //new ComplexRectangle(-0.9766, -0.5340, -0.0340, 0.4085);
38 //new ComplexRectangle(-0.76238, -0.70118, 0.20230, 0.26350);
39 //new ComplexRectangle(-0.736597, -0.728654, 0.237329, 0.245273);
40
41 protected GradientManager m_gradientManager;
42
43 public void testFrac() throws Exception {
44
45 m_gradientManager = new GradientManager();
46 m_gradientManager.init(null);
47
48 Image image = createImage();
49
50 Drawing drawing = makeNewDrawing(INITIAL_RECT, INITIAL_ITERATIONS, image, null);
51 FractalCalculator fractalCalculator = makeNewCalculator(drawing);
52 try {
53 fractalCalculator.run();
54 } catch (Throwable throwable) {
55 throwable.printStackTrace();
56 throw new Exception(throwable.getMessage());
57 }
58
59 IOUtil.save(image, new File("fracTest.bmp"));
60 }
61
62 protected Image createImage() {
63 Image image = new BufferedImage(m_imageWidth,
64 m_imageHeight,
65 BufferedImage.TYPE_INT_RGB);
66 return image;
67 }
68
69 protected FractalCalculator makeNewCalculator(Drawing d) {
70 return new MandelbrotCalculator(this, d);
71 }
72
73 /*protected void adjustZoomFactor(ComplexRectangle cr) {
74 zoomFactor = ORIGINAL_RECT.getWidth() / cr.getWidth();
75 }*/
76
77 protected Drawing makeNewDrawing(ComplexRectangle rect,
78 int maxIterations,
79 Image image, String color) {
80 expandRectToFitImage(rect);
81 //adjustZoomFactor(rect);
82 return new Drawing(rect, maxIterations, image, null, color);
83 }
84
85 /*public static String doubleAsString(double d) {
86 // This global method encapsulates a trick to prevent undesirable rounding
87 // effects from being noticable to the user.
88 // The scale is determined by the zoom factor of the current drawing.
89 int scale = getBigDecimalScale();
90 BigDecimal big = new BigDecimal(d);
91 return big.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
92 }
93
94 private static int getBigDecimalScale() {
95 // Determine how many decimal places of resolution are required (min = 4).
96 // This quantity is directly related to the base 10 log of the zoomFactor.
97 // Note: Math.log(x) uses base E (2.71828...) not base 10, so in order to
98 // compute a base 10 logarithm, use: Math.log( x ) / Math.log( 10 ).
99 double zoom = getZoomFactor();
100 if (zoom < 1.0) {
101 zoom = 1.0; // forces log( zoom ) >= 0
102 }
103 int scale = (int) (Math.log(zoom) / 2.3); // Math.log( 10 ) = 2.3
104 return scale + 4;
105 }
106
107 public static double getZoomFactor() {
108 return zoomFactor;
109 }*/
110
111 protected void expandRectToFitImage(ComplexRectangle complexRect) {
112 // The complex rectangle must be scaled to fit the pixel image view.
113 // Method: compare the width/height ratios of the two rectangles.
114 double imageWHRatio = 1.0;
115 double complexWHRatio = 1.0;
116 double iMin = complexRect.getIMin();
117 double iMax = complexRect.getIMax();
118 double rMin = complexRect.getRMin();
119 double rMax = complexRect.getRMax();
120 double complexWidth = rMax - rMin;
121 double complexHeight = iMax - iMin;
122
123 if ((m_imageWidth != 0) && (m_imageHeight != 0)) {
124 imageWHRatio = ((double) m_imageWidth / (double) m_imageHeight);
125 } else {
126 return;
127 }
128
129 if ((complexWidth != 0) && (complexHeight != 0)) {
130 complexWHRatio = complexWidth / complexHeight;
131 } else {
132 return;
133 }
134
135 if (imageWHRatio == complexWHRatio) {
136 return;
137 }
138
139 if (imageWHRatio < complexWHRatio) {
140 // Expand vertically
141 double newHeight = complexWidth / imageWHRatio;
142 double heightDifference = Math.abs(newHeight - complexHeight);
143 iMin = iMin - heightDifference / 2;
144 iMax = iMax + heightDifference / 2;
145 } else {
146 // Expand horizontally
147 double newWidth = complexHeight * imageWHRatio;
148 double widthDifference = Math.abs(newWidth - complexWidth);
149 rMin = rMin - widthDifference / 2;
150 rMax = rMax + widthDifference / 2;
151 }
152 complexRect.set(rMin, rMax, iMin, iMax);
153 }
154
155
156 /***********************************88
157 * CALLBACKS
158 */
159
160 public int getImageHeight() {
161 return m_imageHeight;
162 }
163
164 public int getImageWidth() {
165 return m_imageWidth;
166 }
167
168 public Color[] getCurrentColorMap() {
169
170 //rainbows
171 /*float saturation = (float) 1.0;
172 float brightness = (float) 1.0;
173 Color[] colorMap = new Color[numColors];
174 for (int colorNum = 0; colorNum < numColors; colorNum++) {
175 float hue = (float) colorNum / (float) numColors;
176 colorMap[colorNum] = new Color(Color.HSBtoRGB(hue, saturation, brightness));
177 }*/
178
179 Gradient gradient = m_gradientManager.getGradient(27);
180
181 Color[] colorMap = new Color[numColors];
182 for (int colorNum = 0; colorNum < numColors; colorNum++) {
183 float f = colorNum;
184 f /= numColors;
185 int color = gradient.getColor(f);
186 colorMap[colorNum] = new Color(color);
187 }
188
189 return colorMap;
190 }
191
192 public void setStatus2(String s) {
193 System.out.println(s);
194 }
195
196 public void outOfMemory(boolean b) {
197 System.out.println("OOM");
198 System.exit(0);
199 }
200
201 public void calculatorCallback(boolean success,
202 Drawing newDrawing) {
203 //do something
204 }
205 }