Make functions const when possible
This commit is contained in:
parent
e8a8dede55
commit
a688086d27
5 changed files with 14 additions and 14 deletions
|
|
@ -23,7 +23,7 @@ impl AtomicMin {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unset value is usize_max
|
/// Unset value is usize_max
|
||||||
pub fn as_atomic_usize(&self) -> &AtomicUsize {
|
pub const fn as_atomic_usize(&self) -> &AtomicUsize {
|
||||||
&self.val
|
&self.val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ impl Display for ColorType {
|
||||||
impl ColorType {
|
impl ColorType {
|
||||||
/// Get the code used by the PNG specification to denote this color type
|
/// Get the code used by the PNG specification to denote this color type
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn png_header_code(&self) -> u8 {
|
pub const fn png_header_code(&self) -> u8 {
|
||||||
match self {
|
match self {
|
||||||
ColorType::Grayscale { .. } => 0,
|
ColorType::Grayscale { .. } => 0,
|
||||||
ColorType::RGB { .. } => 2,
|
ColorType::RGB { .. } => 2,
|
||||||
|
|
@ -56,7 +56,7 @@ impl ColorType {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn channels_per_pixel(&self) -> u8 {
|
pub(crate) const fn channels_per_pixel(&self) -> u8 {
|
||||||
match self {
|
match self {
|
||||||
ColorType::Grayscale { .. } | ColorType::Indexed { .. } => 1,
|
ColorType::Grayscale { .. } | ColorType::Indexed { .. } => 1,
|
||||||
ColorType::GrayscaleAlpha => 2,
|
ColorType::GrayscaleAlpha => 2,
|
||||||
|
|
@ -66,12 +66,12 @@ impl ColorType {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn is_rgb(&self) -> bool {
|
pub(crate) const fn is_rgb(&self) -> bool {
|
||||||
matches!(self, ColorType::RGB { .. } | ColorType::RGBA)
|
matches!(self, ColorType::RGB { .. } | ColorType::RGBA)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn is_gray(&self) -> bool {
|
pub(crate) const fn is_gray(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
self,
|
self,
|
||||||
ColorType::Grayscale { .. } | ColorType::GrayscaleAlpha
|
ColorType::Grayscale { .. } | ColorType::GrayscaleAlpha
|
||||||
|
|
@ -79,12 +79,12 @@ impl ColorType {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn has_alpha(&self) -> bool {
|
pub(crate) const fn has_alpha(&self) -> bool {
|
||||||
matches!(self, ColorType::GrayscaleAlpha | ColorType::RGBA)
|
matches!(self, ColorType::GrayscaleAlpha | ColorType::RGBA)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn has_trns(&self) -> bool {
|
pub(crate) const fn has_trns(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
ColorType::Grayscale { transparent_shade } => transparent_shade.is_some(),
|
ColorType::Grayscale { transparent_shade } => transparent_shade.is_some(),
|
||||||
ColorType::RGB { transparent_color } => transparent_color.is_some(),
|
ColorType::RGB { transparent_color } => transparent_color.is_some(),
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ impl IhdrData {
|
||||||
/// Bits per pixel
|
/// Bits per pixel
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn bpp(&self) -> usize {
|
pub const fn bpp(&self) -> usize {
|
||||||
self.bit_depth as usize * self.color_type.channels_per_pixel() as usize
|
self.bit_depth as usize * self.color_type.channels_per_pixel() as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,7 +38,7 @@ impl IhdrData {
|
||||||
let h = self.height as usize;
|
let h = self.height as usize;
|
||||||
let bpp = self.bpp();
|
let bpp = self.bpp();
|
||||||
|
|
||||||
fn bitmap_size(bpp: usize, w: usize, h: usize) -> usize {
|
const fn bitmap_size(bpp: usize, w: usize, h: usize) -> usize {
|
||||||
((w * bpp + 7) / 8) * h
|
((w * bpp + 7) / 8) * h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,7 @@ impl Options {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_preset_2(self) -> Self {
|
const fn apply_preset_2(self) -> Self {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -904,7 +904,7 @@ fn postprocess_chunks<T>(png: &mut PngData, opts: &Options, orig_ihdr: &IhdrData
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if an image was already optimized prior to oxipng's operations
|
/// Check if an image was already optimized prior to oxipng's operations
|
||||||
fn is_fully_optimized(original_size: usize, optimized_size: usize, opts: &Options) -> bool {
|
const fn is_fully_optimized(original_size: usize, optimized_size: usize, opts: &Options) -> bool {
|
||||||
original_size <= optimized_size && !opts.force
|
original_size <= optimized_size && !opts.force
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -241,13 +241,13 @@ impl PngImage {
|
||||||
|
|
||||||
/// Return the number of channels in the image, based on color type
|
/// Return the number of channels in the image, based on color type
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn channels_per_pixel(&self) -> usize {
|
pub const fn channels_per_pixel(&self) -> usize {
|
||||||
self.ihdr.color_type.channels_per_pixel() as usize
|
self.ihdr.color_type.channels_per_pixel() as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the number of bytes per channel in the image
|
/// Return the number of bytes per channel in the image
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn bytes_per_channel(&self) -> usize {
|
pub const fn bytes_per_channel(&self) -> usize {
|
||||||
match self.ihdr.bit_depth {
|
match self.ihdr.bit_depth {
|
||||||
BitDepth::Sixteen => 2,
|
BitDepth::Sixteen => 2,
|
||||||
// Depths lower than 8 will round up to 1 byte
|
// Depths lower than 8 will round up to 1 byte
|
||||||
|
|
@ -472,7 +472,7 @@ fn write_png_block(key: &[u8], chunk: &[u8], output: &mut Vec<u8>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Integer approximation for i * log2(i) - much faster than float calculations
|
// Integer approximation for i * log2(i) - much faster than float calculations
|
||||||
fn ilog2i(i: u32) -> u32 {
|
const fn ilog2i(i: u32) -> u32 {
|
||||||
let log = 32 - i.leading_zeros() - 1;
|
let log = 32 - i.leading_zeros() - 1;
|
||||||
i * log + ((i - (1 << log)) << 1)
|
i * log + ((i - (1 << log)) << 1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue