Use PngResult where applicable (#752)

This commit is contained in:
Luracasmus 2025-12-10 23:34:46 +01:00 committed by GitHub
parent 0b68c08852
commit f99ce277da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 7 deletions

View file

@ -86,7 +86,7 @@ impl RawImage {
color_type: ColorType, color_type: ColorType,
bit_depth: BitDepth, bit_depth: BitDepth,
data: Vec<u8>, data: Vec<u8>,
) -> Result<Self, PngError> { ) -> PngResult<Self> {
// Validate bit depth // Validate bit depth
let valid_depth = match color_type { let valid_depth = match color_type {
ColorType::Grayscale { .. } => true, ColorType::Grayscale { .. } => true,

View file

@ -7,7 +7,7 @@ use rgb::ComponentSlice;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use crate::{ use crate::{
Options, Options, PngResult,
apng::*, apng::*,
colors::{BitDepth, ColorType}, colors::{BitDepth, ColorType},
deflate, deflate,
@ -45,18 +45,18 @@ pub struct PngData {
impl PngData { impl PngData {
/// Create a new `PngData` struct by opening a file /// Create a new `PngData` struct by opening a file
#[inline] #[inline]
pub fn new(filepath: &Path, opts: &Options) -> Result<Self, PngError> { pub fn new(filepath: &Path, opts: &Options) -> PngResult<Self> {
let byte_data = Self::read_file(filepath)?; let byte_data = Self::read_file(filepath)?;
Self::from_slice(&byte_data, opts) Self::from_slice(&byte_data, opts)
} }
pub fn read_file(filepath: &Path) -> Result<Vec<u8>, PngError> { pub fn read_file(filepath: &Path) -> PngResult<Vec<u8>> {
fs::read(filepath).map_err(|e| PngError::ReadFailed(filepath.display().to_string(), e)) fs::read(filepath).map_err(|e| PngError::ReadFailed(filepath.display().to_string(), e))
} }
/// Create a new `PngData` struct by reading a slice /// Create a new `PngData` struct by reading a slice
pub fn from_slice(byte_data: &[u8], opts: &Options) -> Result<Self, PngError> { pub fn from_slice(byte_data: &[u8], opts: &Options) -> PngResult<Self> {
let mut byte_offset: usize = 0; let mut byte_offset: usize = 0;
// Test that png header is valid // Test that png header is valid
let header = byte_data.get(0..8).ok_or(PngError::TruncatedData)?; let header = byte_data.get(0..8).ok_or(PngError::TruncatedData)?;
@ -248,7 +248,7 @@ impl PngData {
} }
impl PngImage { impl PngImage {
pub fn new(ihdr: IhdrData, compressed_data: &[u8]) -> Result<Self, PngError> { pub fn new(ihdr: IhdrData, compressed_data: &[u8]) -> PngResult<Self> {
let raw_data = deflate::inflate(compressed_data, ihdr.raw_data_size())?; let raw_data = deflate::inflate(compressed_data, ihdr.raw_data_size())?;
// Reject files with incorrect width/height or truncated data // Reject files with incorrect width/height or truncated data
@ -332,7 +332,7 @@ impl PngImage {
} }
/// Reverse all filters applied on the image, returning an unfiltered IDAT bytestream /// Reverse all filters applied on the image, returning an unfiltered IDAT bytestream
fn unfilter_image(&self) -> Result<Vec<u8>, PngError> { fn unfilter_image(&self) -> PngResult<Vec<u8>> {
let mut unfiltered = Vec::with_capacity(self.data.len()); let mut unfiltered = Vec::with_capacity(self.data.len());
let bpp = self.bytes_per_channel() * self.channels_per_pixel(); let bpp = self.bytes_per_channel() * self.channels_per_pixel();
let mut last_line: Vec<u8> = Vec::new(); let mut last_line: Vec<u8> = Vec::new();