Skip to content

Commit

Permalink
Support importing palette from Gimp *.gpl file
Browse files Browse the repository at this point in the history
  • Loading branch information
CJLove committed May 25, 2020
1 parent 6a1a52b commit 0b24855
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions src/cmd/palette/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,26 @@ pub struct PaletteImportArgs {

/// Palette import command
pub fn palette_import(g_args: &GlobalArgs, args: &PaletteImportArgs) -> Result<(), Error> {
let png_bytes = common::read_file_bin(&args.input_file)?;
let pal_config = VeraPaletteLoadConfig {
direct_load: true,
include_defaults: false,
sort: false,
let is_gpl = args.input_file.ends_with("gpl") || args.input_file.ends_with("GPL");
let palette = match is_gpl {
true => {
let pal_config = VeraPaletteLoadConfig {
direct_load: true,
include_defaults: false,
sort: false,
};
VeraPalette::derive_from_gpl(&args.id, &args.input_file, &pal_config).expect("Error")
}
false => {
let png_bytes = common::read_file_bin(&args.input_file)?;
let pal_config = VeraPaletteLoadConfig {
direct_load: true,
include_defaults: false,
sort: false,
};
VeraPalette::derive_from_png(&args.id, png_bytes, &pal_config).expect("error")
}
};
let palette = VeraPalette::derive_from_png(&args.id, png_bytes, &pal_config)?;
// load up the project json
let project_file = match &g_args.project_file {
Some(f) => f,
Expand Down
1 change: 1 addition & 0 deletions vera/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ serde_derive = "1"
png = "0.15"
permutate = "0.3"
aloevera_util = { path = "../util", version = "0.2.2" }
gimp_palette = { version = "0.1.1" }
51 changes: 51 additions & 0 deletions vera/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
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(
Expand Down
21 changes: 21 additions & 0 deletions vera/tests/data/palette/palette-gimp.gpl
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

39 changes: 39 additions & 0 deletions vera/tests/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 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(())
}

0 comments on commit 0b24855

Please sign in to comment.