Skip to content

Commit

Permalink
Remove BGR and BGRA color types
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia committed May 22, 2021
1 parent e402570 commit e3f81a4
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 714 deletions.
2 changes: 1 addition & 1 deletion README.md
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
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
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);
}

Ok(())
}
}
10 changes: 1 addition & 9 deletions src/codecs/avif/encoder.rs
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
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
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
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

0 comments on commit e3f81a4

Please sign in to comment.