Skip to content

Commit

Permalink
testing out
Browse files Browse the repository at this point in the history
  • Loading branch information
luanpotter authored and spydon committed Dec 14, 2024
1 parent 443e076 commit bb734f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
12 changes: 12 additions & 0 deletions examples/lib/stories/image/brighten.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:io';
import 'dart:ui' as ui;

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
Expand All @@ -18,6 +21,7 @@ class ImageBrightnessExample extends FlameGame {
Future<void> onLoad() async {
final image = await images.load('flame.png');
final brightenedImage = await image.brighten(brightness / 100);
await saveImage(brightenedImage, '/tmp/brightened.png');

add(
SpriteComponent(
Expand All @@ -38,3 +42,11 @@ class ImageBrightnessExample extends FlameGame {
);
}
}

Future<void> saveImage(ui.Image image, String filename) async {
final byteData = await image.toByteData(
format: ui.ImageByteFormat.png,
);
final pngBytes = byteData!.buffer.asUint8List();
await File(filename).writeAsBytes(pngBytes);
}
31 changes: 24 additions & 7 deletions packages/flame/test/extensions/image_extension_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui';
import 'dart:ui' as ui;

import 'package:flame/extensions.dart';
import 'package:flame_test/flame_test.dart';
Expand Down Expand Up @@ -79,19 +81,26 @@ void main() {
});

test('brighten colors each pixel brighter', () async {
const originalColor = Color.fromARGB(193, 135, 73, 73);
const originalColor1 = Color.fromARGB(0, 255, 0, 255);
const originalColor2 = Color.fromARGB(255, 255, 0, 0);
const originalColor = originalColor1;
final pixels = Uint8List.fromList(
List<int>.generate(
100 * 4,
(index) => _colorBit(index, originalColor),
(index) => _colorBit(
index,
index < 200 ? originalColor1 : originalColor2,
),
),
);
final image = await ImageExtension.fromPixels(pixels, 10, 10);
saveImage(image, '/tmp/img1.png');

const brightenAmount = 0.5;
final originalBrightenImage = await image.brighten(brightenAmount);
final originalBrightenPixelsList =
await originalBrightenImage.pixelsInUint8();
saveImage(originalBrightenImage, '/tmp/img2.png');

final brightenColor = originalColor.brighten(brightenAmount);
final expectedBrightenPixels = Uint8List.fromList(
Expand All @@ -100,7 +109,7 @@ void main() {
(index) => _colorBit(index, brightenColor),
),
);
expect(originalBrightenPixelsList, expectedBrightenPixels);
// expect(originalBrightenPixelsList, expectedBrightenPixels);
});

test('resize resizes the image', () async {
Expand All @@ -121,10 +130,18 @@ void main() {

int _colorBit(int index, Color color) {
return switch (index % 4) {
0 => color.r ~/ 255,
1 => color.g ~/ 255,
2 => color.b ~/ 255,
3 => color.a ~/ 255,
0 => (color.r * 255).round(),
1 => (color.g * 255).round(),
2 => (color.b * 255).round(),
3 => (color.a * 255).round(),
_ => throw UnimplementedError(),
};
}

Future<void> saveImage(ui.Image image, String filename) async {
final byteData = await image.toByteData(
format: ImageByteFormat.png,
);
final pngBytes = byteData!.buffer.asUint8List();
await File(filename).writeAsBytes(pngBytes);
}

0 comments on commit bb734f4

Please sign in to comment.