       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
     961
     962
     963
     964
     965
     966
     967
     968
     969
     970
     971
     972
     973
     974
     975
     976
     977
     978
     979
     980
     981
     982
     983
     984
     985
     986
     987
     988
     989
     990
     991
     992
     993
     994
     995
     996
     997
     998
     999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036

    use num_iter;
    use std::fs::File;
    use std::io;
    use std::io::{BufRead, BufReader, BufWriter, Seek, Write};
    use std::path::Path;

    #[cfg(feature = "bmp")]
    use bmp;
    #[cfg(feature = "gif_codec")]
    use gif;
    #[cfg(feature = "hdr")]
    use hdr;
    #[cfg(feature = "ico")]
    use ico;
    #[cfg(feature = "jpeg")]
    use jpeg;
    #[cfg(feature = "png_codec")]
    use png;
    #[cfg(feature = "pnm")]
    use pnm;
    #[cfg(feature = "tga")]
    use tga;
    #[cfg(feature = "tiff")]
    use tiff;
    #[cfg(feature = "webp")]
    use webp;

    use buffer::{ConvertBuffer, GrayAlphaImage, GrayImage, ImageBuffer, Pixel, RgbImage, RgbaImage, BgrImage, BgraImage};
    use flat::FlatSamples;
    use color;
    use image;
    use image::{GenericImage, GenericImageView, ImageDecoder, ImageFormat, ImageOutputFormat,
                ImageResult};
    use imageops;

    /// A Dynamic Image
    #[derive(Clone)]
    pub enum DynamicImage {
        /// Each pixel in this image is 8-bit Luma
        ImageLuma8(GrayImage),

        /// Each pixel in this image is 8-bit Luma with alpha
        ImageLumaA8(GrayAlphaImage),

        /// Each pixel in this image is 8-bit Rgb
        ImageRgb8(RgbImage),

        /// Each pixel in this image is 8-bit Rgb with alpha
        ImageRgba8(RgbaImage),

        /// Each pixel in this image is 8-bit Bgr
        ImageBgr8(BgrImage),

        /// Each pixel in this image is 8-bit Bgr with alpha
        ImageBgra8(BgraImage),
    }

    macro_rules! dynamic_map(
            ($dynimage: expr, ref $image: ident => $action: expr) => (
                    match $dynimage {
                            DynamicImage::ImageLuma8(ref $image) => DynamicImage::ImageLuma8($action),
                            DynamicImage::ImageLumaA8(ref $image) => DynamicImage::ImageLumaA8($action),
                            DynamicImage::ImageRgb8(ref $image) => DynamicImage::ImageRgb8($action),
                            DynamicImage::ImageRgba8(ref $image) => DynamicImage::ImageRgba8($action),
                            DynamicImage::ImageBgr8(ref $image) => DynamicImage::ImageBgr8($action),
                            DynamicImage::ImageBgra8(ref $image) => DynamicImage::ImageBgra8($action),
                    }
            );

            ($dynimage: expr, ref mut $image: ident => $action: expr) => (
                    match $dynimage {
                            DynamicImage::ImageLuma8(ref mut $image) => DynamicImage::ImageLuma8($action),
                            DynamicImage::ImageLumaA8(ref mut $image) => DynamicImage::ImageLumaA8($action),
                            DynamicImage::ImageRgb8(ref mut $image) => DynamicImage::ImageRgb8($action),
                            DynamicImage::ImageRgba8(ref mut $image) => DynamicImage::ImageRgba8($action),
                            DynamicImage::ImageBgr8(ref mut $image) => DynamicImage::ImageBgr8($action),
                            DynamicImage::ImageBgra8(ref mut $image) => DynamicImage::ImageBgra8($action),
                    }
            );

            ($dynimage: expr, ref $image: ident -> $action: expr) => (
                    match $dynimage {
                            DynamicImage::ImageLuma8(ref $image) => $action,
                            DynamicImage::ImageLumaA8(ref $image) => $action,
                            DynamicImage::ImageRgb8(ref $image) => $action,
                            DynamicImage::ImageRgba8(ref $image) => $action,
                            DynamicImage::ImageBgr8(ref $image) => $action,
                            DynamicImage::ImageBgra8(ref $image) => $action,
                    }
            );

            ($dynimage: expr, ref mut $image: ident -> $action: expr) => (
                    match $dynimage {
                            DynamicImage::ImageLuma8(ref mut $image) => $action,
                            DynamicImage::ImageLumaA8(ref mut $image) => $action,
                            DynamicImage::ImageRgb8(ref mut $image) => $action,
                            DynamicImage::ImageRgba8(ref mut $image) => $action,
                            DynamicImage::ImageBgr8(ref mut $image) => $action,
                            DynamicImage::ImageBgra8(ref mut $image) => $action,
                    }
            );
    );

    impl DynamicImage {
        /// Creates a dynamic image backed by a buffer of grey pixels.
        pub fn new_luma8(w: u32, h: u32) -> DynamicImage {
            DynamicImage::ImageLuma8(ImageBuffer::new(w, h))
        }

        /// Creates a dynamic image backed by a buffer of grey
        /// pixels with transparency.
        pub fn new_luma_a8(w: u32, h: u32) -> DynamicImage {
            DynamicImage::ImageLumaA8(ImageBuffer::new(w, h))
        }

        /// Creates a dynamic image backed by a buffer of RGB pixels.
        pub fn new_rgb8(w: u32, h: u32) -> DynamicImage {
            DynamicImage::ImageRgb8(ImageBuffer::new(w, h))
        }

        /// Creates a dynamic image backed by a buffer of RGBA pixels.
        pub fn new_rgba8(w: u32, h: u32) -> DynamicImage {
            DynamicImage::ImageRgba8(ImageBuffer::new(w, h))
        }

        /// Creates a dynamic image backed by a buffer of BGRA pixels.
        pub fn new_bgra8(w: u32, h: u32) -> DynamicImage {
            DynamicImage::ImageBgra8(ImageBuffer::new(w, h))
        }

        /// Creates a dynamic image backed by a buffer of BGR pixels.
        pub fn new_bgr8(w: u32, h: u32) -> DynamicImage {
            DynamicImage::ImageBgr8(ImageBuffer::new(w, h))
        }

        /// Returns a copy of this image as an RGB image.
        pub fn to_rgb(&self) -> RgbImage {
            dynamic_map!(*self, ref p -> {
                p.convert()
            })
        }

        /// Returns a copy of this image as an RGBA image.
        pub fn to_rgba(&self) -> RgbaImage {
            dynamic_map!(*self, ref p -> {
                p.convert()
            })
        }

        /// Returns a copy of this image as an BGR image.
        pub fn to_bgr(&self) -> BgrImage {
            dynamic_map!(*self, ref p -> {
                p.convert()
            })
        }

        /// Returns a copy of this image as an BGRA image.
        pub fn to_bgra(&self) -> BgraImage {
            dynamic_map!(*self, ref p -> {
                p.convert()
            })
        }

        /// Returns a copy of this image as a Luma image.
        pub fn to_luma(&self) -> GrayImage {
            dynamic_map!(*self, ref p -> {
                p.convert()
            })
        }

        /// Returns a copy of this image as a LumaA image.
        pub fn to_luma_alpha(&self) -> GrayAlphaImage {
            dynamic_map!(*self, ref p -> {
                p.convert()
            })
        }

        /// Return a cut out of this image delimited by the bounding rectangle.
        pub fn crop(&mut self, x: u32, y: u32, width: u32, height: u32) -> DynamicImage {
            dynamic_map!(*self, ref mut p => imageops::crop(p, x, y, width, height).to_image())
        }

        /// Return a reference to an 8bit RGB image
        pub fn as_rgb8(&self) -> Option<&RgbImage> {
            match *self {
                DynamicImage::ImageRgb8(ref p) => Some(p),
                _ => None,
            }
        }

        /// Return a mutable reference to an 8bit RGB image
        pub fn as_mut_rgb8(&mut self) -> Option<&mut RgbImage> {
            match *self {
                DynamicImage::ImageRgb8(ref mut p) => Some(p),
                _ => None,
            }
        }

        /// Return a reference to an 8bit BGR image
        pub fn as_bgr8(&self) -> Option<&BgrImage> {
            match *self {
                DynamicImage::ImageBgr8(ref p) => Some(p),
                _ => None,
            }
        }

        /// Return a mutable reference to an 8bit BGR image
        pub fn as_mut_bgr8(&mut self) -> Option<&mut BgrImage> {
            match *self {
                DynamicImage::ImageBgr8(ref mut p) => Some(p),
                _ => None,
            }
        }

        /// Return a reference to an 8bit RGBA image
        pub fn as_rgba8(&self) -> Option<&RgbaImage> {
            match *self {
                DynamicImage::ImageRgba8(ref p) => Some(p),
                _ => None,
            }
        }

        /// Return a mutable reference to an 8bit RGBA image
        pub fn as_mut_rgba8(&mut self) -> Option<&mut RgbaImage> {
            match *self {
                DynamicImage::ImageRgba8(ref mut p) => Some(p),
                _ => None,
            }
        }

        /// Return a reference to an 8bit BGRA image
        pub fn as_bgra8(&self) -> Option<&BgraImage> {
            match *self {
                DynamicImage::ImageBgra8(ref p) => Some(p),
                _ => None,
            }
        }

        /// Return a mutable reference to an 8bit RGBA image
        pub fn as_mut_bgra8(&mut self) -> Option<&mut BgraImage> {
            match *self {
                DynamicImage::ImageBgra8(ref mut p) => Some(p),
                _ => None,
            }
        }

        /// Return a reference to an 8bit Grayscale image
        pub fn as_luma8(&self) -> Option<&GrayImage> {
            match *self {
                DynamicImage::ImageLuma8(ref p) => Some(p),
                _ => None,
            }
        }

        /// Return a mutable reference to an 8bit Grayscale image
        pub fn as_mut_luma8(&mut self) -> Option<&mut GrayImage> {
            match *self {
                DynamicImage::ImageLuma8(ref mut p) => Some(p),
                _ => None,
            }
        }

        /// Return a reference to an 8bit Grayscale image with an alpha channel
        pub fn as_luma_alpha8(&self) -> Option<&GrayAlphaImage> {
            match *self {
                DynamicImage::ImageLumaA8(ref p) => Some(p),
                _ => None,
            }
        }

        /// Return a mutable reference to an 8bit Grayscale image with an alpha channel
        pub fn as_mut_luma_alpha8(&mut self) -> Option<&mut GrayAlphaImage> {
            match *self {
                DynamicImage::ImageLumaA8(ref mut p) => Some(p),
                _ => None,
            }
        }

        /// Return this image's pixels as a byte vector.
        pub fn raw_pixels(&self) -> Vec<u8> {
            image_to_bytes(self)
        }

        /// Return a view on the raw sample buffer.
        pub fn as_flat_samples(&self) -> FlatSamples<&[u8]> {
            dynamic_map!(*self, ref p -> p.as_flat_samples())
        }

        /// Return this image's color type.
        pub fn color(&self) -> color::ColorType {
            match *self {
                DynamicImage::ImageLuma8(_) => color::ColorType::Gray(8),
                DynamicImage::ImageLumaA8(_) => color::ColorType::GrayA(8),
                DynamicImage::ImageRgb8(_) => color::ColorType::RGB(8),
                DynamicImage::ImageRgba8(_) => color::ColorType::RGBA(8),
                DynamicImage::ImageBgra8(_) => color::ColorType::BGRA(8),
                DynamicImage::ImageBgr8(_) => color::ColorType::BGR(8),
            }
        }

        /// Return a grayscale version of this image.
        pub fn grayscale(&self) -> DynamicImage {
            match *self {
                DynamicImage::ImageLuma8(ref p) => DynamicImage::ImageLuma8(p.clone()),
                DynamicImage::ImageLumaA8(ref p) => DynamicImage::ImageLuma8(imageops::grayscale(p)),
                DynamicImage::ImageRgb8(ref p) => DynamicImage::ImageLuma8(imageops::grayscale(p)),
                DynamicImage::ImageRgba8(ref p) => DynamicImage::ImageLuma8(imageops::grayscale(p)),
                DynamicImage::ImageBgr8(ref p) => DynamicImage::ImageLuma8(imageops::grayscale(p)),
                DynamicImage::ImageBgra8(ref p) => DynamicImage::ImageLuma8(imageops::grayscale(p)),
            }
        }

        /// Invert the colors of this image.
        /// This method operates inplace.
        pub fn invert(&mut self) {
            dynamic_map!(*self, ref mut p -> imageops::invert(p))
        }

        /// Resize this image using the specified filter algorithm.
        /// Returns a new image. The image's aspect ratio is preserved.
        /// The image is scaled to the maximum possible size that fits
        /// within the bounds specified by ```nwidth``` and ```nheight```.
        pub fn resize(&self, nwidth: u32, nheight: u32, filter: imageops::FilterType) -> DynamicImage {
            let (width2, height2) =
                resize_dimensions(self.width(), self.height(), nwidth, nheight, false);

            self.resize_exact(width2, height2, filter)
        }

        /// Resize this image using the specified filter algorithm.
        /// Returns a new image. Does not preserve aspect ratio.
        /// ```nwidth``` and ```nheight``` are the new image's dimensions
        pub fn resize_exact(
            &self,
            nwidth: u32,
            nheight: u32,
            filter: imageops::FilterType,
        ) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::resize(p, nwidth, nheight, filter))
        }

        /// Scale this image down to fit within a specific size.
        /// Returns a new image. The image's aspect ratio is preserved.
        /// The image is scaled to the maximum possible size that fits
        /// within the bounds specified by ```nwidth``` and ```nheight```.
        ///
        /// This method uses a fast integer algorithm where each source
        /// pixel contributes to exactly one target pixel.
        /// May give aliasing artifacts if new size is close to old size.
        pub fn thumbnail(&self, nwidth: u32, nheight: u32) -> DynamicImage {
            let (width2, height2) =
                resize_dimensions(self.width(), self.height(), nwidth, nheight, false);
            self.thumbnail_exact(width2, height2)
        }

        /// Scale this image down to a specific size.
        /// Returns a new image. Does not preserve aspect ratio.
        /// ```nwidth``` and ```nheight``` are the new image's dimensions.
        /// This method uses a fast integer algorithm where each source
        /// pixel contributes to exactly one target pixel.
        /// May give aliasing artifacts if new size is close to old size.
        pub fn thumbnail_exact(&self, nwidth: u32, nheight: u32) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::thumbnail(p, nwidth, nheight))
        }

        /// Resize this image using the specified filter algorithm.
        /// Returns a new image. The image's aspect ratio is preserved.
        /// The image is scaled to the maximum possible size that fits
        /// within the larger (relative to aspect ratio) of the bounds
        /// specified by ```nwidth``` and ```nheight```, then cropped to
        /// fit within the other bound.
        pub fn resize_to_fill(
            &self,
            nwidth: u32,
            nheight: u32,
            filter: imageops::FilterType,
        ) -> DynamicImage {
            let (width2, height2) =
                resize_dimensions(self.width(), self.height(), nwidth, nheight, true);

            let mut intermediate = self.resize_exact(width2, height2, filter);
            let (iwidth, iheight) = intermediate.dimensions();
            let ratio = u64::from(iwidth) * u64::from(nheight);
            let nratio = u64::from(nwidth) * u64::from(iheight);

            if nratio > ratio {
                intermediate.crop(0, (iheight - nheight) / 2, nwidth, nheight)
            } else {
                intermediate.crop((iwidth - nwidth) / 2, 0, nwidth, nheight)
            }
        }

        /// Performs a Gaussian blur on this image.
        /// ```sigma``` is a measure of how much to blur by.
        pub fn blur(&self, sigma: f32) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::blur(p, sigma))
        }

        /// Performs an unsharpen mask on this image.
        /// ```sigma``` is the amount to blur the image by.
        /// ```threshold``` is a control of how much to sharpen.
        ///
        /// See <https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking>
        pub fn unsharpen(&self, sigma: f32, threshold: i32) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::unsharpen(p, sigma, threshold))
        }

        /// Filters this image with the specified 3x3 kernel.
        pub fn filter3x3(&self, kernel: &[f32]) -> DynamicImage {
            if kernel.len() != 9 {
                panic!("filter must be 3 x 3")
            }

            dynamic_map!(*self, ref p => imageops::filter3x3(p, kernel))
        }

        /// Adjust the contrast of this image.
        /// ```contrast``` is the amount to adjust the contrast by.
        /// Negative values decrease the contrast and positive values increase the contrast.
        pub fn adjust_contrast(&self, c: f32) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::contrast(p, c))
        }

        /// Brighten the pixels of this image.
        /// ```value``` is the amount to brighten each pixel by.
        /// Negative values decrease the brightness and positive values increase it.
        pub fn brighten(&self, value: i32) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::brighten(p, value))
        }

        /// Hue rotate the supplied image.
        /// `value` is the degrees to rotate each pixel by.
        /// 0 and 360 do nothing, the rest rotates by the given degree value.
        /// just like the css webkit filter hue-rotate(180)
        pub fn huerotate(&self, value: i32) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::huerotate(p, value))
        }

        /// Flip this image vertically
        pub fn flipv(&self) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::flip_vertical(p))
        }

        /// Flip this image horizontally
        pub fn fliph(&self) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::flip_horizontal(p))
        }

        /// Rotate this image 90 degrees clockwise.
        pub fn rotate90(&self) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::rotate90(p))
        }

        /// Rotate this image 180 degrees clockwise.
        pub fn rotate180(&self) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::rotate180(p))
        }

        /// Rotate this image 270 degrees clockwise.
        pub fn rotate270(&self) -> DynamicImage {
            dynamic_map!(*self, ref p => imageops::rotate270(p))
        }

        /// Encode this image and write it to ```w```
        pub fn write_to<W: Write, F: Into<ImageOutputFormat>>(
            &self,
            w: &mut W,
            format: F,
        ) -> ImageResult<()> {
            let mut bytes = self.raw_pixels();
            let (width, height) = self.dimensions();
            let mut color = self.color();
            let format = format.into();

            #[allow(deprecated)]
            match format {
                #[cfg(feature = "png_codec")]
                image::ImageOutputFormat::PNG => {
                    let p = png::PNGEncoder::new(w);
                    match *self {
                        DynamicImage::ImageBgra8(_) => {
                            bytes=self.to_rgba().iter().cloned().collect();
                            color=color::ColorType::RGBA(8);
                        },
                        DynamicImage::ImageBgr8(_) => {
                            bytes=self.to_rgb().iter().cloned().collect();
                            color=color::ColorType::RGB(8);
                        },
                        _ => {},
                    }
                    try!(p.encode(&bytes, width, height, color));
                    Ok(())
                }
                #[cfg(feature = "pnm")]
                image::ImageOutputFormat::PNM(subtype) => {
                    let mut p = pnm::PNMEncoder::new(w).with_subtype(subtype);
                    match *self {
                        DynamicImage::ImageBgra8(_) => {
                            bytes=self.to_rgba().iter().cloned().collect();
                            color=color::ColorType::RGBA(8);
                        },
                        DynamicImage::ImageBgr8(_) => {
                            bytes=self.to_rgb().iter().cloned().collect();
                            color=color::ColorType::RGB(8);
                        },
                        _ => {},
                    }
                    try!(p.encode(&bytes[..], width, height, color));
                    Ok(())
                }
                #[cfg(feature = "jpeg")]
                image::ImageOutputFormat::JPEG(quality) => {
                    let mut j = jpeg::JPEGEncoder::new_with_quality(w, quality);

                    try!(j.encode(&bytes, width, height, color));
                    Ok(())
                }

                #[cfg(feature = "gif_codec")]
                image::ImageOutputFormat::GIF => {
                    let mut g = gif::Encoder::new(w);

                    try!(g.encode(&gif::Frame::from_rgba(
                        width as u16,
                        height as u16,
                        &mut *self.to_rgba().iter().cloned().collect::<Vec<u8>>()
                    )));
                    Ok(())
                }

                #[cfg(feature = "ico")]
                image::ImageOutputFormat::ICO => {
                    let i = ico::ICOEncoder::new(w);

                    try!(i.encode(&bytes, width, height, color));
                    Ok(())
                }

                #[cfg(feature = "bmp")]
                image::ImageOutputFormat::BMP => {
                    let mut b = bmp::BMPEncoder::new(w);
                    try!(b.encode(&bytes, width, height, color));
                    Ok(())
                }

                image::ImageOutputFormat::Unsupported(msg) => {
                    Err(image::ImageError::UnsupportedError(msg))
                }
            }
        }

        /// Saves the buffer to a file at the path specified.
        ///
        /// The image format is derived from the file extension.
        pub fn save<Q>(&self, path: Q) -> io::Result<()>
        where
            Q: AsRef<Path>,
        {
            dynamic_map!(*self, ref p -> {
                p.save(path)
            })
        }
    }

    #[allow(deprecated)]
    impl GenericImageView for DynamicImage {
        type Pixel = color::Rgba<u8>;
        type InnerImageView = Self;

        fn dimensions(&self) -> (u32, u32) {
            dynamic_map!(*self, ref p -> p.dimensions())
        }

        fn bounds(&self) -> (u32, u32, u32, u32) {
            dynamic_map!(*self, ref p -> p.bounds())
        }

        fn get_pixel(&self, x: u32, y: u32) -> color::Rgba<u8> {
            dynamic_map!(*self, ref p -> p.get_pixel(x, y).to_rgba())
        }

        fn inner(&self) -> &Self::InnerImageView {
            self
        }
    }

    #[allow(deprecated)]
    impl GenericImage for DynamicImage {
        type InnerImage = DynamicImage;

        fn put_pixel(&mut self, x: u32, y: u32, pixel: color::Rgba<u8>) {
            match *self {
                DynamicImage::ImageLuma8(ref mut p) => p.put_pixel(x, y, pixel.to_luma()),
                DynamicImage::ImageLumaA8(ref mut p) => p.put_pixel(x, y, pixel.to_luma_alpha()),
                DynamicImage::ImageRgb8(ref mut p) => p.put_pixel(x, y, pixel.to_rgb()),
                DynamicImage::ImageRgba8(ref mut p) => p.put_pixel(x, y, pixel),
                DynamicImage::ImageBgr8(ref mut p) => p.put_pixel(x, y, pixel.to_bgr()),
                DynamicImage::ImageBgra8(ref mut p) => p.put_pixel(x, y, pixel.to_bgra()),
            }
        }
        /// DEPRECATED: Use iterator `pixels_mut` to blend the pixels directly.
        fn blend_pixel(&mut self, x: u32, y: u32, pixel: color::Rgba<u8>) {
            match *self {
                DynamicImage::ImageLuma8(ref mut p) => p.blend_pixel(x, y, pixel.to_luma()),
                DynamicImage::ImageLumaA8(ref mut p) => p.blend_pixel(x, y, pixel.to_luma_alpha()),
                DynamicImage::ImageRgb8(ref mut p) => p.blend_pixel(x, y, pixel.to_rgb()),
                DynamicImage::ImageRgba8(ref mut p) => p.blend_pixel(x, y, pixel),

                DynamicImage::ImageBgr8(ref mut p) => p.blend_pixel(x, y, pixel.to_bgr()),
                DynamicImage::ImageBgra8(ref mut p) => p.blend_pixel(x, y, pixel.to_bgra()),
            }
        }

        /// DEPRECATED: Do not use is function: It is unimplemented!
        fn get_pixel_mut(&mut self, _: u32, _: u32) -> &mut color::Rgba<u8> {
            unimplemented!()
        }

        fn inner_mut(&mut self) -> &mut Self::InnerImage {
            self
        }
    }

    /// Decodes an image and stores it into a dynamic image
    pub fn decoder_to_image<I: ImageDecoder>(codec: I) -> ImageResult<DynamicImage> {
        let color = codec.colortype();
        let (w, h) = codec.dimensions();
        let buf = try!(codec.read_image());

        // TODO: Avoid this cast by having ImageBuffer use u64's
        assert!(w <= u32::max_value() as u64);
        assert!(h <= u32::max_value() as u64);
        let (w, h) = (w as u32, h as u32);

        let image = match color {
            color::ColorType::RGB(8) => {
                ImageBuffer::from_raw(w, h, buf).map(DynamicImage::ImageRgb8)
            }

            color::ColorType::RGBA(8) => {
                ImageBuffer::from_raw(w, h, buf).map(DynamicImage::ImageRgba8)
            }

            color::ColorType::BGR(8) => {
                ImageBuffer::from_raw(w, h, buf).map(DynamicImage::ImageBgr8)
            }

            color::ColorType::BGRA(8) => {
                ImageBuffer::from_raw(w, h, buf).map(DynamicImage::ImageBgra8)
            }

            color::ColorType::Gray(8) => {
                ImageBuffer::from_raw(w, h, buf).map(DynamicImage::ImageLuma8)
            }

            color::ColorType::GrayA(8) => {
                ImageBuffer::from_raw(w, h, buf).map(DynamicImage::ImageLumaA8)
            }
            color::ColorType::Gray(bit_depth)
                if bit_depth == 1 || bit_depth == 2 || bit_depth == 4 =>
            {
                gray_to_luma8(bit_depth, w, h, &buf).map(DynamicImage::ImageLuma8)
            }
            _ => return Err(image::ImageError::UnsupportedColor(color)),
        };
        match image {
            Some(image) => Ok(image),
            None => Err(image::ImageError::DimensionError),
        }
    }

    fn gray_to_luma8(bit_depth: u8, w: u32, h: u32, buf: &[u8]) -> Option<GrayImage> {
        // Note: this conversion assumes that the scanlines begin on byte boundaries
        let mask = (1u8 << bit_depth as usize) - 1;
        let scaling_factor = 255 / ((1 << bit_depth as usize) - 1);
        let bit_width = w * u32::from(bit_depth);
        let skip = if bit_width % 8 == 0 {
            0
        } else {
            (8 - bit_width % 8) / u32::from(bit_depth)
        };
        let row_len = w + skip;
        let mut p = Vec::new();
        let mut i = 0;
        for v in buf {
            for shift in num_iter::range_step_inclusive(8i8-(bit_depth as i8), 0, -(bit_depth as i8)) {
                // skip the pixels that can be neglected because scanlines should
                // start at byte boundaries
                if i % (row_len as usize) < (w as usize) {
                    let pixel = (v & mask << shift as usize) >> shift as usize;
                    p.push(pixel * scaling_factor);
                }
                i += 1;
            }
        }
        ImageBuffer::from_raw(w, h, p)
    }

    #[allow(deprecated)]
    fn image_to_bytes(image: &DynamicImage) -> Vec<u8> {
        match *image {
            // TODO: consider transmuting
            DynamicImage::ImageLuma8(ref a) => a.iter().cloned().collect(),

            DynamicImage::ImageLumaA8(ref a) => a.iter().cloned().collect(),

            DynamicImage::ImageRgb8(ref a) => a.iter().cloned().collect(),

            DynamicImage::ImageRgba8(ref a) => a.iter().cloned().collect(),

            DynamicImage::ImageBgr8(ref a) => a.iter().cloned().collect(),

            DynamicImage::ImageBgra8(ref a) => a.iter().cloned().collect(),
        }
    }

    /// Open the image located at the path specified.
    /// The image's format is determined from the path's file extension.
    pub fn open<P>(path: P) -> ImageResult<DynamicImage>
    where
        P: AsRef<Path>,
    {
        // thin wrapper function to strip generics before calling open_impl
        open_impl(path.as_ref())
    }

    fn open_impl(path: &Path) -> ImageResult<DynamicImage> {
        let fin = match File::open(path) {
            Ok(f) => f,
            Err(err) => return Err(image::ImageError::IoError(err)),
        };
        let fin = BufReader::new(fin);

        let ext = path.extension()
            .and_then(|s| s.to_str())
            .map_or("".to_string(), |s| s.to_ascii_lowercase());

        let format = match &ext[..] {
            "jpg" | "jpeg" => image::ImageFormat::JPEG,
            "png" => image::ImageFormat::PNG,
            "gif" => image::ImageFormat::GIF,
            "webp" => image::ImageFormat::WEBP,
            "tif" | "tiff" => image::ImageFormat::TIFF,
            "tga" => image::ImageFormat::TGA,
            "bmp" => image::ImageFormat::BMP,
            "ico" => image::ImageFormat::ICO,
            "hdr" => image::ImageFormat::HDR,
            "pbm" | "pam" | "ppm" | "pgm" => image::ImageFormat::PNM,
            format => {
                return Err(image::ImageError::UnsupportedError(format!(
                    "Image format image/{:?} is not supported.",
                    format
                )))
            }
        };

        load(fin, format)
    }

    /// Saves the supplied buffer to a file at the path specified.
    ///
    /// The image format is derived from the file extension. The buffer is assumed to have
    /// the correct format according to the specified color type.

    /// This will lead to corrupted files if the buffer contains malformed data. Currently only
    /// jpeg and png files are supported.
    pub fn save_buffer<P>(
        path: P,
        buf: &[u8],
        width: u32,
        height: u32,
        color: color::ColorType,
    ) -> io::Result<()>
    where
        P: AsRef<Path>,
    {
        // thin wrapper function to strip generics before calling save_buffer_impl
        save_buffer_impl(path.as_ref(), buf, width, height, color)
    }

    fn save_buffer_impl(
        path: &Path,
        buf: &[u8],
        width: u32,
        height: u32,
        color: color::ColorType,
    ) -> io::Result<()> {
        let fout = &mut BufWriter::new(try!(File::create(path)));
        let ext = path.extension()
            .and_then(|s| s.to_str())
            .map_or("".to_string(), |s| s.to_ascii_lowercase());

        match &*ext {
            #[cfg(feature = "ico")]
            "ico" => ico::ICOEncoder::new(fout).encode(buf, width, height, color),
            #[cfg(feature = "jpeg")]
            "jpg" | "jpeg" => jpeg::JPEGEncoder::new(fout).encode(buf, width, height, color),
            #[cfg(feature = "png_codec")]
            "png" => png::PNGEncoder::new(fout).encode(buf, width, height, color),
            #[cfg(feature = "pnm")]
            "pbm" => pnm::PNMEncoder::new(fout)
                .with_subtype(pnm::PNMSubtype::Bitmap(pnm::SampleEncoding::Binary))
                .encode(buf, width, height, color),
            #[cfg(feature = "pnm")]
            "pgm" => pnm::PNMEncoder::new(fout)
                .with_subtype(pnm::PNMSubtype::Graymap(pnm::SampleEncoding::Binary))
                .encode(buf, width, height, color),
            #[cfg(feature = "pnm")]
            "ppm" => pnm::PNMEncoder::new(fout)
                .with_subtype(pnm::PNMSubtype::Pixmap(pnm::SampleEncoding::Binary))
                .encode(buf, width, height, color),
            #[cfg(feature = "pnm")]
            "pam" => pnm::PNMEncoder::new(fout).encode(buf, width, height, color),
            #[cfg(feature = "bmp")]
            "bmp" => bmp::BMPEncoder::new(fout).encode(buf, width, height, color),
            format => Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                &format!("Unsupported image format image/{:?}", format)[..],
            )),
        }
    }

    /// Create a new image from a Reader
    pub fn load<R: BufRead + Seek>(r: R, format: ImageFormat) -> ImageResult<DynamicImage> {
        #[allow(deprecated, unreachable_patterns)]
        // Default is unreachable if all features are supported.
        match format {
            #[cfg(feature = "png_codec")]
            image::ImageFormat::PNG => decoder_to_image(png::PNGDecoder::new(r)?),
            #[cfg(feature = "gif_codec")]
            image::ImageFormat::GIF => decoder_to_image(gif::Decoder::new(r)?),
            #[cfg(feature = "jpeg")]
            image::ImageFormat::JPEG => decoder_to_image(jpeg::JPEGDecoder::new(r)?),
            #[cfg(feature = "webp")]
            image::ImageFormat::WEBP => decoder_to_image(webp::WebpDecoder::new(r)?),
            #[cfg(feature = "tiff")]
            image::ImageFormat::TIFF => decoder_to_image(try!(tiff::TIFFDecoder::new(r))),
            #[cfg(feature = "tga")]
            image::ImageFormat::TGA => decoder_to_image(tga::TGADecoder::new(r)?),
            #[cfg(feature = "bmp")]
            image::ImageFormat::BMP => decoder_to_image(bmp::BMPDecoder::new(r)?),
            #[cfg(feature = "ico")]
            image::ImageFormat::ICO => decoder_to_image(try!(ico::ICODecoder::new(r))),
            #[cfg(feature = "hdr")]
            image::ImageFormat::HDR => decoder_to_image(try!(hdr::HDRAdapter::new(BufReader::new(r)))),
            #[cfg(feature = "pnm")]
            image::ImageFormat::PNM => decoder_to_image(try!(pnm::PNMDecoder::new(BufReader::new(r)))),
            _ => Err(image::ImageError::UnsupportedError(format!(
                "A decoder for {:?} is not available.",
                format
            ))),
        }
    }

    static MAGIC_BYTES: [(&'static [u8], ImageFormat); 17] = [
        (b"\x89PNG\r\n\x1a\n", ImageFormat::PNG),
        (&[0xff, 0xd8, 0xff], ImageFormat::JPEG),
        (b"GIF89a", ImageFormat::GIF),
        (b"GIF87a", ImageFormat::GIF),
        (b"RIFF", ImageFormat::WEBP), // TODO: better magic byte detection, see https://github.com/image-rs/image/issues/660
        (b"MM.*", ImageFormat::TIFF),
        (b"II*.", ImageFormat::TIFF),
        (b"BM", ImageFormat::BMP),
        (&[0, 0, 1, 0], ImageFormat::ICO),
        (b"#?RADIANCE", ImageFormat::HDR),
        (b"P1", ImageFormat::PNM),
        (b"P2", ImageFormat::PNM),
        (b"P3", ImageFormat::PNM),
        (b"P4", ImageFormat::PNM),
        (b"P5", ImageFormat::PNM),
        (b"P6", ImageFormat::PNM),
        (b"P7", ImageFormat::PNM),
    ];

    /// Create a new image from a byte slice
    ///
    /// Makes an educated guess about the image format.
    /// TGA is not supported by this function.
    pub fn load_from_memory(buffer: &[u8]) -> ImageResult<DynamicImage> {
        load_from_memory_with_format(buffer, try!(guess_format(buffer)))
    }

    /// Create a new image from a byte slice
    #[inline(always)]
    pub fn load_from_memory_with_format(buf: &[u8], format: ImageFormat) -> ImageResult<DynamicImage> {
        let b = io::Cursor::new(buf);
        load(b, format)
    }

    /// Guess image format from memory block
    ///
    /// Makes an educated guess about the image format based on the Magic Bytes at the beginning.
    /// TGA is not supported by this function.
    /// This is not to be trusted on the validity of the whole memory block
    pub fn guess_format(buffer: &[u8]) -> ImageResult<ImageFormat> {
        for &(signature, format) in &MAGIC_BYTES {
            if buffer.starts_with(signature) {
                return Ok(format);
            }
        }
        Err(image::ImageError::UnsupportedError(
            "Unsupported image format".to_string(),
        ))
    }

    /// Calculates the width and height an image should be resized to.
    /// This preserves aspect ratio, and based on the `fill` parameter
    /// will either fill the dimensions to fit inside the smaller constraint
    /// (will overflow the specified bounds on one axis to preserve
    /// aspect ratio), or will shrink so that both dimensions are
    /// completely contained with in the given `width` and `height`,
    /// with empty space on one axis.
    fn resize_dimensions(width: u32, height: u32, nwidth: u32, nheight: u32, fill: bool) -> (u32, u32) {
        let ratio = u64::from(width) * u64::from(nheight);
        let nratio = u64::from(nwidth) * u64::from(height);

        let use_width = if fill {
            nratio > ratio
        } else {
            nratio <= ratio
        };
        let intermediate = if use_width {
            u64::from(height) * u64::from(nwidth) / u64::from(width)
        } else {
            u64::from(width) * u64::from(nheight) / u64::from(height)
        };
        if use_width {
            if intermediate <= u64::from(::std::u32::MAX) {
                (nwidth, intermediate as u32)
            } else {
                (
                    (u64::from(nwidth) * u64::from(::std::u32::MAX) / intermediate) as u32,
                    ::std::u32::MAX,
                )
            }
        } else if intermediate <= u64::from(::std::u32::MAX) {
            (intermediate as u32, nheight)
        } else {
            (
                ::std::u32::MAX,
                (u64::from(nheight) * u64::from(::std::u32::MAX) / intermediate) as u32,
            )
        }
    }

    #[cfg(test)]
    mod bench {
        #[cfg(feature = "benchmarks")]
        use test;

        #[bench]
        #[cfg(feature = "benchmarks")]
        fn bench_conversion(b: &mut test::Bencher) {
            let a = super::DynamicImage::ImageRgb8(::ImageBuffer::new(1000, 1000));
            b.iter(|| a.to_luma());
            b.bytes = 1000 * 1000 * 3
        }
    }

    #[cfg(test)]
    mod test {
        #[test]
        fn test_empty_file() {
            assert!(super::load_from_memory(b"").is_err());
        }

        quickcheck! {
            fn resize_bounds_correctly_width(old_w: u32, new_w: u32) -> bool {
                if old_w == 0 || new_w == 0 { return true; }
                let result = super::resize_dimensions(old_w, 400, new_w, ::std::u32::MAX, false);
                result.0 == new_w && result.1 == (400 as f64 * new_w as f64 / old_w as f64) as u32
            }
        }

        quickcheck! {
            fn resize_bounds_correctly_height(old_h: u32, new_h: u32) -> bool {
                if old_h == 0 || new_h == 0 { return true; }
                let result = super::resize_dimensions(400, old_h, ::std::u32::MAX, new_h, false);
                result.1 == new_h && result.0 == (400 as f64 * new_h as f64 / old_h as f64) as u32
            }
        }

        #[test]
        fn resize_handles_fill() {
            let result = super::resize_dimensions(100, 200, 200, 500, true);
            assert!(result.0 == 250);
            assert!(result.1 == 500);

            let result = super::resize_dimensions(200, 100, 500, 200, true);
            assert!(result.0 == 500);
            assert!(result.1 == 250);
        }

        #[test]
        fn resize_handles_overflow() {
            let result = super::resize_dimensions(100, ::std::u32::MAX, 200, ::std::u32::MAX, true);
            assert!(result.0 == 100);
            assert!(result.1 == ::std::u32::MAX);

            let result = super::resize_dimensions(::std::u32::MAX, 100, ::std::u32::MAX, 200, true);
            assert!(result.0 == ::std::u32::MAX);
            assert!(result.1 == 100);
        }

        #[test]
        fn gray_to_luma8_skip() {
            let check = |bit_depth, w, h, from, to| {
                assert_eq!(
                    super::gray_to_luma8(bit_depth, w, h, from).map(super::GrayImage::into_raw),
                    Some(to));
            };
            // Bit depth 1, skip is more than half a byte
            check(
                1, 10, 2,
                &[0b11110000, 0b11000000, 0b00001111, 0b11000000],
                vec![255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255]);
            // Bit depth 2, skip is more than half a byte
            check(
                2, 5, 2,
                &[0b11110000, 0b11000000, 0b00001111, 0b11000000],
                vec![255, 255, 0, 0, 255, 0, 0, 255, 255, 255]);
            // Bit depth 2, skip is 0
            check(
                2, 4, 2,
                &[0b11110000, 0b00001111],
                vec![255, 255, 0, 0, 0, 0, 255, 255]);
            // Bit depth 4, skip is half a byte
            check(
                4, 1, 2,
                &[0b11110011, 0b00001100],
                vec![255, 0]);
        }
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)