Skip to content

Commit

Permalink
Isolate the calculation of the histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
kra-mo committed Sep 18, 2024
1 parent e092610 commit 187da6c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
14 changes: 8 additions & 6 deletions lib/editor_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class _SlyEditorPageState extends State<SlyEditorPage> {

late final SlyImage _originalImage = widget.image;
late SlyImage _editedImage;
late Widget _histogram = getHistogram(_editedImage);
Widget? _histogram;

Uint8List? _originalImageData;
Uint8List? _editedImageData;
Expand Down Expand Up @@ -220,17 +220,19 @@ class _SlyEditorPageState extends State<SlyEditorPage> {
_saveOnLoad = false;
}

if (!mounted) return;
setState(() {
_histogram = getHistogram(_editedImage);
});

_editedImage.encode(format: 'JPEG75').then((data) {
if (!mounted) return;
setState(() {
_editedImageData = data;
});
});

getHistogram(_editedImage).then((data) {
if (!mounted) return;
setState(() {
_histogram = data;
});
});
}
}

Expand Down
11 changes: 7 additions & 4 deletions lib/histogram.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:fl_chart/fl_chart.dart';

import 'image.dart';

LineChart getHistogram(SlyImage image) {
final imageData = image.getHistogramData();

LineChart _buildHistogram(Uint8List imagdeData) {
final List<List<FlSpot>> spots = [
List.generate(16, (index) => FlSpot(index.toDouble(), 0)),
List.generate(16, (index) => FlSpot(index.toDouble(), 0)),
List.generate(16, (index) => FlSpot(index.toDouble(), 0))
];

int channel = 0;
for (int pixel in imageData) {
for (int pixel in imagdeData) {
int i = pixel >> 4;
if (i == 0 && pixel != 0) {
i = 1;
Expand Down Expand Up @@ -67,3 +66,7 @@ LineChart getHistogram(SlyImage image) {
),
);
}

Future<LineChart> getHistogram(SlyImage image) async {
return await compute(_buildHistogram, await image.getHistogramData());
}
10 changes: 6 additions & 4 deletions lib/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,13 @@ class SlyImage {

/// Returns a short list representing the RGB colors across the image,
/// useful for building a histogram.
Uint8List getHistogramData() {
final resizedImage =
img.copyResize(_image, width: 20, height: 20).convert(numChannels: 3);
Future<Uint8List> getHistogramData() async {
final cmd = img.Command()
..image(_image)
..copyResize(width: 20, height: 20)
..convert(numChannels: 3);

return resizedImage.buffer.asUint8List();
return (await cmd.executeThread()).outputImage!.buffer.asUint8List();
}

void dispose() {
Expand Down

0 comments on commit 187da6c

Please sign in to comment.