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

Remove Bgr and Bgra color types #1482

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ All image processing functions provided operate on types that implement the `Gen
| AVIF | Only 8-bit | Lossy |
| PNM | PBM, PGM, PPM, standard PAM | Yes |
| DDS | DXT1, DXT3, DXT5 | No |
| TGA | Yes | RGB(8), RGBA(8), BGR(8), BGRA(8), Gray(8), GrayA(8) |
| TGA | Yes | RGB(8), RGBA(8), Gray(8), GrayA(8) |
| farbfeld | Yes | Yes |

### The [`ImageDecoder`](https://docs.rs/image/*/image/trait.ImageDecoder.html) and [`ImageDecoderExt`](https://docs.rs/image/*/image/trait.ImageDecoderExt.html) Traits
Expand Down
6 changes: 1 addition & 5 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::{Deref, DerefMut, Index, IndexMut, Range};
use std::path::Path;
use std::slice::{ChunksExact, ChunksExactMut};

use crate::color::{FromColor, Luma, LumaA, Rgb, Rgba, Bgr, Bgra};
use crate::color::{FromColor, Luma, LumaA, Rgb, Rgba};
use crate::flat::{FlatSamples, SampleLayout};
use crate::dynimage::{save_buffer, save_buffer_with_format};
use crate::error::ImageResult;
Expand Down Expand Up @@ -1296,10 +1296,6 @@ pub type RgbaImage = ImageBuffer<Rgba<u8>, Vec<u8>>;
pub type GrayImage = ImageBuffer<Luma<u8>, Vec<u8>>;
/// Sendable grayscale + alpha channel image buffer
pub type GrayAlphaImage = ImageBuffer<LumaA<u8>, Vec<u8>>;
/// Sendable Bgr image buffer
pub(crate) type BgrImage = ImageBuffer<Bgr<u8>, Vec<u8>>;
/// Sendable Bgr + alpha channel image buffer
pub(crate) type BgraImage = ImageBuffer<Bgra<u8>, Vec<u8>>;
/// Sendable 16-bit Rgb image buffer
pub(crate) type Rgb16Image = ImageBuffer<Rgb<u16>, Vec<u16>>;
/// Sendable 16-bit Rgb + alpha channel image buffer
Expand Down
7 changes: 6 additions & 1 deletion src/codecs/avif/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R> {
}

fn color_type(&self) -> ColorType {
ColorType::Bgra8
ColorType::Rgba8
}

fn into_reader(self) -> ImageResult<Self::Reader> {
Expand Down Expand Up @@ -158,6 +158,11 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for AvifDecoder<R> {
}
}

// Convert Bgra to Rgba
for chunk in buf.chunks_exact_mut(4) {
chunk.swap(0, 2);
}
HeroicKatora marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}
}
10 changes: 1 addition & 9 deletions src/codecs/avif/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::cmp::min;
use crate::{ColorType, ImageBuffer, ImageFormat, Pixel};
use crate::{ImageError, ImageResult};
use crate::buffer::ConvertBuffer;
use crate::color::{FromColor, Luma, LumaA, Bgr, Bgra, Rgb, Rgba};
use crate::color::{FromColor, Luma, LumaA, Rgb, Rgba};
use crate::error::{EncodingError, ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind};

use bytemuck::{Pod, PodCastError, try_cast_slice, try_cast_slice_mut};
Expand Down Expand Up @@ -190,14 +190,6 @@ impl<W: Write> AvifEncoder<W> {
let image = try_from_raw::<Rgb<u8>>(data, width, height)?;
Ok(convert_into(&mut self.fallback, image))
}
ColorType::Bgr8 => {
let image = try_from_raw::<Bgr<u8>>(data, width, height)?;
Ok(convert_into(&mut self.fallback, image))
}
ColorType::Bgra8 => {
let image = try_from_raw::<Bgra<u8>>(data, width, height)?;
Ok(convert_into(&mut self.fallback, image))
}
// we need to really convert data..
ColorType::L16 => {
let buffer = cast_buffer(data)?;
Expand Down
28 changes: 2 additions & 26 deletions src/codecs/jpeg/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::{self, Write};

use num_iter::range_step;

use crate::{Bgr, Bgra, ColorType, GenericImageView, ImageBuffer, Luma, LumaA, Pixel, Rgb, Rgba};
use crate::{ColorType, GenericImageView, ImageBuffer, Luma, LumaA, Pixel, Rgb, Rgba};
use crate::error::{ImageError, ImageResult, ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind};
use crate::image::{ImageEncoder, ImageFormat};
use crate::utils::clamp;
Expand Down Expand Up @@ -460,14 +460,6 @@ impl<'a, W: Write> JpegEncoder<'a, W> {
let image: ImageBuffer<Rgba<_>, _> = ImageBuffer::from_raw(width, height, image).unwrap();
self.encode_image(&image)
},
ColorType::Bgr8 => {
let image: ImageBuffer<Bgr<_>, _> = ImageBuffer::from_raw(width, height, image).unwrap();
self.encode_image(&image)
},
ColorType::Bgra8 => {
let image: ImageBuffer<Bgra<_>, _> = ImageBuffer::from_raw(width, height, image).unwrap();
self.encode_image(&image)
},
_ => {
Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
Expand Down Expand Up @@ -868,7 +860,7 @@ mod tests {
#[cfg(feature = "benchmarks")]
use test::{Bencher};

use crate::{Bgra, ImageBuffer, ImageEncoder, ImageError};
use crate::{ImageBuffer, ImageEncoder, ImageError};
use crate::color::ColorType;
use crate::error::ParameterErrorKind::DimensionMismatch;
use crate::image::ImageDecoder;
Expand Down Expand Up @@ -987,22 +979,6 @@ mod tests {
}
}

#[test]
fn test_bgra16() {
// Test encoding an RGBA 16-bit image.
// Jpeg is RGB 8-bit, so the conversion should be done on the fly
let mut encoded = Vec::new();
let max = std::u16::MAX;
let image: ImageBuffer<Bgra<u16>, _> = ImageBuffer::from_raw(
1, 1, vec![0, max / 2, max, max]).unwrap();
let mut encoder = JpegEncoder::new_with_quality(&mut encoded, 100);
encoder.encode_image(&image).unwrap();
let decoded = decode(&encoded);
assert!(decoded[0] > 200, "bad red channel in {:?}", &decoded);
assert!(100 < decoded[1] && decoded[1] < 150, "bad green channel in {:?}", &decoded);
assert!(decoded[2] < 50, "bad blue channel in {:?}", &decoded);
}

#[test]
fn test_build_jfif_header() {
let mut buf = vec![];
Expand Down
16 changes: 0 additions & 16 deletions src/codecs/tga/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,6 @@ mod tests {
assert_eq!(decoded.as_slice(), image);
}

#[test]
fn round_trip_single_pixel_bgr() {
let image = [0, 1, 2];
let decoded = round_trip_image(&image, 1, 1, ColorType::Bgr8);
assert_eq!(decoded.len(), image.len());
assert_eq!(decoded.as_slice(), [2, 1, 0]);
}

#[test]
fn round_trip_single_pixel_bgra() {
let image = [0, 1, 2, 3];
let decoded = round_trip_image(&image, 1, 1, ColorType::Bgra8);
assert_eq!(decoded.len(), image.len());
assert_eq!(decoded.as_slice(), [2, 1, 0, 3]);
}

#[test]
fn round_trip_gray() {
let image = [0, 1, 2];
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/tga/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl Header {

if width > 0 && height > 0 {
let (num_alpha_bits, other_channel_bits, image_type) = match color_type {
ColorType::Rgba8 | ColorType::Bgra8 => (8, 24, ImageType::RawTrueColor),
ColorType::Rgb8 | ColorType::Bgr8 => (0, 24, ImageType::RawTrueColor),
ColorType::Rgba8 => (8, 24, ImageType::RawTrueColor),
ColorType::Rgb8 => (0, 24, ImageType::RawTrueColor),
ColorType::La8 => (8, 8, ImageType::RawGrayScale),
ColorType::L8 => (0, 8, ImageType::RawGrayScale),
_ => {
Expand Down
Loading