| 1 | /* |
| 2 | * Copyright (c) 2023. |
| 3 | * |
| 4 | * This software is free software; |
| 5 | * |
| 6 | * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license |
| 7 | */ |
| 8 | |
| 9 | use craterust-analyzer cargo zune-core 0.5.1 crate/::bit_depthrust-analyzer cargo zune-core 0.5.1 bit_depth/::BitDepthrust-analyzer cargo zune-core 0.5.1 bit_depth/BitDepth#; |
| 10 | use craterust-analyzer cargo zune-core 0.5.1 crate/::colorspacerust-analyzer cargo zune-core 0.5.1 colorspace/::ColorSpacerust-analyzer cargo zune-core 0.5.1 colorspace/ColorSpace#; |
| 11 | |
| 12 | /// Encoder options that are flags |
| 13 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, 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:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 14 | struct EncoderFlagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags# { |
| 15 | /// Whether JPEG images should be encoded as progressive images |
| 16 | jpeg_encode_progressiverust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#jpeg_encode_progressive.: bool, |
| 17 | /// Whether JPEG images should use optimized huffman tables |
| 18 | jpeg_optimize_huffmanrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#jpeg_optimize_huffman.: bool, |
| 19 | /// Whether to not preserve metadata across image transformations |
| 20 | image_strip_metadatarust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#image_strip_metadata.: bool |
| 21 | } |
| 22 | |
| 23 | /// Options shared by some of the encoders in |
| 24 | /// the `zune-` family of image crates |
| 25 | #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 26 | pub struct EncoderOptionsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions# { |
| 27 | widthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#width.: usize, |
| 28 | heightrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#height.: usize, |
| 29 | colorspacerust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#colorspace.: ColorSpacerust-analyzer cargo zune-core 0.5.1 colorspace/ColorSpace#, |
| 30 | qualityrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#quality.: u8, |
| 31 | depthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#depth.: BitDepthrust-analyzer cargo zune-core 0.5.1 bit_depth/BitDepth#, |
| 32 | num_threadsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#num_threads.: u8, |
| 33 | effortrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#effort.: u8, |
| 34 | flagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#flags.: EncoderFlagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags# |
| 35 | } |
| 36 | |
| 37 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for EncoderOptionsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions# { |
| 38 | fn defaultrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions][Default]default().() -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions][Default] { |
| 39 | Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions][Default] { |
| 40 | widthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#width.: 0, |
| 41 | heightrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#height.: 0, |
| 42 | colorspacerust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#colorspace.: ColorSpacerust-analyzer cargo zune-core 0.5.1 colorspace/ColorSpace#::RGBrust-analyzer cargo zune-core 0.5.1 colorspace/ColorSpace#RGB#, |
| 43 | qualityrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#quality.: 80, |
| 44 | depthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#depth.: BitDepthrust-analyzer cargo zune-core 0.5.1 bit_depth/BitDepth#::Eightrust-analyzer cargo zune-core 0.5.1 bit_depth/BitDepth#Eight#, |
| 45 | num_threadsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#num_threads.: 4, |
| 46 | effortrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#effort.: 4, |
| 47 | flagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#flags.: EncoderFlagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#::defaultrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderFlags][Default]default().() |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl EncoderOptionsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions# { |
| 53 | /// Create new encode options |
| 54 | /// |
| 55 | /// # Arguments |
| 56 | /// |
| 57 | /// * `width`: Image width |
| 58 | /// * `height`: Image height |
| 59 | /// * `colorspace`: Image colorspaces |
| 60 | /// * `depth`: Image depth |
| 61 | /// |
| 62 | /// returns: EncoderOptions |
| 63 | /// |
| 64 | pub fn newrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]new().( |
| 65 | width: usize, height: usize, colorspace: ColorSpacerust-analyzer cargo zune-core 0.5.1 colorspace/ColorSpace#, depth: BitDepthrust-analyzer cargo zune-core 0.5.1 bit_depth/BitDepth# |
| 66 | ) -> EncoderOptionsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions# { |
| 67 | EncoderOptionsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions# { |
| 68 | widthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#width., |
| 69 | heightrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#height., |
| 70 | colorspacerust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#colorspace., |
| 71 | depthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#depth., |
| 72 | ..Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions][Default]default().() |
| 73 | } |
| 74 | } |
| 75 | /// Get the width for which the image will be encoded in |
| 76 | pub const fn widthrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]width().(&self) -> usize { |
| 77 | self.widthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#width. |
| 78 | } |
| 79 | |
| 80 | /// Get height for which the image will be encoded in |
| 81 | /// |
| 82 | /// returns: usize |
| 83 | /// |
| 84 | /// # Panics |
| 85 | /// If height is zero |
| 86 | pub fn heightrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]height().(&self) -> usize { |
| 87 | assert_nerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_ne!!(self.heightrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#height., 0); |
| 88 | self.heightrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#height. |
| 89 | } |
| 90 | /// Get the depth for which the image will be encoded in |
| 91 | pub const fn depthrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]depth().(&self) -> BitDepthrust-analyzer cargo zune-core 0.5.1 bit_depth/BitDepth# { |
| 92 | self.depthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#depth. |
| 93 | } |
| 94 | /// Get the quality for which the image will be encoded with |
| 95 | /// |
| 96 | /// # Lossy |
| 97 | /// - Higher quality means some images take longer to write and |
| 98 | /// are big but they look good |
| 99 | /// |
| 100 | /// - Lower quality means small images and low quality. |
| 101 | /// |
| 102 | /// # Lossless |
| 103 | /// - High quality indicates more time is spent in making the file |
| 104 | /// smaller |
| 105 | /// |
| 106 | /// - Low quality indicates less time is spent in making the file bigger |
| 107 | pub const fn qualityrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]quality().(&self) -> u8 { |
| 108 | self.qualityrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#quality. |
| 109 | } |
| 110 | /// Get the colorspace for which the image will be encoded in |
| 111 | pub const fn colorspacerust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]colorspace().(&self) -> ColorSpacerust-analyzer cargo zune-core 0.5.1 colorspace/ColorSpace# { |
| 112 | self.colorspacerust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#colorspace. |
| 113 | } |
| 114 | pub const fn effortrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]effort().(&self) -> u8 { |
| 115 | self.effortrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#effort. |
| 116 | } |
| 117 | |
| 118 | /// Set width for the image to be encoded |
| 119 | pub fn set_widthrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_width().(mut self, width: usize) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 120 | self.widthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#width. = width; |
| 121 | self |
| 122 | } |
| 123 | |
| 124 | /// Set height for the image to be encoded |
| 125 | pub fn set_heightrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_height().(mut self, height: usize) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 126 | self.heightrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#height. = height; |
| 127 | self |
| 128 | } |
| 129 | /// Set depth for the image to be encoded |
| 130 | pub fn set_depthrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_depth().(mut self, depth: BitDepthrust-analyzer cargo zune-core 0.5.1 bit_depth/BitDepth#) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 131 | self.depthrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#depth. = depth; |
| 132 | self |
| 133 | } |
| 134 | /// Set quality of the image to be encoded |
| 135 | /// |
| 136 | /// Quality is clamped from 0..100 |
| 137 | /// |
| 138 | /// Quality means different options depending on the encoder, see |
| 139 | /// [get_quality](Self::quality) |
| 140 | pub fn set_qualityrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_quality().(mut self, quality: u8) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 141 | self.qualityrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#quality. = quality.clamprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][Ord]clamp().(0, 100); |
| 142 | self |
| 143 | } |
| 144 | /// Set colorspace for the image to be encoded |
| 145 | pub fn set_colorspacerust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_colorspace().(mut self, colorspace: ColorSpacerust-analyzer cargo zune-core 0.5.1 colorspace/ColorSpace#) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 146 | self.colorspacerust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#colorspace. = colorspace; |
| 147 | self |
| 148 | } |
| 149 | /// Set the number of threads allowed for multithreaded encoding |
| 150 | /// where supported |
| 151 | /// |
| 152 | /// Zero means use a single thread |
| 153 | pub fn set_num_threadsrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_num_threads().(mut self, threads: u8) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 154 | self.num_threadsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#num_threads. = threads; |
| 155 | |
| 156 | self |
| 157 | } |
| 158 | pub fn set_effortrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_effort().(mut self, effort: u8) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 159 | self.effortrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#effort. = effort; |
| 160 | self |
| 161 | } |
| 162 | |
| 163 | /// Return number of threads configured for multithreading |
| 164 | /// where possible |
| 165 | /// |
| 166 | /// This is used for multi-threaded encoders, |
| 167 | /// currently only jpeg-xl |
| 168 | pub const fn num_threadsrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]num_threads().(&self) -> u8 { |
| 169 | self.num_threadsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#num_threads. |
| 170 | } |
| 171 | |
| 172 | /// Set whether the encoder should remove metadata from the image |
| 173 | /// |
| 174 | /// When set to `true`, supported encoders will strip away metadata |
| 175 | /// from the resulting image. If set to false, where supported, encoders |
| 176 | /// will not remove metadata from images |
| 177 | pub fn set_strip_metadatarust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_strip_metadata().(mut self, yes: bool) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 178 | self.flagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#flags..image_strip_metadatarust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#image_strip_metadata. = yes; |
| 179 | self |
| 180 | } |
| 181 | /// Whether or not the encoder should remove metadata from the image |
| 182 | /// |
| 183 | /// The default value is false, and encoders that respect this try to preserve as much |
| 184 | /// data as possible from one image to another |
| 185 | pub const fn strip_metadatarust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]strip_metadata().(&self) -> bool { |
| 186 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.flagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#flags..image_strip_metadatarust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#image_strip_metadata. |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /// JPEG options |
| 191 | impl EncoderOptionsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions# { |
| 192 | /// Whether the jpeg encoder should encode the image in progressive mode |
| 193 | /// |
| 194 | /// Default is `false`. |
| 195 | /// |
| 196 | /// This may be used to create slightly smaller images at the cost of more processing |
| 197 | /// time |
| 198 | pub const fn jpeg_encode_progressiverust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]jpeg_encode_progressive().(&self) -> bool { |
| 199 | self.flagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#flags..jpeg_encode_progressiverust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#jpeg_encode_progressive. |
| 200 | } |
| 201 | |
| 202 | /// Whether the jpeg encoder should optimize huffman tables to create smaller files |
| 203 | /// at the cost of processing time |
| 204 | /// |
| 205 | /// Default is `false`. |
| 206 | pub const fn jpeg_optimized_huffman_tablesrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]jpeg_optimized_huffman_tables().(&self) -> bool { |
| 207 | self.flagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#flags..jpeg_optimize_huffmanrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#jpeg_optimize_huffman. |
| 208 | } |
| 209 | |
| 210 | /// Set whether the jpeg encoder should encode the imagei in progressive mode |
| 211 | /// |
| 212 | /// Default is `false` |
| 213 | pub fn set_jpeg_encode_progressiverust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions]set_jpeg_encode_progressive().(mut self, yes: bool) -> Selfrust-analyzer cargo zune-core 0.5.1 options/encoder/impl#[EncoderOptions] { |
| 214 | self.flagsrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderOptions#flags..jpeg_optimize_huffmanrust-analyzer cargo zune-core 0.5.1 options/encoder/EncoderFlags#jpeg_optimize_huffman. = yes; |
| 215 | self |
| 216 | } |
| 217 | } |