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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
//! # bmp_rs
//!
//! A bitmap file decoder for Microsoft *bmp* files.
//!
//! ## Example
//!
//! ```rust,no_run
//! use std::fs::File;
//! use bmp_rs::{
//!     Result,
//!     Builder,
//! };
//!
//! struct ImageBuilder {
//!     // Your builder type that is able to construct an image
//! }
//!
//! struct Image {
//!     // Your image type that represents a bitmap
//! }
//!
//! impl Builder for ImageBuilder {
//!     type TResult = Image; // Your image type
//!
//!     fn set_size( &mut self, width: u32, height: u32 ) {
//!         // Set image size
//!     }
//!
//!     fn set_pixel( &mut self, x: u32, y: u32, r: u8, g: u8, b: u8, a: u8 ) {
//!         // Set a specific pixel within that image to the given color
//!     }
//!
//!     fn build( &mut self ) -> Result<Self::TResult> {
//!         // Build and return your final image
//!         Ok ( Image { } )
//!     }
//! }
//!
//! fn main() {
//!     let mut file = File::open( "image.bmp" ).unwrap();
//!     let image = bmp_rs::decode( &mut file, ImageBuilder { } );
//!     // Do something with your image
//! }
//! ```
//!
extern crate byteorder;

use std::io;
use std::error;
use std::fmt;

use byteorder::{
    ReadBytesExt,
    LittleEndian,
    BigEndian,
};

#[derive( Debug )]
pub enum DecodingError {
    IOError( io::Error ),
}

pub type Result<TResult> = std::result::Result<TResult, DecodingError>;

impl DecodingError {
    fn new_io( message : &str ) -> DecodingError {
        DecodingError::IOError(
            io::Error::new( io::ErrorKind::InvalidData, message ) )
    }
}

impl fmt::Display for DecodingError {
    fn fmt( &self, formatter: &mut fmt::Formatter ) -> fmt::Result {
        match *self {
            DecodingError::IOError( ref error )
                => write!( formatter, "IO error: {}", *error ),
        }
    }
}

impl error::Error for DecodingError {
    fn description( &self ) -> &str {
        match *self {
            DecodingError::IOError( ref error ) => error.description(),
        }
    }

    fn cause( &self ) -> Option<&error::Error> {
        match *self {
            DecodingError::IOError( ref error ) => Some( error ),
        }
    }
}

impl From<io::Error> for DecodingError {
    fn from( error: io::Error ) -> Self {
        DecodingError::IOError( error )
    }
}

#[derive( Clone, Copy )]
struct Color {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
}

pub trait Builder {
    type TResult;

    fn set_size( &mut self, width: u32, height: u32 );
    fn set_pixel( &mut self, x: u32, y: u32, r: u8, g: u8, b: u8, a: u8 );
    fn build( &mut self ) -> Result<Self::TResult>;
}

const MSVERSION2_SIZE: isize = 12;
const MSVERSION3_SIZE: isize = 40;
const MSVERSION4_SIZE: isize = 108;
const MSVERSION5_SIZE: isize = 124;

#[derive( PartialEq, Eq, Clone, Copy )]
enum Version {
    Microsoft2 = MSVERSION2_SIZE,
    Microsoft3 = MSVERSION3_SIZE,
    Microsoft4 = MSVERSION4_SIZE,
    Microsoft5 = MSVERSION5_SIZE,
}

impl Version { // TODO: Replace with TryFrom when available.
    fn from_isize( size: isize ) -> Result<Version> {
        match size {
            MSVERSION2_SIZE => Ok( Version::Microsoft2 ),
            MSVERSION3_SIZE => Ok( Version::Microsoft3 ),
            MSVERSION4_SIZE => Ok( Version::Microsoft4 ),
            MSVERSION5_SIZE => Ok( Version::Microsoft5 ),
            _ => Err( DecodingError::new_io(
                    &format!( "Invalid bitmap header {},", size ) ) ),
        }
    }
}

