Skip to content

Commit

Permalink
Add a netpbm encoder and decoder, fix some minor bugs (#8)
Browse files Browse the repository at this point in the history
Closes #4.
  • Loading branch information
matanlurey authored Aug 31, 2024
1 parent 543c000 commit af664e0
Show file tree
Hide file tree
Showing 16 changed files with 1,092 additions and 17 deletions.
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

0 comments on commit af664e0

Please sign in to comment.