refactor: Better custom image resizer
parent
d182e1ecea
commit
8366ccee70
@ -1,67 +1,103 @@
|
|||||||
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;
|
|
||||||
|
var imageBytes = arguments.bytes;
|
||||||
|
String? blurhash;
|
||||||
|
|
||||||
|
var originalWidth = 0;
|
||||||
|
var originalHeight = 0;
|
||||||
|
var width = 0;
|
||||||
|
var height = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
nativeImg = await native.Image.loadEncoded(arguments.bytes); // load on web
|
// for the other platforms
|
||||||
} on UnsupportedError {
|
final dartCodec = await instantiateImageCodec(arguments.bytes);
|
||||||
try {
|
final frameCount = dartCodec.frameCount;
|
||||||
// for the other platforms
|
final dartFrame = await dartCodec.getNextFrame();
|
||||||
final dartCodec = await instantiateImageCodec(arguments.bytes);
|
final rgbaData = await dartFrame.image.toByteData();
|
||||||
final dartFrame = await dartCodec.getNextFrame();
|
if (rgbaData == null) {
|
||||||
final rgbaData = await dartFrame.image.toByteData();
|
return null;
|
||||||
if (rgbaData == null) {
|
}
|
||||||
return null;
|
final rgba = Uint8List.view(
|
||||||
}
|
rgbaData.buffer,
|
||||||
final rgba = Uint8List.view(
|
rgbaData.offsetInBytes,
|
||||||
rgbaData.buffer,
|
rgbaData.lengthInBytes,
|
||||||
rgbaData.offsetInBytes,
|
);
|
||||||
rgbaData.lengthInBytes,
|
|
||||||
);
|
|
||||||
|
|
||||||
final width = dartFrame.image.width;
|
width = originalWidth = dartFrame.image.width;
|
||||||
final height = dartFrame.image.height;
|
height = originalHeight = dartFrame.image.height;
|
||||||
|
|
||||||
dartFrame.image.dispose();
|
var nativeImg = native.Image.fromRGBA(width, height, rgba);
|
||||||
dartCodec.dispose();
|
|
||||||
|
|
||||||
nativeImg = native.Image.fromRGBA(width, height, rgba);
|
dartFrame.image.dispose();
|
||||||
} catch (e, s) {
|
dartCodec.dispose();
|
||||||
Logs().e("Could not generate preview", e, s);
|
|
||||||
rethrow;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final width = nativeImg.width;
|
if (arguments.calcBlurhash) {
|
||||||
final height = nativeImg.height;
|
// scale down image for blurhashing to speed it up
|
||||||
|
final (blurW, blurH) = _scaleToBox(width, height, boxSize: 100);
|
||||||
|
final blurhashImg = nativeImg.resample(
|
||||||
|
blurW, blurH,
|
||||||
|
// nearest is unsupported...
|
||||||
|
native.Transform.bilinear,
|
||||||
|
);
|
||||||
|
|
||||||
final max = arguments.maxDimension;
|
blurhash = blurhashImg.toBlurhash(3, 3);
|
||||||
if (width > max || height > max) {
|
|
||||||
var w = max, h = max;
|
blurhashImg.free();
|
||||||
if (width > height) {
|
|
||||||
h = max * height ~/ width;
|
|
||||||
} else {
|
|
||||||
w = max * width ~/ height;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final scaledImg = nativeImg.resample(w, h, native.Transform.lanczos);
|
if (frameCount > 1) {
|
||||||
nativeImg.free();
|
// Don't scale down animated images, since those would lose frames.
|
||||||
nativeImg = scaledImg;
|
nativeImg.free();
|
||||||
|
} else {
|
||||||
|
final max = arguments.maxDimension;
|
||||||
|
if (width > max || height > max) {
|
||||||
|
(width, height) = _scaleToBox(width, height, boxSize: max);
|
||||||
|
|
||||||
|
final scaledImg =
|
||||||
|
nativeImg.resample(width, height, native.Transform.lanczos);
|
||||||
|
nativeImg.free();
|
||||||
|
nativeImg = scaledImg;
|
||||||
|
}
|
||||||
|
|
||||||
|
imageBytes = await nativeImg.toJpeg(75);
|
||||||
|
nativeImg.free();
|
||||||
|
}
|
||||||
|
} catch (e, s) {
|
||||||
|
Logs().e("Could not generate preview", e, s);
|
||||||
}
|
}
|
||||||
final jpegBytes = await nativeImg.toJpeg(75);
|
|
||||||
|
|
||||||
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…
Reference in New Issue