1
- use crate :: util:: point:: Point ;
1
+ use crate :: util:: image:: Image ;
2
+ use crate :: util:: PixelData ;
2
3
3
4
pub enum ColourMethod {
4
5
Iteration ,
5
- IterationSquareRoot ,
6
- Histogram
6
+ // IterationSquareRoot,
7
+ // Histogram
7
8
}
8
9
9
10
impl ColourMethod {
10
- pub fn run < T > ( & self , points : & Vec < Point < T > > , image : & mut Vec < u8 > , maximum_iterations : usize , display_glitches : bool ) {
11
+ pub fn run ( & self , pixels : & Vec < PixelData > , image : & mut Image , maximum_iterations : usize ) {
11
12
// Palette is temporarily here
12
13
let mut colours = Vec :: new ( ) ;
13
14
@@ -55,112 +56,101 @@ impl ColourMethod {
55
56
56
57
match self {
57
58
ColourMethod :: Iteration => {
58
- for point in points {
59
- let index = point. index ;
60
-
61
- if point. glitched && display_glitches {
62
- image[ 3 * index] = 255u8 ;
63
- image[ 3 * index + 1 ] = 0u8 ;
64
- image[ 3 * index + 2 ] = 0u8 ;
65
- } else if point. iterations >= maximum_iterations {
66
- image[ 3 * index] = 0u8 ;
67
- image[ 3 * index + 1 ] = 0u8 ;
68
- image[ 3 * index + 2 ] = 0u8 ;
59
+ for pixel in pixels {
60
+ let ( red, green, blue) = if pixel. glitched && image. display_glitches {
61
+ ( 255 , 0 , 0 )
62
+ } else if pixel. iteration >= maximum_iterations {
63
+ ( 0 , 0 , 0 )
69
64
} else {
70
65
// 0.1656
71
- let hue = 0.1656 * ( point. iterations as f32 + point. smooth ) % 8192.0 ;
72
-
73
- let colour = colours[ ( hue. floor ( ) as usize ) % 8192 ] ;
74
- let colour2 = colours[ ( hue. floor ( ) as usize + 1 ) % 8192 ] ;
75
-
76
- let red = ( colour. 0 + ( ( colour2. 0 - colour. 0 ) * hue. fract ( ) ) ) as u8 ;
77
- let green = ( colour. 1 + ( ( colour2. 1 - colour. 1 ) * hue. fract ( ) ) ) as u8 ;
78
- let blue = ( colour. 2 + ( ( colour2. 2 - colour. 2 ) * hue. fract ( ) ) ) as u8 ;
79
-
80
- image[ 3 * index] = red;
81
- image[ 3 * index + 1 ] = green;
82
- image[ 3 * index + 2 ] = blue;
83
- }
84
- }
85
- } ,
86
- ColourMethod :: IterationSquareRoot => {
87
- for point in points {
88
- let index = point. index ;
66
+ let hue = ( 0.1656 * pixel. iteration as f64 ) as usize % 8192 ;
89
67
90
- if point. glitched && display_glitches {
91
- image[ 3 * index] = 255u8 ;
92
- image[ 3 * index + 1 ] = 0u8 ;
93
- image[ 3 * index + 2 ] = 0u8 ;
94
- } else if point. iterations >= maximum_iterations {
95
- image[ 3 * index] = 0u8 ;
96
- image[ 3 * index + 1 ] = 0u8 ;
97
- image[ 3 * index + 2 ] = 0u8 ;
98
- } else {
99
- let hue = 1600.0 * ( point. iterations as f32 + point. smooth ) . sqrt ( ) % 8192.0 ;
100
-
101
- let colour = colours[ ( hue. floor ( ) as usize ) % 8192 ] ;
102
- let colour2 = colours[ ( hue. floor ( ) as usize + 1 ) % 8192 ] ;
68
+ let colour = colours[ hue] ;
103
69
104
- let red = ( colour. 0 + ( ( colour2. 0 - colour. 0 ) * hue. fract ( ) ) ) as u8 ;
105
- let green = ( colour. 1 + ( ( colour2. 1 - colour. 1 ) * hue. fract ( ) ) ) as u8 ;
106
- let blue = ( colour. 2 + ( ( colour2. 2 - colour. 2 ) * hue. fract ( ) ) ) as u8 ;
70
+ ( colour. 0 as u8 , colour. 1 as u8 , colour. 2 as u8 )
71
+ } ;
107
72
108
- image[ 3 * index] = red;
109
- image[ 3 * index + 1 ] = green;
110
- image[ 3 * index + 2 ] = blue;
111
- }
73
+ image. plot ( pixel. image_x , pixel. image_y , red, green, blue) ;
112
74
}
113
75
} ,
114
- ColourMethod :: Histogram => {
115
- let mut iteration_counts = vec ! [ 0usize ; maximum_iterations + 1 ] ;
116
-
117
- for point in points {
118
- iteration_counts[ point. iterations as usize ] += 1
119
- }
120
-
121
- for i in 1 ..iteration_counts. len ( ) {
122
- iteration_counts[ i] += iteration_counts[ i - 1 ] ;
123
- }
124
-
125
- let total = iteration_counts[ maximum_iterations - 1 ] ;
126
-
127
- for point in points {
128
- let index = point. index ;
129
-
130
- if point. glitched && display_glitches {
131
- image[ 3 * index] = 255u8 ;
132
- image[ 3 * index + 1 ] = 0u8 ;
133
- image[ 3 * index + 2 ] = 0u8 ;
134
- } else if point. iterations >= maximum_iterations {
135
- image[ 3 * index] = 0u8 ;
136
- image[ 3 * index + 1 ] = 0u8 ;
137
- image[ 3 * index + 2 ] = 0u8 ;
138
- } else {
139
- let factor = if point. smooth == std:: f32:: NAN || point. iterations as f32 + point. smooth < 0.0 {
140
- point. iterations as f32
141
- } else {
142
- point. iterations as f32 + point. smooth
143
- } ;
144
-
145
- let v1 = iteration_counts[ factor as usize ] as f32 / total as f32 ;
146
- let v2 = iteration_counts[ factor as usize + 1 ] as f32 / total as f32 ;
147
-
148
- // the hue is used to smooth the histogram bins. The hue is in the range 0.0-1.0
149
- let hue = ( v1 + ( v2 - v1) * factor. fract ( ) ) * 8192.0 ;
150
-
151
- let colour = colours[ hue. floor ( ) as usize % 8192 ] ;
152
- let colour2 = colours[ ( hue. floor ( ) as usize + 1 ) % 8192 ] ;
153
-
154
- let red = ( colour. 0 + ( ( colour2. 0 - colour. 0 ) * factor. fract ( ) ) ) as u8 ;
155
- let green = ( colour. 1 + ( ( colour2. 1 - colour. 1 ) * factor. fract ( ) ) ) as u8 ;
156
- let blue = ( colour. 2 + ( ( colour2. 2 - colour. 2 ) * factor. fract ( ) ) ) as u8 ;
157
-
158
- image[ 3 * index] = red;
159
- image[ 3 * index + 1 ] = green;
160
- image[ 3 * index + 2 ] = blue;
161
- }
162
- }
163
- }
76
+ // ColourMethod::IterationSquareRoot => {
77
+ // for point in points {
78
+ // let index = point.index;
79
+ //
80
+ // if point.glitched && display_glitches {
81
+ // image[3 * index] = 255u8;
82
+ // image[3 * index + 1] = 0u8;
83
+ // image[3 * index + 2] = 0u8;
84
+ // } else if point.iterations >= maximum_iterations {
85
+ // image[3 * index] = 0u8;
86
+ // image[3 * index + 1] = 0u8;
87
+ // image[3 * index + 2] = 0u8;
88
+ // } else {
89
+ // let hue = 1600.0 * (point.iterations as f32 + point.smooth).sqrt() % 8192.0;
90
+ //
91
+ // let colour = colours[(hue.floor() as usize) % 8192];
92
+ // let colour2 = colours[(hue.floor() as usize + 1) % 8192];
93
+ //
94
+ // let red = (colour.0 + ((colour2.0 - colour.0) * hue.fract())) as u8;
95
+ // let green = (colour.1 + ((colour2.1 - colour.1) * hue.fract())) as u8;
96
+ // let blue = (colour.2 + ((colour2.2 - colour.2) * hue.fract())) as u8;
97
+ //
98
+ // image[3 * index] = red;
99
+ // image[3 * index + 1] = green;
100
+ // image[3 * index + 2] = blue;
101
+ // }
102
+ // }
103
+ // },
104
+ // ColourMethod::Histogram => {
105
+ // let mut iteration_counts = vec![0usize; maximum_iterations + 1];
106
+ //
107
+ // for point in points {
108
+ // iteration_counts[point.iterations as usize] += 1
109
+ // }
110
+ //
111
+ // for i in 1..iteration_counts.len() {
112
+ // iteration_counts[i] += iteration_counts[i - 1];
113
+ // }
114
+ //
115
+ // let total = iteration_counts[maximum_iterations - 1];
116
+ //
117
+ // for point in points {
118
+ // let index = point.index;
119
+ //
120
+ // if point.glitched && display_glitches {
121
+ // image[3 * index] = 255u8;
122
+ // image[3 * index + 1] = 0u8;
123
+ // image[3 * index + 2] = 0u8;
124
+ // } else if point.iterations >= maximum_iterations {
125
+ // image[3 * index] = 0u8;
126
+ // image[3 * index + 1] = 0u8;
127
+ // image[3 * index + 2] = 0u8;
128
+ // } else {
129
+ // let factor = if point.smooth == std::f32::NAN || point.iterations as f32 + point.smooth < 0.0 {
130
+ // point.iterations as f32
131
+ // } else {
132
+ // point.iterations as f32 + point.smooth
133
+ // };
134
+ //
135
+ // let v1 = iteration_counts[factor as usize] as f32 / total as f32;
136
+ // let v2 = iteration_counts[factor as usize + 1] as f32 / total as f32;
137
+ //
138
+ // // the hue is used to smooth the histogram bins. The hue is in the range 0.0-1.0
139
+ // let hue = (v1 + (v2 - v1) * factor.fract()) * 8192.0;
140
+ //
141
+ // let colour = colours[hue.floor() as usize % 8192];
142
+ // let colour2 = colours[(hue.floor() as usize + 1) % 8192];
143
+ //
144
+ // let red = (colour.0 + ((colour2.0 - colour.0) * factor.fract())) as u8;
145
+ // let green = (colour.1 + ((colour2.1 - colour.1) * factor.fract())) as u8;
146
+ // let blue = (colour.2 + ((colour2.2 - colour.2) * factor.fract())) as u8;
147
+ //
148
+ // image[3 * index] = red;
149
+ // image[3 * index + 1] = green;
150
+ // image[3 * index + 2] = blue;
151
+ // }
152
+ // }
153
+ // }
164
154
}
165
155
}
166
156
}
0 commit comments