Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is crop function lock image object? #609

Open
SG-XM opened this issue Jan 24, 2024 · 2 comments
Open

Is crop function lock image object? #609

SG-XM opened this issue Jan 24, 2024 · 2 comments

Comments

@SG-XM
Copy link

SG-XM commented Jan 24, 2024

I want to crop one Image into several part. so I have some code below:

List<Future<Uint8List>> resTask = [];
  for (int i = 0; i < param.type; i++) {
    for (int j = 0; j < param.type; j++) {
       ... 
      var task = Future(() {
        imglib.Image croppedImage = imglib.copyCrop(image!,
            x: curX, y: curY, width: curW, height: curH);
        Uint8List picturePiece;
        picturePiece = imglib.encodeJpg(croppedImage);
        return picturePiece;
      });
      resTask.add(task);
    }
  }
  await Future.wait(resTask).then((value) {
    pieces.addAll(value.map((e) => CropImageEntity(e)));
  });

Even I have use List<Future>to dispatch task, it seems to be sync, only after the last crop task finished the next one could be start.

@SG-XM
Copy link
Author

SG-XM commented Jan 24, 2024

Is there any way to split image into several parts with specific Rect in parallel?For 50M jpeg,crop one time cost 2s,so i want to crop in parallel. Thanks for your help!

@brendan-duncan
Copy link
Owner

Unfortunately Dart does not have multithreading support. Your code will all run in the main thread.

Dart does support Isolates, which are more similar to separate processes, they have isolated memory. The Image library Command API supports running commands in Isolates. It does need to copy memory between the isolates, due to the lack of shared memory, so it's not as efficient as regular threads.

final image = Image(width: 256, height: 256);

final List<Future<Uint8List>> resTask = [];
const curW = 128;
const curH = 128;
for (var curY = 0; curY < image.height; curY += curH) {
  for (var curX = 0; curX < image.width; curX += curW) {
    final task = Future(() async {
      final cmd = Command()
        ..image(image) // This will copy the image to the isolate
        ..copyCrop(x: curX, y: curY, width: curW, height: curH)
        ..encodeJpg(); // encode the cropped image to a JPEG
      // Execute the commands in an Isolate thread and wait for the
      // results.
      final result = await cmd.executeThread();
      // The resulting bytes of the last command, encodeJpg
      return result.outputBytes!;
    });
    resTask.add(task);
  }
}

await Future.wait(resTask).then((value) {
  // 4 jpeg files with the cropped images
  expect(value.length, equals(4));
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants