Skip to content
Newer
Older
100644 120 lines (94 sloc) 3.69 KB
2f7562b @dbyrne Changed to produce PNG output
authored May 27, 2010
1 import java.awt.image.BufferedImage
2 import java.awt.Color
3 import javax.imageio.ImageIO
4 import java.io.File
5 import java.io.IOException
8d74fd5 @dbyrne Affine transformation, Variation, and Post Transformation working
authored May 5, 2010
6 import scala.util.Random
ac018db @dbyrne Split into multiple files
authored May 15, 2010
7 import FractalComponents._
2245d6d @dbyrne Sample Flames in seperate file
authored May 24, 2010
8 import SampleFlames._
8d74fd5 @dbyrne Affine transformation, Variation, and Post Transformation working
authored May 5, 2010
9
8b66595 @dbyrne Lots of updates
authored May 14, 2010
10 /*
11 Copyright (C) 2010 David Byrne
12 david.r.byrne@gmail.com
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27
8d74fd5 @dbyrne Affine transformation, Variation, and Post Transformation working
authored May 5, 2010
28 object FractalFlames {
bc3aeb3 @dbyrne Sample flames moved to seperate file. Uses a more functional style.
authored May 13, 2010
29
8d74fd5 @dbyrne Affine transformation, Variation, and Post Transformation working
authored May 5, 2010
30 def main(args: Array[String]){
dafeacc @dbyrne Bubble variation added
authored May 27, 2010
31 val renderer=new Renderer(1500,1500)
4e13922 @dbyrne continuous updating added
authored May 11, 2010
32 while (true) {
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
33 renderer.render()
4e13922 @dbyrne continuous updating added
authored May 11, 2010
34 }
8d74fd5 @dbyrne Affine transformation, Variation, and Post Transformation working
authored May 5, 2010
35 }
36 }
37
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
38 class Renderer(xres:Int,yres:Int) {
bc3aeb3 @dbyrne Sample flames moved to seperate file. Uses a more functional style.
authored May 13, 2010
39
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
40 val superSampling = 2
41 var values = Array.ofDim[Double](xres*superSampling,yres*superSampling,4)
4e13922 @dbyrne continuous updating added
authored May 11, 2010
42
dafeacc @dbyrne Bubble variation added
authored May 28, 2010
43 val flame = bubbleSample
ac018db @dbyrne Split into multiple files
authored May 15, 2010
44
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
45 def render() {
bc3aeb3 @dbyrne Sample flames moved to seperate file. Uses a more functional style.
authored May 13, 2010
46
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
47 var alpha = Array.ofDim[Double](xres*superSampling,yres*superSampling)
48 var valFinal = Array.ofDim[Double](xres,yres,3)
bc3aeb3 @dbyrne Sample flames moved to seperate file. Uses a more functional style.
authored May 13, 2010
49
8b66595 @dbyrne Lots of updates
authored May 14, 2010
50 val (minX, maxX, minY, maxY) = flame coords
defe429 @dbyrne colors fixed
authored May 9, 2010
51 val rangeX = maxX - minX
52 val rangeY = maxY - minY
4ad3c13 @dbyrne Flame class added
authored May 7, 2010
53 var count = 0
4f961e0 @dbyrne colors fixed
authored May 17, 2010
54
e98097f @dbyrne Super sampling added
authored May 18, 2010
55 val colors = flame colors
4f961e0 @dbyrne colors fixed
authored May 17, 2010
56 val maxColorIndex = colors.size - 1
57
8b66595 @dbyrne Lots of updates
authored May 14, 2010
58 while (count < 1000000) {
59 val p = flame.points.next()
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
60 val pixelX = math.round(((p._1.x - minX) / rangeX) * xres * superSampling).asInstanceOf[Int]
61 val pixelY = math.round(((p._1.y - minY) / rangeY) * yres * superSampling).asInstanceOf[Int]
62 if (pixelX < xres * superSampling && pixelX > 0 && pixelY < yres * superSampling && pixelY > 0) {
8b66595 @dbyrne Lots of updates
authored May 14, 2010
63 values(pixelX)(pixelY)(0) += 1 //Pixel Density
4f961e0 @dbyrne colors fixed
authored May 17, 2010
64 val c = colors(math.round(p._2 * maxColorIndex).asInstanceOf[Int])
65 values(pixelX)(pixelY)(1) += c.getRed()
66 values(pixelX)(pixelY)(2) += c.getGreen()
67 values(pixelX)(pixelY)(3) += c.getBlue()
8d74fd5 @dbyrne Affine transformation, Variation, and Post Transformation working
authored May 5, 2010
68 }
69 count += 1
70 }
ac018db @dbyrne Split into multiple files
authored May 15, 2010
71
defe429 @dbyrne colors fixed
authored May 9, 2010
72 var max = 0.0
600e61a @dbyrne gamma correction applied to full super sample
authored May 18, 2010
73
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
74 for (r <- 0 until yres * superSampling; c <- 0 until xres * superSampling) {
600e61a @dbyrne gamma correction applied to full super sample
authored May 19, 2010
75 if (values(c)(r)(0) > max) {
76 max = values(c)(r)(0)
77 }
78 alpha(c)(r) = math.log(values(c)(r)(0)) //log of the density
79 }
80
81 max = math.log(max)
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
82 for (r <- 0 until yres * superSampling; c <- 0 until xres * superSampling) {
600e61a @dbyrne gamma correction applied to full super sample
authored May 19, 2010
83 alpha(c)(r) = alpha(c)(r) / max //replace log density with alpha values
84 }
85
86 val recipGamma = 1.0/flame.gamma
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
87 for (r <- 0 until yres; c <- 0 until xres) {
e98097f @dbyrne Super sampling added
authored May 19, 2010
88 for (r2 <- r * superSampling until (r+1) * superSampling;
89 c2 <- c * superSampling until (c+1) * superSampling) {
90
600e61a @dbyrne gamma correction applied to full super sample
authored May 19, 2010
91 for (i <- 0 until 3) {
92 valFinal(c)(r)(i) += (values(c2)(r2)(i+1)/values(c2)(r2)(0)) * math.pow(alpha(c2)(r2), recipGamma)
e98097f @dbyrne Super sampling added
authored May 19, 2010
93 }
94
defe429 @dbyrne colors fixed
authored May 9, 2010
95 }
e98097f @dbyrne Super sampling added
authored May 19, 2010
96
600e61a @dbyrne gamma correction applied to full super sample
authored May 19, 2010
97 for (i <- 0 until 3) {
e98097f @dbyrne Super sampling added
authored May 19, 2010
98 valFinal(c)(r)(i) = valFinal(c)(r)(i) / (superSampling*superSampling)
99 }
5281a1c @dbyrne Affine transformation coefficients fixed
authored May 7, 2010
100 }
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
101
102 val bImage = new BufferedImage(xres, yres, BufferedImage.TYPE_INT_RGB)
e98097f @dbyrne Super sampling added
authored May 19, 2010
103
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
104 for (r <- 0 until yres; c <- 0 until xres) {
e98097f @dbyrne Super sampling added
authored May 19, 2010
105
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
106 val color = new Color(math.round(valFinal(c)(r)(0)).asInstanceOf[Int],
600e61a @dbyrne gamma correction applied to full super sample
authored May 19, 2010
107 math.round(valFinal(c)(r)(1)).asInstanceOf[Int],
2f7562b @dbyrne Changed to produce PNG output
authored May 28, 2010
108 math.round(valFinal(c)(r)(2)).asInstanceOf[Int])
109 bImage.setRGB(c, r, color.getRGB)
110 }
111
112 try {
113 val outputfile = new File("saved.png")
114 ImageIO.write(bImage, "png", outputfile)
115 } catch {
116 case e:IOException => println("Could not save image")
5281a1c @dbyrne Affine transformation coefficients fixed
authored May 7, 2010
117 }
8d74fd5 @dbyrne Affine transformation, Variation, and Post Transformation working
authored May 5, 2010
118 }
5e9afdf @dbyrne Rings variation fixed
authored May 19, 2010
119 }
Something went wrong with that request. Please try again.