diff --git a/packages/wasm_packages/flutter_example/assets/images/code.png b/packages/wasm_packages/flutter_example/assets/images/code.png new file mode 100644 index 0000000..1bb2a9d Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/code.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/contrast20.png b/packages/wasm_packages/flutter_example/assets/images/contrast20.png new file mode 100644 index 0000000..ac1665e Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/contrast20.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/convert.gif b/packages/wasm_packages/flutter_example/assets/images/convert.gif new file mode 100644 index 0000000..01d9e70 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/convert.gif differ diff --git a/packages/wasm_packages/flutter_example/assets/images/crop-100x52.png b/packages/wasm_packages/flutter_example/assets/images/crop-100x52.png new file mode 100644 index 0000000..646e944 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/crop-100x52.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/flip-h.png b/packages/wasm_packages/flutter_example/assets/images/flip-h.png new file mode 100644 index 0000000..41802c3 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/flip-h.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/gray-rot90.png b/packages/wasm_packages/flutter_example/assets/images/gray-rot90.png new file mode 100644 index 0000000..0b5d408 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/gray-rot90.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/grayscale.png b/packages/wasm_packages/flutter_example/assets/images/grayscale.png new file mode 100644 index 0000000..328871d Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/grayscale.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/resize-102x50-exact.png b/packages/wasm_packages/flutter_example/assets/images/resize-102x50-exact.png new file mode 100644 index 0000000..11a7746 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/resize-102x50-exact.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/resize-102x50.jpg b/packages/wasm_packages/flutter_example/assets/images/resize-102x50.jpg new file mode 100644 index 0000000..96a97b1 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/resize-102x50.jpg differ diff --git a/packages/wasm_packages/flutter_example/assets/images/resize-102x50.png b/packages/wasm_packages/flutter_example/assets/images/resize-102x50.png new file mode 100644 index 0000000..d38b08e Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/resize-102x50.png differ diff --git a/packages/wasm_packages/flutter_example/assets/images/webp-convert-blur2.bmp b/packages/wasm_packages/flutter_example/assets/images/webp-convert-blur2.bmp new file mode 100644 index 0000000..97aa55a Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/webp-convert-blur2.bmp differ diff --git a/packages/wasm_packages/flutter_example/assets/images/webp-convert.bmp b/packages/wasm_packages/flutter_example/assets/images/webp-convert.bmp new file mode 100644 index 0000000..93950b7 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/webp-convert.bmp differ diff --git a/packages/wasm_packages/flutter_example/assets/images/webp-example.webp b/packages/wasm_packages/flutter_example/assets/images/webp-example.webp new file mode 100644 index 0000000..8bbe329 Binary files /dev/null and b/packages/wasm_packages/flutter_example/assets/images/webp-example.webp differ diff --git a/packages/wasm_packages/flutter_example/pubspec.yaml b/packages/wasm_packages/flutter_example/pubspec.yaml index e525317..35b61b5 100644 --- a/packages/wasm_packages/flutter_example/pubspec.yaml +++ b/packages/wasm_packages/flutter_example/pubspec.yaml @@ -110,6 +110,7 @@ flutter: # To add assets to your application, add an assets section, like this: assets: - assets/google_fonts/Cousine/ + - assets/images/ - packages/sql_parser/assets/sql_parser_wasm.wasm # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg diff --git a/packages/wasm_packages/flutter_example/test/image_test.dart b/packages/wasm_packages/flutter_example/test/image_test.dart new file mode 100644 index 0000000..317f590 --- /dev/null +++ b/packages/wasm_packages/flutter_example/test/image_test.dart @@ -0,0 +1,232 @@ +import 'dart:typed_data'; + +import 'package:flutter/services.dart' show rootBundle; +import 'package:flutter/widgets.dart'; +import 'package:flutter_example/image_rs_state.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:image_rs/image_rs.dart'; +// ignore: depend_on_referenced_packages +import 'package:wasm_run/wasm_run.dart'; + +Future imageState() async { + WidgetsFlutterBinding.ensureInitialized(); + await WasmRunLibrary.setUp( + override: false, + isFlutter: true, + loadAsset: rootBundle.load, + ); + final parser = await createImageRs( + wasiConfig: const WasiConfig(preopenedDirs: [], webBrowserFileSystem: {}), + ); + return ImageRsState(parser); +} + +void main() { + group('image rs', () { + test('front multiple ops', () async { + final state = await imageState(); + final codeImage = await rootBundle.load('assets/images/code.png'); + final flipH = await rootBundle.load('assets/images/flip-h.png'); + final contrast20 = await rootBundle.load('assets/images/contrast20.png'); + final convertGif = await rootBundle.load('assets/images/convert.gif'); + final crop100x52 = await rootBundle.load('assets/images/crop-100x52.png'); + final grayscale = await rootBundle.load('assets/images/grayscale.png'); + final grayrot90 = await rootBundle.load('assets/images/gray-rot90.png'); + final resize102x50Jpg = + await rootBundle.load('assets/images/resize-102x50.jpg'); + final resize102x50 = + await rootBundle.load('assets/images/resize-102x50.png'); + final resize102x50Exact = + await rootBundle.load('assets/images/resize-102x50-exact.png'); + final webpconvertblur2 = + await rootBundle.load('assets/images/webp-convert-blur2.bmp'); + final webpconvertBmp = + await rootBundle.load('assets/images/webp-convert.bmp'); + final webpexample = + await rootBundle.load('assets/images/webp-example.webp'); + + ImageRef? initialRef; + + void validateState({ + ImageRef? previousRef, + String filename = 'code.png', + ImageFormat format = ImageFormat.png, + ColorType color = ColorType.rgba8, + Matcher error = isEmpty, + int height = 223, + int width = 581, + required Uint8List buffer, + }) { + expect(state.filename, filename); + expect(state.format, format); + expect(state.color, color); + expect(state.previousRef, previousRef ?? initialRef); + final ref = state.ref!; + expect(state.error, error); + expect(ref.color, color); + expect(ref.height, height); + expect(ref.width, width); + expect(state.bytes, buffer); + } + + state.loadImage('code.png', codeImage.buffer.asUint8List()); + validateState(buffer: codeImage.buffer.asUint8List()); + initialRef = state.ref; + + /// + state.op( + (ref) => state.imageRs.operations.flipHorizontal(imageRef: ref), + ); + final flipHRef = state.ref!; + validateState(buffer: flipH.buffer.asUint8List()); + state.revert(); + + /// + expect(state.ref, initialRef); + expect(state.previousRef, flipHRef); + + /// + state.op( + (ref) => state.imageRs.operations.grayscale(imageRef: ref), + ); + validateState( + buffer: grayscale.buffer.asUint8List(), + color: ColorType.la8, + ); + final grayscaleRef = state.ref; + + /// grayrot90 + state.op( + (ref) => state.imageRs.operations.rotate90(imageRef: ref), + ); + validateState( + previousRef: grayscaleRef, + color: ColorType.la8, + buffer: grayrot90.buffer.asUint8List(), + height: 581, + width: 223, + ); + final grayscaleRotRef = state.ref; + state.revert(); + validateState( + previousRef: grayscaleRotRef, + color: ColorType.la8, + buffer: grayscale.buffer.asUint8List(), + ); + + state.loadImage('code.png', codeImage.buffer.asUint8List()); + initialRef = state.ref; + + /// + state.op( + (ref) => state.imageRs.operations.adjustContrast(c: 20, imageRef: ref), + ); + validateState(buffer: contrast20.buffer.asUint8List()); + final contrastRef = state.ref; + state.revert(); + + /// + state.setFormat(ImageFormat.gif); + validateState( + buffer: convertGif.buffer.asUint8List(), + format: ImageFormat.gif, + previousRef: contrastRef, + ); + state.setFormat(ImageFormat.png); + + /// + state.op( + (ref) => state.imageRs.operations.crop( + imageCrop: const ImageCrop(x: 0, y: 0, width: 100, height: 52), + imageRef: ref, + ), + ); + validateState( + buffer: crop100x52.buffer.asUint8List(), + width: 100, + height: 52, + ); + state.revert(); + + /// + state.op( + (ref) => state.imageRs.operations.resizeExact( + size: const ImageSize(width: 102, height: 50), + filter: FilterType.nearest, + imageRef: ref, + ), + ); + validateState( + buffer: resize102x50Exact.buffer.asUint8List(), + width: 102, + height: 50, + ); + state.revert(); + final resizedRef = state.ref; + + /// + state.op( + (ref) => state.imageRs.operations.resize( + size: const ImageSize(width: 102, height: 50), + filter: FilterType.nearest, + imageRef: ref, + ), + ); + validateState( + buffer: resize102x50.buffer.asUint8List(), + width: 102, + height: 39, + ); + + /// + state.setFormat(ImageFormat.jpeg); + validateState( + previousRef: resizedRef, + buffer: resize102x50Jpg.buffer.asUint8List(), + format: ImageFormat.jpeg, + width: 102, + height: 39, + ); + state.revert(); + + /// + initialRef = state.ref; + state.loadImage('webp-example.webp', webpexample.buffer.asUint8List()); + + validateState( + filename: 'webp-example.webp', + buffer: webpexample.buffer.asUint8List(), + format: ImageFormat.webP, + color: ColorType.rgb8, + width: 320, + height: 241, + ); + final webpRef = state.ref; + + /// + state.setFormat(ImageFormat.bmp); + validateState( + filename: 'webp-example.webp', + buffer: webpconvertBmp.buffer.asUint8List(), + format: ImageFormat.bmp, + color: ColorType.rgb8, + width: 320, + height: 241, + ); + + /// + state.op( + (ref) => state.imageRs.operations.blur(value: 2, imageRef: ref), + ); + validateState( + filename: 'webp-example.webp', + buffer: webpconvertblur2.buffer.asUint8List(), + format: ImageFormat.bmp, + color: ColorType.rgb8, + width: 320, + height: 241, + previousRef: webpRef, + ); + }); + }); +}