Support transparency
This commit is contained in:
@@ -12,12 +12,12 @@ enum IndexPerByte {
|
||||
}
|
||||
|
||||
pub struct IndexedImage {
|
||||
palette: Vec<Color>,
|
||||
raw_data: std::vec::IntoIter<u8>,
|
||||
index_byte: IndexPerByte,
|
||||
next_func: fn(&mut Self) -> Option<Color>,
|
||||
width: u16,
|
||||
height: u16,
|
||||
pub palette: Vec<Color>,
|
||||
raw_data: std::vec::IntoIter<u8>,
|
||||
index_byte: IndexPerByte,
|
||||
next_func: fn(&mut Self) -> Option<Color>,
|
||||
width: u16,
|
||||
height: u16,
|
||||
}
|
||||
|
||||
impl IndexedImage {
|
||||
|
@@ -30,7 +30,35 @@ pub struct Arguments {
|
||||
color_depth: ColorType,
|
||||
|
||||
#[clap(value_enum, value_parser, default_value_t=ClutAlignment::None)]
|
||||
clut_align: ClutAlignment
|
||||
clut_align: ClutAlignment,
|
||||
|
||||
#[clap(long="semi-trans", default_value_t=false)]
|
||||
semi_transparent: bool,
|
||||
#[clap(long="color-trans", default_value_t=false)]
|
||||
transparent_palette: bool
|
||||
}
|
||||
|
||||
fn modify_palette(mut image: IndexedImage, clut_align: ClutAlignment, semi_transparent: bool, transparent_palette: bool) -> IndexedImage {
|
||||
if semi_transparent {
|
||||
for color in image.palette.iter_mut() {
|
||||
*color = PSXColor::semi_transparent(color.get_red(), color.get_green(), color.get_blue());
|
||||
}
|
||||
}
|
||||
|
||||
if transparent_palette {
|
||||
if clut_align == ClutAlignment::Block {
|
||||
for color in image.palette.iter_mut().step_by(16) {
|
||||
*color = PSXColor::transparent();
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
if let Some(first_color) = image.palette.get_mut(0) {
|
||||
*first_color = PSXColor::transparent();
|
||||
}
|
||||
}
|
||||
}
|
||||
image
|
||||
}
|
||||
|
||||
fn encode<T: PSXImageConverter>(image: T, color_depth: ColorType, clut_align: ClutAlignment, output: &mut dyn Write) -> Result<(), Error> {
|
||||
@@ -95,7 +123,7 @@ fn convert_full16(input: Input, output: &mut dyn Write) -> Result<(), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: ColorType, clut_align: ClutAlignment) -> Result<(), Error> {
|
||||
fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: ColorType, clut_align: ClutAlignment, semi_transparent: bool, transparent_palette: bool) -> Result<(), Error> {
|
||||
match png::Decoder::new(input).read_info() {
|
||||
Ok(reader) => {
|
||||
let output_type = {
|
||||
@@ -105,7 +133,8 @@ fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: Color
|
||||
_ => return Err(Error::from_str("ColorType not supported"))
|
||||
}
|
||||
};
|
||||
encode(IndexedImage::new(reader, output_type)?, color_type, clut_align, output)
|
||||
|
||||
encode(modify_palette(IndexedImage::new(reader, output_type)?, clut_align, semi_transparent, transparent_palette), color_type, clut_align, output)
|
||||
},
|
||||
Err(error) => Err(Error::from_error(error))
|
||||
}
|
||||
@@ -114,6 +143,6 @@ fn convert_palette_based(input: Input, output: &mut dyn Write, color_type: Color
|
||||
pub fn convert(args: Arguments, input: Input, output: &mut dyn Write) -> Result<(), Error> {
|
||||
match args.color_depth {
|
||||
ColorType::Full16 => convert_full16(input, output),
|
||||
_ => convert_palette_based(input, output, args.color_depth, args.clut_align),
|
||||
_ => convert_palette_based(input, output, args.color_depth, args.clut_align, args.semi_transparent, args.transparent_palette),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user