Skip to content

Commit

Permalink
RGBA -> RGB now happens in C
Browse files Browse the repository at this point in the history
  • Loading branch information
drxddy committed Mar 21, 2024
1 parent 4ea9cba commit 4e959be
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 50 deletions.
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'
# platform :ios, '12.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
blurhash_ffi: 4831b96320d4273876c9a2fd3f7d50b8a3a53509
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7

PODFILE CHECKSUM: 70d9d25280d0dd177a5f637cdb0f0b0b12c6a189
PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796

COCOAPODS: 1.12.1
COCOAPODS: 1.15.2
8 changes: 4 additions & 4 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C8080294A63A400263BE5 = {
Expand Down Expand Up @@ -457,7 +457,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -584,7 +584,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -633,7 +633,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.2.3"
version: "1.2.4"
boolean_selector:
dependency: transitive
description:
Expand Down
31 changes: 15 additions & 16 deletions lib/blurhash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ class BlurhashFFI {
ImageProvider imageProvider) async {
final completer = Completer<BlurHashImageInfo>();
final listener = ImageStreamListener((imageInfo, _) async {
final ByteData? bytes =
await imageInfo.image.toByteData(format: ui.ImageByteFormat.rawRgba);
final ByteData? bytes = await imageInfo.image
.toByteData(format: ui.ImageByteFormat.rawRgba);
if (bytes == null) {
completer.completeError(BlurhashFFIException(
'Could not decode Image from Image provider',
Expand All @@ -186,15 +186,10 @@ class BlurhashFFI {
return;
}
final Uint8List list = bytes.buffer.asUint8List();
final rgbBytes = Uint8List(list.length ~/ 4 * 3);

This comment has been minimized.

Copy link
@drxddy

drxddy Mar 21, 2024

Author Member

we no longer need to convert from RGBA to RGB now, it's inferred in the C side.

for (var i = 0, j = 0; i < list.length; i += 4, j += 3) {
rgbBytes[j] = list[i];
rgbBytes[j + 1] = list[i + 1];
rgbBytes[j + 2] = list[i + 2];
}

if (!completer.isCompleted) {
completer.complete(BlurHashImageInfo(imageInfo.image.height,
imageInfo.image.width, imageInfo.image.width * 3, rgbBytes));
imageInfo.image.width, imageInfo.image.width * 4, list));
}
}, onError: (dynamic exception, StackTrace? stackTrace) {
completer.completeError(exception, stackTrace);
Expand Down Expand Up @@ -356,7 +351,6 @@ class BlurhashFFI {
data.pixelsPointer,
data.rowStride,
);
data.free();
final String resultString = result.cast<Utf8>().toDartString();
final _EncodeResponse response =
_EncodeResponse(data.id, resultString);
Expand All @@ -365,7 +359,6 @@ class BlurhashFFI {
} else if (data is _DecodeRequest) {
final Pointer<Uint8> result = _bindings.decode(data.blurHashPointer,
data.width, data.height, data.punch, data.channels);
data.free();
final Uint8List resultImage = result.asTypedList(
data.width * data.height * data.channels,
// preffer way but works only from dart 3.1.0, and requre to change generated bindings
Expand Down Expand Up @@ -402,7 +395,7 @@ class BlurhashFFI {
final stackTrace = StackTrace.current;
throw BlurhashFFIException(
'ERORR: ${Isolate.current.debugName}', stackTrace, e);
}
}
}

final ReceivePort helperReceivePort = ReceivePort()..listen(onSend);
Expand All @@ -423,7 +416,11 @@ class BlurhashFFIException extends Error {
[this.level = Level.SEVERE]);
}

class _DecodeToArrayRequest {
mixin Freeable {
void free();
}

class _DecodeToArrayRequest with Freeable{
final int id;
final String blurHash;
final int width;
Expand All @@ -444,6 +441,7 @@ class _DecodeToArrayRequest {
return _bhptr!.cast<Char>();
}

@override
void free() {
if (_bhptr != null) {
malloc.free(_bhptr!);
Expand All @@ -452,7 +450,7 @@ class _DecodeToArrayRequest {
}
}

class _DecodeRequest {
class _DecodeRequest with Freeable{
final int id;
final String blurHash;
final int width;
Expand All @@ -471,7 +469,7 @@ class _DecodeRequest {
}
return _bhptr!.cast<Char>();
}

@override
void free() {
if (_bhptr != null) {
malloc.free(_bhptr!);
Expand All @@ -480,7 +478,7 @@ class _DecodeRequest {
}
}

class _EncodeRequest {
class _EncodeRequest with Freeable{
final int id;
final Uint8List pixels;
final int width;
Expand All @@ -501,6 +499,7 @@ class _EncodeRequest {
return pixelsPtr!;
}

@override
void free() {
if (pixelsPtr != null) {
calloc.free(pixelsPtr!);
Expand Down
20 changes: 1 addition & 19 deletions lib/blurhash_ffi_bindings_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BlurhashFfiBindings {
/// `yComponents` - The number of components in the Y direction. Must be between 1 and 9. 3 to 5 is usually a good range for this.
/// `width` - The width in pixels of the supplied image.
/// `height` - The height in pixels of the supplied image.
/// `rgb` - A pointer to the pixel data. This is supplied in RGB order, with 3 bytes per pixels.
/// `rgb` - A pointer to the pixel data. This is supplied in RGBA format, with 4 bytes per pixels.
/// `bytesPerRow` - The number of bytes per row of the RGB pixel data.
ffi.Pointer<ffi.Char> blurHashForPixels(
int xComponents,
Expand Down Expand Up @@ -166,22 +166,4 @@ class BlurhashFfiBindings {
'freePixelArray');
late final _freePixelArray =
_freePixelArrayPtr.asFunction<void Function(ffi.Pointer<ffi.Uint8>)>();

/// freeBlurhash : Frees the blurhash string
/// Parameters :
/// blurhash : Blurhash string pointer which will be freed.
/// Returns : void (None)
void freeString(
ffi.Pointer<ffi.Char> blurhash,
) {
return _freeString(
blurhash,
);
}

late final _freeStringPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Char>)>>(
'freeString');
late final _freeString =
_freeStringPtr.asFunction<void Function(ffi.Pointer<ffi.Char>)>();
}
10 changes: 7 additions & 3 deletions src/blurhash_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const char *blurHashForPixels(int xComponents, int yComponents, int width, int h
}
}

free(rgb);

This comment has been minimized.

Copy link
@drxddy

drxddy Mar 21, 2024

Author Member

Freeing in the C side, not from the Dart side as previously done.


float *dc = &factors[0];
float *ac = dc + 3;
int acCount = xComponents * yComponents - 1;
Expand Down Expand Up @@ -88,12 +90,14 @@ static float *multiplyBasisFunction(int xComponent, int yComponent, int width, i
float r = 0, g = 0, b = 0;
float normalisation = (xComponent == 0 && yComponent == 0) ? 1 : 2;

int channels = (int)bytesPerRow / width;

for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
float basis = cosf(M_PI * xComponent * x / width) * cosf(M_PI * yComponent * y / height);
r += basis * sRGBToLinear(rgb[3 * x + 0 + y * bytesPerRow]);
g += basis * sRGBToLinear(rgb[3 * x + 1 + y * bytesPerRow]);
b += basis * sRGBToLinear(rgb[3 * x + 2 + y * bytesPerRow]);
r += basis * sRGBToLinear(rgb[channels * x + 0 + y * bytesPerRow]);

This comment has been minimized.

Copy link
@drxddy

drxddy Mar 21, 2024

Author Member

do not assume 3 channels only.

g += basis * sRGBToLinear(rgb[channels * x + 1 + y * bytesPerRow]);
b += basis * sRGBToLinear(rgb[channels * x + 2 + y * bytesPerRow]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/blurhash_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
`yComponents` - The number of components in the Y direction. Must be between 1 and 9. 3 to 5 is usually a good range for this.
`width` - The width in pixels of the supplied image.
`height` - The height in pixels of the supplied image.
`rgb` - A pointer to the pixel data. This is supplied in RGB order, with 3 bytes per pixels.
`rgb` - A pointer to the pixel data. This is supplied in RGBA format, with 4 bytes per pixels.
`bytesPerRow` - The number of bytes per row of the RGB pixel data.
*/

Expand Down

0 comments on commit 4e959be

Please sign in to comment.