Skip to content

Commit

Permalink
Reuse code moved into pkg/lodim.
Browse files Browse the repository at this point in the history
  • Loading branch information
matanlurey committed Sep 7, 2024
1 parent ce147f6 commit c379ef6
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 334 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.1.0-alpha+1

- Upgraded to `lodim ^0.1.6`, which now hosts some of the original functionality
of this package in a more general-purpose way (i.e. `fill`, `copy`, ...).

## 0.1.0-alpha

Initial preview release 🎉!
Expand Down
2 changes: 1 addition & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
final blue = (y / (imageHeight / 2) * 255).toInt();
image.fill(
abgr8888.create(blue: blue),
target: Rect.fromLTWH(0, y, imageWidth, 1),
Rect.fromLTWH(0, y, imageWidth, 1),
);
}

Expand Down
40 changes: 5 additions & 35 deletions lib/src/buffer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:typed_data';

import 'package:lodim/lodim.dart' as lodim;
import 'package:pxl/src/blend.dart';
import 'package:pxl/src/format.dart';
import 'package:pxl/src/geometry.dart';
Expand Down Expand Up @@ -196,44 +197,16 @@ abstract base mixin class Buffer<T extends Object?> {
return _ScaledBuffer(this, scale);
}

/// Returns a lazy iterable of pixels in the buffer from [start] to [end].
///
/// The returned iterable will contain all pixels in the buffer that are
/// within the rectangle defined by [start] and [end], inclusive.
///
/// The provided positions are clamped to the bounds of the buffer, and yield
/// no pixels if `start > end`.
Iterable<T> getRange(Pos start, Pos end) {
final bottomRight = bounds.bottomRight;
start = start.clamp(Pos.zero, bottomRight);
end = end.clamp(Pos.zero, bottomRight);
if (Pos.byRowMajor(start, end) > 0) {
return const Iterable.empty();
}
return getRangeUnsafe(start, end);
}

/// Returns a lazy iterable of pixels in the buffer from [start] to [end].
///
/// The returned iterable will contain all pixels in the buffer that are
/// within the rectangle defined by [start] and [end], inclusive.
///
/// The provided positions must be `(0, 0) <= start <= end < (width, height)`
/// or the behavior is undefined.
Iterable<T> getRangeUnsafe(Pos start, Pos end) {
final iStart = start.y * width + start.x;
final iEnd = end.y * width + end.x;
return data.skip(iStart).take(iEnd - iStart + 1);
}

/// Returns a lazy iterable of pixels in the rectangle defined by [rect].
///
/// The returned iterable will contain all pixels in the buffer that are
/// within the rectangle defined by [rect].
///
/// The provided rectangle is clamped to the bounds of the buffer and yields
/// no pixels if the rectangle is empty.
Iterable<T> getRect(Rect rect) => getRectUnsafe(rect.intersect(bounds));
Iterable<T> getRect(Rect rect) {
return getRectUnsafe(rect.intersect(bounds));
}

/// Returns a lazy iterable of pixels in the rectangle defined by [rect].
///
Expand All @@ -243,10 +216,7 @@ abstract base mixin class Buffer<T extends Object?> {
/// The provided rectangle must be contained within the bounds of the buffer
/// or the behavior is undefined.
Iterable<T> getRectUnsafe(Rect rect) {
if (rect.width == width) {
return getRangeUnsafe(rect.topLeft, rect.bottomRight - const Pos(1, 1));
}
return rect.positions.map(getUnsafe);
return lodim.getRect(rect, getUnsafe);
}

@override
Expand Down
Loading

0 comments on commit c379ef6

Please sign in to comment.