Skip to content

Commit 2065af4

Browse files
Fix alpha color handling for jpeg encoder
1 parent 176d439 commit 2065af4

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lib/src/formats/jpeg_encoder.dart

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:typed_data';
22

3+
import '../../image.dart';
34
import '../color/color.dart';
45
import '../color/format.dart';
56
import '../exif/exif_data.dart';
@@ -122,6 +123,8 @@ class JpegEncoder extends Encoder {
122123
return fp.getBytes();
123124
}
124125

126+
static const _black = const ConstColorRgb8(0, 0, 0);
127+
125128
void _calculateYUV(
126129
Image image,
127130
int x,
@@ -153,6 +156,14 @@ class JpegEncoder extends Encoder {
153156
if (p.format != Format.uint8) {
154157
p = p.convert(format: Format.uint8);
155158
}
159+
if (p.length > 3) {
160+
final backgroundColor = image.backgroundColor ?? _black;
161+
final a = p.aNormalized;
162+
final invA = 1.0 - a;
163+
p..r = (p.r * a + backgroundColor.r * invA).round()
164+
..g = (p.g * a + backgroundColor.r * invA).round()
165+
..b = (p.b * a + backgroundColor.r * invA).round();
166+
}
156167
final r = p.r.toInt();
157168
final g = p.g.toInt();
158169
final b = p.b.toInt();

0 commit comments

Comments
 (0)