struct Core {
    width: u32,
    height: u32,
    bpp: u32,
    planes: u16,
    bottom_up: bool,
}

impl Core {
    fn from_buffer( buf: &[u8], version: Version ) -> Result<Core> {
        let mut cursor = io::Cursor::new( buf );

        let ( width, height ) =
            match version {
                Version::Microsoft2 => {
                    let mut dimension: [i16; 2] = [0; 2];
                    cursor.read_i16_into::<LittleEndian>( &mut dimension )?;

                    ( dimension[0] as i32, dimension[1] as i32 )
                },
                _ => {
                    let mut dimension: [i32; 2] = [0; 2];
                    cursor.read_i32_into::<LittleEndian>( &mut dimension )?;

                    ( dimension[0], dimension[1] )
                },
            };

        let bottom_up = if height.signum() == 1 { true } else { false };
        let width = width.checked_abs()
            .ok_or( DecodingError::new_io( "Invalid width." ) )? as u32;
        let height = height.checked_abs()
            .ok_or( DecodingError::new_io( "Invalid height." ) )? as u32;

        let planes = cursor.read_u16::<LittleEndian>()?;
        if planes != 1 {
            return Err( DecodingError::new_io(
                &format!( "Invalid number of color planes {}.", planes ) ) );
        }

        let bpp = cursor.read_u16::<LittleEndian>()? as u32;
        match bpp {
            1 | 4 | 8 | 16 | 24 | 32 => {
                if version == Version::Microsoft2
                    && ( bpp == 16 || bpp == 32 ) {

                    return Err( DecodingError::new_io(
                        &format!( "Invalid bits per pixel {}.", bpp ) ) );
                }
            },
            _ => return Err( DecodingError::new_io(
                &format!( "Invalid bits per pixel {}.", bpp ) ) ),
        }

        Ok( Core { width, height, bpp, planes, bottom_up } )
    }
}

struct Palette {
    colors: Vec<Color>,
}

impl Palette {
    fn from_buffer(
        buf: &[u8], size: usize, color_size: usize, has_alpha: bool ) -> Result<Palette> {

        let iter = buf.chunks( color_size );
        let mut colors = Vec::with_capacity( size );

        for x in iter {
            let a = match has_alpha {
                true => x[3],
                false => 255,
            };

            colors.push(
                Color {
                    b: x[0],
                    g: x[1],
                    r: x[2],
                    a,
                } );
        }
        Ok( Palette { colors } )
    }
}

#[derive( PartialEq, Eq, Clone, Copy )]
enum Compression {
    RLE8Bit = 1,
    RLE4Bit = 2,
    Bitfield = 3,
}

struct Info {
    compression: Option<Compression>,
    image_size: u32,
    ppm_x: i32,
    ppm_y: i32,
    used_colors: u32,
    important_colors: u32,
}

impl Info {
    fn from_buffer( buf: &[u8], bpp: u32 ) -> Result<Info> {
        let mut cursor = io::Cursor::new( buf );

        let compression = match cursor.read_u32::<LittleEndian>()? {
            0 => None,
            1 if bpp == 8 => Some( Compression::RLE8Bit ),
            2 if bpp == 4 => Some( Compression::RLE4Bit ),
            3 if bpp == 16 || bpp == 32 => Some( Compression::Bitfield ),
            v @ _ => return Err( DecodingError::new_io(
                &format!( "Invalid compression {} for {}-bit", v, bpp ) ) ),
        };

        let image_size = cursor.read_u32::<LittleEndian>()?;
        let ppm_x = cursor.read_i32::<LittleEndian>()?;
        let ppm_y = cursor.read_i32::<LittleEndian>()?;
        let used_colors = cursor.read_u32::<LittleEndian>()?;
        let important_colors = cursor.read_u32::<LittleEndian>()?;

        Ok ( Info {
            compression,
            image_size,
            ppm_x,
            ppm_y,
            used_colors,
            important_colors,
        } )
    }
}

struct BitfieldMask {
    red: u32,
    green: u32,
    blue: u32,
    alpha: u32,
}

impl BitfieldMask {
    fn from_buffer( buf: &[u8], has_alpha: bool ) -> Result<BitfieldMask> {
        let mut cursor = io::Cursor::new( buf );

        let red = cursor.read_u32::<LittleEndian>()?;
        let green = cursor.read_u32::<LittleEndian>()?;
        let blue = cursor.read_u32::<LittleEndian>()?;
        let alpha = match has_alpha {
            true => cursor.read_u32::<LittleEndian>()?,
            false => 0,
        };

        Ok( BitfieldMask { red, green, blue, alpha } )
    }
}

struct BMPExtra {
    color_space_type: u32,
    red_x: i32,
    red_y: i32,
    red_z: i32,
    green_x: i32,
    green_y: i32,
    green_z: i32,
    blue_x: i32,
    blue_y: i32,
    blue_z: i32,
    gamma_red: u32,
    gamma_green: u32,
    gamma_blue: u32,
}

impl BMPExtra {
    fn from_buffer( buf: &[u8] ) -> Result<BMPExtra> {
        let mut cursor = io::Cursor::new( buf );

        let color_space_type = cursor.read_u32::<LittleEndian>()?;
        let red_x = cursor.read_i32::<LittleEndian>()?;
        let red_y = cursor.read_i32::<LittleEndian>()?;
        let red_z = cursor.read_i32::<LittleEndian>()?;
        let green_x = cursor.read_i32::<LittleEndian>()?;
        let green_y = cursor.read_i32::<LittleEndian>()?;
        let green_z = cursor.read_i32::<LittleEndian>()?;
        let blue_x = cursor.read_i32::<LittleEndian>()?;
        let blue_y = cursor.read_i32::<LittleEndian>()?;
        let blue_z = cursor.read_i32::<LittleEndian>()?;
        let gamma_red = cursor.read_u32::<LittleEndian>()?;
        let gamma_green = cursor.read_u32::<LittleEndian>()?;
        let gamma_blue = cursor.read_u32::<LittleEndian>()?;

        Ok( BMPExtra {
            color_space_type,
            red_x,
            red_y,
            red_z,
            green_x,
            green_y,
            green_z,
            blue_x,
            blue_y,
            blue_z,
            gamma_red,
            gamma_green,
            gamma_blue,
        } )
    }
}

struct BMPProfile {
    intent: u32,
    data: u32,
    size: u32,
    reserved: u32,
}

impl BMPProfile {
    fn from_buffer( buf: &[u8] ) -> Result<BMPProfile> {
        let mut cursor = io::Cursor::new( buf );

        let intent = cursor.read_u32::<LittleEndian>()?;
        let data = cursor.read_u32::<LittleEndian>()?;
        let size = cursor.read_u32::<LittleEndian>()?;
        let reserved = cursor.read_u32::<LittleEndian>()?;

        Ok( BMPProfile {
            intent,
            data,
            size,
            reserved,
        } )
    }
}

struct Header {
    version: Version,
    core: Core,
    info: Option<Info>,
    palette: Option<Palette>,
    bitmask: Option<BitfieldMask>,
    extra: Option<BMPExtra>,
    profile: Option<BMPProfile>,
}

