| 1 | use craterust-analyzer cargo tiff 0.11.3 crate/::encoderrust-analyzer cargo tiff 0.11.3 encoder/::compressionrust-analyzer cargo tiff 0.11.3 encoder/compression/::*; |
| 2 | use flate2rust-analyzer cargo flate2 1.1.2 crate/::{writerust-analyzer cargo flate2 1.1.2 write/::ZlibEncoderrust-analyzer cargo flate2 1.1.2 zlib/write/ZlibEncoder#, Compressionrust-analyzer cargo flate2 1.1.2 Compression# as FlateCompressionrust-analyzer cargo flate2 1.1.2 Compression#}; |
| 3 | |
| 4 | /// The Deflate algorithm used to compress image data in TIFF files. |
| 5 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)] |
| 6 | pub struct Deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate# { |
| 7 | levelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate#level.: FlateCompressionrust-analyzer cargo flate2 1.1.2 Compression#, |
| 8 | } |
| 9 | |
| 10 | /// The level of compression used by the Deflate algorithm. |
| 11 | /// It allows trading compression ratio for compression speed. |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 13 | #[non_exhaustive] |
| 14 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 15 | pub enum DeflateLevelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/DeflateLevel# { |
| 16 | /// The fastest possible compression mode. |
| 17 | Fastrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/DeflateLevel#Fast# = 1, |
| 18 | /// The conserative choice between speed and ratio. |
| 19 | #[defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/default:] |
| 20 | Balancedrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/DeflateLevel#Balanced# = 6, |
| 21 | /// The best compression available with Deflate. |
| 22 | Bestrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/DeflateLevel#Best# = 9, |
| 23 | } |
| 24 | |
| 25 | impl Deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate# { |
| 26 | /// Create a new deflate compressor with a specific level of compression. |
| 27 | pub fn with_levelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate]with_level().(level: DeflateLevelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/DeflateLevel#) -> Selfrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate] { |
| 28 | Selfrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate] { |
| 29 | levelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate#level.: FlateCompressionrust-analyzer cargo flate2 1.1.2 Compression#::newrust-analyzer cargo flate2 1.1.2 impl#[Compression]new().(level as u32), |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate# { |
| 35 | fn defaultrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][Default]default().() -> Selfrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][Default] { |
| 36 | Selfrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][Default]::with_levelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate]with_level().(DeflateLevelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/DeflateLevel#::defaultrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[DeflateLevel][Default]default().()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl Compressionrust-analyzer cargo tiff 0.11.3 encoder/compression/Compression# for Deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate# { |
| 41 | const COMPRESSION_METHODrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][Compression]COMPRESSION_METHOD.: CompressionMethodrust-analyzer cargo tiff 0.11.3 tags/CompressionMethod# = CompressionMethodrust-analyzer cargo tiff 0.11.3 tags/CompressionMethod#::Deflaterust-analyzer cargo tiff 0.11.3 tags/CompressionMethod#Deflate#; |
| 42 | |
| 43 | fn get_algorithmrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][Compression]get_algorithm().(&self) -> Compressorrust-analyzer cargo tiff 0.11.3 encoder/compression/Compressor# { |
| 44 | Compressorrust-analyzer cargo tiff 0.11.3 encoder/compression/Compressor#::Deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/Compressor#Deflate#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl CompressionAlgorithmrust-analyzer cargo tiff 0.11.3 encoder/compression/CompressionAlgorithm# for Deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate# { |
| 49 | fn write_torust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][CompressionAlgorithm]write_to().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(&mut self, writer: &mut W, bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<u64, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 50 | let mut encoder = ZlibEncoderrust-analyzer cargo flate2 1.1.2 zlib/write/ZlibEncoder#::newrust-analyzer cargo flate2 1.1.2 zlib/write/impl#[`ZlibEncoder<W>`]new().(writer, self.levelrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate#level.); |
| 51 | encoder.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(bytes)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 52 | encoder.try_finishrust-analyzer cargo flate2 1.1.2 zlib/write/impl#[`ZlibEncoder<W>`]try_finish().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 53 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(encoder.total_outrust-analyzer cargo flate2 1.1.2 zlib/write/impl#[`ZlibEncoder<W>`]total_out().()) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #[cfg(test)] |
| 58 | mod testsrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/tests/ { |
| 59 | use superrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/::*; |
| 60 | use craterust-analyzer cargo tiff 0.11.3 crate/::encoderrust-analyzer cargo tiff 0.11.3 encoder/::compressionrust-analyzer cargo tiff 0.11.3 encoder/compression/::testsrust-analyzer cargo tiff 0.11.3 encoder/compression/tests/::TEST_DATArust-analyzer cargo tiff 0.11.3 encoder/compression/tests/TEST_DATA.; |
| 61 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#; |
| 62 | |
| 63 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 64 | fn test_deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/tests/test_deflate().() { |
| 65 | const EXPECTED_COMPRESSED_DATArust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/tests/EXPECTED_COMPRESSED_DATA.: [u8; 64] = [ |
| 66 | 0x78, 0x9C, 0x15, 0xC7, 0xD1, 0x0D, 0x80, 0x20, 0x0C, 0x04, 0xD0, 0x55, 0x6E, 0x02, |
| 67 | 0xA7, 0x71, 0x81, 0xA6, 0x41, 0xDA, 0x28, 0xD4, 0xF4, 0xD0, 0xF9, 0x81, 0xE4, 0xFD, |
| 68 | 0xBC, 0xD3, 0x9C, 0x58, 0x04, 0x1C, 0xE9, 0xBD, 0xE2, 0x8A, 0x84, 0x5A, 0xD1, 0x7B, |
| 69 | 0xE7, 0x97, 0xF4, 0xF8, 0x08, 0x8D, 0xF6, 0x66, 0x21, 0x3D, 0x3A, 0xE4, 0xA9, 0x91, |
| 70 | 0x3E, 0xAC, 0xF1, 0x98, 0xB9, 0x70, 0x17, 0x13, |
| 71 | ]; |
| 72 | |
| 73 | let mut compressed_data = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::<u8>::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 74 | let mut writer = Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]new().(&mut compressed_data); |
| 75 | Deflaterust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/Deflate#::defaultrust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][Default]default().().write_torust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/impl#[Deflate][CompressionAlgorithm]write_to().(&mut writer, TEST_DATArust-analyzer cargo tiff 0.11.3 encoder/compression/tests/TEST_DATA.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 76 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(EXPECTED_COMPRESSED_DATArust-analyzer cargo tiff 0.11.3 encoder/compression/deflate/tests/EXPECTED_COMPRESSED_DATA., compressed_data.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 77 | } |
| 78 | } |