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
use std::io::{ self, Read };
use std::{ cmp, str };
use std::iter::repeat;
use num::FromPrimitive;
use byteorder::{ ReadBytesExt, BigEndian };
use num::range_step;

use image::{
    DecodingResult,
    ImageResult,
    ImageDecoder,
    ImageError
};
use color::{ self, ColorType };

use super::filter::unfilter;
use super::hash::Crc32;
use super::zlib::ZlibDecoder;

use utils::expand_packed;


pub static PNGSIGNATURE: [u8; 8] = [137, 80, 78, 71, 13, 10, 26, 10];


#[derive(Clone, Copy, PartialEq)]
enum PNGState {
    Start,
    HaveSignature,
    HaveIHDR,
    HavePLTE,
    HaveFirstIDat,
    #[allow(dead_code)]
    HaveLastIDat,
    #[allow(dead_code)]
    HaveIEND
}

enum_from_primitive! {
#[derive(Clone, Copy, Debug, PartialEq)]
enum InterlaceMethod {
    None = 0,
    Adam7 = 1
}
}

/// This iterator iterates over the different passes of an image Adam7 encoded
/// PNG image
/// The glorious pattern is:
///     16462646
///     77777777
///     56565656
///     77777777
///     36463646
///     77777777
///     56565656
///     77777777
///
#[derive(Clone, Copy)]
struct Adam7Iterator {
    line: u32,
    lines: u32,
    line_width: u32,
    current_pass: u8,
    width: u32,
    height: u32,
}

impl Adam7Iterator {
    pub fn new(width: u32, height: u32) -> Adam7Iterator {
        let mut this = Adam7Iterator {
            line: 0,
            lines: 0,
            line_width: 0,
            current_pass: 1,
            width: width,
            height: height
        };
        this.init_pass();
        this
    }

    /// Calculates the bounds of the current pass
    fn init_pass(&mut self) {
        let w = self.width as f64;
        let h = self.height as f64;
        let (line_width, lines) = match self.current_pass {
            1 => (w/8.0, h/8.0),
            2 => ((w-4.0)/8.0, h/8.0),
            3 => (w/4.0, (h-4.0)/8.0),
            4 => ((w-2.0)/4.0, h/4.0),
            5 => (w/2.0, (h-2.0)/4.0),
            6 => ((w-1.0)/2.0, h/2.0),
            7 => (w, (h-1.0)/2.0),
            _ => unreachable!()
        };
        self.line_width = line_width.ceil() as u32;
        self.lines = lines.ceil() as u32;
        self.line = 0;
    }
}

/// Iterates over the (passes, lines, widths)
impl Iterator for Adam7Iterator {
    type Item = (u8, u32, u32);
    fn next(&mut self) -> Option<(u8, u32, u32)> {
        if self.line < self.lines {
            let this_line = self.line;
            self.line += 1;
            Some((self.current_pass, this_line, self.line_width))
        } else if self.current_pass < 7 {
            self.current_pass += 1;
            self.init_pass();
            self.next()
        } else {
            None
        }
    }
}


/// The representation of a PNG decoder
///
/// Currently does not support decoding of interlaced images
pub struct PNGDecoder<R> {
    z: ZlibDecoder<IDATReader<R>>,
    crc: Crc32,
    previous: Vec<u8>,
    state: PNGState,

    width: u32,
    height: u32,

    bit_depth: u8,
    data_color_type: u8,
    data_pixel_type: ColorType,

    palette: Option<Vec<(u8, u8, u8)>>,
    trns: Option<Vec<u8>>,

    interlace_method: InterlaceMethod,
    pass_iterator: Option<Adam7Iterator>,

    chunk_length: u32,
    chunk_type: Vec<u8>,

    bpp: u8,
    bits_per_pixel: u8,
    decoded_rows: u32,
}

impl<R: Read> PNGDecoder<R> {
    /// Create a new decoder that decodes from the stream ```r```
    pub fn new(r: R) -> PNGDecoder<R> {
        let idat_reader = IDATReader::new(r);

        PNGDecoder {
            previous: Vec::new(),
            state: PNGState::Start,
            z: ZlibDecoder::new(idat_reader),
            crc: Crc32::new(),

            width: 0,
            height: 0,

            bit_depth: 0,
            data_color_type: 0,
            data_pixel_type: ColorType::Gray(1),

            palette: None,
            trns: None,

            interlace_method: InterlaceMethod::None,
            pass_iterator: None,

            chunk_length: 0,
            chunk_type: Vec::new(),

            bpp: 0,
            bits_per_pixel: 0,
            decoded_rows: 0,
        }
    }

    /// Returns a reference to the color palette used for indexed
    /// color images.
    /// Each array element is a tuple of RGB values.
    pub fn palette <'a>(&'a self) -> &'a [(u8, u8, u8)] {
        match self.palette {
            Some(ref p) => &p,
            None        => &[]
        }
    }

    fn read_signature(&mut self) -> ImageResult<bool> {
        let mut png = Vec::with_capacity(8);
        try!(self.z.inner().r.by_ref().take(8).read_to_end(&mut png));

        Ok(&png == &PNGSIGNATURE)
    }

    fn parse_ihdr(&mut self, buf: Vec<u8>) -> ImageResult<()> {
        self.crc.update(&*buf);
        let mut m = io::Cursor::new(buf);

        self.width = try!(m.read_u32::<BigEndian>());
        self.height = try!(m.read_u32::<BigEndian>());

        self.bit_depth = try!(try!(m.by_ref().bytes().next().ok_or(ImageError::ImageEnd)));
        self.data_color_type = try!(try!(m.by_ref().bytes().next().ok_or(ImageError::ImageEnd)));

        self.data_pixel_type = match (self.data_color_type, self.bit_depth) {
            (0, 1)  => ColorType::Gray(1),
            (0, 2)  => ColorType::Gray(2),
            (0, 4)  => ColorType::Gray(4),
            (0, 8)  => ColorType::Gray(8),
            (0, 16) => ColorType::Gray(16),
            (2, 8)  => ColorType::RGB(8),
            (2, 16) => ColorType::RGB(16),
            (3, 1)  => ColorType::RGB(8),
            (3, 2)  => ColorType::RGB(8),
            (3, 4)  => ColorType::RGB(8),
            (3, 8)  => ColorType::RGB(8),
            (4, 8)  => ColorType::GrayA(8),
            (4, 16) => ColorType::GrayA(16),
            (6, 8)  => ColorType::RGBA(8),
            (6, 16) => ColorType::RGBA(16),
            (_, _)  => return Err(ImageError::FormatError(
                "Invalid color/bit depth combination.".to_string()
            ))
        };

        let compression_method = try!(try!(m.by_ref().bytes().next().ok_or(ImageError::ImageEnd)));
        if compression_method != 0 {
            return Err(ImageError::UnsupportedError(format!(
                "The compression method {} is not supported.",
                compression_method
            )))
        }

        let filter_method = try!(try!(m.by_ref().bytes().next().ok_or(ImageError::ImageEnd)));
        if filter_method != 0 {
            return Err(ImageError::UnsupportedError(format!(
                "The filter method {} is not supported.",
                filter_method
            )))
        }

        self.interlace_method = match FromPrimitive::from_u8(try!(try!(m.by_ref().bytes().next().ok_or(ImageError::ImageEnd)))) {
            Some(method) => method,
            None => return Err(ImageError::UnsupportedError(
                "Unsupported interlace method.".to_string()
            ))
        };
        if self.interlace_method == InterlaceMethod::Adam7 {
            self.pass_iterator = Some(Adam7Iterator::new(self.width, self.height))
        }

        let channels = match self.data_color_type {
            0 => 1,
            2 => 3,
            3 => 1,
            4 => 2,
            6 => 4,
            _ => return Err(ImageError::FormatError("Unknown color type.".to_string()))
        };

        self.bits_per_pixel = channels * self.bit_depth;
        self.bpp = (self.bits_per_pixel + 7) / 8;
        self.previous = repeat(0u8).take(self.raw_row_length(self.width) as usize).collect();

        Ok(())
    }

    fn raw_row_length(&self, width: u32) -> u32 {
        (self.bits_per_pixel as u32 * width + 7) / 8
    }

    fn parse_trns(&mut self, len: usize) -> ImageResult<()> {
        let length =  match self.data_color_type {
            0 => 2,
            2 => 6,
            3 => len,
            _ => {
                return Err(ImageError::FormatError(format!(
                    "tRNS chunk may not appear for color type {}", self.data_color_type
                )))
            }
        };
        if length != len {
            return Err(ImageError::FormatError("Invalid tRNS chunk signature.".to_string()))
        }
        let mut buf = Vec::with_capacity(length as usize);
        try!(self.z.inner().r.by_ref().take(length as u64).read_to_end(&mut buf));
        self.crc.update(&*buf);
        self.trns = Some(buf);
        return Ok(())
    }

    fn parse_plte(&mut self, buf: Vec<u8>) -> ImageResult<()> {
        self.crc.update(&*buf);

        let len = buf.len() / 3;

        if len > 256 || len > (1 << self.bit_depth as usize) || buf.len() % 3 != 0{
            return Err(ImageError::FormatError("Color palette malformed.".to_string()))
        }

        let p: Vec<(u8, u8, u8)> = (0usize..256).map(|i| {
            if i < len {
                let r = buf[3 * i];
                let g = buf[3 * i + 1];
                let b = buf[3 * i + 2];

                (r, g, b)
            } else {
                (0, 0, 0)
            }
        }).collect();

        self.palette = Some(p);

        Ok(())
    }

    fn read_metadata(&mut self) -> ImageResult<()> {
        if !try!(self.read_signature()) {
            return Err(ImageError::FormatError("Could not read PNG signature.".to_string()))
        }

        self.state = PNGState::HaveSignature;

        loop {
            let length = try!(self.z.inner().r.read_u32::<BigEndian>());
            let mut chunk = Vec::with_capacity(4);
            try!(self.z.inner().r.by_ref().take(4).read_to_end(&mut chunk));

            self.chunk_length = length;
            self.chunk_type   = chunk.clone();

            self.crc.update(&*chunk);

            match (&*self.chunk_type, self.state) {
                (b"IHDR", PNGState::HaveSignature) => {
                    if length != 13 {
                        return Err(ImageError::FormatError("Invalid PNG signature.".to_string()))
                    }

                    let mut d = Vec::with_capacity(length as usize);
                    try!(self.z.inner().r.by_ref().take(length as u64).read_to_end(&mut d));
                    try!(self.parse_ihdr(d));

                    self.state = PNGState::HaveIHDR;
                }

                (b"PLTE", PNGState::HaveIHDR) => {
                    let mut d = Vec::with_capacity(length as usize);
                    try!(self.z.inner().r.by_ref().take(length as u64).read_to_end(&mut d));
                    try!(self.parse_plte(d));
                    self.state = PNGState::HavePLTE;
                }

                (b"tRNS", _) => {
                    try!(self.parse_trns(length as usize));
                }

                (b"IDAT", PNGState::HaveIHDR) if self.data_color_type != 3 => {
                    self.state = PNGState::HaveFirstIDat;
                    self.z.inner().set_inital_length(self.chunk_length);
                    self.z.inner().crc.update(&self.chunk_type);

                    break;
                }

                (b"IDAT", PNGState::HavePLTE) if self.data_color_type == 3 => {
                    self.state = PNGState::HaveFirstIDat;
                    self.z.inner().set_inital_length(self.chunk_length);
                    self.z.inner().crc.update(&self.chunk_type);

                    break;
                }

                _ => {
                    let mut b = Vec::with_capacity(length as usize);
                    try!(self.z.inner().r.by_ref().take(length as u64).read_to_end(&mut b));
                    self.crc.update(&*b);
                }
            }

            let chunk_crc = try!(self.z.inner().r.read_u32::<BigEndian>());
            let crc = self.crc.checksum();

            if crc != chunk_crc {
                return Err(ImageError::FormatError("CRC checksum invalid.".to_string()))
            }

            self.crc.reset();
        }

        Ok(())
    }

    fn extract_scanline(&mut self, buf: &mut [u8], rlength: u32) -> ImageResult<u32> {
        let filter_type = match FromPrimitive::from_u8(try!(try!(self.z.by_ref().bytes().next().ok_or(ImageError::ImageEnd)))) {
            Some(v) => v,
            _ => return Err(ImageError::FormatError("Unknown filter type.".to_string()))
        };

        {
            let mut read = 0usize;
            let read_buffer = &mut buf[..rlength as usize];
            while read < rlength as usize {
                let r = try!(self.z.read(&mut read_buffer[read..]));
                read += r;
            }
        }

        unfilter(filter_type, self.bpp as usize, &self.previous, &mut buf[..rlength as usize]);
        ::copy_memory(&buf[..rlength as usize], &mut self.previous);


        if let Some(ref palette) = self.palette {
            expand_palette(buf, &palette, rlength as usize, self.bit_depth, self.trns.as_ref().map(|v| &**v));
        } else if let Some(ref trns) = self.trns {
            let mut _trns = [0u8; 4];
            let trns = match self.data_color_type {
                0 => {
                    _trns[0] = trns[1];
                    &_trns[..1]
                },
                2 => {
                    _trns[0] = trns[1];
                    _trns[2] = trns[5];
                    _trns[1] = trns[3];
                    &_trns[..3]
                },
                // panic is ok this should have been catched earlier
                _ => panic!("invalid color type for transparency")
            };
            if self.bit_depth < 8 {
                expand_trns_line_nbits(
                    buf,
                    trns[0],
                    self.bit_depth
                );
            } else {
                expand_trns_line(
                    buf,
                    trns,
                    color::num_components(self.data_pixel_type)
                );
            }
        }

        self.decoded_rows += 1;

        Ok(self.decoded_rows)
    }
}

