fix: resize images in a separate isolate

This commit is contained in:
Sorunome 2020-10-06 18:22:50 +02:00
commit a113f99135
6 changed files with 131 additions and 113 deletions

105
lib/utils/resize_image.dart Normal file
View file

@ -0,0 +1,105 @@
import 'dart:ui';
import 'dart:typed_data';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:native_imaging/native_imaging.dart' as native;
import 'run_in_background.dart';
Future<MatrixImageFile> resizeImage(MatrixImageFile file,
{int max = 800}) async {
// we want to resize the image in a separate isolate, because otherwise that can
// freeze up the UI a bit
// we can't do width / height fetching in a separate isolate, as that may use the UI stuff
await native.init();
_IsolateArgs args;
try {
final nativeImg = native.Image();
await nativeImg.loadEncoded(file.bytes);
file.width = nativeImg.width();
file.height = nativeImg.height();
args = _IsolateArgs(
width: file.width, height: file.height, bytes: file.bytes, max: max);
nativeImg.free();
} on UnsupportedError {
final dartCodec = await instantiateImageCodec(file.bytes);
final dartFrame = await dartCodec.getNextFrame();
file.width = dartFrame.image.width;
file.height = dartFrame.image.height;
final rgbaData = await dartFrame.image.toByteData();
final rgba = Uint8List.view(
rgbaData.buffer, rgbaData.offsetInBytes, rgbaData.lengthInBytes);
dartFrame.image.dispose();
dartCodec.dispose();
args = _IsolateArgs(
width: file.width, height: file.height, bytes: rgba, max: max);
}
final res = await runInBackground(_isolateFunction, args);
file.blurhash = res.blurhash;
final thumbnail = MatrixImageFile(
bytes: res.jpegBytes,
name: file.name != null
? 'scaled_' + file.name.split('.').first + '.jpg'
: 'thumbnail.jpg',
mimeType: 'image/jpeg',
width: res.width,
height: res.height,
);
// only return the thumbnail if the size actually decreased
return thumbnail.size >= file.size ? file : thumbnail;
}
class _IsolateArgs {
final int width;
final int height;
final Uint8List bytes;
final int max;
final String name;
_IsolateArgs({this.width, this.height, this.bytes, this.max, this.name});
}
class _IsolateResponse {
final String blurhash;
final Uint8List jpegBytes;
final int width;
final int height;
_IsolateResponse({this.blurhash, this.jpegBytes, this.width, this.height});
}
Future<_IsolateResponse> _isolateFunction(_IsolateArgs args) async {
await native.init();
var nativeImg = native.Image();
try {
await nativeImg.loadEncoded(args.bytes);
} on UnsupportedError {
nativeImg.loadRGBA(args.width, args.height, args.bytes);
}
if (args.width > args.max || args.height > args.max) {
var w = args.max, h = args.max;
if (args.width > args.height) {
h = args.max * args.height ~/ args.width;
} else {
w = args.max * args.width ~/ args.height;
}
final scaledImg = nativeImg.resample(w, h, native.Transform.lanczos);
nativeImg.free();
nativeImg = scaledImg;
}
final jpegBytes = await nativeImg.toJpeg(75);
final blurhash = nativeImg.toBlurhash(3, 3);
final ret = _IsolateResponse(
blurhash: blurhash,
jpegBytes: jpegBytes,
width: nativeImg.width(),
height: nativeImg.height());
nativeImg.free();
return ret;
}

View file

@ -16,11 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'dart:typed_data';
import 'dart:ui';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:native_imaging/native_imaging.dart' as native;
import 'resize_image.dart';
extension RoomSendFileExtension on Room {
Future<String> sendFileEventWithThumbnail(
@ -33,50 +31,7 @@ extension RoomSendFileExtension on Room {
MatrixFile thumbnail;
try {
if (file is MatrixImageFile) {
await native.init();
var nativeImg = native.Image();
try {
await nativeImg.loadEncoded(file.bytes);
file.width = nativeImg.width();
file.height = nativeImg.height();
} on UnsupportedError {
final dartCodec = await instantiateImageCodec(file.bytes);
final dartFrame = await dartCodec.getNextFrame();
file.width = dartFrame.image.width;
file.height = dartFrame.image.height;
final rgbaData = await dartFrame.image.toByteData();
final rgba = Uint8List.view(
rgbaData.buffer, rgbaData.offsetInBytes, rgbaData.lengthInBytes);
dartFrame.image.dispose();
dartCodec.dispose();
nativeImg.loadRGBA(file.width, file.height, rgba);
}
const max = 800;
if (file.width > max || file.height > max) {
var w = max, h = max;
if (file.width > file.height) {
h = max * file.height ~/ file.width;
} else {
w = max * file.width ~/ file.height;
}
final scaledImg = nativeImg.resample(w, h, native.Transform.lanczos);
nativeImg.free();
nativeImg = scaledImg;
}
final jpegBytes = await nativeImg.toJpeg(75);
file.blurhash = nativeImg.toBlurhash(3, 3);
thumbnail = MatrixImageFile(
bytes: jpegBytes,
name: 'thumbnail.jpg',
mimeType: 'image/jpeg',
width: nativeImg.width(),
height: nativeImg.height(),
);
nativeImg.free();
thumbnail = await resizeImage(file);
if (thumbnail.size > file.size ~/ 2) {
thumbnail = null;

View file

@ -0,0 +1,12 @@
import 'package:isolate/isolate.dart';
import 'dart:async';
Future<T> runInBackground<T, U>(
FutureOr<T> Function(U arg) function, U arg) async {
final isolate = await IsolateRunner.spawn();
try {
return await isolate.run(function, arg);
} finally {
await isolate.close();
}
}