Skip to content

Commit

Permalink
Fix alpha color handling for jpeg encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed Jan 13, 2025
1 parent 176d439 commit 2065af4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/formats/jpeg_encoder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:typed_data';

import '../../image.dart';
import '../color/color.dart';
import '../color/format.dart';
import '../exif/exif_data.dart';
Expand Down Expand Up @@ -122,6 +123,8 @@ class JpegEncoder extends Encoder {
return fp.getBytes();
}

static const _black = const ConstColorRgb8(0, 0, 0);

void _calculateYUV(
Image image,
int x,
Expand Down Expand Up @@ -153,6 +156,14 @@ class JpegEncoder extends Encoder {
if (p.format != Format.uint8) {
p = p.convert(format: Format.uint8);
}
if (p.length > 3) {
final backgroundColor = image.backgroundColor ?? _black;
final a = p.aNormalized;
final invA = 1.0 - a;
p..r = (p.r * a + backgroundColor.r * invA).round()
..g = (p.g * a + backgroundColor.r * invA).round()
..b = (p.b * a + backgroundColor.r * invA).round();
}
final r = p.r.toInt();
final g = p.g.toInt();
final b = p.b.toInt();
Expand Down

0 comments on commit 2065af4

Please sign in to comment.