impl<R: Read> ImageDecoder for PNGDecoder<R> {
    fn dimensions(&mut self) -> ImageResult<(u32, u32)> {
        if self.state == PNGState::Start {
            let _ = try!(self.read_metadata());
        }

        Ok((self.width, self.height))
    }

    fn colortype(&mut self) -> ImageResult<ColorType> {
        if self.state == PNGState::Start {
            let _ = try!(self.read_metadata());
        }
        let bits = self.bit_depth;
        if self.trns.is_some() {
            Ok(match self.data_pixel_type {
                ColorType::RGB(n) => ColorType::RGBA(n),
                ColorType::Gray(_) if bits == 1 || bits == 2 || bits == 4 => ColorType::GrayA(8),
                _ => return Err(ImageError::FormatError(
                    "Invalid transparency data".to_string()
                ))
            })
        } else {
            Ok(self.data_pixel_type)
        }
    }

    fn row_len(&mut self) -> ImageResult<usize> {
        if self.state == PNGState::Start {
            let _ = try!(self.read_metadata());
        }

        let bits = color::bits_per_pixel(try!(self.colortype()));

        Ok((bits * self.width as usize + 7) / 8)
    }

    fn read_scanline(&mut self, buf: &mut [u8]) -> ImageResult<u32> {
        if self.state == PNGState::Start {
            let _ = try!(self.read_metadata());
        }
        if self.interlace_method != InterlaceMethod::None {
            return Err(ImageError::UnsupportedError("Image is interlaced, extraction of single scanlines is unsupported".to_string()))
        }
        let rlength = self.raw_row_length(self.width);
        self.extract_scanline(buf, rlength)
    }

    fn read_image(&mut self) -> ImageResult<DecodingResult> {
        if self.state == PNGState::Start {
            let _ = try!(self.read_metadata());
        }
        let max_rowlen = try!(self.row_len());
        let mut buf: Vec<u8> = repeat(0u8).take(max_rowlen * self.height as usize).collect();
        if let Some(pass_iterator) = self.pass_iterator { // Method == Adam7
            let mut pass_buf: Vec<u8> = repeat(0u8).take(max_rowlen).collect();
            let mut old_pass = 1;
            let bytes = color::bits_per_pixel(try!(self.colortype()))/8;
            for (pass, line, width) in pass_iterator {
                let rlength = self.raw_row_length(width);
                if old_pass != pass {
                    // new subimage, reset previous
                    for v in self.previous.iter_mut() {
                        *v = 0;
                    }
                }
                let bits = color::bits_per_pixel(try!(self.colortype()));
                let _ = try!(
                    self.extract_scanline(&mut pass_buf[..
                        ((bits * width as usize + 7) / 8)
                    ], rlength)
                );
                expand_pass(
                    &mut buf, self.width * bytes as u32,
                    &mut pass_buf[..width as usize * bytes], pass, line, bytes as u8
                );
                old_pass = pass;
            }
            Ok(DecodingResult::U8(buf))
        } else {
            for chunk in buf.chunks_mut(max_rowlen) {
                let _ = try!(self.read_scanline(chunk));
            }
            Ok(DecodingResult::U8(buf))
        }
    }
}

