11use crate :: util:: PixelData ;
22use crate :: math:: Reference ;
33
4+ use std:: io:: prelude:: * ;
5+ use std:: fs:: File ;
6+ use std:: slice;
7+
48use exr:: prelude:: simple_image;
59
610pub enum DataType {
711 COLOUR ,
812 RAW ,
13+ KFB ,
914 BOTH
1015}
1116
1217pub struct DataExport {
1318 image_width : usize ,
1419 image_height : usize ,
1520 rgb : Vec < u8 > ,
16- palette : Vec < ( f32 , f32 , f32 ) > ,
21+ palette : Vec < ( u8 , u8 , u8 ) > ,
1722 iterations : Vec < u32 > ,
1823 smooth : Vec < f32 > ,
1924 pub display_glitches : bool ,
@@ -35,7 +40,7 @@ impl DataExport {
3540 data_type
3641 }
3742 } ,
38- DataType :: RAW => {
43+ DataType :: RAW | DataType :: KFB => {
3944 DataExport {
4045 image_width,
4146 image_height,
@@ -75,23 +80,23 @@ impl DataExport {
7580 self . rgb [ k + 2 ] = 0 ;
7681 } else {
7782 let colour = self . palette [ 10 * pixel. iteration % 1024 ] ;
78- self . rgb [ k] = colour. 0 as u8 ;
79- self . rgb [ k + 1 ] = colour. 1 as u8 ;
80- self . rgb [ k + 2 ] = colour. 2 as u8 ;
83+ self . rgb [ k] = colour. 0 ;
84+ self . rgb [ k + 1 ] = colour. 1 ;
85+ self . rgb [ k + 2 ] = colour. 2 ;
8186 }
8287 } else if pixel. iteration >= maximum_iteration {
8388 self . rgb [ k] = 0 ;
8489 self . rgb [ k + 1 ] = 0 ;
8590 self . rgb [ k + 2 ] = 0 ;
8691 } else {
8792 let colour = self . palette [ 10 * pixel. iteration % 1024 ] ;
88- self . rgb [ k] = colour. 0 as u8 ;
89- self . rgb [ k + 1 ] = colour. 1 as u8 ;
90- self . rgb [ k + 2 ] = colour. 2 as u8 ;
93+ self . rgb [ k] = colour. 0 ;
94+ self . rgb [ k + 1 ] = colour. 1 ;
95+ self . rgb [ k + 2 ] = colour. 2 ;
9196 } ;
9297 }
9398 } ,
94- DataType :: RAW => {
99+ DataType :: RAW | DataType :: KFB => {
95100 let escape_radius_ln = 1e16f32 . ln ( ) ;
96101
97102 for pixel in pixel_data {
@@ -123,9 +128,9 @@ impl DataExport {
123128 self . iterations [ k / 3 ] = 0x00000000
124129 } else {
125130 let colour = self . palette [ 10 * pixel. iteration % 1024 ] ;
126- self . rgb [ k] = colour. 0 as u8 ;
127- self . rgb [ k + 1 ] = colour. 1 as u8 ;
128- self . rgb [ k + 2 ] = colour. 2 as u8 ;
131+ self . rgb [ k] = colour. 0 ;
132+ self . rgb [ k + 1 ] = colour. 1 ;
133+ self . rgb [ k + 2 ] = colour. 2 ;
129134 self . iterations [ k / 3 ] = 0x00000000
130135 }
131136 } else if pixel. iteration >= maximum_iteration {
@@ -135,9 +140,9 @@ impl DataExport {
135140 self . iterations [ k / 3 ] = 0xFFFFFFFF ;
136141 } else {
137142 let colour = self . palette [ 10 * pixel. iteration % 1024 ] ;
138- self . rgb [ k] = colour. 0 as u8 ;
139- self . rgb [ k + 1 ] = colour. 1 as u8 ;
140- self . rgb [ k + 2 ] = colour. 2 as u8 ;
143+ self . rgb [ k] = colour. 0 ;
144+ self . rgb [ k + 1 ] = colour. 1 ;
145+ self . rgb [ k + 2 ] = colour. 2 ;
141146 self . iterations [ k / 3 ] = pixel. iteration as u32 ;
142147 } ;
143148
@@ -148,14 +153,17 @@ impl DataExport {
148153 }
149154 }
150155
151- pub fn save ( & mut self , filename : & str ) {
156+ pub fn save ( & mut self , filename : & str , maximum_iteration : usize ) {
152157 match self . data_type {
153158 DataType :: COLOUR => {
154159 self . save_colour ( filename) ;
155160 } ,
156161 DataType :: RAW => {
157162 self . save_raw ( filename) ;
158163 } ,
164+ DataType :: KFB => {
165+ self . save_kfb ( filename, maximum_iteration) ;
166+ }
159167 DataType :: BOTH => {
160168 self . save_colour ( filename) ;
161169 self . save_raw ( filename) ;
@@ -180,7 +188,60 @@ impl DataExport {
180188 image. write_to_file ( filename. to_owned ( ) + ".exr" , simple_image:: write_options:: high ( ) ) . unwrap ( ) ;
181189 }
182190
183- fn generate_colour_palette ( ) -> Vec < ( f32 , f32 , f32 ) > {
191+ fn save_kfb ( & mut self , filename : & str , maximum_iteration : usize ) {
192+ let mut file = File :: create ( filename. to_owned ( ) + ".kfb" ) . unwrap ( ) ;
193+
194+ file. write_all ( b"KFB" ) . unwrap ( ) ;
195+
196+ let test1 = [ self . image_width as u32 ] ;
197+ let test2 = [ self . image_height as u32 ] ;
198+
199+ // iteration division??
200+ let test3 = [ 0.1f32 ] ;
201+
202+ // Colours in colourmap
203+ let test5 = DataExport :: generate_colour_palette ( ) ;
204+
205+ // Number of colours in colourmap
206+ let test4 = [ test5. len ( ) as u32 ] ;
207+
208+ // Maxmimum iteration
209+ let test6 = [ maximum_iteration as u32 ] ;
210+
211+ file. write_all ( unsafe {
212+ slice:: from_raw_parts ( test1. as_ptr ( ) as * const u8 , 4 )
213+ } ) . unwrap ( ) ;
214+ file. write_all ( unsafe {
215+ slice:: from_raw_parts ( test2. as_ptr ( ) as * const u8 , 4 )
216+ } ) . unwrap ( ) ;
217+
218+ file. write_all ( unsafe {
219+ slice:: from_raw_parts ( self . iterations . as_ptr ( ) as * const u8 , self . iterations . len ( ) * 4 )
220+ } ) . unwrap ( ) ;
221+
222+ file. write_all ( unsafe {
223+ slice:: from_raw_parts ( test3. as_ptr ( ) as * const u8 , 4 )
224+ } ) . unwrap ( ) ;
225+
226+ file. write_all ( unsafe {
227+ slice:: from_raw_parts ( test4. as_ptr ( ) as * const u8 , 4 )
228+ } ) . unwrap ( ) ;
229+
230+ file. write_all ( unsafe {
231+ slice:: from_raw_parts ( test5. as_ptr ( ) as * const u8 , 3 * test5. len ( ) )
232+ } ) . unwrap ( ) ;
233+
234+ file. write_all ( unsafe {
235+ slice:: from_raw_parts ( test6. as_ptr ( ) as * const u8 , 4 )
236+ } ) . unwrap ( ) ;
237+
238+ file. write_all ( unsafe {
239+ slice:: from_raw_parts ( self . smooth . as_ptr ( ) as * const u8 , self . iterations . len ( ) * 4 )
240+ } ) . unwrap ( ) ;
241+
242+ }
243+
244+ fn generate_colour_palette ( ) -> Vec < ( u8 , u8 , u8 ) > {
184245 let mut colours = Vec :: with_capacity ( 1024 ) ;
185246
186247 for i in 0 ..1024 {
@@ -222,7 +283,7 @@ impl DataExport {
222283 blue = 0.0 + factor * ( 100.0 - 0.0 ) ;
223284 }
224285
225- colours. push ( ( red, green, blue) )
286+ colours. push ( ( red as u8 , green as u8 , blue as u8 ) )
226287 }
227288
228289 colours
0 commit comments