1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! # BMP monochrome
//!
//! This library creates black and white bitmap with no extra dependencies,
//! especially useful to encode QR-codes
//!

#![deny(missing_docs)]

use std::io::Error;

mod bit;

const B: u8 = 66;
const M: u8 = 77;

/// Represent the data that are going to be encoded in the bitmap
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DataMatrix {
    data: Vec<bool>,
    width: usize,
}

/// Internal error struct
#[derive(Debug)]
pub struct BmpError;

impl DataMatrix {

    /// Creates a new DataMatrix, failing if `data` is empty or its length not a multiple of `width`
    pub fn new(data: Vec<bool>, width: usize) -> Result<DataMatrix, BmpError> {
        if data.is_empty() || data.len() % width != 0 {
            Err(BmpError)
        } else {
            Ok(DataMatrix { data, width })
        }
    }

    fn height(&self) -> usize {
        self.data.len() / self.width
    }

    fn get(&self, i: usize, j: usize) -> Option<&bool> {
        let h = self.height() - i - 1;
        self.data.get(h * self.width + j)
    }

    /// multiply by `mul` every pixel
    pub fn mul(&self, mul: usize) -> DataMatrix {
        let mut data = vec![];

        for i in 0..self.height() {
            let mut row = vec![];
            for j in 0..self.width {
                for _ in 0..mul {
                    row.push(self.get(i, j).unwrap());
                }
            }
            for _ in 0..mul {
                data.extend(row.clone());
            }
        }

        let width = self.width * mul;
        DataMatrix { data, width }
    }

    /// add `white_space` pixels around
    pub fn add_whitespace(&self, white_space: usize) -> DataMatrix {
        let width = self.width + white_space * 2;
        let mut data = vec![];
        for _ in 0..white_space {
            data.extend(vec![false; width]);
        }
        for vec in self.data.chunks(self.width) {
            for _ in 0..white_space {
                data.push(false);
            }
            data.extend(vec);
            for _ in 0..white_space {
                data.push(false);
            }
        }
        for _ in 0..white_space {
            data.extend(vec![false; width]);
        }

        DataMatrix { data, width }
    }

    /// Returns a monocromatic bitmap
    pub fn bmp(&self) -> Result<Vec<u8>, BmpError> {
        let matrix = self.clone();
        let width = matrix.width as u32;
        let height = matrix.height() as u32;

        let mut bmp_data = vec![];
        let header = BmpHeader { height, width };
        let padding = header.padding() as u8;
        bmp_data.extend(header.write());

        let mut data = Vec::new();
        let mut writer = bit::BitStreamWriter::new(&mut data);

        for i in 0..height as usize {
            for j in 0..width as usize {
                if *matrix.get(i, j).unwrap() {
                    writer.write(1, 1)?;
                } else {
                    writer.write(0, 1)?;
                }
            }
            writer.write(0, 8 - (width % 8) as u8)?; // 0
            writer.write(0, padding * 8)?; // 0
        }
        writer.flush().unwrap();
        bmp_data.extend(data);

        Ok(bmp_data)
    }
}

impl From<std::io::Error> for BmpError {
    fn from(_: Error) -> Self {
        BmpError
    }
}

struct BmpHeader {
    height: u32,
    width: u32,
}

impl BmpHeader {
    /*pub fn from_bytes(_bytes: Vec<u8>) {
        unimplemented!();
    }*/

    pub fn write(&self) -> Vec<u8> {
        let color_pallet_size = 2 * 4; // 2 colors each 4 bytes
        let header_size = 2 + 12 + 40 + color_pallet_size;
        let bytes_per_row = self.bytes_per_row();
        let padding = self.padding();
        let data_size = (bytes_per_row + padding) * (self.height as u32);
        let total_size = header_size + data_size;
        let mut output = vec![];

        // https://en.wikipedia.org/wiki/BMP_file_format
        output.push(B);
        output.push(M);
        output.extend(&total_size.to_le_bytes()); // size of the bmp
        output.extend(&0u16.to_le_bytes()); // creator1
        output.extend(&0u16.to_le_bytes()); // creator2
        output.extend(&header_size.to_le_bytes()); // pixel offset
        output.extend(&40u32.to_le_bytes()); // dib header size
        output.extend(&(self.width as u32).to_le_bytes()); // width
        output.extend(&(self.height as u32).to_le_bytes()); // height
        output.extend(&1u16.to_le_bytes()); // planes
        output.extend(&1u16.to_le_bytes()); // bitsperpixel
        output.extend(&0u32.to_le_bytes()); // no compression
        output.extend(&data_size.to_le_bytes()); // size of the raw bitmap data with padding
        output.extend(&2835u32.to_le_bytes()); // hres
        output.extend(&2835u32.to_le_bytes()); // vres
        output.extend(&2u32.to_le_bytes()); // num_colors
        output.extend(&2u32.to_le_bytes()); // num_imp_colors

        // color_pallet
        output.extend(&0x00_FF_FF_FFu32.to_le_bytes());
        output.extend(&0x00_00_00_00u32.to_le_bytes());

        output
    }

