icu_lib 0.10.0

Image Converter Ultra Library
Documentation
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
use crate::{EncoderParams, PngColorMode, PngCompression};
use png;
use std::io::Cursor;

use crate::endecoder::{EnDecoder, ImageInfo};
use crate::midata::MiData;
use serde_json::json;

pub struct AutoDetect {}

pub struct PNG {}

pub struct JPEG {}

pub struct BMP {}

pub struct GIF {}

pub struct TIFF {}

pub struct WEBP {}

pub struct ICO {}

pub struct PBM {}

pub struct PGM {}

pub struct PPM {}

pub struct PAM {}

pub struct TGA {}

fn decode_raster(
    data: &[u8],
) -> Result<(image::DynamicImage, image::ImageFormat), image::ImageError> {
    let format = image::guess_format(data)?;
    let image = image::load_from_memory_with_format(data, format)?;
    Ok((image, format))
}

fn decode_fixed(data: &[u8], format: image::ImageFormat, label: &str) -> MiData {
    match image::load_from_memory_with_format(data, format) {
        Ok(image) => MiData::RGBA(image.to_rgba8()),
        Err(error) => {
            log::error!("Failed to decode {label}: {error}");
            MiData::RGBA(image::RgbaImage::new(0, 0))
        }
    }
}

fn empty_image_info(data_size: usize) -> ImageInfo {
    ImageInfo {
        width: 0,
        height: 0,
        data_size: data_size as u32,
        format: "unknown".to_string(),
        other_info: serde_json::Value::Null,
    }
}

impl EnDecoder for AutoDetect {
    fn can_decode(&self, data: &[u8]) -> bool {
        image::guess_format(data).is_ok()
    }

    fn encode(&self, _data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        unimplemented!()
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        match decode_raster(&data) {
            Ok((image, _)) => MiData::RGBA(image.to_rgba8()),
            Err(error) => {
                log::error!("Failed to decode raster image: {error}");
                MiData::RGBA(image::RgbaImage::new(0, 0))
            }
        }
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        let (img, img_format) = match decode_raster(data) {
            Ok(decoded) => decoded,
            Err(error) => {
                log::error!("Failed to inspect raster image: {error}");
                return empty_image_info(data.len());
            }
        };

        let mut other_info = serde_json::Map::new();

        other_info.insert(
            "Color Type".to_string(),
            json!(format!("{:?}", img.color())),
        );

        // Try to parse EXIF data
        if let Ok(reader) = exif::Reader::new().read_from_container(&mut std::io::Cursor::new(data))
        {
            let mut exif_map = serde_json::Map::new();
            for field in reader.fields() {
                exif_map.insert(
                    field.tag.to_string(),
                    json!(field.display_value().with_unit(&reader).to_string()),
                );
            }
            if !exif_map.is_empty() {
                other_info.insert("Exif".to_string(), serde_json::Value::Object(exif_map));
            }
        }

        ImageInfo {
            width: img.width(),
            height: img.height(),
            data_size: img.as_bytes().len() as u32,
            format: img_format.to_mime_type().to_owned(),
            other_info: serde_json::Value::Object(other_info),
        }
    }
}

fn png_compression(compression: PngCompression) -> png::Compression {
    match compression {
        PngCompression::Fast => png::Compression::Fast,
        PngCompression::Balanced => png::Compression::Balanced,
        PngCompression::Best => png::Compression::High,
    }
}

fn png_bit_depth(bpp: u8) -> Result<png::BitDepth, String> {
    match bpp {
        1 => Ok(png::BitDepth::One),
        2 => Ok(png::BitDepth::Two),
        4 => Ok(png::BitDepth::Four),
        8 => Ok(png::BitDepth::Eight),
        _ => Err(format!("unsupported indexed PNG bit depth: {bpp}")),
    }
}

fn pack_png_indexes(indexes: &[u8], width: u32, height: u32, bpp: u8) -> Result<Vec<u8>, String> {
    let width = usize::try_from(width).map_err(|_| "PNG width does not fit usize")?;
    let height = usize::try_from(height).map_err(|_| "PNG height does not fit usize")?;
    let pixel_count = width
        .checked_mul(height)
        .ok_or("indexed PNG dimensions overflow")?;
    if indexes.len() != pixel_count {
        return Err(format!(
            "indexed PNG has {} indexes, expected {pixel_count}",
            indexes.len()
        ));
    }
    let pixels_per_byte = 8usize / usize::from(bpp);
    let row_bytes = width
        .checked_add(pixels_per_byte - 1)
        .ok_or("indexed PNG row size overflow")?
        / pixels_per_byte;
    let mut packed = vec![
        0;
        row_bytes
            .checked_mul(height)
            .ok_or("indexed PNG size overflow")?
    ];
    for (row, source) in indexes.chunks_exact(width).enumerate() {
        for (column, index) in source.iter().copied().enumerate() {
            let shift = (pixels_per_byte - 1 - column % pixels_per_byte) * usize::from(bpp);
            packed[row * row_bytes + column / pixels_per_byte] |= index << shift;
        }
    }
    Ok(packed)
}

fn write_indexed_png(
    width: u32,
    height: u32,
    palette: &[[u8; 4]],
    indexes: &[u8],
    bpp: u8,
    compression: PngCompression,
) -> Result<Vec<u8>, String> {
    png_bit_depth(bpp)?;
    if width == 0 || height == 0 {
        return Err("indexed PNG dimensions must be non-zero".to_string());
    }
    let max_colors = 1usize << bpp;
    if palette.is_empty() || palette.len() > max_colors {
        return Err(format!(
            "indexed PNG palette has {} colors, expected 1..={max_colors}",
            palette.len()
        ));
    }
    if indexes
        .iter()
        .any(|index| usize::from(*index) >= palette.len())
    {
        return Err("indexed PNG contains an out-of-range palette index".to_string());
    }
    let packed = pack_png_indexes(indexes, width, height, bpp)?;
    let rgb = palette
        .iter()
        .flat_map(|color| [color[0], color[1], color[2]])
        .collect::<Vec<_>>();
    let alpha = palette.iter().map(|color| color[3]).collect::<Vec<_>>();
    let mut output = Vec::new();
    {
        let mut encoder = png::Encoder::new(&mut output, width, height);
        encoder.set_compression(png_compression(compression));
        encoder.set_filter(png::Filter::NoFilter);
        encoder.set_color(png::ColorType::Indexed);
        encoder.set_depth(png_bit_depth(bpp)?);
        encoder.set_palette(rgb);
        encoder.set_trns(alpha);
        let mut writer = encoder.write_header().map_err(|error| error.to_string())?;
        writer
            .write_image_data(&packed)
            .map_err(|error| error.to_string())?;
    }
    Ok(output)
}

fn quantized_png(
    image: &image::RgbaImage,
    bpp: u8,
    dither: Option<u32>,
    compression: PngCompression,
) -> Result<Vec<u8>, String> {
    png_bit_depth(bpp)?;
    if image.width() == 0 || image.height() == 0 {
        return Err("PNG dimensions must be non-zero".to_string());
    }
    let quantizer = color_quant::NeuQuant::new(
        dither.unwrap_or(30).clamp(1, 30) as i32,
        1usize << bpp,
        image.as_raw(),
    );
    let palette = quantizer
        .color_map_rgba()
        .chunks_exact(4)
        .map(|c| [c[0], c[1], c[2], c[3]])
        .collect::<Vec<_>>();
    let mut indexed_image = image.clone();
    if dither.is_some() {
        image::imageops::dither(&mut indexed_image, &quantizer);
    }
    let indexes = indexed_image
        .pixels()
        .map(|pixel| quantizer.index_of(&pixel.0) as u8)
        .collect::<Vec<_>>();
    write_indexed_png(
        image.width(),
        image.height(),
        &palette,
        &indexes,
        bpp,
        compression,
    )
}

fn encode_png(data: &MiData, params: &EncoderParams) -> Result<Vec<u8>, String> {
    match (data, params.png_color_mode) {
        (MiData::INDEXED(indexed), PngColorMode::Preserve) => write_indexed_png(
            indexed.width,
            indexed.height,
            &indexed.palette,
            &indexed.indexes,
            indexed.bpp,
            params.png_compression,
        ),
        (_, PngColorMode::Preserve) => {
            Err("PNG preserve mode requires indexed image data".to_string())
        }
        (MiData::RGBA(image), PngColorMode::Indexed(bpp)) => {
            quantized_png(image, bpp, params.dither, params.png_compression)
        }
        (MiData::INDEXED(indexed), PngColorMode::Indexed(bpp)) => {
            quantized_png(&indexed.rgba, bpp, params.dither, params.png_compression)
        }
        (MiData::RGBA(image), mode @ (PngColorMode::Rgb | PngColorMode::Rgba)) => {
            encode_direct_png(image, mode, params.png_compression)
        }
        (MiData::INDEXED(indexed), mode @ (PngColorMode::Rgb | PngColorMode::Rgba)) => {
            encode_direct_png(&indexed.rgba, mode, params.png_compression)
        }
        _ => Err("PNG encoding requires RGBA or indexed image data".to_string()),
    }
}

fn encode_direct_png(
    image: &image::RgbaImage,
    mode: PngColorMode,
    compression: PngCompression,
) -> Result<Vec<u8>, String> {
    if image.width() == 0 || image.height() == 0 {
        return Err("PNG dimensions must be non-zero".to_string());
    }
    let (color_type, bytes) = match mode {
        PngColorMode::Rgb => (
            png::ColorType::Rgb,
            image
                .pixels()
                .flat_map(|pixel| [pixel[0], pixel[1], pixel[2]])
                .collect::<Vec<_>>(),
        ),
        PngColorMode::Rgba => (png::ColorType::Rgba, image.as_raw().clone()),
        _ => return Err("direct PNG mode must be RGB or RGBA".to_string()),
    };
    let mut output = Vec::new();
    {
        let mut encoder = png::Encoder::new(&mut output, image.width(), image.height());
        encoder.set_compression(png_compression(compression));
        encoder.set_color(color_type);
        encoder.set_depth(png::BitDepth::Eight);
        let mut writer = encoder.write_header().map_err(|error| error.to_string())?;
        writer
            .write_image_data(&bytes)
            .map_err(|error| error.to_string())?;
    }
    Ok(output)
}

impl EnDecoder for PNG {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Png
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, encoder_params: EncoderParams) -> Vec<u8> {
        match encode_png(data, &encoder_params) {
            Ok(output) => output,
            Err(error) => {
                log::error!("Failed to encode PNG: {error}");
                Vec::new()
            }
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        match image::load_from_memory_with_format(&data, image::ImageFormat::Png) {
            Ok(image) => MiData::RGBA(image.to_rgba8()),
            Err(error) => {
                log::error!("Failed to decode PNG: {error}");
                MiData::RGBA(image::RgbaImage::new(0, 0))
            }
        }
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        let mut info = AutoDetect {}.info(data);

        // Add PNG specific info
        if let Ok(decoder) = png::Decoder::new(Cursor::new(data)).read_info() {
            let png_info = decoder.info();
            if let serde_json::Value::Object(ref mut map) = info.other_info {
                map.insert(
                    "PNG Color Type".to_string(),
                    json!(format!("{:?}", png_info.color_type)),
                );
                map.insert(
                    "Bit Depth".to_string(),
                    json!(format!("{:?}", png_info.bit_depth)),
                );
                if png_info.trns.is_some() {
                    map.insert("Transparent".to_string(), json!("Yes"));
                }
                map.insert("Interlaced".to_string(), json!(png_info.interlaced));
            }
        }

        info
    }
}

