-
-
Notifications
You must be signed in to change notification settings - Fork 109
Improved error handling for tinytga #430
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87a5647
Improved error handling for tinytga
rfuest b16499d
Update tinytga/src/lib.rs
rfuest ce2d7cd
Update tinytga/src/lib.rs
rfuest 2c89948
Update tinytga/src/lib.rs
rfuest d27ab13
Fix link
rfuest 16bb445
Add changelog entries
rfuest 9d6ac7c
Update tinytga readme
rfuest fd4031d
Update tinytga/CHANGELOG.md
rfuest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]), | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.