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

New palette changer script #1108

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

Talon1024
Copy link
Contributor

@Talon1024 Talon1024 commented Sep 8, 2023

This script allows a user to change Freedoom's palette, while also changing any graphics that use the old palette to use the new colours.

Since some of the other scripts in the Freedoom repo use pypng, and I needed access to low-level PNG features, this script also uses pypng. It should work with Python 3.8 (Ubuntu 20.04).

This script allows a user to change Freedoom's palette, while also changing any graphics that use the old palette to use the new colours
@selliott512
Copy link
Contributor

This seems like a neat idea, but before I dig into it, and you spend more time on it, I'd like to understand the big picture better, and I'd like to tell you about some scripts I added, just in case there's overlap:

The fix-deutex-pngs make target and script runs all the PNGs through deutex, which standardizes them, including indexing them with the standard pallet.

The fix-legacy-transparency-pngs make target, which invokes the map-color-index script`, remaps color 255 to similar color 133.

Is that relevant to what you want to do?

@Talon1024
Copy link
Contributor Author

Talon1024 commented Sep 9, 2023

I know about your scripts and what they do, but IIRC neither of your scripts offered the features I wanted. Furthermore, you used the pypng library, but you didn't even leave a comment in the script code about that.

I took a bit of code (recursive directory/file processing) from your map-color-index script.

@selliott512
Copy link
Contributor

OK, I understand it better. Thanks for explaining.

Yes, I should've documented the pypy requirement somewhere - I started out with Pillow, but it didn't give me the low level control I needed, and rebuilding IDAT by hand was more than I wanted to get into. That script will be run infrequently anyway. I'll open a PR to add it to the Required Software section of COMPILING.adoc, but I'll note that it's only needed for that one script.

I'm glad my script could be helpful in that way.

Do not update a colour if all of its duplicates have not been replaced
Attempt to check whether the IDAT(s) contain any modified colours before re-writing it
Properly processing the IDAT chunks in a PNG file requires decompression, decoding, de-filtering, and de-interlacing. Using pypng takes care of this for the developer.
@Talon1024 Talon1024 changed the title Initial work on palette changer script New palette changer script Sep 10, 2023
@Talon1024 Talon1024 marked this pull request as ready for review September 10, 2023 11:32
Don't just "return True" at the end of process_png.
Use relpath for more readable paths
Most of the features a PNG decoder should have, except for deinterlacing, are implemented.
@mc776
Copy link
Contributor

mc776 commented Sep 29, 2023

Confirmed it builds properly on my end without having to install anything else. Takes like 2 more seconds but that's not unexpected given all the stuff it's got to do.

Which... stuff... I'm still not qualified to give the final review for. I've approved the PR but someone else who knows what they're doing please take one last look.

@selliott512
Copy link
Contributor

I can look at it this weekend.

Unless I'm missing something this change just adds a script that can optionally be run, so it does not impact the usual build time.

Add a description of the script to the ArgumentParser
Remove the directory argument, since it has limited use, and is a potential security flaw
Rename "dir" argument to "directory", since "dir" is a built-in function.
@selliott512
Copy link
Contributor

Could you provide an example invocation of this script? My first guess was:

scripts/update-palette lumps/playpal/playpal-base.lmp

But that just truncates lumps/playpal/playpal-base.lmp.

@Talon1024
Copy link
Contributor Author

The script uses argparse, so you can run it with -h for help.

You're supposed to give it a new palette, like the one in this zip file. If you extract it to /tmp/, you run the script like this:

scripts/update-palette /tmp/palbluekn3.data

Or if you just want to see what would change without actually making any changes:

scripts/update-palette -d /tmp/palbluekn3.data

You should then see a bunch of lines like patches/thing.png was changed

Copy link
Contributor

@selliott512 selliott512 left a comment

Choose a reason for hiding this comment

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

Neat script. I know your motivation may be to fix the color 255 issue, but there are other things that could be done as well.

Freedoom could include a directory of optional palettes that could be applied. For example, just for the fun of it I tried making a gray scale palette and passing it to the script. Here's how E1M6 looks with it in GZDoom:
e1m6-gray
The code and palette I used:
gray.zip

Since Freedoom is inclined to accommodate color blindness maybe there could be a palette for that, although I'm not sure exactly that that means (high color saturation?, a brightness difference between red and green?). But there may be other reasons, like people preferring an overall hue or tone. If you did this maybe you could make up a unique extension like pal or palette instead of data just to make it clearer what the files are.

If I were to have done this I probably would've had it skip any PNG that does not exactly have the current game palette (that is not exactly lumps/playpal/playpal-base.lmp). Then the new palette would be blindly applied leaving it an exercise for the user to make sure that it lines up index by index as intended, and no other chunks would be updated. You've solved a harder problem, and that's great, but it may be slower in some cases.

When and if this gets incorporated in the build my inclination would be for the default all target to not depend on it, but maybe fix or dist so it gets run less frequently. This is assuming you end up fixing the color 255 issue with it. I'm personally not too worried about color 255 even though I wrote scripts to remap it. How many custom PWADs actually have custom color 255?

This script edits both PNGs files and lumps/playpal/playpal-base.lmp. You could have options to only do one or the other since I can see either being useful depending on the situation.

Anyway, currently this PR just adds a script without anything to invoke it, which makes it quite safe. I'm happy to apply this now. Just let me know when and if you're done. My suggestions are just that, so don't feel that you have to do anything.

scripts/update-palette Show resolved Hide resolved
scripts/update-palette Show resolved Hide resolved
scripts/update-palette Show resolved Hide resolved
Copy link
Member

@fragglet fragglet left a comment

Choose a reason for hiding this comment

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

Sorry for being a party pooper but I really want to hear a convincing answer to this question before this PR is merged.

Freedoom needs a very good reason to have a custom PNG decoder, but there is no good reason to write one.
Furthermore, this may make it easier to write code to modify true-colour PNGs. However, because of issues I'm having (see comments), the code is currently disabled.
@mc776
Copy link
Contributor

mc776 commented Nov 4, 2023

For whatever little it's worth, I just tried building this and confirm I didn't need to install anything new.

EDIT: I just realized this script isn't run during build, disregard

@selliott512
Copy link
Contributor

The custom PNG decoder appears to be gone as per fragglet's feedback. Looks good me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants