Skip to content
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

Feature: Extend CLI #14

Merged
merged 3 commits into from
Jul 19, 2023
Merged

Conversation

dcvz
Copy link
Contributor

@dcvz dcvz commented Jul 18, 2023

This PR extends the CLI tool to handle both converting from binary > png and vice versa. As part of that it refactors the CLI code to handle subcommands.

# converting from bin to png
pigment64 to-png tests/ia4.png.bin -f ia4 --width 16 --height 1
# converting ci image from bin to png
pigment64 to-png tests/ci4.data.bin -f ci4 -p tests/ci4.tlut.bin --width 4 --height 4

# converting png to bin
pigment64 --bin pigment64 to-binary tests/ia4.png -f ia4

Copy link
Contributor

@AngheloAlf AngheloAlf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of my comments are mostly ignorance of Rust stuff

// Convert the image
let mut bin: Vec<u8> = Vec::new();
if let BinaryFormat::Palette = args.format {
pigment64::create_palette_from_png(&mut input_reader, &mut bin)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some places say crate:: and others say pigment64::. Is it needed to be different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so that has to do with when something comes from the library and what comes from within the cli package. Things prefixed with pigment64 are being imported from the lib - the cli is a user of the lib. Things with crate are within the cli package and are not part of the lib.

BufWriter::new(output_file).write_all(&bin)?;
}

Ok(())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for returning an empty Ok(()) instead of just a void return? I don't see any Error anywhere

Copy link
Contributor Author

@dcvz dcvz Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be hard see unless you’re familiar with it, but some things that /could/ error are being handled with ? at the end, which would bubble the error out. If you make it to the last Ok(()) then nothing before failed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's what ? mean. Gotcha, thanks.

BinaryFormat::Ia16 => ImageType::Ia16,
BinaryFormat::Rgba16 => ImageType::Rgba16,
BinaryFormat::Rgba32 => ImageType::Rgba32,
_ => panic!("cannot convert palette to native format"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to use ::Palette explicitly here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? I know we explicitly will only handle these no matter what other items we may add to facilitate pigment use. So that’s why I went for this option. I’m fine either way.

BinaryFormat::Ia16 => ImageSize::Bits16,
BinaryFormat::Rgba16 => ImageSize::Bits16,
BinaryFormat::Rgba32 => ImageSize::Bits32,
_ => panic!("cannot convert palette to native format"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered above.

@@ -94,6 +94,17 @@ impl PNGImage {
}
}

/// Writes the image as a PNG to the given writer.
/// This is useful for when you need to flip an existing image.
pub fn as_png<W: Write>(&self, writer: &mut W) -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be more clear to have this function named as write_as_png?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We’re using this naming semantic everywhere else so I kept it. We’re writing data to some medium for sure. I don’t feel strongly about it. Do you find this name confusing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, if this naming scheme is already being used then it is fine.
I think it may be a bit confusing to read something like image.as_png(), but that discussion is out of scope for this PR

@ethteck ethteck merged commit 3ee622e into decompals:main Jul 19, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants