Skip to content

Commit

Permalink
Merge pull request #11 from nkming2/support-webp-extended-format
Browse files Browse the repository at this point in the history
Support webp extended format
  • Loading branch information
CaiJingLong committed Mar 31, 2022
2 parents 448f76b + 1f16c45 commit 72cdae4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
Binary file added example/asset/demo_extended.webp
Binary file not shown.
1 change: 1 addition & 0 deletions library/lib/src/decoder/async/decoder.dart
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:image_size_getter/src/core/size.dart';

import 'package:image_size_getter/src/entity/block_entity.dart';
Expand Down
23 changes: 18 additions & 5 deletions library/lib/src/decoder/async/webp_decoder.dart
Expand Up @@ -5,15 +5,28 @@ class WebpDecoder extends AsyncImageDecoder {

@override
Future<Size> get size async {
final widthList = await input.getRange(0x1a, 0x1c);
final heightList = await input.getRange(0x1c, 0x1e);
final width = convertRadix16ToInt(widthList, reverse: true);
final height = convertRadix16ToInt(heightList, reverse: true);
return Size(width, height);
if (await _isExtendedFormat()) {
final widthList = await input.getRange(0x18, 0x1b);
final heightList = await input.getRange(0x1b, 0x1d);
final width = convertRadix16ToInt(widthList, reverse: true) + 1;
final height = convertRadix16ToInt(heightList, reverse: true) + 1;
return Size(width, height);
} else {
final widthList = await input.getRange(0x1a, 0x1c);
final heightList = await input.getRange(0x1c, 0x1e);
final width = convertRadix16ToInt(widthList, reverse: true);
final height = convertRadix16ToInt(heightList, reverse: true);
return Size(width, height);
}
}

Future<bool> isLossy() async {
final v8Tag = await input.getRange(12, 16);
return v8Tag[3] == 0x20;
}

Future<bool> _isExtendedFormat() async {
final chunkHeader = await input.getRange(12, 16);
return ListEquality().equals(chunkHeader, "VP8X".codeUnits);
}
}
1 change: 1 addition & 0 deletions library/lib/src/decoder/sync/decoder.dart
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:image_size_getter/src/core/size.dart';

import 'package:image_size_getter/src/entity/block_entity.dart';
Expand Down
23 changes: 18 additions & 5 deletions library/lib/src/decoder/sync/webp_decoder.dart
Expand Up @@ -7,15 +7,28 @@ class WebpDecoder extends ImageDecoder {

@override
Size get size {
final widthList = input.getRange(0x1a, 0x1c);
final heightList = input.getRange(0x1c, 0x1e);
final width = convertRadix16ToInt(widthList, reverse: true);
final height = convertRadix16ToInt(heightList, reverse: true);
return Size(width, height);
if (_isExtendedFormat()) {
final widthList = input.getRange(0x18, 0x1b);
final heightList = input.getRange(0x1b, 0x1d);
final width = convertRadix16ToInt(widthList, reverse: true) + 1;
final height = convertRadix16ToInt(heightList, reverse: true) + 1;
return Size(width, height);
} else {
final widthList = input.getRange(0x1a, 0x1c);
final heightList = input.getRange(0x1c, 0x1e);
final width = convertRadix16ToInt(widthList, reverse: true);
final height = convertRadix16ToInt(heightList, reverse: true);
return Size(width, height);
}
}

Future<bool> isLossy() async {
final v8Tag = input.getRange(12, 16);
return v8Tag[3] == 0x20;
}

bool _isExtendedFormat() {
final chunkHeader = input.getRange(12, 16);
return ListEquality().equals(chunkHeader, "VP8X".codeUnits);
}
}
7 changes: 7 additions & 0 deletions library/test/image_size_getter_test.dart
Expand Up @@ -12,6 +12,13 @@ void main() {
await expectLater(size, Size(988, 466));
});

test('Test webp extended format size', () async {
final file = File('../example/asset/demo_extended.webp');
final size = ImageSizeGetter.getSize(FileInput(file));
print('size = $size');
await expectLater(size, Size(988, 466));
});

test('Test jpeg size', () async {
final file = File('../example/asset/IMG_20180908_080245.jpg');
final size = ImageSizeGetter.getSize(FileInput(file));
Expand Down

0 comments on commit 72cdae4

Please sign in to comment.