Skip to content
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
5 changes: 5 additions & 0 deletions packages/image_size_getter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG

- [CHANGELOG](#changelog)
- [2.4.1](#241)
- [2.4.0](#240)
- [2.3.0+1](#2301)
- [2.3.0](#230)
Expand All @@ -18,6 +19,10 @@
- [0.1.1](#011)
- [0.1.0](#010)

## 2.4.1

- Fix: Corrected orientation retrieval in JPEG images.

## 2.4.0

- Add `isExtensionSupported` to `BaseDecoder`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class JpegDecoder extends BaseDecoder with SimpleTypeValidator {
throw Exception('Invalid jpeg file');
}

// Check for App1 block
if (block.type == 0xE1) {
final app1BlockData = await input.getRange(
start,
Expand All @@ -91,7 +92,6 @@ class JpegDecoder extends BaseDecoder with SimpleTypeValidator {
if (block.type == 0xC0 || block.type == 0xC2) {
final widthList = await input.getRange(start + 7, start + 9);
final heightList = await input.getRange(start + 5, start + 7);
orientation = (await input.getRange(start + 9, start + 10))[0];
return _getSize(widthList, heightList, orientation);
} else {
start += block.length;
Expand Down
2 changes: 1 addition & 1 deletion packages/image_size_getter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: image_size_getter
description: Get image width and height, the library does not completely decode the image file, just read the metadata to get the image width and height.
version: 2.4.0
version: 2.4.1
homepage: https://github.com/CaiJingLong/dart_image_size_getter

topics:
Expand Down
225 changes: 225 additions & 0 deletions packages/image_size_getter/test/image_size_getter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,229 @@ void main() {
print('size = ${result.size} (decoded by ${result.decoder.decoderName})');
});
});

group('Test async methods', () {
test('Test getSizeAsync with gif', () async {
final file = File('../../example/asset/dialog.gif');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(688, 1326));
});

test('Test getSizeAsync with jpeg', () async {
final file = File('../../example/asset/IMG_20180908_080245.jpg');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(4032, 3024));
});

test('Test getSizeAsync with non-standard jpeg', () async {
final file = File('../../example/asset/test.MP.jpg');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(3840, 2160, needRotate: true));
});

test('Test getSizeAsync with png', () async {
final file = File('../../example/asset/ic_launcher.png');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(96, 96));
});

test('Test getSizeAsync with webp', () async {
final file = File('../../example/asset/demo.webp');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(988, 466));
});

test('Test getSizeAsync with webp extended format', () async {
final file = File('../../example/asset/demo_extended.webp');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(988, 466));
});

test('Test getSizeAsync with webp lossless format', () async {
final file = File('../../example/asset/demo_lossless.webp');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(988, 466));
});

test('Test getSizeAsync with bmp', () async {
final file = File('../../example/asset/demo.bmp');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(256, 256));
});

test('Test getSizeAsync with orientation jpeg', () async {
final file = File('../../example/asset/have_orientation_exif_3.jpg');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(533, 799));

final file2 = File('../../example/asset/have_orientation_exif_6.jpg');
final input2 = FileInput(file2);
final asyncInput2 = AsyncImageInput.input(input2);

final size2 = await ImageSizeGetter.getSizeAsync(asyncInput2);
expect(size2, Size(3264, 2448, needRotate: true));
});

test('Test getSizeResultAsync with gif', () async {
final file = File('../../example/asset/dialog.gif');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final result = await ImageSizeGetter.getSizeResultAsync(asyncInput);
expect(result.size, Size(688, 1326));
expect(result.decoder.decoderName, 'gif');
});

test('Test getSizeResultAsync with jpeg', () async {
final file = File('../../example/asset/IMG_20180908_080245.jpg');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final result = await ImageSizeGetter.getSizeResultAsync(asyncInput);
expect(result.size, Size(4032, 3024));
expect(result.decoder.decoderName, 'jpeg');
});

test('Test getSizeResultAsync with png', () async {
final file = File('../../example/asset/ic_launcher.png');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final result = await ImageSizeGetter.getSizeResultAsync(asyncInput);
expect(result.size, Size(96, 96));
expect(result.decoder.decoderName, 'png');
});

test('Test getSizeResultAsync with webp', () async {
final file = File('../../example/asset/demo.webp');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final result = await ImageSizeGetter.getSizeResultAsync(asyncInput);
expect(result.size, Size(988, 466));
expect(result.decoder.decoderName, 'webp');
});

test('Test getSizeResultAsync with bmp', () async {
final file = File('../../example/asset/demo.bmp');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

final result = await ImageSizeGetter.getSizeResultAsync(asyncInput);
expect(result.size, Size(256, 256));
expect(result.decoder.decoderName, 'bmp');
});

test('Test getSizeAsync with non-existent file', () async {
final file = File('../../example/asset/non_existent.jpg');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

expect(
() async => await ImageSizeGetter.getSizeAsync(asyncInput),
throwsA(isA<StateError>()),
);
});

test('Test getSizeResultAsync with non-existent file', () async {
final file = File('../../example/asset/non_existent.jpg');
final input = FileInput(file);
final asyncInput = AsyncImageInput.input(input);

expect(
() async => await ImageSizeGetter.getSizeResultAsync(asyncInput),
throwsA(isA<StateError>()),
);
});

test('Test getSizeAsync with unsupported format', () async {
// Create a temporary file with unsupported content
final tempFile = File('../../example/asset/temp_unsupported.txt');
Copy link

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test creates temporary files in the asset directory which could cause issues if tests run concurrently or fail unexpectedly. Consider using a system temp directory or test-specific directory to avoid potential conflicts with existing asset files.

Suggested change
final tempFile = File('../../example/asset/temp_unsupported.txt');
final tempFile = File('${Directory.systemTemp.path}/temp_unsupported_${DateTime.now().microsecondsSinceEpoch}.txt');

Copilot uses AI. Check for mistakes.
await tempFile.writeAsString('This is not an image file');

try {
final input = FileInput(tempFile);
final asyncInput = AsyncImageInput.input(input);

expect(
() async => await ImageSizeGetter.getSizeAsync(asyncInput),
throwsA(isA<UnsupportedError>()),
);
} finally {
// Clean up
if (await tempFile.exists()) {
await tempFile.delete();
}
}
});

test('Test getSizeResultAsync with unsupported format', () async {
// Create a temporary file with unsupported content
final tempFile = File('../../example/asset/temp_unsupported2.txt');
Copy link

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous test, this creates a temporary file in the asset directory. Consider using a system temp directory or test-specific directory to avoid potential conflicts with existing asset files.

Suggested change
final tempFile = File('../../example/asset/temp_unsupported2.txt');
final tempFile = File('${Directory.systemTemp.path}/temp_unsupported2.txt');

Copilot uses AI. Check for mistakes.
await tempFile.writeAsString('This is not an image file');

try {
final input = FileInput(tempFile);
final asyncInput = AsyncImageInput.input(input);

expect(
() async => await ImageSizeGetter.getSizeResultAsync(asyncInput),
throwsA(isA<UnsupportedError>()),
);
} finally {
// Clean up
if (await tempFile.exists()) {
await tempFile.delete();
}
}
});

test('Test getSizeAsync with memory input via AsyncImageInput', () async {
final file = File('../../example/asset/ic_launcher.png');
final bytes = await file.readAsBytes();
final memoryInput = MemoryInput(bytes);
final asyncInput = AsyncImageInput.input(memoryInput);

final size = await ImageSizeGetter.getSizeAsync(asyncInput);
expect(size, Size(96, 96));
});

test('Test getSizeResultAsync with memory input via AsyncImageInput', () async {
final file = File('../../example/asset/ic_launcher.png');
final bytes = await file.readAsBytes();
final memoryInput = MemoryInput(bytes);
final asyncInput = AsyncImageInput.input(memoryInput);

final result = await ImageSizeGetter.getSizeResultAsync(asyncInput);
expect(result.size, Size(96, 96));
expect(result.decoder.decoderName, 'png');
});
});
}