Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ExtendedColorType when encoding #2142

Merged
merged 7 commits into from
Feb 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cargo fmt
fintelia committed Feb 19, 2024
commit df271ef8f0bad5bcbfd25b8cbbc95460b4921f85
7 changes: 5 additions & 2 deletions src/codecs/bmp/encoder.rs
Original file line number Diff line number Diff line change
@@ -287,7 +287,10 @@ fn get_unsupported_error_message(c: ExtendedColorType) -> String {
}

/// Returns a tuple representing: (dib header size, written pixel size, palette color count).
fn get_pixel_info(c: ExtendedColorType, palette: Option<&[[u8; 3]]>) -> io::Result<(u32, u32, u32)> {
fn get_pixel_info(
c: ExtendedColorType,
palette: Option<&[[u8; 3]]>,
) -> io::Result<(u32, u32, u32)> {
let sizes = match c {
ExtendedColorType::Rgb8 => (BITMAPINFOHEADER_SIZE, 3, 0),
ExtendedColorType::Rgba8 => (BITMAPV4HEADER_SIZE, 4, 0),
@@ -316,7 +319,7 @@ fn get_pixel_info(c: ExtendedColorType, palette: Option<&[[u8; 3]]>) -> io::Resu
mod tests {
use super::super::BmpDecoder;
use super::BmpEncoder;

use crate::image::ImageDecoder;
use crate::ExtendedColorType;
use std::io::Cursor;
8 changes: 6 additions & 2 deletions src/codecs/ico/encoder.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ use byteorder::{LittleEndian, WriteBytesExt};
use std::borrow::Cow;
use std::io::{self, Write};


use crate::error::{ImageError, ImageResult, ParameterError, ParameterErrorKind};
use crate::image::ImageEncoder;

@@ -73,7 +72,12 @@ impl<'a> IcoFrame<'a> {
/// Construct a new `IcoFrame` by encoding `buf` as a PNG
///
/// The `width` and `height` must be between 1 and 256 (inclusive)
pub fn as_png(buf: &[u8], width: u32, height: u32, color_type: ExtendedColorType) -> ImageResult<Self> {
pub fn as_png(
buf: &[u8],
width: u32,
height: u32,
color_type: ExtendedColorType,
) -> ImageResult<Self> {
let mut image_data: Vec<u8> = Vec::new();
PngEncoder::new(&mut image_data).write_image(buf, width, height, color_type)?;

8 changes: 6 additions & 2 deletions src/codecs/qoi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Decoding and encoding of QOI images

use crate::{
error::{DecodingError, EncodingError}, ColorType, ExtendedColorType, ImageDecoder, ImageEncoder, ImageError, ImageFormat, ImageResult
error::{DecodingError, EncodingError},
ColorType, ExtendedColorType, ImageDecoder, ImageEncoder, ImageError, ImageFormat, ImageResult,
};
use std::io::{Read, Write};

@@ -72,7 +73,10 @@ impl<W: Write> ImageEncoder for QoiEncoder<W> {
height: u32,
color_type: ExtendedColorType,
) -> ImageResult<()> {
if !matches!(color_type, ExtendedColorType::Rgba8 | ExtendedColorType::Rgb8) {
if !matches!(
color_type,
ExtendedColorType::Rgba8 | ExtendedColorType::Rgb8
) {
return Err(ImageError::Encoding(EncodingError::new(
ImageFormat::Qoi.into(),
format!("unsupported color type {color_type:?}. Supported are Rgba8 and Rgb8."),
29 changes: 23 additions & 6 deletions src/codecs/tga/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::header::Header;
use crate::{
codecs::tga::header::ImageType, error::EncodingError, ExtendedColorType, ImageEncoder, ImageError, ImageFormat, ImageResult
codecs::tga::header::ImageType, error::EncodingError, ExtendedColorType, ImageEncoder,
ImageError, ImageFormat, ImageResult,
};
use std::{convert::TryFrom, error, fmt, io::Write};

@@ -84,7 +85,11 @@ impl<W: Write> TgaEncoder<W> {
}

/// Writes the run-length encoded buffer to the writer
fn run_length_encode(&mut self, image: &[u8], color_type: ExtendedColorType) -> ImageResult<()> {
fn run_length_encode(
&mut self,
image: &[u8],
color_type: ExtendedColorType,
) -> ImageResult<()> {
use PacketType::*;

let bytes_per_pixel = color_type.bits_per_pixel() / 8;
@@ -193,7 +198,8 @@ impl<W: Write> TgaEncoder<W> {
ExtendedColorType::Rgb8 | ExtendedColorType::Rgba8 => {
let mut image = Vec::from(buf);

for pixel in image.chunks_mut(usize::from(color_type.bits_per_pixel() / 8)) {
for pixel in image.chunks_mut(usize::from(color_type.bits_per_pixel() / 8))
{
pixel.swap(0, 2);
}

@@ -211,7 +217,8 @@ impl<W: Write> TgaEncoder<W> {
ExtendedColorType::Rgb8 | ExtendedColorType::Rgba8 => {
let mut image = Vec::from(buf);

for pixel in image.chunks_mut(usize::from(color_type.bits_per_pixel() / 8)) {
for pixel in image.chunks_mut(usize::from(color_type.bits_per_pixel() / 8))
{
pixel.swap(0, 2);
}

@@ -337,7 +344,12 @@ mod tests {
mod compressed {
use super::*;

fn round_trip_image(image: &[u8], width: u32, height: u32, c: ExtendedColorType) -> Vec<u8> {
fn round_trip_image(
image: &[u8],
width: u32,
height: u32,
c: ExtendedColorType,
) -> Vec<u8> {
let mut encoded_data = Vec::new();
{
let encoder = TgaEncoder::new(&mut encoded_data);
@@ -443,7 +455,12 @@ mod tests {
mod uncompressed {
use super::*;

fn round_trip_image(image: &[u8], width: u32, height: u32, c: ExtendedColorType) -> Vec<u8> {
fn round_trip_image(
image: &[u8],
width: u32,
height: u32,
c: ExtendedColorType,
) -> Vec<u8> {
let mut encoded_data = Vec::new();
{
let encoder = TgaEncoder::new(&mut encoded_data).disable_rle();
3 changes: 2 additions & 1 deletion src/codecs/tga/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
error::{UnsupportedError, UnsupportedErrorKind}, ExtendedColorType, ImageError, ImageFormat, ImageResult
error::{UnsupportedError, UnsupportedErrorKind},
ExtendedColorType, ImageError, ImageFormat, ImageResult,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::io::{Read, Write};
8 changes: 7 additions & 1 deletion src/codecs/tiff.rs
Original file line number Diff line number Diff line change
@@ -325,7 +325,13 @@ impl<W: Write + Seek> TiffEncoder<W> {
///
/// Panics if `width * height * color_type.bytes_per_pixel() != data.len()`.
#[track_caller]
pub fn encode(self, buf: &[u8], width: u32, height: u32, color_type: ExtendedColorType) -> ImageResult<()> {
pub fn encode(
self,
buf: &[u8],
width: u32,
height: u32,
color_type: ExtendedColorType,
) -> ImageResult<()> {
let expected_buffer_len = color_type.buffer_size(width, height);
assert_eq!(
expected_buffer_len,
8 changes: 7 additions & 1 deletion src/codecs/webp/encoder.rs
Original file line number Diff line number Diff line change
@@ -30,7 +30,13 @@ impl<W: Write> WebPEncoder<W> {
///
/// Panics if `width * height * color.bytes_per_pixel() != data.len()`.
#[track_caller]
pub fn encode(self, buf: &[u8], width: u32, height: u32, color_type: ExtendedColorType) -> ImageResult<()> {
pub fn encode(
self,
buf: &[u8],
width: u32,
height: u32,
color_type: ExtendedColorType,
) -> ImageResult<()> {
let expected_buffer_len = color_type.buffer_size(width, height);
assert_eq!(
expected_buffer_len,
2 changes: 1 addition & 1 deletion src/dynimage.rs
Original file line number Diff line number Diff line change
@@ -15,11 +15,11 @@ use crate::color::{self, IntoColor};
use crate::error::{ImageError, ImageResult, ParameterError, ParameterErrorKind};
use crate::flat::FlatSamples;
use crate::image::{GenericImage, GenericImageView, ImageDecoder, ImageEncoder, ImageFormat};
use crate::{imageops, ExtendedColorType};
use crate::io::free_functions;
use crate::math::resize_dimensions;
use crate::traits::Pixel;
use crate::{image, Luma, LumaA};
use crate::{imageops, ExtendedColorType};
use crate::{Rgb32FImage, Rgba32FImage};

/// A Dynamic Image
5 changes: 4 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,10 @@
use num_traits::{Bounded, Num, NumCast};
use std::ops::AddAssign;

use crate::{color::{Luma, LumaA, Rgb, Rgba}, ExtendedColorType};
use crate::{
color::{Luma, LumaA, Rgb, Rgba},
ExtendedColorType,
};

/// Types which are safe to treat as an immutable byte slice in a pixel layout
/// for image encoding.