-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add support for importing palette based on Gimp .gpl files #18
Changes from 3 commits
0b24855
ef809cf
1b28362
0af4127
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,11 @@ | |
|
||
//! Vera Palette definition | ||
|
||
extern crate gimp_palette; | ||
|
||
use crate::{Assemblable, AssembledPrimitive}; | ||
use crate::{Error, ErrorKind}; | ||
use gimp_palette::{NewPaletteError, Palette}; | ||
use std::fmt; | ||
|
||
const PALETTE_SIZE: usize = 256; | ||
|
@@ -164,6 +167,54 @@ impl VeraPalette { | |
self.entries.len() * 2 | ||
} | ||
|
||
/// Derives a palette from the given Gimp gpl file | ||
pub fn derive_from_gpl( | ||
id: &str, | ||
gpl_file: &str, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of consistency with other similar functions, it might be better to accept a vec here similar to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In its current form the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's such a simple file format that I think creating a function to read the bytes directly and return a vec of hex tuples right in the |
||
config: &VeraPaletteLoadConfig, | ||
) -> Result<Self, Error> { | ||
let gimp_palette = match Palette::read_from_file(&gpl_file) { | ||
Ok(p) => p, | ||
Err(e) => match e { | ||
NewPaletteError::NoColors => { | ||
return Err(ErrorKind::GenericError(format!("No colors loaded")).into()) | ||
} | ||
NewPaletteError::InvalidData { line_num, val } => { | ||
return Err(ErrorKind::GenericError(format!( | ||
"Line {} has invalid data: {}", | ||
line_num, val | ||
)) | ||
.into()) | ||
} | ||
NewPaletteError::IoErr(io_err) => { | ||
return Err(ErrorKind::GenericError(format!("I/O error {}", io_err)).into()) | ||
} | ||
}, | ||
}; | ||
debug!( | ||
"Palette load: Gimp palette {} with {} colors", | ||
gimp_palette.get_name(), | ||
gimp_palette.get_colors().len() | ||
); | ||
let mut palette = match config.include_defaults { | ||
true => VeraPalette::blank_with_defaults(id), | ||
false => VeraPalette::blank(id), | ||
}; | ||
for color in gimp_palette.get_colors() { | ||
palette.add_entry( | ||
config.direct_load, | ||
color.r as u8, | ||
color.g as u8, | ||
color.b as u8, | ||
)?; | ||
} | ||
if config.sort { | ||
palette.sort(); | ||
} | ||
info!("Palette creation successful"); | ||
Ok(palette) | ||
} | ||
|
||
/// Derives a palette from the given png image | ||
/// this will fail if the image > 254 distinct RGB or index values | ||
pub fn derive_from_png( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
GIMP Palette | ||
Name: TestPalette | ||
Columns: 0 | ||
# | ||
0 0 0 Untitled | ||
71 71 71 Untitled | ||
255 255 255 Untitled | ||
34 68 17 Untitled | ||
136 102 17 Untitled | ||
79 16 16 Untitled | ||
79 16 176 Untitled | ||
34 34 34 Untitled | ||
255 255 128 Untitled | ||
144 255 80 Untitled | ||
153 153 153 Untitled | ||
0 0 112 Untitled | ||
69 36 19 Untitled | ||
246 25 25 Untitled | ||
25 246 246 Untitled | ||
233 25 246 Untitled | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,3 +82,42 @@ fn palette_8bpp_indexed() -> Result<(), Error> { | |
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn palette_gimp() -> Result<(), Error> { | ||
init_test_logger(); | ||
// Unclear why the path needs to be different for this test compared to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clarification! |
||
// the other tests which use include_bytes(). | ||
let test_gpl = "tests/data/palette/palette-gimp.gpl"; | ||
let pal_config = VeraPaletteLoadConfig { | ||
direct_load: true, | ||
include_defaults: false, | ||
sort: false, | ||
}; | ||
let palette = VeraPalette::derive_from_gpl("pal", &test_gpl, &pal_config)?; | ||
assert_eq!( | ||
palette.value_at_index(0)?, | ||
VeraPaletteEntry { r: 0, g: 0, b: 0 } | ||
); | ||
assert_eq!( | ||
palette.value_at_index(2)?, | ||
VeraPaletteEntry { | ||
r: 15, | ||
g: 15, | ||
b: 15 | ||
} | ||
); | ||
assert_eq!( | ||
palette.value_at_index(4)?, | ||
VeraPaletteEntry { r: 8, g: 6, b: 1 } | ||
); | ||
assert_eq!( | ||
palette.value_at_index(10)?, | ||
VeraPaletteEntry { r: 9, g: 9, b: 9 } | ||
); | ||
println!("{}", palette); | ||
|
||
assert_eq!(palette.len(), 16); | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering if there's a better way to do this rather than relying on file extension. I believe PNGs have magic bytes that can be used to identify them from raw data (first 8 bytes might be the same). A better workflow might be:
PNG
andGPL
variantsGimp Palette
(if that's consistent)derive
function that takes the bytes and enum type and decides where to go from thereI might be getting ahead of myself a bit here, this is an easy enough refactor to do at a later date.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per my first comment, rolling a replacement for the
gimp_palette
crate is a first step toward enabling this sort of refactoring.PNG files have an 8 byte header that could be used for identification (https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header).