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

LibGfx: Add APNG Writing Support to PNGWriter #24021

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

OfficialPixelBrush
Copy link
Contributor

This is a relatively straight-forward commit, allowing programs to export APNG files, with a custom frame duration, blend mode, etc.
This is also my first non-image commit for SerenityOS! (Woohoo!)

To Summarize what this adds:

  • writing of acTL Chunks (Animation Control)
  • writing of fcTL Chunks (Frame Control)
  • writing of fdAT Chunks (Frame Data)
  • an overlying function, into which a bitmap vector can be passed, alongside a few parameters.

@github-actions github-actions bot added the 👀 pr-needs-review PR needs review from a maintainer or community member label Apr 19, 2024
@OfficialPixelBrush OfficialPixelBrush changed the title LibGfx: Added APNG Writing Support to PNGWriter LibGfx: Add APNG Writing Support to PNGWriter Apr 19, 2024
Copy link
Member

@AtkinsSJ AtkinsSJ left a comment

Choose a reason for hiding this comment

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

I'm not a graphics person so I'll let @nico or @LucasChollet weigh in on that. Generally it seems good. You do need to run clang-format on your changes though, that's what CI is complaining about.

Userland/Libraries/LibGfx/ImageFormats/PNGWriter.cpp Outdated Show resolved Hide resolved
Userland/Libraries/LibGfx/ImageFormats/PNGWriter.h Outdated Show resolved Hide resolved
Userland/Libraries/LibGfx/ImageFormats/PNGWriter.cpp Outdated Show resolved Hide resolved
@OfficialPixelBrush OfficialPixelBrush force-pushed the apng-writing branch 3 times, most recently from 566cd6a to 4ddcf0a Compare April 19, 2024 12:43
Copy link
Contributor

@nico nico left a comment

Choose a reason for hiding this comment

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

Seems generally alright. Might be nice to include a test.

For the "screen recording" use case, it'd probably nice to have an API that streams the images instead of having to assemble them all into a big vector.

Also, maybe it makes sense to put this in the PR that adds a call to the new function? As is, this is adding a dead function. (Not the end of the world if its caller comes soon after.)

dbgln("frame #{}", sequence_number);
TRY(writer.add_fcTL_chunk(sequence_number, b.width(), b.height(), delay_num, delay_den, dispose_op, blend_op));
sequence_number++;
TRY(writer.add_IDAT_chunk(b, true, sequence_number));
Copy link
Contributor

Choose a reason for hiding this comment

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

This is writing bitmap[0] a second time. Is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

APNG accepts two formats. In this, the thumbnail is separate from the first frame. In the other, it's the same as the first frame. I was originally going to allow the submission of a separate thumbnail image, but this probably has little purpose in this context. I might rework it a bit.

@@ -281,4 +322,26 @@ ErrorOr<ByteBuffer> PNGWriter::encode(Gfx::Bitmap const& bitmap, Options options
return ByteBuffer::copy(writer.m_data);
}

ErrorOr<ByteBuffer> PNGWriter::encodeAnimated(AK::Vector<Gfx::Bitmap const&> bitmap, u16 delay_num = 1, u16 delay_den = 30, u8 dispose_op = 0, u8 blend_op = 0, Options options)
Copy link
Contributor

Choose a reason for hiding this comment

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

serenity style is encode_animated

@OfficialPixelBrush
Copy link
Contributor Author

Good point. Depends on how I do it. The APNG could be created somewhere, then appended onto with each frame.

Right now, I'm not really sure where I could use the function, outside of the extension of shot I've begun to develop that allows recording of APNGs.

@LucasChollet
Copy link
Member

I guess this can be used in pixel paint but that might require some work as I don't think we support animated images there.
Another option (often the best one when introducing something related to image format), add support for it in image.

@OfficialPixelBrush
Copy link
Contributor Author

I was thinking this'd be a great opportunity for someone to add basic animation capabilities to PixelPaint.

Will need to look into image. Thanks for the pointers!

Copy link

stale bot commented May 12, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions!

@stale stale bot added the stale label May 12, 2024
@nico
Copy link
Contributor

nico commented May 18, 2024

We now have an animation tool that can write animated webp files. It uses a streaming API too. Maybe this could use that same API (AnimationWriter) and we could hook it up to animation? Would be cool if that could write apngs as well :)

@stale stale bot removed the stale label May 18, 2024
@OfficialPixelBrush OfficialPixelBrush force-pushed the apng-writing branch 2 times, most recently from f3f7daa to 3d861e6 Compare May 20, 2024 16:07
Copy link
Member

@LucasChollet LucasChollet left a comment

Choose a reason for hiding this comment

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

You should be able to rebase it now that GIF support was merged :)


// Let's get rid of the previously written trailer
if (!is_first_frame)
TRY(m_stream.seek(-4, SeekMode::FromEndPosition));
Copy link
Member

Choose a reason for hiding this comment

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

While I still have that in mind:
Please use SeekMode::FromCurrentPosition (89aa18f)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cheers! Will get on that asap!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 pr-needs-review PR needs review from a maintainer or community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants