ICFP 2007 Contest: https://web.archive.org/web/20090301164728/https://save-endo.cs.uu.nl/
1// TODO: Better wrapper here 2pub const SIZE: usize = 600; 3pub type Bitmap = [[(u8, u8, u8, u8); SIZE]; SIZE]; 4 5pub fn new_bitmap() -> Bitmap { 6 [[(0, 0, 0, 0); SIZE]; SIZE] 7} 8 9fn output_u8(vec: &mut Vec<u8>, n: u8) { 10 vec.push(n); 11} 12 13fn output_u16(vec: &mut Vec<u8>, n: u16) { 14 vec.push(n as u8); 15 vec.push((n >> 8) as u8); 16} 17 18fn output_u32(vec: &mut Vec<u8>, n: u32) { 19 vec.push(n as u8); 20 vec.push((n >> 8) as u8); 21 vec.push((n >> 16) as u8); 22 vec.push((n >> 24) as u8); 23} 24 25pub fn to_data(bmp: &Bitmap) -> Vec<u8> { 26 let mut ret = Vec::new(); 27 let row_padding = 3 - (3 * SIZE % 4); 28 output_u16(&mut ret, 0x4d42); // BM 29 output_u32(&mut ret, (0x36 + SIZE * (3 * SIZE + row_padding)) as u32); // Size 30 output_u16(&mut ret, 0x0); // Reserved 31 output_u16(&mut ret, 0x0); // Reserved 32 output_u32(&mut ret, 0x36); // Offset of pixel array 33 output_u32(&mut ret, 0x28); // Length of DIB header 34 output_u32(&mut ret, SIZE as u32); // Width 35 output_u32(&mut ret, SIZE as u32); // Height 36 output_u16(&mut ret, 0x1); // Color planes 37 output_u16(&mut ret, 0x18); // Bits per pixel 38 output_u32(&mut ret, 0x0); // No pixel array compression 39 output_u32(&mut ret, 0x0); // Horizontal resolution 40 output_u32(&mut ret, 0x0); // Vertical resolution 41 output_u32(&mut ret, 0x0); // Colors in the palette 42 output_u32(&mut ret, 0x0); // All colors are important 43 44 for y in (0..SIZE).rev() { 45 for x in 0..SIZE { 46 let (r, g, b, _) = bmp[x][y]; 47 output_u8(&mut ret, b); 48 output_u8(&mut ret, g); 49 output_u8(&mut ret, r); 50 } 51 52 for _ in 0..row_padding { 53 output_u8(&mut ret, 0); 54 } 55 } 56 57 return ret; 58}