Giter Site home page Giter Site logo

folksable / blurhash_ffi Goto Github PK

View Code? Open in Web Editor NEW
29.0 1.0 6.0 11.46 MB

a port of blurhash to flutter written in c via dart:ffi

Home Page: https://pub.dev/packages/blurhash_ffi

License: BSD 3-Clause "New" or "Revised" License

Java 0.64% Kotlin 0.13% Ruby 4.85% Swift 1.65% Objective-C 0.04% Dart 37.79% CMake 20.36% C++ 21.76% C 12.78%
blurhash dart ffi-bindings flutter image-processing ndk-cmake

blurhash_ffi's Introduction

blurhash_ffi

A Blurhash compact Image placeholder encoder and decoder FFI implementation for flutter in C, Supports Android, iOS, Linux, macOS and Windows.

Matches the official Blurhash implementation in performance and quality.

blurhash_ffi

Usage

To use this plugin, add blurhash_ffi as a dependency in your pubspec.yaml file

One Step (both Encoding & Decoding) Usage

import 'package:blurhash_ffi/blurhash_ffi.dart';

/// Encoding and Decoding all in One Step
///
/// `ImageProvider` in    -> Send your Image to be encoded.
/// `ImageProvider` out   -> Get your blurry image version.

class BlurhashMyImage extends StatelessWidget {
  final String imageUrl;
  const BlurhashMyImage({required this.imageUrl, super.key});

  @override
  Widget build(BuildContext context) {
    return Image(
      image: BlurhashTheImage(
        NetworkImage(imageUrl),  // you can use any image provider of your choice.
          decodingHeight: 1920, decodingWidth: 1080),
      alignment: Alignment.center,
      fit: BoxFit.cover
    );
  }
}

Encoding

import 'package:blurhash_ffi/blurhash_ffi.dart';

/// Encoding a blurhash from an image provider
///
/// You can use any ImageProvider you want, including NetworkImage, FileImage, MemoryImage, AssetImage, etc.
final imageProvider = NetworkImage('https://picsum.photos/512');
final imageProvider2 = AssetImage('assets/image.jpg');

/// Signature
/// static Future<String> encode(
///   ImageProvider imageProvider, {
///   int componentX = 4,
///   int componentY = 3,
/// })
/// may throw `BlurhashFFIException` if encoding fails.
final String blurHash = await BlurhashFFI.encode(imageProvider);

Decoding

import 'package:blurhash_ffi/blurhash_ffi.dart';
import 'dart:ui' as ui;
/// You have 3 ways to decode a blurhash 
///
/// 1. Using the `BlurhashFfi` widget
/// 2. Using the `BlurhashFfiImage` ImageProvider
/// 3. Using the `BlurhashFfi.decode` static method

/// 1. Using the `BlurhashFfi` widget (same constructor as flutter_blurhash's Blurhash widget)
class BlurHashApp extends StatelessWidget {
  const BlurHashApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp(
    home: Scaffold(
      appBar: AppBar(title: const Text("BlurHash")),
      body: const SizedBox.expand(
        child: Center(
          child: AspectRatio(
            aspectRatio: 1.6,
            child: BlurhashFfi(hash: "L5H2EC=PM+yV0g-mq.wG9c010J}I"),
          ),
        ),
      ),
    ),
  );
}

/// 2. Using the `BlurhashFfiImage` ImageProvider
final imageProvider = BlurhashFfiImage("L5H2EC=PM+yV0g-mq.wG9c010J}I");
class BlurHashApp2 extends StatelessWidget {
  const BlurHashApp2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp(
    home: Scaffold(
      appBar: AppBar(title: const Text("BlurHash")),
      body: const SizedBox.expand(
        child: Center(
          child: AspectRatio(
            aspectRatio: 1.6,
            child: Image(
              image: imageProvider,
              fit: BoxFit.cover, 
            ),
          ),
        ),
      ),
    ),
  );
}

/// 3. Using the `BlurhashFfi.decode` static method which returns dart:ui.Image
/// Signature 
/// static Future<ui.Image> decode(
///   String blurHash, {
///   int width = 32,
///   int height = 32,
///   int punch = 1,
/// })
/// may throw `BlurhashFFIException` if decoding fails.
final ui.Image image = await BlurhashFFI.decode("L5H2EC=PM+yV0g-mq.wG9c010J}I");

