Skip to content

Commit

Permalink
Merge pull request #578 from gmilou/encode-start
Browse files Browse the repository at this point in the history
Add a start method to the PngEncoder.
  • Loading branch information
brendan-duncan authored Sep 27, 2023
2 parents 7840839 + 958fb66 commit b6a51ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/src/formats/png_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ class PngEncoder extends Encoder {
}
}

/// Start encoding a PNG.
///
/// Call this method once before calling addFrame.
void start(int frameCount) {
_frames = frameCount;
isAnimated = frameCount > 1;
}

/// Finish encoding a PNG, and return the resulting bytes.
///
/// Call this method to finalize the encoding, after all addFrame calls.
Uint8List? finish() {
Uint8List? bytes;

Expand All @@ -116,11 +127,10 @@ class PngEncoder extends Encoder {
@override
Uint8List encode(Image image, {bool singleFrame = false}) {
if (!image.hasAnimation || singleFrame) {
isAnimated = false;
start(1);
addFrame(image);
} else {
isAnimated = true;
_frames = image.frames.length;
start(image.frames.length);
repeat = image.loopCount;

if (image.hasPalette) {
Expand Down
14 changes: 14 additions & 0 deletions test/formats/png_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ void main() {
..writeAsBytesSync(png);
});

test('encodeAnimation with mulitple single frame Images', () {
final encoder = PngEncoder()..start(10);
for (var i = 0; i < 10; i++) {
final frame = Image(width: 480, height: 120)..loopCount = 10;
drawString(frame, i.toString(), font: arial48, x: 100, y: 60);
encoder.addFrame(frame);
}

final png = encoder.finish()!;
File('$testOutputPath/png/encodeAnimation.png')
..createSync(recursive: true)
..writeAsBytesSync(png);
});

test('textData', () {
final img = Image(width: 16, height: 16, textData: {"foo": "bar"});
final png = PngEncoder().encode(img);
Expand Down

0 comments on commit b6a51ff

Please sign in to comment.