impl Header {
    fn from_reader( input: &mut io::Read ) -> Result<Header> {
        let version = Version::from_isize(
            input.read_u32::<LittleEndian>()? as isize )?;

        // Read core header
        let mut buffer = vec![0; ( version as usize ) - 4];

        input.read_exact( &mut buffer )?;

        let core = Core::from_buffer( &buffer, version )?;

        // Read Info header
        let info = match version {
            Version::Microsoft2 => None,
            _ => Some( Info::from_buffer( &buffer[12..], core.bpp )? ),
        };

        // Read the Bitmask
        let bitmask = match info {
            Some( ref i ) => match i.compression {
                Some( Compression::Bitfield ) => {
                    if version == Version::Microsoft3 {
                        // The bitmask needs to be read from the buffer
                        let mut mask_buffer = vec![0; 12];
                        input.read_exact( &mut mask_buffer )?;

                        Some( BitfieldMask::from_buffer( &mask_buffer, false )? )
                    } else {
                        // The bitmask is part of the header buffer
                        Some( BitfieldMask::from_buffer( &buffer[36..], true )? ) // 36
                    }
                },
                _ if core.bpp == 16 => {
                    // Default 16-bit mask
                    Some( BitfieldMask {
                        red: 0x7C00,
                        green: 0x3E0,
                        blue: 0x1F,
                        alpha: 0x00,
                    } )
                },
                _ if core.bpp == 32 => {
                    // Default 32-bit mask
                    Some( BitfieldMask {
                        red: 0xFF0000,
                        green: 0xFF00,
                        blue: 0xFF,
                        alpha: 0x00,
                    } )
                },
                _ => None,
            },
            _ => None,
        };

        // Read Extra header
        let extra = match version {
            Version::Microsoft4
            | Version::Microsoft5
                => Some( BMPExtra::from_buffer( &buffer[52..] )? ), // 52
            _ => None,
        };

        // Read profile header
        let profile = match version {
            Version::Microsoft5
                => Some( BMPProfile::from_buffer( &buffer[104..] )? ),
            _ => None,
        };

        // Read palette
        let palette_size = match info {
            Some( ref i ) if i.used_colors == 0 && core.bpp < 16 => 1 << core.bpp,
            Some( ref i ) => i.used_colors,
            None => 1 << core.bpp,
        };

        // TODO: Check if the size is sensible with the bitmap offset

        let palette = if palette_size > 0 {
            let palette_size = palette_size as usize;
            let color_size = match version {
                Version::Microsoft2 => 3,
                _ => 4,
            } as usize;

            let mut buffer = vec![0; palette_size * color_size];
            input.read_exact( &mut buffer )?;

            // TODO: Last parameter indicated real alpha values in the bitmap.
            // I have yet to find one where this is anything else than 0.
            Some( Palette::from_buffer( &buffer, palette_size, color_size, false )? )
        } else {
            None
        };

        Ok ( Header {
            version,
            core,
            info,
            palette,
            bitmask,
            extra,
            profile,
        } )
    }
}

fn decode_1bpp<TBuilder: Builder>(
    width: u32, row: u32, buf: &[u8], palette: &[Color], mask: &BitfieldMask, builder: &mut TBuilder ) {

    let mut x: u32 = 0;

    for byte in buf {
        for bit in (0..8).rev() {

            let color: Color = palette[ ( ( *byte >> bit ) & 0x01 ) as usize ];
            builder.set_pixel( x, row, color.r, color.g, color.b, color.a );

            x += 1;
            if x >= width {
                return;
            }
        }
    }
}

fn decode_4bpp<TBuilder: Builder>(
    width: u32, row: u32, buf: &[u8], palette: &[Color], mask: &BitfieldMask, builder: &mut TBuilder ) {

    let mut x: u32 = 0;

    for byte in buf {
        let color = palette[ ( ( *byte >> 4 ) & 0x0F ) as usize ];
        builder.set_pixel( x, row, color.r, color.g, color.b, color.a );

        x += 1;
        if x >= width {
            break;
        }

        let color = palette[ ( *byte & 0x0F ) as usize ];
        builder.set_pixel( x, row, color.r, color.g, color.b, color.a );

        x += 1;
        if x >= width {
            break;
        }
    }
}

fn decode_8bpp<TBuilder: Builder>(
    width: u32, row: u32, buf: &[u8], palette: &[Color], _mask: &BitfieldMask, builder: &mut TBuilder ) {

    let mut x: u32 = 0;

    for byte in buf {
        let color = palette[ *byte as usize ];
        builder.set_pixel( x, row, color.r, color.g, color.b, color.a );

        x += 1;
        if x >= width {
            break;
        }
    }
}

fn clamp8bit( value: u32, mask: u32, shr_count: u32, mask_max: u32, default: u8 ) -> u8 {
    match mask_max {
        0 => default,
        max @ _ => ( ( 255 * ( ( value & mask ) >> shr_count ) ) / max ) as u8,
    }
}

fn decode_16bpp<TBuilder: Builder>(
    width: u32, row: u32, buf: &[u8], palette: &[Color], mask: &BitfieldMask, builder: &mut TBuilder ) {

    let mut x: u32 = 0;

    let alpha_shift = mask.alpha.trailing_zeros();
    let red_shift = mask.red.trailing_zeros();
    let green_shift = mask.green.trailing_zeros();
    let blue_shift = mask.blue.trailing_zeros();

    let alpha_max = mask.alpha.checked_shr( alpha_shift ).unwrap_or( 0 );
    let red_max = mask.red.checked_shr( red_shift ).unwrap_or( 0 );
    let green_max = mask.green.checked_shr( green_shift ).unwrap_or( 0 );
    let blue_max = mask.blue.checked_shr( blue_shift ).unwrap_or( 0 );

    for mut bytes in buf.chunks( 2 ) {
        let color = bytes.read_u16::<LittleEndian>().unwrap() as u32;

        builder.set_pixel(
            x,
            row,
            clamp8bit( color, mask.red, red_shift, red_max, 0 ),
            clamp8bit( color, mask.green, green_shift, green_max, 0 ),
            clamp8bit( color, mask.blue, blue_shift, blue_max, 0 ),
            clamp8bit( color, mask.alpha, alpha_shift, alpha_max, 255 ) );

        x += 1;
        if x >= width {
            break;
        }
    }
}

fn decode_24bpp<TBuilder: Builder>(
    width: u32, row: u32, buf: &[u8], palette: &[Color], mask: &BitfieldMask, builder: &mut TBuilder ) {

    let mut x: u32 = 0;

    for bytes in buf.chunks( 3 ) {
        builder.set_pixel( x, row, bytes[2], bytes[1], bytes[0], 255 );

        x += 1;
        if x >= width {
            break;
        }
    }
}

fn decode_32bpp<TBuilder: Builder>(
    width: u32, row: u32, buf: &[u8], palette: &[Color], mask: &BitfieldMask, builder: &mut TBuilder ) {

    let mut x: u32 = 0;

    let alpha_shift = mask.red.trailing_zeros();
    let red_shift = mask.red.trailing_zeros();
    let green_shift = mask.green.trailing_zeros();
    let blue_shift = mask.blue.trailing_zeros();

    let alpha_max = mask.alpha.checked_shr( alpha_shift ).unwrap_or( 0 );
    let red_max = mask.red.checked_shr( red_shift ).unwrap_or( 0 );
    let green_max = mask.green.checked_shr( green_shift ).unwrap_or( 0 );
    let blue_max = mask.blue.checked_shr( blue_shift ).unwrap_or( 0 );

    for mut bytes in buf.chunks( 4 ) {
        let color = bytes.read_u32::<LittleEndian>().unwrap() as u32;

        builder.set_pixel(
            x,
            row,
            clamp8bit( color, mask.red, red_shift, red_max, 0 ),
            clamp8bit( color, mask.green, green_shift, green_max, 0 ),
            clamp8bit( color, mask.blue, blue_shift, blue_max, 0 ),
            clamp8bit( color, mask.alpha, alpha_shift, alpha_max, 255 ) );

        x += 1;
        if x >= width {
            break;
        }
    }
}

fn decode_nothing<TBuilder: Builder>(
    _: u32, _: u32, _: &[u8], _: &[Color], _: &BitfieldMask, _: &mut TBuilder ) {
    // no-op
}