impl EnDecoder for JPEG {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Jpeg
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, encoder_params: EncoderParams) -> Vec<u8> {
        let image = match data {
            MiData::RGBA(image) => image,
            MiData::INDEXED(indexed) => &indexed.rgba,
            _ => return Vec::new(),
        };
        if !(1..=100).contains(&encoder_params.jpeg_quality) {
            log::error!("Failed to encode JPEG: quality must be between 1 and 100");
            return Vec::new();
        }
        let background = encoder_params.jpeg_background;
        let rgb = image
            .pixels()
            .flat_map(|pixel| {
                let alpha = u32::from(pixel[3]);
                std::array::from_fn::<_, 3, _>(|channel| {
                    let source = u32::from(pixel[channel]);
                    let background = u32::from(background[channel]);
                    ((source * alpha + background * (255 - alpha) + 127) / 255) as u8
                })
            })
            .collect::<Vec<_>>();
        let mut output = Vec::new();
        let encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(
            &mut output,
            encoder_params.jpeg_quality,
        );
        if let Err(error) = image::ImageEncoder::write_image(
            encoder,
            &rgb,
            image.width(),
            image.height(),
            image::ExtendedColorType::Rgb8,
        ) {
            log::error!("Failed to encode JPEG: {error}");
            return Vec::new();
        }
        output
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        match image::load_from_memory_with_format(&data, image::ImageFormat::Jpeg) {
            Ok(image) => MiData::RGBA(image.to_rgba8()),
            Err(error) => {
                log::error!("Failed to decode JPEG: {error}");
                MiData::RGBA(image::RgbaImage::new(0, 0))
            }
        }
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for BMP {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Bmp
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Bmp).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        decode_fixed(&data, image::ImageFormat::Bmp, "BMP")
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for GIF {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Gif
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Gif).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        match image::load_from_memory_with_format(&data, image::ImageFormat::Gif) {
            Ok(image) => MiData::RGBA(image.to_rgba8()),
            Err(error) => {
                log::error!("Failed to decode GIF: {error}");
                MiData::RGBA(image::RgbaImage::new(0, 0))
            }
        }
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for TIFF {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Tiff
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Tiff).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        decode_fixed(&data, image::ImageFormat::Tiff, "TIFF")
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for WEBP {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::WebP
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::WebP).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        decode_fixed(&data, image::ImageFormat::WebP, "WEBP")
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for ICO {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Ico
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Ico).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        decode_fixed(&data, image::ImageFormat::Ico, "ICO")
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for PBM {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Pnm
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::GRAY(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Pnm).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        match image::load_from_memory_with_format(&data, image::ImageFormat::Pnm) {
            Ok(image) => MiData::GRAY(image.to_luma_alpha8()),
            Err(error) => {
                log::error!("Failed to decode PBM: {error}");
                MiData::RGBA(image::RgbaImage::new(0, 0))
            }
        }
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for PGM {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Pnm
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::GRAY(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Pnm).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        match image::load_from_memory_with_format(&data, image::ImageFormat::Pnm) {
            Ok(image) => MiData::GRAY(image.to_luma_alpha8()),
            Err(error) => {
                log::error!("Failed to decode PGM: {error}");
                MiData::RGBA(image::RgbaImage::new(0, 0))
            }
        }
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for PPM {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Pnm
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Pnm).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        decode_fixed(&data, image::ImageFormat::Pnm, "PNM")
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

impl EnDecoder for PAM {
    fn can_decode(&self, data: &[u8]) -> bool {
        if let Ok(format) = image::guess_format(data) {
            format == image::ImageFormat::Pnm
        } else {
            false
        }
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Pnm).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        decode_fixed(&data, image::ImageFormat::Pnm, "PNM")
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}

#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod tests {
    use super::{AutoDetect, BMP, JPEG, PNG};
    use crate::endecoder::EnDecoder;
    use crate::midata::MiData;
    use crate::EncoderParams;
    use image::GenericImageView;

    #[test]
    fn jpeg_encode_accepts_rgba_input() {
        let image = image::RgbaImage::from_vec(2, 1, vec![255, 0, 0, 0, 0, 128, 255, 255]).unwrap();

        let encoded = JPEG {}.encode(&MiData::RGBA(image), EncoderParams::default());

        assert!(!encoded.is_empty());
        assert_eq!(
            image::guess_format(&encoded).unwrap(),
            image::ImageFormat::Jpeg
        );
        assert_eq!(
            image::load_from_memory(&encoded).unwrap().dimensions(),
            (2, 1)
        );
    }

    #[test]
    fn format_probes_reject_non_raster_data_without_panicking() {
        let data = vec![0x19, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        assert!(!PNG {}.can_decode(&data));
        assert!(!JPEG {}.can_decode(&data));
        assert!(!BMP {}.can_decode(&data));
    }

    #[test]
    fn autodetect_rejects_truncated_raster_without_panicking() {
        let data = b"\x89PNG\r\n\x1a\n".to_vec();
        assert!(AutoDetect {}.can_decode(&data));
        assert!(matches!(
            AutoDetect {}.decode(data.clone()),
            MiData::RGBA(image) if image.width() == 0 && image.height() == 0
        ));
        let info = AutoDetect {}.info(&data);
        assert_eq!((info.width, info.height), (0, 0));
        assert_eq!(info.data_size, data.len() as u32);
    }
}

impl EnDecoder for TGA {
    fn can_decode(&self, _data: &[u8]) -> bool {
        false
    }

    fn encode(&self, data: &MiData, _encoder_params: EncoderParams) -> Vec<u8> {
        match data {
            MiData::RGBA(img) => {
                let mut buf = Cursor::new(Vec::new());
                img.write_to(&mut buf, image::ImageFormat::Tga).unwrap();
                buf.into_inner()
            }
            _ => Vec::new(),
        }
    }

    fn decode(&self, data: Vec<u8>) -> MiData {
        MiData::RGBA(
            image::load_from_memory_with_format(&data, image::ImageFormat::Tga)
                .unwrap()
                .to_rgba8(),
        )
    }

    fn info(&self, data: &[u8]) -> ImageInfo {
        AutoDetect {}.info(data)
    }
}