Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a netpbm encoder and decoder, fix some minor bugs #8

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion lib/src/buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ part 'buffer/pixels.dart';
/// representation of pixel data.
///
/// {@category Buffers}
abstract base mixin class Buffer<T> {
abstract base mixin class Buffer<T extends Object?> {
/// Format of the pixel data in the buffer.
PixelFormat<T, void> get format;

Expand Down Expand Up @@ -223,6 +223,43 @@ abstract base mixin class Buffer<T> {
}
return rect.positions.map(getUnsafe);
}

@override
String toString() => bufferToLongString(this);

String get _typeName => 'Buffer';

/// Converts a [Buffer] to a string like [Buffer.toString].
///
/// The string will be long and detailed, suitable for debugging, and even if
/// the dimensions of the buffer are large, the buffer will not be truncated,
/// which may result in a very long string for large buffers.
static String bufferToLongString<T>(Buffer<T> buffer) {
return _bufferToStringImpl(buffer);
}

static String _bufferToStringImpl<T>(Buffer<T> buffer) {
final output = StringBuffer('${buffer._typeName} {\n');
output.writeln(' width: ${buffer.width},');
output.writeln(' height: ${buffer.height},');
output.writeln(' format: ${buffer.format},');
output.writeln(' data: (${buffer.length}) [');

for (var y = 0; y < buffer.height; y++) {
output.write(' ');
for (var x = 0; x < buffer.width; x++) {
output.write(buffer.format.describe(buffer.getUnsafe(Pos(x, y))));
if (x < buffer.width - 1) {
output.write(', ');
}
}
output.writeln(',');
}

output.writeln(' ]');
output.write('}');
return output.toString();
}
}

abstract final class _Buffer<T> with Buffer<T> {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/buffer/pixels_float.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ final class Float32x4Pixels extends Pixels<Float32x4> {

@override
final Float32x4List data;

@override
String get _typeName => 'Float32x4Pixels';
}
3 changes: 3 additions & 0 deletions lib/src/buffer/pixels_int.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ final class IntPixels extends Pixels<int> {

@override
final TypedDataList<int> data;

@override
String get _typeName => 'IntPixels';
}
1 change: 1 addition & 0 deletions lib/src/codec.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'codec/netpbm.dart';
export 'codec/unpng.dart';
Loading