mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
BMP export: use the full palette for opaque pixels
This commit is contained in:
1 parent
0ed0f286ce
commit
e81fa94f3f
2 files changed
+26
-2
No files matched your search
@@ -820,8 +820,9 @@ fn export_mode_specific(doc: &Document, format: Format, opts: &ExportOptions) ->
|
||||
let Some(table) = doc.color_table.as_ref().filter(|t| !t.colors.is_empty() && t.colors.len() <= 256) else { return Ok(None) };
|
||||
// PNG transparency belongs to a palette index, not its RGB value. Keep the
|
||||
// transparent entry out of opaque lookup, even when it duplicates an opaque colour.
|
||||
let png_transparent_index = table.transparent.map(usize::from).filter(|_| format == Format::Png);
|
||||
let (opaque_indices, colors): (Vec<u8>, Vec<_>) =
|
||||
table.colors.iter().copied().enumerate().filter(|(i, _)| Some(*i) != table.transparent.map(usize::from)).map(|(i, c)| (i as u8, c)).unzip();
|
||||
table.colors.iter().copied().enumerate().filter(|(i, _)| Some(*i) != png_transparent_index).map(|(i, c)| (i as u8, c)).unzip();
|
||||
let opaque = photocraft_doc::ColorTable { colors, transparent: None };
|
||||
let mut idx = try_buffer(doc.size.area() as usize, 1)?;
|
||||
photocraft_compose::render_bands(doc, doc.bounds(), 0, |band| -> Result<(), IoError> {
|
||||
@@ -830,7 +831,7 @@ fn export_mode_specific(doc: &Document, format: Format, opts: &ExportOptions) ->
|
||||
Some(t) if p[3] < 0.5 => t,
|
||||
_ => *opaque_indices
|
||||
.get(opaque.nearest([p[0], p[1], p[2]]))
|
||||
.ok_or_else(|| IoError::Unsupported("Indexed PNG palette has no opaque entry; add an opaque colour or convert to RGB".into()))?,
|
||||
.ok_or_else(|| IoError::Unsupported("Indexed palette has no opaque entry; add an opaque colour or convert to RGB".into()))?,
|
||||
};
|
||||
idx.push(i);
|
||||
}
|
||||
|
||||
@@ -176,6 +176,29 @@ fn indexed_bmp_warns_that_it_has_no_transparency() {
|
||||
assert!(r.warnings.iter().any(|w| w.contains("no transparency")), "{:?}", r.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_bmp_exports_when_its_only_palette_entry_is_marked_transparent() {
|
||||
let mut d = doc(ColorMode::Indexed);
|
||||
d.color_table = Some(ColorTable { colors: vec![[255, 0, 0]], transparent: Some(0) });
|
||||
|
||||
let r = photocraft_io::export(&d, "x.bmp", &Default::default()).unwrap();
|
||||
|
||||
assert_eq!(u16::from_le_bytes([r.bytes[28], r.bytes[29]]), 8, "8 bits per pixel");
|
||||
let back = photocraft_io::import("x.bmp", &r.bytes).unwrap().document;
|
||||
assert_eq!(&back.layers[0].surface().unwrap().rgba(1, 1)[..3], &[1.0, 0.0, 0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_bmp_matches_opaque_pixels_against_the_transparent_palette_entry() {
|
||||
let mut d = doc(ColorMode::Indexed);
|
||||
d.color_table = Some(ColorTable { colors: vec![[255, 0, 0], [0, 0, 255]], transparent: Some(0) });
|
||||
|
||||
let r = photocraft_io::export(&d, "x.bmp", &Default::default()).unwrap();
|
||||
|
||||
let back = photocraft_io::import("x.bmp", &r.bytes).unwrap().document;
|
||||
assert_eq!(&back.layers[0].surface().unwrap().rgba(1, 1)[..3], &[1.0, 0.0, 0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_bmp_without_a_usable_table_falls_back_to_rgb() {
|
||||
// No table (or one past 256 entries) keeps the old 24-bit path rather than failing the save.
|
||||
|
||||
Reference in new issue
Block a user