Skip to content

Commit

Permalink
feat: 5952 - better image compression for Prices (#6048)
Browse files Browse the repository at this point in the history
Impacted files:
* `image_compute_container.dart`: added the compression quality parameter
* `background_task_image.dart`: added the compression quality and force quality parameters; same parameters as before for OxF images (quality 100, no forced compression for non cropped images)
* `background_task_add_price.dart`: always compress the proof image, with quality 80
  • Loading branch information
monsieurtanuki authored Dec 20, 2024
1 parent 7c4c142 commit a7a9990
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ class BackgroundTaskAddPrice extends BackgroundTaskPrice {
cropY1: cropY1,
cropX2: cropX2,
cropY2: cropY2,
compressQuality: 80,
forceCompression: true,
overlayPainter: offsets.isEmpty
? null
: EraserPainter(
Expand Down
29 changes: 18 additions & 11 deletions packages/smooth_app/lib/background/background_task_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,24 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
required final int cropX2,
required final int cropY2,
final CustomPainter? overlayPainter,
required final int compressQuality,
required final bool forceCompression,
}) async {
final ui.Image full = await loadUiImage(
await (await BackgroundTaskUpload.getFile(fullPath)).readAsBytes());
if (cropX1 == 0 &&
cropY1 == 0 &&
cropX2 == _cropConversionFactor &&
cropY2 == _cropConversionFactor &&
rotationDegrees == 0) {
if (!isPictureBigEnough(full.width, full.height)) {
return null;
}
// in that case, no need to crop
if (overlayPainter == null) {
return fullPath;
if (!forceCompression) {
if (cropX1 == 0 &&
cropY1 == 0 &&
cropX2 == _cropConversionFactor &&
cropY2 == _cropConversionFactor &&
rotationDegrees == 0) {
if (!isPictureBigEnough(full.width, full.height)) {
return null;
}
// in that case, no need to crop
if (overlayPainter == null) {
return fullPath;
}
}
}

Expand Down Expand Up @@ -296,6 +300,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
await saveJpeg(
file: await BackgroundTaskUpload.getFile(croppedPath),
source: cropped,
quality: compressQuality,
);
return croppedPath;
}
Expand All @@ -314,6 +319,8 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
cropY1: cropY1,
cropX2: cropX2,
cropY2: cropY2,
compressQuality: 100,
forceCompression: false,
);
if (path == null) {
// TODO(monsieurtanuki): maybe something more refined when we dismiss the picture, like alerting the user, though it's not supposed to happen anymore from upstream.
Expand Down
8 changes: 7 additions & 1 deletion packages/smooth_app/lib/helpers/image_compute_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class _ImageComputeContainer {
required this.rawData,
required this.width,
required this.height,
required this.quality,
}) : rootIsolateToken = ui.RootIsolateToken.instance;

final File file;
final ByteData rawData;
final int width;
final int height;
final int quality;
final ui.RootIsolateToken? rootIsolateToken;

bool get isIsolatePossible => rootIsolateToken != null;
Expand Down Expand Up @@ -47,6 +49,8 @@ Future<void> saveBmp({
rawData: rawData,
width: source.width,
height: source.height,
// whatever, we don't use it for bmp
quality: 100,
);
if (container.isIsolatePossible) {
try {
Expand All @@ -68,6 +72,7 @@ Future<void> saveBmp({
Future<void> saveJpeg({
required final File file,
required final ui.Image source,
required final int quality,
}) async {
final ByteData? rawData = await source.toByteData(
format: ui.ImageByteFormat.rawRgba,
Expand All @@ -80,6 +85,7 @@ Future<void> saveJpeg({
rawData: rawData,
width: source.width,
height: source.height,
quality: quality,
);
if (container.isIsolatePossible) {
try {
Expand Down Expand Up @@ -148,7 +154,7 @@ Future<void> _saveJpeg(
final Uint8List jpegData = await FlutterImageCompress.compressWithList(
bmpData,
autoCorrectionAngle: false,
quality: 100,
quality: container.quality,
format: CompressFormat.jpeg,
minWidth: container.width,
minHeight: container.height,
Expand Down

0 comments on commit a7a9990

Please sign in to comment.