Skip to content

Commit

Permalink
Image.getBytes should convert image if order has different number of …
Browse files Browse the repository at this point in the history
…channels
  • Loading branch information
brendan-duncan committed Jan 12, 2024
1 parent bef921e commit a618de1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
16 changes: 13 additions & 3 deletions lib/src/image/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,19 @@ class Image extends Iterable<Pixel> {

/// Similar to toUint8List, but will convert the channels of the image pixels
/// to the given [order]. If that happens, the returned bytes will be a copy
/// and not a direct view of the image data.
Uint8List getBytes({ChannelOrder? order}) =>
data?.getBytes(order: order) ?? toUint8List();
/// and not a direct view of the image data. If the number of channels needed
/// by [order] differs from what the image has, the bytes will come from a
/// converted image. If the converted image needs an alpha channel added,
/// then you can use the [alpha] argument to specify the value of the
/// added alpha channel.
Uint8List getBytes({ChannelOrder? order, num? alpha}) {
var self = this;
if (channelOrderLength[order] != numChannels) {
self = convert(numChannels: channelOrderLength[order], alpha: alpha);
}
return self.data?.getBytes(order: order,
inPlace: self != this) ?? toUint8List();
}

/// The length in bytes of the image data buffer.
int get lengthInBytes => data?.buffer.lengthInBytes ?? 0;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/image/image_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ abstract class ImageData extends Iterable<Pixel> {
/// Similar to toUint8List, but will convert the channels of the image pixels
/// to the given [order]. If that happens, the returned bytes will be a copy
/// and not a direct view of the image data.
Uint8List getBytes({ChannelOrder? order}) {
Uint8List getBytes({ChannelOrder? order, bool inPlace = false}) {
if (order == null) {
return toUint8List();
}
Expand All @@ -77,7 +77,7 @@ abstract class ImageData extends Iterable<Pixel> {
if (order == ChannelOrder.abgr ||
order == ChannelOrder.argb ||
order == ChannelOrder.bgra) {
final tempImage = clone();
final tempImage = inPlace ? this : clone();
if (order == ChannelOrder.abgr) {
for (final p in tempImage) {
final r = p.r;
Expand Down Expand Up @@ -119,7 +119,7 @@ abstract class ImageData extends Iterable<Pixel> {
}
} else if (numChannels == 3) {
if (order == ChannelOrder.bgr) {
final tempImage = clone();
final tempImage = inPlace ? this : clone();
for (final p in tempImage) {
final r = p.r;
p
Expand Down
12 changes: 12 additions & 0 deletions test/image/image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ void main() {
}
}
});

test('alpha_bmp_1bpp', () async {
final img = await decodePngFile('test/_data/png/alpha_bmp.png');
final bg = Image(width: img!.width, height: img.height)
Expand All @@ -208,5 +209,16 @@ void main() {
final bpp1 = bg.convert(format: Format.uint1, numChannels: 1);
await encodeBmpFile('$testOutputPath/bmp/alpha_bmp_cvt.bmp', bpp1);
});

test('GetBytes rgb->argb', () {
final i1 = Image(width: 1, height: 1)
..setPixelRgb(0, 0, 32, 64, 128);
final b1 = i1.getBytes(order: ChannelOrder.argb);
expect(b1.length, equals(4));
expect(b1[0], equals(255));
expect(b1[1], equals(32));
expect(b1[2], equals(64));
expect(b1[3], equals(128));
});
});
}

0 comments on commit a618de1

Please sign in to comment.