refactor: Better custom image resizer

pull/1388/merge
Krille 8 months ago
parent d182e1ecea
commit 8366ccee70
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652

@ -1,21 +1,43 @@
import 'dart:typed_data';
import 'dart:ui'; import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:native_imaging/native_imaging.dart' as native; import 'package:native_imaging/native_imaging.dart' as native;
(int, int) _scaleToBox(int width, int height, {required int boxSize}) {
final fit = applyBoxFit(
BoxFit.scaleDown,
Size(width.toDouble(), height.toDouble()),
Size(boxSize.toDouble(), boxSize.toDouble()),
).destination;
return (fit.width.round(), fit.height.round());
}
Future<MatrixImageFileResizedResponse?> customImageResizer( Future<MatrixImageFileResizedResponse?> customImageResizer(
MatrixImageFileResizeArguments arguments, MatrixImageFileResizeArguments arguments,
) async { ) async {
if (kIsWeb) {
throw UnsupportedError(
'customImageResizer only supports non-web platforms.',
);
}
await native.init(); await native.init();
late native.Image nativeImg;
try { var imageBytes = arguments.bytes;
nativeImg = await native.Image.loadEncoded(arguments.bytes); // load on web String? blurhash;
} on UnsupportedError {
var originalWidth = 0;
var originalHeight = 0;
var width = 0;
var height = 0;
try { try {
// for the other platforms // for the other platforms
final dartCodec = await instantiateImageCodec(arguments.bytes); final dartCodec = await instantiateImageCodec(arguments.bytes);
final frameCount = dartCodec.frameCount;
final dartFrame = await dartCodec.getNextFrame(); final dartFrame = await dartCodec.getNextFrame();
final rgbaData = await dartFrame.image.toByteData(); final rgbaData = await dartFrame.image.toByteData();
if (rgbaData == null) { if (rgbaData == null) {
@ -27,41 +49,55 @@ Future<MatrixImageFileResizedResponse?> customImageResizer(
rgbaData.lengthInBytes, rgbaData.lengthInBytes,
); );
final width = dartFrame.image.width; width = originalWidth = dartFrame.image.width;
final height = dartFrame.image.height; height = originalHeight = dartFrame.image.height;
var nativeImg = native.Image.fromRGBA(width, height, rgba);
dartFrame.image.dispose(); dartFrame.image.dispose();
dartCodec.dispose(); dartCodec.dispose();
nativeImg = native.Image.fromRGBA(width, height, rgba); if (arguments.calcBlurhash) {
} catch (e, s) { // scale down image for blurhashing to speed it up
Logs().e("Could not generate preview", e, s); final (blurW, blurH) = _scaleToBox(width, height, boxSize: 100);
rethrow; final blurhashImg = nativeImg.resample(
} blurW, blurH,
} // nearest is unsupported...
native.Transform.bilinear,
);
blurhash = blurhashImg.toBlurhash(3, 3);
final width = nativeImg.width; blurhashImg.free();
final height = nativeImg.height; }
if (frameCount > 1) {
// Don't scale down animated images, since those would lose frames.
nativeImg.free();
} else {
final max = arguments.maxDimension; final max = arguments.maxDimension;
if (width > max || height > max) { if (width > max || height > max) {
var w = max, h = max; (width, height) = _scaleToBox(width, height, boxSize: max);
if (width > height) {
h = max * height ~/ width;
} else {
w = max * width ~/ height;
}
final scaledImg = nativeImg.resample(w, h, native.Transform.lanczos); final scaledImg =
nativeImg.resample(width, height, native.Transform.lanczos);
nativeImg.free(); nativeImg.free();
nativeImg = scaledImg; nativeImg = scaledImg;
} }
final jpegBytes = await nativeImg.toJpeg(75);
imageBytes = await nativeImg.toJpeg(75);
nativeImg.free();
}
} catch (e, s) {
Logs().e("Could not generate preview", e, s);
}
return MatrixImageFileResizedResponse( return MatrixImageFileResizedResponse(
bytes: jpegBytes, bytes: imageBytes,
width: nativeImg.width, width: width,
height: nativeImg.height, height: height,
blurhash: arguments.calcBlurhash ? nativeImg.toBlurhash(3, 3) : null, originalWidth: originalWidth,
originalHeight: originalHeight,
blurhash: blurhash,
); );
} }

Loading…
Cancel
Save