pub fn decode<TBuilder: Builder>(
    input: &mut io::Read, mut builder: TBuilder ) -> Result<TBuilder::TResult> {

    // Read file header
    let mut header: [u8; 14] = [0; 14];
    input.read_exact( &mut header )?;

    let mut cursor = io::Cursor::new( header );
    if header[0] != 0x42 && header[1] != 0x4D {
        return Err( DecodingError::new_io( "Invalid bitmap file." ) );
    }

    // TODO: Make sensible decisions about ridiculous big files

    cursor.set_position( 10 );
    let _ = cursor.read_u32::<LittleEndian>()?; // Offset

    // TODO: Make sensible decisions about the offset to the pixel data

    // Read bitmap header
    let header = Header::from_reader( input )?;

    // Set output size
    builder.set_size( header.core.width, header.core.height );

    // Read pixel data
    let size = ( ( header.core.width * header.core.bpp + 31 ) / 32 ) * 4;
    let mut buffer = vec![0; size as usize];
    let width = header.core.width;
    let height = header.core.height;
    let bpp = header.core.bpp;
    let info = header.info;
    let compression = match header.version {
        Version::Microsoft2 => false,
        _ if match info {
            Some( ref i ) => if i.compression.is_some() { true } else { false },
            None => false,
        } => true,
        _ => false,
    };
    let palette = match bpp {
        1 | 4 | 8 => header.palette.unwrap().colors,
        _ => Vec::new(),
    };

    let mask = match bpp {
        16 | 32 => header.bitmask.unwrap(),
        _ => BitfieldMask { red: 0, green: 0, blue: 0, alpha: 0 }
    };

    let decode_row = match bpp {
        1 => decode_1bpp::<TBuilder>,
        4 if !compression => decode_4bpp::<TBuilder>,
        8 if !compression => decode_8bpp::<TBuilder>,
        16 => decode_16bpp::<TBuilder>,
        24 => decode_24bpp::<TBuilder>,
        32 => decode_32bpp::<TBuilder>,
        _ => decode_nothing::<TBuilder>,
    };

    if bpp == 8 && compression {
        let count = info.unwrap().image_size as usize;
        if count == 0 {
            panic!( "Image size in bytes can't be null when using RLE8 compression" );
        }
        let mut buffer = vec![0; count];
        input.read_exact( &mut buffer )?;
        let buffer = buffer;

        let mut x: u32 = 0;
        let mut y: u32 = if header.core.bottom_up { height - 1 } else { 0 };
        let mut index: usize = 0;
        let row_mod: i32 = if header.core.bottom_up { -1 } else { 1 };

        loop {
            if index >= count {
                break;
            }

            let first = buffer[ index ] as usize;
            let second = buffer[ index + 1 ] as usize;
            index += 2;

            if first == 0 {
                if second == 0 {
                    x = 0;
                    y = ( ( y as i32 ) + row_mod ) as u32;

                } else if second == 1 {
                    break;

                } else if second == 2 {
                    let dx = buffer[ index ] as u32;
                    let dy = buffer[ index + 1 ] as i32 * row_mod;
                    index += 2;

                    x += dx;
                    y = ( y as i32 + dy ) as u32;

                } else {
                    for _ in 0..second {
                        if x >= width {
                            x = 0;
                            y = ( ( y as i32 ) + row_mod ) as u32;
                        }

                        let color = palette[ buffer[ index ] as usize ];

                        builder.set_pixel( x, y, color.r, color.g, color.b, color.a );
                        x += 1;

                        index += 1;
                        if index >= count {
                            break;
                        }
                    }
                    index += match second % 2 {
                        0 => 0,
                        _ => 1,
                    };
                }

            } else {
                let color = palette[ second ];

                for _ in 0..first {
                    if x >= width {
                        x = 0;
                        y = ( ( y as i32 ) + row_mod ) as u32;
                    }

                    builder.set_pixel( x, y, color.r, color.g, color.b, color.a );
                    x += 1;
                }
            }
        }

    } else if bpp == 4 && compression {
        let count = info.unwrap().image_size as usize;
        if count == 0 {
            panic!( "Image size in bytes can't be null when using RLE4 compression" );
        }
        let mut buffer = vec![0; count];
        input.read_exact( &mut buffer )?;
        let buffer = buffer;

        let mut x: u32 = 0;
        let mut y: u32 = if header.core.bottom_up { height - 1 } else { 0 };
        let mut index: usize = 0;
        let row_mod: i32 = if header.core.bottom_up { -1 } else { 1 };

        loop {
            if index >= count {
                break;
            }
            let first = buffer[ index ] as usize;
            let second = buffer[ index + 1 ];
            index += 2;

            if first == 0 {
                if second == 0 {
                    x = 0;
                    y = ( ( y as i32 ) + row_mod ) as u32;

                } else if second == 1 {
                    break;

                } else if second == 2 {
                    let dx = buffer[ index ] as u32;
                    let dy = buffer[ index + 1 ] as i32 * row_mod;
                    index += 2;

                    x += dx;
                    y = ( y as i32 + dy ) as u32;

                } else {
                    let even = second % 2 == 0;
                    let second_len = if !even {
                        second as usize + 1
                    } else {
                        second as usize
                    } / 2;

                    for i in 0..second_len {
                        if x >= width {
                            x = 0;
                            y = ( ( y as i32 ) + row_mod ) as u32;
                        }

                        let byte = buffer[ index ];
                        let color = palette[ ( ( byte >> 4 ) & 0x0F ) as usize ];

                        builder.set_pixel( x, y, color.r, color.g, color.b, color.a );
                        x += 1;

                        if i < second_len - 1 {
                            if x >= width {
                                x = 0;
                                y = ( ( y as i32 ) + row_mod ) as u32;
                            }

                            let color = palette[ ( byte & 0x0F ) as usize ];

                            builder.set_pixel( x, y, color.r, color.g, color.b, color.a );
                            x += 1;
                        } else if even {
                            if x >= width {
                                x = 0;
                                y = ( ( y as i32 ) + row_mod ) as u32;
                            }

                            let color = palette[ ( byte & 0x0F ) as usize ];

                            builder.set_pixel( x, y, color.r, color.g, color.b, color.a );
                            x += 1;
                        }

                        index += 1;
                        if index >= count {
                            break;
                        }
                    }
                    index += match second_len % 2 {
                        0 => 0,
                        _ => 1,
                    };
                }

            } else {
                let color1 = palette[ ( ( second >> 4 ) & 0x0F ) as usize ];
                let color2 = palette[ ( second & 0x0F ) as usize ];

                let mut control = false;
                for _ in 0..first {
                    let color = match control {
                        true => {
                            control = false;
                            &color2
                        },
                        false => {
                            control = true;
                            &color1
                        },
                    };

                    if x >= width {
                        x = 0;
                        y = ( ( y as i32 ) + row_mod ) as u32;
                    }

                    builder.set_pixel( x, y, color.r, color.g, color.b, color.a );
                    x += 1;
                }
            }
        }
    } else {
        for y in 0..height {
            input.read_exact( &mut buffer )?;

            let row = if header.core.bottom_up { height - y - 1 } else { y };

            decode_row( width, row, &buffer, &palette, &mask, &mut builder );
        }
    }

    builder.build()
}

#[cfg( test )]
mod tests {
    use std::io;
    use std::error::Error;

    use super::DecodingError;

    #[test]
    fn decoding_error_new_io_test() {
        let error = DecodingError::new_io( "This is an error!" );
        let io_error = io::Error::new(
            io::ErrorKind::InvalidData, "This is an error!" );

        match error {
            DecodingError::IOError( error ) => {
                assert_eq!(
                    error.description(),
                    io_error.description() );
                assert_eq!(
                    error.kind(),
                    io_error.kind() );
            },
            _ => panic!( "No IO Error" ),
        }
    }

    #[test]
    fn decoding_error_fmt_test() {
        let error = DecodingError::new_io( "FooBar!" );

        assert_eq!( "IO error: FooBar!", format!( "{}", error ) );
    }

    #[test]
    fn decoding_error_from_io_error_test() {
        let io_error = io::Error::new(
            io::ErrorKind::InvalidData, "This is an error!" );

        let error : DecodingError = io_error.into();

        assert!( match error {
            DecodingError::IOError( error ) => true,
            _ => false,
        } );
    }
}