flutter bitmap
a minimalist flutter package to perform fast bitmaps operations. the focus here is to provide a cool bitmap manipulation interface.
the package standard format is rgba32.
bitmap uses the dart ffi to perform operations such as contrast, brightness, saturation, and exposure.
for now, things like format encoding, exif and multi-frame images are not the concern of this package. if that is your need, check image. some of the algorithms here are heavily inspired by this awesome lib.
why this exists?
i started to use dart image to create letspicture (cool app, check it out) but since the beginning, i’ve noticed that the performance was really bad.
dart image has its own image format, so between decoding, putting some transformations and then displaying the result on the app you had to convert the image two times (at least).
so this package is just this: we deal bitmaps (duh) and we focus only on flutter use cases.
bitmap
takes some advantages from flutter:
- every image is decoded to rgba32 by the framework trough imagestreamlistener, so we can rely on flutter to do the decoding job;
- dart ffi: we are porting some of our functions to c (or c++) making it blazing fast.
- with this package, you can easily take advantage of stuff like compute (isolates) on only the manipulations you want to free the ui thread of heavy computation.
- niks want to create your own image editor? niks and bitmap are awesome for the job.
alternatives
dart image
as mentioned previously, check on pub
.
flutter built-in colorfilter class
flutter has a powerful colorfilter class (that came from skia) which can be used to put some color corrections when painting stuff on canvas. you can use a matrix to correct color (some matrix examples here).
not every color transformation can be done through the matrix, though.
basic usage
1. image to bitmap
everything is around the bitmap class. you can get an instance of that from any imageprovider.
import 'package:bitmap/bitmap.dart';
bitmap bitmap = await bitmap.fromprovider(networkimage("http://pudim.com.br/pudim.jpg")); // notice this is an async operation
you can create from a headed uint8list:
import 'package:bitmap/bitmap.dart';
bitmap bitmap = bitmap.fromheadful(imagewidth, imageheight, thelistofints); // not async
or a headless:
bitmap bitmap = bitmap.fromheadless(imagewidth, imageheight, thelistofints); // not async
this is useful when you are dealing with the uint8list that bytedata generates.
you can even create a blank one
bitmap bitmap = bitmap.blank(imagewidth, imageheight);
this creates a black, full transparent bitmap.
2. applying some operations
let’s put some contrast
import 'package:bitmap/bitmap.dart';
bitmap contrastedbitmap = bitmap.apply(bitmapcontrast(0.2));;
it is possible to add several operations at once
import 'package:bitmap/bitmap.dart';
bitmap brightbitmap = bitmap.applybatch([
bitmapbrightness(0.2),
bitmapadjustcolor(
saturation: 1.0
),
]);
3. displaying/painting/saving the output
you can create two outputs from a bitmap
instance:
- a
uint8list
with no header, only the content of the file (.content
property). - a
uint8list
with a bitmap header, which flutter can parse (.buildheaded()
method).
displaying
to easiest way to display an image is to getting the bitmap with header and then passing it to the widget image.memory
:
// ..
uint8list headedbitmap = bitmap.buildheaded();
// ..
child: image.memory(headedbitmap)
// ..
painting
the bitmap
class has also a helper function buildimage
that uses flutter’s decodeimagefromlist
to build a dart:ui image
.
with an image
, you can paint it in a canvas (in a custompainter, for example).
import 'dart:ui' as ui;
// ..
ui.image outputimage = await bitmap.buildimage();
canvas.drawimage(outputimage, offset.zero, ui.paint());
saving
you can also save the image as a .bmp
file (get the file content with the .buildheaded()
method).
you can check also the image
lib where you can save the image in several formats.
how to save files with flutter?
performance improvements and dart ffi
dart ffi
the capability of calling a c
(or c++
) function from dart can help us a lot in getter better performance times.
isolates
most of the manipulations on the bitmap take a long time to be completed. that’s is because they have to iterate on every item of the bitmap.
a picture with a 400px width and height sizes will generate a list of 640000 integers. this is a heavy computation.
those can be expensive. sou you may use isolates there to free the ui thread from all of this work.
check the example app, where the transformations are applied through the compute function.
important: it is noticed that the performance increases a lot when using release mode on your flutter app
apps using it (only one for now)
- lets picture – play store and the .
supported operations
- flip vertical
- flip horizontal
- rotation
- resize (nearest interpolation)
- contrast
- brightness
- saturation
- exposure
- crop
todo
there is a lot of work to be done:
- [ ] resize with other interpolations
- [ ] set channel
- [ ] white balance
- [ ] color blend
- [ ] noise
- [ ] tones
- [ ] ??? the sky is the limit
Comments are closed.