Release Isolate and it's memory

do this only when you are done with encoding/decoding blurhashes

import 'package:blurhash_ffi/blurhash_ffi.dart';

BlurhashFFi.free();

check the example for more details

contributions in the form of PR's and Issues are a welcome

blurhash_ffi's People

Contributors

dhikshith12 avatar felipecastrosales avatar iliser avatar wh201906 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

blurhash_ffi's Issues

Error when encoding in compute

Good day

I am getting the following error when I call the following in a compute method:
hash = await BlurhashFFI.encode(imageProvider);

Error: BlurException: #0 BlurhashFFI.encode (package:blurhash_ffi/blurhash.dart:88:7)

What could be the cause? Please assist.

Docs: Wrong parameter

In docs, BlurhashFfiImage appears using hash as named parameter (1); but in the implementation the hash is a positional parameter (2).

image

image

Get blurhash from image path

Hello,

I wanted to ask if it's feasible to obtain the blurhash string from an image path. In our app, the client calculates the hash before uploading a new profile picture. Currently, we're utilizing the blurhash_dart package for both encoding and decoding. I'd be open to switching to this package if it offers better performance.

In any case, great work!

Bad state: Future already completed when using CachedNetworkImageProvider

The package works wonderfully when using NetworkImage provider

final imageProvider = NetworkImage(imageUrl);
final String blurHash = await BlurhashFFI.encode(imageProvider);

However, when trying to use a CachedNetworkImageProvider:

final imageProvider = CachedNetworkImageProvider(imageUrl);
final String blurHash = await BlurhashFFI.encode(imageProvider);

Then we get the following error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:43:31)
#1      BlurhashFFI._getImageInfoFromImageProvider.<anonymous closure> (package:blurhash_ffi/blurhash.dart:195:17)
<asynchronous suspension>

Despite the error, a blur hash string is still returned....

Migrating the native code to rust

One fell swoop for (#1 & #2 ),

#1, Rust and flutter_rust_bridge has support for web through WASM, probably there is a way to target current C code to compile for WASM. there is no official implementation for WASM in ffi or ffigen.

#2, this issues arose because MSVC does not support c99 standard which includes the Variable Length Arrays(VLA's), i'm not a expert not them to try and fight the compiler to get this feature working, i tried alloaca, but still no result.

I'm thinking Implementing encoder and the decoder in Rust will solve the above two issues, so i think i'm gonna need some time to learn rust ( heard good things, but did not work with rust before).

Any help in migrating to rust will be of a great contribution to this project.

Thank you.

`blurhash_ffi` - `flutter_blurhash` | Bad state?

Hello,

I currently use flutter_blurhash in my projects, and sometimes I come across the Bad state, as you can see here in this issue.

I quickly analyzed the package, and the widget implementation resembles the above.

I would like to know if there is something similar in this package (the issue cited); or if any treatment was carried out for this - and if so, which one?

Thanks

Windows C Code errors

Currently experiencing a bug on version 1.0.6 when building on windows.

I have tried a flutter clean, and cache repair still no luck.

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.10.0, on Microsoft Windows [Version 10.0.22000.2176], locale en-ZA)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.5.1)
[√] Android Studio (version 2022.2)
[√] VS Code (version 1.80.0)
[√] Connected device (3 available)
[√] Network resources

• No issues found!

Nuget.exe not found, trying to download or use cached version.
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(36,16): error C2057: expected constant expression [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(36,27): error C2466: cannot allocate an array of constant size 0 [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(36,29): error C2057: expected constant expression [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(36,40): error C2466: cannot allocate an array of constant size 0 [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(36,40): error C2087: 'factors': missing subscript [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(36,8): error C2133: 'factors': unknown size [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(37,36): warning C4034: sizeof returns 0 [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(208,15): error C2057: expected constant expression [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(208,26): error C2466: cannot allocate an array of constant size 0 [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(208,8): error C2133: 'colors': unknown size [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
E:\pageme\windows\flutter\ephemeral\.plugin_symlinks\blurhash_ffi\src\blurhash_ffi.c(282,14): warning C4090: 'function': different 'const' qualifiers [E:\pageme\build\windows\plugins\blurhash_ffi\shared\blurhash_ffi.vcxproj]
Exception: Build process failed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.