macro_rules! expand_pass(
    ($img:expr, $scanline:expr, $j:ident, $pos:expr, $bytes_pp:expr) => {
        for ($j, pixel) in $scanline.chunks($bytes_pp).enumerate() {
            for (offset, val) in pixel.iter().enumerate() {
                $img[$pos + offset] = *val
            }
        }
    }
);

fn expand_pass(
    img: &mut[u8], width: u32, scanline: &mut[u8],
    pass: u8, line_no: u32, bytes_pp: u8) {
    let line_no = line_no as usize;
    let width = width as usize;
    let bytes_pp = bytes_pp as usize;
    match pass {
        1 => expand_pass!(img, scanline, j,  8*line_no    * width + bytes_pp * j*8     , bytes_pp),
        2 => expand_pass!(img, scanline, j,  8*line_no    * width + bytes_pp *(j*8 + 4), bytes_pp),
        3 => expand_pass!(img, scanline, j, (8*line_no+4) * width + bytes_pp * j*4     , bytes_pp),
        4 => expand_pass!(img, scanline, j,  4*line_no    * width + bytes_pp *(j*4 + 2), bytes_pp),
        5 => expand_pass!(img, scanline, j, (4*line_no+2) * width + bytes_pp * j*2     , bytes_pp),
        6 => expand_pass!(img, scanline, j,  2*line_no    * width + bytes_pp *(j*2+1)  , bytes_pp),
        7 => expand_pass!(img, scanline, j, (2*line_no+1) * width + bytes_pp * j       , bytes_pp),
        _ => {}
    }
}

fn expand_trns_line(buf: &mut[u8], trns: &[u8], channels: usize) {
    let channels = channels as isize;
    let i = range_step(buf.len() as isize / (channels+1) * channels - channels, -channels, -channels);
    let j = range_step(buf.len() as isize - (channels+1), -(channels+1), -(channels+1));
    let channels = channels as usize;
    for (i, j) in i.zip(j) {
        let i_pixel = i as usize;
        let j_chunk = j as usize;
        if &buf[i_pixel..i_pixel+channels] == trns {
            buf[j_chunk+channels] = 0
        } else {
            buf[j_chunk+channels] = 0xFF
        }
        for k in (0..channels).rev() {
            buf[j_chunk+k] = buf[i_pixel+k];
        }
    }
}

