Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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 embedded-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ fixed_point = [ "fixed" ]

[dev-dependencies]
arrayvec = { version = "0.5.1", default-features = false }
tinytga = { version = "0.3.2", features = [ "graphics" ] }
tinytga = { version = "0.3.2" }
6 changes: 3 additions & 3 deletions embedded-graphics/src/image/image_drawable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ pub trait ImageDrawableExt: Sized {
/// display, with their top-left corners positioned at `(100, 100)` and `(100, 140)`.
///
/// ```rust
/// use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*, primitives::Rectangle};
/// use embedded_graphics::{image::Image, pixelcolor::Rgb888, prelude::*, primitives::Rectangle};
/// # use embedded_graphics::mock_display::MockDisplay as Display;
/// use tinytga::Tga;
///
/// let mut display: Display<Rgb565> = Display::default();
/// let mut display: Display<Rgb888> = Display::default();
///
/// let sprite_atlas: Tga<Rgb565> = Tga::from_slice(include_bytes!(
/// let sprite_atlas: Tga<Rgb888> = Tga::from_slice(include_bytes!(
/// "../../../assets/tiles.tga"
/// ))
/// .unwrap();
Expand Down
12 changes: 6 additions & 6 deletions embedded-graphics/src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
//! with embedded-graphics.
//!
//! ```rust
//! use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*};
//! use embedded_graphics::{image::Image, pixelcolor::Rgb888, prelude::*};
//! # use embedded_graphics::mock_display::MockDisplay as Display;
//! use tinytga::Tga;
//!
//! let mut display: Display<Rgb565> = Display::default();
//! let mut display: Display<Rgb888> = Display::default();
//!
//! // Load the TGA file.
//! // Note that the color type is set explicitly to match the format used in the TGA file,
//! // otherwise the compiler might infer an incorrect type.
//! let tga: Tga<Rgb565> = Tga::from_slice(include_bytes!(
//! let tga: Tga<Rgb888> = Tga::from_slice(include_bytes!(
//! "../../../simulator/examples/assets/rust-pride.tga"
//! ))
//! .unwrap();
Expand All @@ -51,16 +51,16 @@
//! which this example takes advantage of.
//!
//! ```rust
//! use embedded_graphics::{image::Image, pixelcolor::Rgb565, prelude::*, primitives::Rectangle};
//! use embedded_graphics::{image::Image, pixelcolor::Rgb888, prelude::*, primitives::Rectangle};
//! # use embedded_graphics::mock_display::MockDisplay as Display;
//! use tinytga::Tga;
//!
//! let mut display: Display<Rgb565> = Display::default();
//! let mut display: Display<Rgb888> = Display::default();
//!
//! // Load the TGA file with the sprite atlas.
//! // Note that the color type is set explicitly to match the format used in the TGA file,
//! // otherwise the compiler might infer an incorrect type.
//! let sprite_atlas: Tga<Rgb565> = Tga::from_slice(include_bytes!(
//! let sprite_atlas: Tga<Rgb888> = Tga::from_slice(include_bytes!(
//! "../../../assets/tiles.tga"
//! ))
//! .unwrap();
Expand Down
2 changes: 1 addition & 1 deletion simulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ name = "contains"

[dependencies]
image = "0.23.0"
tinytga = { version = "0.3.2", features = [ "graphics" ] }
tinytga = { version = "0.3.2" }

[dependencies.sdl2]
version = "0.32.2"
Expand Down
12 changes: 10 additions & 2 deletions tinytga/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@
### Changed

- **(breaking)** [#407](https://github.com/jamwaffles/embedded-graphics/pull/407) The `image_descriptor` in `TgaHeader` was replaced by `image_origin` and `alpha_channel_bits`.
- **(breaking)** [#407](https://github.com/jamwaffles/embedded-graphics/pull/407) The `Pixel` type returned by `TgaIterator` now uses `u16` coordinates.
- **(breaking)** [#420](https://github.com/jamwaffles/embedded-graphics/pull/420) To support the new embedded-graphics 0.7 image API a color type parameter was added to `Tga`. To use this crate without the `graphics` flag enabled replace `Tga` by `TgaRaw`.
- **(breaking)** [#420](https://github.com/jamwaffles/embedded-graphics/pull/420) To support the new embedded-graphics 0.7 image API a color type parameter was added to `Tga`.
- **(breaking)** [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) The `graphics` feature was removed and the `embedded-graphics` dependency is now non optional.
- **(breaking)** [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) `Tga` no longer implements `IntoIterator`. Pixel iterators can now be created using the `pixels` and `raw_pixels` methods.
- **(breaking)** [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) `Tga::from_slice` now checks that the specified color type matches the bit depth of the image.
- **(breaking)** [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) The `TgaFooter` struct was replaced by the `raw_developer_dictionary` and `raw_extension_area` methods in `Tga`.
- **(breaking)** [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) `Tga::width` and `Tga::height` were replaced by `Tga::size` which requires `embedded_graphics::geometry::OriginDimensions` to be in scope (also included in the embedded-graphics `prelude`).
- **(breaking)** [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) The color map can now be accessed using the new `ColorMap` type.

### Added

- [#407](https://github.com/jamwaffles/embedded-graphics/pull/407) Added support for bottom-left origin images to `TgaIterator`.
- [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) The image ID can now be accessed using `Tga::image_id`.

### Fixed

- [#407](https://github.com/jamwaffles/embedded-graphics/pull/407) Additional data in `pixel_data`, beyond `width * height` pixels, is now discarded by `TgaIterator`.
- [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) Images with unsupported BPP values in the header no longer cause panics. Instead an error is returned by `from_slice`.
- [#430](https://github.com/jamwaffles/embedded-graphics/pull/430) Errors during the execution of a pixel iterator no longer cause panics. Instead the corrupted portion of the image is filled with black pixels.

## [0.3.2] - 2020-03-20

Expand Down
8 changes: 0 additions & 8 deletions tinytga/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@ exclude = [
[badges]
circle-ci = { repository = "jamwaffles/embedded-graphics", branch = "master" }

[[test]]
name = "embedded_graphics"
required-features = ["graphics"]

[dependencies.nom]
version = "5.1.0"
default-features = false

[dependencies.embedded-graphics]
version = "0.6.0"
optional = true

[features]
graphics = ["embedded-graphics"]
103 changes: 59 additions & 44 deletions tinytga/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,96 @@

## [Documentation](https://docs.rs/tinytga)

A small TGA parser designed for embedded, no-std environments but usable anywhere. Beyond
parsing the image header, no other allocations are made.
A small TGA parser designed for use with [embedded-graphics] targetting no-std environments but
usable anywhere. Beyond parsing the image header, no other allocations are made.

To access the individual pixels in an image, the `TgaRaw` struct implements `IntoIterator`. It is
also possible to access the unaltered raw image data by reading the `pixel_data` field. This
data will need to be interpreted according to the `image_type` specified in the header.

## Features

* `graphics` - enables [embedded-graphics] integration.
tinytga provides two methods of accessing the pixel data inside a TGA file. The most convenient
way is to use a color type provided by [embedded-graphics] to define the format stored inside
the TGA file. But it is also possible to directly access the raw pixel representation instead.

## Examples

### Load a Run Length Encoded (RLE) TGA image

```rust
use tinytga::{ImageOrigin, ImageType, Pixel, TgaRaw, TgaFooter, TgaHeader};
use embedded_graphics::{prelude::*, pixelcolor::Rgb888};
use tinytga::{Bpp, ImageOrigin, ImageType, RawPixel, Tga, TgaHeader};

// Include an image from a local path as bytes
let data = include_bytes!("../tests/chessboard_4px_rle.tga");

// Create a TGA instance from a byte slice.
// The color type is set by defining the type of the `img` variable.
let img: Tga<Rgb888> = Tga::from_slice(data).unwrap();

// Check the size of the image.
assert_eq!(img.size(), Size::new(4, 4));

// Collect pixels into a vector.
let pixels: Vec<_> = img.pixels().collect();
```

### Drawing an image using `embedded-graphics`

This example demonstrates how a TGA image can be drawn to a [embedded-graphics] draw target.

```rust
use embedded_graphics::{image::Image, pixelcolor::Rgb888, prelude::*};
use tinytga::Tga;

// Include an image from a local path as bytes
let data = include_bytes!("../tests/chessboard_4px_rle.tga");
let tga: Tga<Rgb888> = Tga::from_slice(data).unwrap();

let image = Image::new(&tga, Point::zero());

image.draw(&mut display)?;
```

### Accessing raw pixel data

If you do not want to use the color types provided by [embedded-graphics] you can also access
the raw image data.

```rust
use embedded_graphics::{prelude::*, pixelcolor::Rgb888};
use tinytga::{Bpp, ImageOrigin, ImageType, RawPixel, Tga, TgaHeader};

// Include an image from a local path as bytes.
let data = include_bytes!("../tests/chessboard_4px_rle.tga");

// Create a TGA instance from a byte slice
let img = TgaRaw::from_slice(data).unwrap();
// Create a TGA instance from a byte slice.
let img = Tga::from_slice_raw(data).unwrap();

// Take a look at the header
// Take a look at the raw image header.
assert_eq!(
img.header,
img.raw_header(),
TgaHeader {
id_len: 0,
has_color_map: false,
image_type: ImageType::RleTruecolor,
color_map_start: 0,
color_map_len: 0,
color_map_depth: 0,
color_map_depth: None,
x_origin: 0,
y_origin: 4,
width: 4,
height: 4,
pixel_depth: 24,
pixel_depth: Bpp::Bits24,
image_origin: ImageOrigin::TopLeft,
alpha_channel_depth: 0,
}
);

// Take a look at the footer
assert_eq!(
img.footer,
Some(TgaFooter {
extension_area_offset: 0,
developer_directory_offset: 0
})
);

// Collect pixels into a `Vec<Pixel>`
let pixels = img.into_iter().collect::<Vec<Pixel>>();
// Collect raw pixels into a vector.
let pixels: Vec<_> = img.raw_pixels().collect();
```

### Use with `embedded-graphics`

This example demonstrates [embedded-graphics] support by rendering a TGA image to a mock
display.
## Embedded-graphics drawing performance

The `graphics` feature of `tinytga` needs to be enabled in `Cargo.toml` to use the `Tga` object
with embedded-graphics.

```rust
use embedded_graphics::{image::Image, pixelcolor::Rgb888, prelude::*};
use tinytga::Tga;

let tga: Tga<Rgb888> = Tga::from_slice(include_bytes!("../tests/rust-rle-bw-topleft.tga")).unwrap();

let image = Image::new(&tga, Point::zero());

image.draw(&mut display)?;
```
`tinytga` uses different code paths to draw images with different `ImageOrigin` .
The performance difference between the origins will depend on the display driver, but using
images with the origin at the top left corner will generally result in the best performance.

[embedded-graphics]: https://docs.rs/embedded-graphics

Expand Down
75 changes: 75 additions & 0 deletions tinytga/src/color_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::{parse_error::ParseError, Bpp, TgaHeader};
use nom::bytes::complete::take;

/// Color map.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct ColorMap<'a> {
/// First color index.
start_index: u16,
/// Number of entries.
length: u16,
/// Entry bit depth.
entry_bpp: Bpp,
/// Color map data.
data: &'a [u8],
}

impl<'a> ColorMap<'a> {
pub(crate) fn parse(
input: &'a [u8],
header: &TgaHeader,
) -> Result<(&'a [u8], Option<Self>), ParseError> {
if !header.has_color_map {
return Ok((input, None));
}

let entry_bpp = header.color_map_depth.ok_or(ParseError::ColorMap)?;

let length = usize::from(header.color_map_len) * usize::from(entry_bpp.bytes());

let (input, color_map_data) =
take(length)(input).map_err(|_: nom::Err<()>| ParseError::ColorMap)?;

Ok((
input,
Some(Self {
start_index: header.color_map_start,
length: header.color_map_len,
entry_bpp,
data: color_map_data,
}),
))
}

/// Returns the bit depth for the entries in the color map.
pub fn entry_bpp(&self) -> Bpp {
self.entry_bpp
}

/// Returns the raw color value for a color map entry.
pub fn get_raw(&self, index: usize) -> Option<u32> {
//TODO: use start_index
if index >= usize::from(self.length) {
return None;
}

let start = index * usize::from(self.entry_bpp.bytes());

Some(match self.entry_bpp {
Bpp::Bits8 => self.data[start] as u32,
Bpp::Bits16 => u32::from_le_bytes([self.data[start], self.data[start + 1], 0, 0]),
Bpp::Bits24 => u32::from_le_bytes([
self.data[start],
self.data[start + 1],
self.data[start + 2],
0,
]),
Bpp::Bits32 => u32::from_le_bytes([
self.data[start],
self.data[start + 1],
self.data[start + 2],
self.data[start + 3],
]),
})
}
}
64 changes: 0 additions & 64 deletions tinytga/src/embedded_graphics.rs

This file was deleted.

Loading