    /// return bytes needed for `width` bits
    fn bytes_per_row(&self) -> u32 {
        (self.width + 7) / 8
    }

    /// return the padding
    fn padding(&self) -> u32 {
        (4 - self.bytes_per_row() % 4) % 4
    }
}

#[cfg(test)]
mod test {
    use crate::*;

    #[test]
    fn test_data_matrix() {
        assert!(DataMatrix::new(vec![], 1).is_err());
        assert!(DataMatrix::new(vec![true], 1).is_ok());
        assert!(DataMatrix::new(vec![true], 2).is_err());
        assert!(DataMatrix::new(vec![true, false], 2).is_ok());
        assert!(DataMatrix::new(vec![true, false], 1).is_ok());
        assert!(DataMatrix::new(vec![true, false, true], 1).is_ok());
        assert!(DataMatrix::new(vec![true, false, true], 2).is_err());
    }

    #[test]
    fn test_padding() {
        let mut header = BmpHeader {
            height: 0,
            width: 0,
        };
        assert_eq!(header.padding(), 0);

        header.width = 1;
        assert_eq!(header.padding(), 3);

        header.width = 9;
        assert_eq!(header.padding(), 2);

        header.width = 17;
        assert_eq!(header.padding(), 1);

        header.width = 25;
        assert_eq!(header.padding(), 0);
    }

    #[test]
    fn test_bytes_per_row() {
        let mut header = BmpHeader {
            height: 0,
            width: 0,
        };
        assert_eq!(header.bytes_per_row(), 0);

        header.width = 1;
        assert_eq!(header.bytes_per_row(), 1);

        header.width = 8;
        assert_eq!(header.bytes_per_row(), 1);

        header.width = 9;
        assert_eq!(header.bytes_per_row(), 2);
    }

    #[test]
    fn test_mul() {
        let data = DataMatrix {
            data: vec![false, true, false, true],
            width: 2,
        };

        let data_bigger = DataMatrix {
            data: vec![
                false, false, true, true, false, false, true, true, false, false, true, true,
                false, false, true, true,
            ],
            width: 4,
        };

        assert_eq!(data.mul(2), data_bigger);
    }

    #[test]
    fn test_add() {
        let data = DataMatrix {
            data: vec![false],
            width: 1,
        };

        let data_bigger = DataMatrix {
            data: vec![false; 25],
            width: 5,
        };

        assert_eq!(data.add_whitespace(2), data_bigger);
    }

    #[test]
    fn test_bmp() {
        let data_test1 = DataMatrix {
            data: vec![false, true, true, false],
            width: 2,
        };
        let bmp_test1 = data_test1.bmp().unwrap();
        let bytes_test1 = include_bytes!("../test_bmp/test1.bmp").to_vec();
        assert_eq!(bmp_test1, bytes_test1);

        let bmp_test2 = data_test1.mul(3).add_whitespace(12).bmp().unwrap();
        let bytes_test2 = include_bytes!("../test_bmp/test2.bmp").to_vec();
        assert_eq!(bmp_test2, bytes_test2);
    }

    /*
    #[test]
    fn test_monochrome_image() {
        // taken from https://github.com/pertbanking/bitmap-monochrome/blob/master/monochrome_image.bmp
        let data = DataMatrix {
            data: vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
                       0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
                       0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0,
                       0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
                       0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0,
                       0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0,
                       0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0,
                       0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
                       0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0,
                       0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0,
                       0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
                       0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0,
                       0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
                       0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
                       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                       0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].iter().map(|e| *e==0).collect(),
            width: 18,
        };
        let bmp_test = data.bmp().unwrap();
        let mut file = File::create("test_bmp/monochrome_image_lib.bmp").unwrap();
        file.write_all(&bmp_test).unwrap();
        let bytes_test = include_bytes!("../test_bmp/monochrome_image.bmp").to_vec();
        let bytes_padding = include_bytes!("../test_bmp/monochrome_image_padding.bmp").to_vec();
        let bytes_final: Vec<u8> = bytes_test.iter().zip(bytes_padding).map(|e| e.0 & e.1).collect();
        assert_eq!(bmp_test, bytes_final);
    }
    */
}