fn expand_trns_line_nbits(buf: &mut[u8], trns: u8, bit_depth: u8) {
    let scaling_factor = (255)/((1u16 << bit_depth) - 1) as u8;
    expand_packed(buf, 2, bit_depth, |pixel, chunk| {
        if pixel == trns {
            chunk[1] = 0
        } else {
            chunk[1] = 0xFF
        }
        chunk[0] = pixel * scaling_factor
    })
}

fn expand_palette(buf: &mut[u8], palette: &[(u8, u8, u8)],
                  entries: usize, bit_depth: u8, trns: Option<&[u8]>) {
    let bpp = 8 / bit_depth as usize;
    let extra = if trns.is_some() { entries * bpp } else { 0 };
    assert_eq!(buf.len(), 3 * (entries * bpp - buf.len() % bpp) + extra);
    if let Some(trns) = trns {
        expand_packed(buf, 4, bit_depth, |i, chunk| {
            let ((r, g, b), a) = (
                palette[i as usize],
                *trns.get(i as usize).unwrap_or(&0xFF)
            );
            chunk[0] = r;
            chunk[1] = g;
            chunk[2] = b;
            chunk[3] = a;
        })
    } else {
        expand_packed(buf, 3, bit_depth, |i, chunk| {
            let (r, g, b) = palette[i as usize];
            chunk[0] = r;
            chunk[1] = g;
            chunk[2] = b;
        })
    }
}

pub struct IDATReader<R> {
    pub r: R,
    pub crc: Crc32,

    eof: bool,
    chunk_length: u32,
}

impl<R: Read> IDATReader<R> {
    pub fn new(r: R) -> IDATReader<R> {
        IDATReader {
            r: r,
            crc: Crc32::new(),
            eof: false,
            chunk_length: 0,
        }
    }

    pub fn set_inital_length(&mut self, len: u32) {
        self.chunk_length = len;
    }
}

impl<R: Read> Read for IDATReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if self.eof {
            return Ok(0);
        }

        let len = buf.len();
        let mut start = 0;

        while start < len {
            let m = cmp::min(len - start, self.chunk_length as usize);

            let slice = &mut buf[start..start + m];
            let r = try!(self.r.read(slice));

            start += r;

            self.chunk_length -= r as u32;
            self.crc.update(&*slice);

            if self.chunk_length == 0 {
                let chunk_crc = try!(self.r.read_u32::<BigEndian>());
                let crc = self.crc.checksum();

                if crc != chunk_crc {
                    return Ok(0);
                }

                self.crc.reset();
                self.chunk_length = try!(self.r.read_u32::<BigEndian>());

                let mut v = Vec::with_capacity(4);
                try!(self.r.by_ref().take(4).read_to_end(&mut v));
                self.crc.update(&v);

                match str::from_utf8(&v) {
                    Ok("IDAT") => (),
                    _ => {
                        self.eof = true;
                        break
                    }
                }
            }
        }

        Ok(start)
    }
}

#[cfg(test)]
mod tests {
    extern crate glob;

    use std::io::{self, Read};
    use std::fs::File;
    use std::ffi::OsStr;
    use std::path::{Path, PathBuf};
    use test;

    use image::{
        ImageDecoder,
        ImageResult,
        DecodingResult
    };

    use super::PNGDecoder;

    /// Filters the testsuite images for certain features
    fn get_testimages(feature: &str, color_type: &str, test_interlaced: bool) -> Vec<PathBuf> {
        // Find the files matching "./src/png/testdata/pngsuite/*.png".
        let pattern: PathBuf = [".", "src", "png", "testdata", "pngsuite", "*.png"].iter().collect();

        let paths = glob::glob(&pattern.display().to_string()).unwrap().filter_map(Result::ok)
            .filter(|ref p| p.file_name().and_then(OsStr::to_str).unwrap().starts_with(feature))
            .filter(|ref p| p.file_name().and_then(OsStr::to_str).unwrap().contains(color_type));

        let ret: Vec<PathBuf> = if test_interlaced {
            paths.collect()
        } else {
            paths.filter(|ref p| !p.file_name().and_then(OsStr::to_str)
                                   .unwrap()
                                   [2..]
                                   .contains("i"))
                                   .collect()
        };

        assert!(ret.len() > 0); // fail if no testimages are available
        ret
    }

    fn load_image<P>(path: P) -> ImageResult<DecodingResult> where P: AsRef<Path> {
        PNGDecoder::new(try!(File::open(path))).read_image()
    }

    #[test]
    /// Test image filters
    fn test_filters() {
        let images = get_testimages("f", "", false);

        for path in images.iter() {
            assert!(match load_image(path) {
                Ok(_) => true,
                Err(err) => { println!("file {:?}, failed with {:?}", path.display(), err); false }
            })
        }
    }
    #[test]
    /// Test basic formats filters
    fn test_basic() {
        let images = get_testimages("b", "", false);

        for path in images.iter() {
            assert!(match load_image(path) {
                Ok(_) => true,
                Err(err) => {println!("file {:?}, failed with {:?}", path.display(), err); false }
            })
        }
    }

    #[test]
    /// Chunk ordering
    fn test_chunk_ordering() {
        let images = get_testimages("o", "", false);

        for path in images.iter() {
            assert!(match load_image(path) {
                Ok(_) => { true },
                Err(err) => {println!("file {:?}, failed with {:?}", path.display(), err); false }
            })
        }
    }

    //#[test]
    //fn render_all() {
    //    let images = get_testimages("f", "", true)
    //        + get_testimages("b", "", true)
    //        + get_testimages("o", "", true);
    //
    //    for path in images.iter() {
    //        match ::open(path) {
    //            Err(_) => {},
    //            Ok(im) => {
    //                let filename = path.filename_str().unwrap().to_string();
    //                let p1 = "target";
    //                let p2 = "reference renderings";
    //                let _ = old_io::fs::mkdir(&Path::new(".").join_many(
    //                    [p1.as_slice(), p2.as_slice()]),
    //                    old_io::UserRWX
    //                );
    //                let p = Path::new(".").join_many([p1.as_slice(), p2.as_slice(),
    //                    filename.as_slice()]);
    //                let fout = File::create(&p).unwrap();
    //
    //                // Write the contents of this image to the Writer in PNG format.
    //                let _ = im.save(fout, ::PNG);
    //            }
    //        };
    //    }
    //}

    #[test]
    /// Test corrupted images, they should all fail
    fn test_corrupted() {
        let images = get_testimages("x", "", true);
        let num_images = images.len();
        let mut fails = 0;

        for path in images.iter() {
            match load_image(path) {
                Ok(_) => println!("corrupted file {} did not fail", path.display()),
                Err(_) => {
                    fails += 1;
                }
            }
        }

        assert_eq!(num_images, fails)
    }
    #[bench]
    /// Test basic formats filters
    fn bench_read_small_files(b: &mut test::Bencher) {
        let image_data: Vec<Vec<u8>> = get_testimages("b", "2c", false).iter().map(|path| {
            let mut buf = Vec::new();
            File::open(path).unwrap().read_to_end(&mut buf).unwrap();
            buf
        }).collect();
        b.iter(|| {
            for data in image_data.clone().into_iter() {
                let data = io::Cursor::new(data);
                let _ = PNGDecoder::new(data).read_image().unwrap();
            }
        });
        b.bytes = image_data.iter().map(|v| v.len()).fold(0, |a, b| a + b) as u64
    }
    #[bench]
    /// Test basic formats filters
    fn bench_read_big_file(b: &mut test::Bencher) {
        let mut image_data = Vec::new();
        File::open(
            &PathBuf::from(".").join("examples").join("fractal.png")
        ).unwrap().read_to_end(&mut image_data).unwrap();
        b.iter(|| {
            let image_data = io::Cursor::new(image_data.clone());
            let _ = PNGDecoder::new(image_data).read_image().unwrap();
        });
        b.bytes = image_data.len() as u64
    }
}