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

Fix Issue #20 #21

Merged
merged 3 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.2.1]

* Fix crash for some specific `Image`s by using an pixel iterator
instead of raw byte access.

## [1.2.0]

* Update image dependency to >=4.0.8
Expand Down
69 changes: 25 additions & 44 deletions lib/src/blurhash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class BlurHash {
);
}

final data = image.getBytes();
final img = image.convert(format: Format.uint8);
final components = List.generate(
numCompY,
(i) => List<ColorTriplet>.filled(numCompX, ColorTriplet(0, 0, 0)),
Expand All @@ -115,20 +115,11 @@ class BlurHash {
for (var y = 0; y < numCompY; ++y) {
for (var x = 0; x < numCompX; ++x) {
final normalisation = (x == 0 && y == 0) ? 1.0 : 2.0;
components[y][x] = _multiplyBasisFunction(
data,
image.width,
image.height,
x,
y,
normalisation,
image.numChannels,
);
components[y][x] = _multiplyBasisFunction(img, x, y, normalisation);
}
}

final hash = _encodeComponents(components);
return BlurHash._(hash, components);
return BlurHash._(_encodeComponents(components), components);
}

/// Construct a [BlurHash] with a single color.
Expand Down Expand Up @@ -301,49 +292,39 @@ Uint8List _transform(
}

ColorTriplet _multiplyBasisFunction(
Uint8List pixels,
int width,
int height,
Image image,
int x,
int y,
double normalisation,
int channelOrderLength,
) {
var r = 0.0;
var g = 0.0;
var b = 0.0;

final bytesPerRow = width * channelOrderLength;

if (channelOrderLength == 3 || channelOrderLength == 4) {
for (var i = 0; i < width; ++i) {
for (var j = 0; j < height; ++j) {
final basis = normalisation *
cos((pi * x * i) / width) *
cos((pi * y * j) / height);
r += basis *
sRgbToLinear(pixels[channelOrderLength * i + 0 + j * bytesPerRow]);
g += basis *
sRgbToLinear(pixels[channelOrderLength * i + 1 + j * bytesPerRow]);
b += basis *
sRgbToLinear(pixels[channelOrderLength * i + 2 + j * bytesPerRow]);
}
if (image.numChannels >= 3) {
for (final pixel in image) {
final basis = normalisation *
cos((pi * x * pixel.x) / image.width) *
cos((pi * y * pixel.y) / image.height);

r += basis * sRgbToLinear(pixel.r as int);
g += basis * sRgbToLinear(pixel.g as int);
b += basis * sRgbToLinear(pixel.b as int);
}
} else if (channelOrderLength == 2) {
for (var i = 0; i < width; ++i) {
for (var j = 0; j < height; ++j) {
final basis = normalisation *
cos((pi * x * i) / width) *
cos((pi * y * j) / height);
final v = basis *
sRgbToLinear(pixels[channelOrderLength * i + 0 + j * bytesPerRow]);
r += v;
g += v;
b += v;
}
} else {
for (final pixel in image) {
final basis = normalisation *
cos((pi * x * pixel.x) / image.width) *
cos((pi * y * pixel.y) / image.height);

final value = sRgbToLinear(pixel.r as int);

r += basis * value;
g += basis * value;
b += basis * value;
}
}

final scale = 1.0 / (width * height);
final scale = 1.0 / (image.width * image.height);
return ColorTriplet(r * scale, g * scale, b * scale);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: blurhash_dart
version: 1.2.0
version: 1.2.1
homepage: https://github.com/justacid/blurhash-dart
description: >
A pure dart implementation of the BlurHash algorithm.
Expand Down