LCOV - code coverage report
Current view: top level - lib/src/extensions - image.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 2 36 5.6 %
Date: 2021-08-10 15:50:53 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:typed_data';
       2             : import 'dart:ui';
       3             : 
       4             : import '../flame.dart';
       5             : import 'color.dart';
       6             : import 'vector2.dart';
       7             : 
       8             : export 'dart:ui' show Image;
       9             : 
      10             : extension ImageExtension on Image {
      11             :   /// Helper method for retrieve the pixel data in a Uint8 format.
      12             :   ///
      13             :   /// Pixel order used the [ImageByteFormat.rawRgba] meaning it is: R G B A.
      14           0 :   Future<Uint8List> pixelsInUint8() async {
      15           0 :     return (await toByteData())!.buffer.asUint8List();
      16             :   }
      17             : 
      18             :   /// Returns the bounding [Rect] of the image.
      19           4 :   Rect getBoundingRect() => Vector2.zero() & size;
      20             : 
      21             :   /// Returns a [Vector2] representing the dimensions of this image.
      22          20 :   Vector2 get size => Vector2Extension.fromInts(width, height);
      23             : 
      24             :   /// Change each pixel's color to be darker and return a new [Image].
      25             :   ///
      26             :   /// The [amount] is a double value between 0 and 1.
      27           0 :   Future<Image> darken(double amount) async {
      28           0 :     assert(amount >= 0 && amount <= 1);
      29             : 
      30           0 :     final pixelData = await pixelsInUint8();
      31           0 :     final newPixelData = Uint8List(pixelData.length);
      32             : 
      33           0 :     for (var i = 0; i < pixelData.length; i += 4) {
      34           0 :       final color = Color.fromARGB(
      35           0 :         pixelData[i + 3],
      36           0 :         pixelData[i + 0],
      37           0 :         pixelData[i + 1],
      38           0 :         pixelData[i + 2],
      39           0 :       ).darken(amount);
      40             : 
      41           0 :       newPixelData[i] = color.red;
      42           0 :       newPixelData[i + 1] = color.green;
      43           0 :       newPixelData[i + 2] = color.blue;
      44           0 :       newPixelData[i + 3] = color.alpha;
      45             :     }
      46             : 
      47           0 :     return Flame.images.decodeImageFromPixels(newPixelData, width, height);
      48             :   }
      49             : 
      50             :   /// Change each pixel's color to be brighter and return a new [Image].
      51             :   ///
      52             :   /// The [amount] is a double value between 0 and 1.
      53           0 :   Future<Image> brighten(double amount) async {
      54           0 :     assert(amount >= 0 && amount <= 1);
      55             : 
      56           0 :     final pixelData = await pixelsInUint8();
      57           0 :     final newPixelData = Uint8List(pixelData.length);
      58             : 
      59           0 :     for (var i = 0; i < pixelData.length; i += 4) {
      60           0 :       final color = Color.fromARGB(
      61           0 :         pixelData[i + 3],
      62           0 :         pixelData[i + 0],
      63           0 :         pixelData[i + 1],
      64           0 :         pixelData[i + 2],
      65           0 :       ).brighten(amount);
      66             : 
      67           0 :       newPixelData[i] = color.red;
      68           0 :       newPixelData[i + 1] = color.green;
      69           0 :       newPixelData[i + 2] = color.blue;
      70           0 :       newPixelData[i + 3] = color.alpha;
      71             :     }
      72             : 
      73           0 :     return Flame.images.decodeImageFromPixels(newPixelData, width, height);
      74             :   }
      75             : }

Generated by: LCOV version 1.15