use super::header::{Header, ImageType, ALPHA_BIT_MASK, SCREEN_ORIGIN_BIT_MASK};
use crate::{
color::{ColorType, ExtendedColorType},
error::{
ImageError, ImageResult, LimitError, LimitErrorKind, UnsupportedError, UnsupportedErrorKind,
},
image::{ImageDecoder, ImageFormat, ImageReadBuffer},
};
use byteorder::ReadBytesExt;
use std::{
io::{self, Read, Seek},
mem,
};
struct ColorMap {
start_offset: usize,
entry_size: usize,
bytes: Vec<u8>,
}
impl ColorMap {
pub(crate) fn from_reader(
r: &mut dyn Read,
start_offset: u16,
num_entries: u16,
bits_per_entry: u8,
) -> ImageResult<ColorMap> {
let bytes_per_entry = (bits_per_entry as usize + 7) / 8;
let mut bytes = vec![0; bytes_per_entry * num_entries as usize];
r.read_exact(&mut bytes)?;
Ok(ColorMap {
entry_size: bytes_per_entry,
start_offset: start_offset as usize,
bytes,
})
}
pub(crate) fn get(&self, index: usize) -> Option<&[u8]> {
let entry = self.start_offset + self.entry_size * index;
self.bytes.get(entry..entry + self.entry_size)
}
}
pub struct TgaDecoder<R> {
r: R,
width: usize,
height: usize,
bytes_per_pixel: usize,
has_loaded_metadata: bool,
image_type: ImageType,
color_type: ColorType,
original_color_type: Option<ExtendedColorType>,
header: Header,
color_map: Option<ColorMap>,
line_read: Option<usize>,
line_remain_buff: Vec<u8>,
}
impl<R: Read + Seek> TgaDecoder<R> {
pub fn new(r: R) -> ImageResult<TgaDecoder<R>> {
let mut decoder = TgaDecoder {
r,
width: 0,
height: 0,
bytes_per_pixel: 0,
has_loaded_metadata: false,
image_type: ImageType::Unknown,
color_type: ColorType::L8,
original_color_type: None,
header: Header::default(),
color_map: None,
line_read: None,
line_remain_buff: Vec::new(),
};
decoder.read_metadata()?;
Ok(decoder)
}
fn read_header(&mut self) -> ImageResult<()> {
self.header = Header::from_reader(&mut self.r)?;
self.image_type = ImageType::new(self.header.image_type);
self.width = self.header.image_width as usize;
self.height = self.header.image_height as usize;
self.bytes_per_pixel = (self.header.pixel_depth as usize + 7) / 8;
Ok(())
}
fn read_metadata(&mut self) -> ImageResult<()> {
if !self.has_loaded_metadata {
self.read_header()?;
self.read_image_id()?;
self.read_color_map()?;
self.read_color_information()?;
self.has_loaded_metadata = true;
}
Ok(())
}
fn read_color_information(&mut self) -> ImageResult<()> {
if self.header.pixel_depth % 8 != 0 || self.header.pixel_depth > 32 {
return Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
ImageFormat::Tga.into(),
UnsupportedErrorKind::Color(ExtendedColorType::Unknown(
self.header.pixel_depth,
)),
),
));
}
let num_alpha_bits = self.header.image_desc & ALPHA_BIT_MASK;
let other_channel_bits = if self.header.map_type != 0 {
self.header.map_entry_size
} else {
if num_alpha_bits > self.header.pixel_depth {
return Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
ImageFormat::Tga.into(),
UnsupportedErrorKind::Color(ExtendedColorType::Unknown(
self.header.pixel_depth,
)),
),
));
}
self.header.pixel_depth - num_alpha_bits
};
let color = self.image_type.is_color();
match (num_alpha_bits, other_channel_bits, color) {
(0, 32, true) => self.color_type = ColorType::Rgba8,
(8, 24, true) => self.color_type = ColorType::Rgba8,
(0, 24, true) => self.color_type = ColorType::Rgb8,
(8, 8, false) => self.color_type = ColorType::La8,
(0, 8, false) => self.color_type = ColorType::L8,
(8, 0, false) => {
self.color_type = ColorType::L8;
self.original_color_type = Some(ExtendedColorType::A8);
}
_ => {
return Err(ImageError::Unsupported(
UnsupportedError::from_format_and_kind(
ImageFormat::Tga.into(),
UnsupportedErrorKind::Color(ExtendedColorType::Unknown(
self.header.pixel_depth,
)),
),
))
}
}
Ok(())
}
fn read_image_id(&mut self) -> ImageResult<()> {
self.r
.seek(io::SeekFrom::Current(i64::from(self.header.id_length)))?;
Ok(())
}
fn read_color_map(&mut self) -> ImageResult<()> {
if self.header.map_type == 1 {
self.color_map = Some(ColorMap::from_reader(
&mut self.r,
self.header.map_origin,
self.header.map_length,
self.header.map_entry_size,
)?);
}
Ok(())
}
fn expand_color_map(&self, pixel_data: &[u8]) -> io::Result<Vec<u8>> {
#[inline]
fn bytes_to_index(bytes: &[u8]) -> usize {
let mut result = 0usize;
for byte in bytes.iter() {
result = result << 8 | *byte as usize;
}
result
}
let bytes_per_entry = (self.header.map_entry_size as usize + 7) / 8;
let mut result = Vec::with_capacity(self.width * self.height * bytes_per_entry);
if self.bytes_per_pixel == 0 {
return Err(io::ErrorKind::Other.into());
}
let color_map = self
.color_map
.as_ref()
.ok_or_else(|| io::Error::from(io::ErrorKind::Other))?;
for chunk in pixel_data.chunks(self.bytes_per_pixel) {
let index = bytes_to_index(chunk);
if let Some(color) = color_map.get(index) {
result.extend_from_slice(color);
} else {
return Err(io::ErrorKind::Other.into());
}
}
Ok(result)
}
fn read_encoded_data(&mut self, num_bytes: usize) -> io::Result<Vec<u8>> {
let mut pixel_data = Vec::with_capacity(num_bytes);
let mut repeat_buf = Vec::with_capacity(self.bytes_per_pixel);
while pixel_data.len() < num_bytes {
let run_packet = self.r.read_u8()?;
if (run_packet & 0x80) != 0 {
let repeat_count = ((run_packet & !0x80) + 1) as usize;
self.r
.by_ref()
.take(self.bytes_per_pixel as u64)
.read_to_end(&mut repeat_buf)?;
let data = repeat_buf
.iter()
.cycle()
.take(repeat_count * self.bytes_per_pixel);
pixel_data.extend(data);
repeat_buf.clear();
} else {
let num_raw_bytes = (run_packet + 1) as usize * self.bytes_per_pixel;
self.r
.by_ref()
.take(num_raw_bytes as u64)
.read_to_end(&mut pixel_data)?;
}
}
if pixel_data.len() > num_bytes {
pixel_data.truncate(num_bytes);
}
Ok(pixel_data)
}
fn read_all_encoded_data(&mut self) -> ImageResult<Vec<u8>> {
let num_bytes = self.width * self.height * self.bytes_per_pixel;
Ok(self.read_encoded_data(num_bytes)?)
}
fn read_encoded_line(&mut self) -> io::Result<Vec<u8>> {
let line_num_bytes = self.width * self.bytes_per_pixel;
let remain_len = self.line_remain_buff.len();
if remain_len >= line_num_bytes {
let bytes = {
let bytes_after = self.line_remain_buff.split_off(line_num_bytes);
mem::replace(&mut self.line_remain_buff, bytes_after)
};
return Ok(bytes);
}
let num_bytes = line_num_bytes - remain_len;
let line_data = self.read_encoded_data(num_bytes)?;
let mut pixel_data = Vec::with_capacity(line_num_bytes);
pixel_data.append(&mut self.line_remain_buff);
pixel_data.extend_from_slice(&line_data[..num_bytes]);
debug_assert!(self.line_remain_buff.is_empty());
self.line_remain_buff
.extend_from_slice(&line_data[num_bytes..]);
Ok(pixel_data)
}
fn reverse_encoding_in_output(&mut self, pixels: &mut [u8]) {
match self.color_type {
ColorType::Rgb8 | ColorType::Rgba8 => {
for chunk in pixels.chunks_mut(self.color_type.bytes_per_pixel().into()) {
chunk.swap(0, 2);
}
}
_ => {}
}
}
fn flip_vertically(&mut self, pixels: &mut [u8]) {
if self.is_flipped_vertically() {
if self.height == 0 {
return;
}
let num_bytes = pixels.len();
let width_bytes = num_bytes / self.height;
for vertical_index in 0..(self.height / 2) {
let vertical_target = (self.height - vertical_index) * width_bytes - width_bytes;
for horizontal_index in 0..width_bytes {
let source = vertical_index * width_bytes + horizontal_index;
let target = vertical_target + horizontal_index;
pixels.swap(target, source);
}
}
}
}
fn is_flipped_vertically(&self) -> bool {
let screen_origin_bit = SCREEN_ORIGIN_BIT_MASK & self.header.image_desc != 0;
!screen_origin_bit
}
fn read_scanline(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if let Some(line_read) = self.line_read {
if line_read == self.height {
return Ok(0);
}
}
let mut pixel_data = if self.image_type.is_encoded() {
self.read_encoded_line()?
} else {
let num_raw_bytes = self.width * self.bytes_per_pixel;
let mut buf = vec![0; num_raw_bytes];
self.r.by_ref().read_exact(&mut buf)?;
buf
};
if self.image_type.is_color_mapped() {
pixel_data = self.expand_color_map(&pixel_data)?;
}
self.reverse_encoding_in_output(&mut pixel_data);
buf[..pixel_data.len()].copy_from_slice(&pixel_data);
self.line_read = Some(self.line_read.unwrap_or(0) + 1);
Ok(pixel_data.len())
}
}
impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for TgaDecoder<R> {
type Reader = TGAReader<R>;
fn dimensions(&self) -> (u32, u32) {
(self.width as u32, self.height as u32)
}
fn color_type(&self) -> ColorType {
self.color_type
}
fn original_color_type(&self) -> ExtendedColorType {
self.original_color_type
.unwrap_or_else(|| self.color_type().into())
}
fn scanline_bytes(&self) -> u64 {
u64::from(self.color_type.bytes_per_pixel()) * self.width as u64
}
fn into_reader(self) -> ImageResult<Self::Reader> {
Ok(TGAReader {
buffer: ImageReadBuffer::new(
#[allow(deprecated)]
self.scanline_bytes(),
self.total_bytes(),
),
decoder: self,
})
}
fn read_image(mut self, buf: &mut [u8]) -> ImageResult<()> {
assert_eq!(u64::try_from(buf.len()), Ok(self.total_bytes()));
let mut fallback_buf = vec![];
let rawbuf = if self.image_type.is_encoded() {
let pixel_data = self.read_all_encoded_data()?;
if self.bytes_per_pixel <= usize::from(self.color_type.bytes_per_pixel()) {
buf[..pixel_data.len()].copy_from_slice(&pixel_data);
&buf[..pixel_data.len()]
} else {
fallback_buf = pixel_data;
&fallback_buf[..]
}
} else {
let num_raw_bytes = self.width * self.height * self.bytes_per_pixel;
if self.bytes_per_pixel <= usize::from(self.color_type.bytes_per_pixel()) {
self.r.by_ref().read_exact(&mut buf[..num_raw_bytes])?;
&buf[..num_raw_bytes]
} else {
fallback_buf.resize(num_raw_bytes, 0u8);
self.r
.by_ref()
.read_exact(&mut fallback_buf[..num_raw_bytes])?;
&fallback_buf[..num_raw_bytes]
}
};
if self.image_type.is_color_mapped() {
let pixel_data = self.expand_color_map(rawbuf)?;
if pixel_data.len() != buf.len() {
return Err(ImageError::Limits(LimitError::from_kind(
LimitErrorKind::DimensionError,
)));
}
buf.copy_from_slice(&pixel_data);
}
self.reverse_encoding_in_output(buf);
self.flip_vertically(buf);
Ok(())
}
}
pub struct TGAReader<R> {
buffer: ImageReadBuffer,
decoder: TgaDecoder<R>,
}
impl<R: Read + Seek> Read for TGAReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let decoder = &mut self.decoder;
self.buffer.read(buf, |buf| decoder.read_scanline(buf))
}
}