forked from brendan-duncan/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpeg_test.dart
63 lines (54 loc) · 2.09 KB
/
jpeg_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import 'dart:io';
import 'package:image/image.dart';
import 'package:test/test.dart';
void main() {
var dir = Directory('test/res/jpg');
var files = dir.listSync(recursive: true);
group('JPEG', () {
for (var f in files) {
if (f is! File || !f.path.endsWith('.jpg')) {
continue;
}
String name = f.path.split(RegExp(r'(/|\\)')).last;
test('$name', () {
List<int> bytes = (f as File).readAsBytesSync();
expect(JpegDecoder().isValidFile(bytes), equals(true));
Image image = JpegDecoder().decodeImage(bytes);
if (image == null) {
throw ImageException('Unable to decode JPEG Image: $name.');
}
List<int> outJpg = JpegEncoder().encodeImage(image);
File('out/jpg/${name}')
..createSync(recursive: true)
..writeAsBytesSync(outJpg);
// Make sure we can read what we just wrote.
Image image2 = JpegDecoder().decodeImage(outJpg);
if (image2 == null) {
throw ImageException('Unable to re-decode JPEG Image: $name.');
}
expect(image.width, equals(image2.width));
expect(image.height, equals(image2.height));
});
}
for (int i = 1; i < 9; ++i) {
test('exif/orientation_$i/landscape', () {
Image image = JpegDecoder().decodeImage(
File('test/res/jpg/landscape_$i.jpg').readAsBytesSync());
expect(image.exif.hasOrientation, equals(true));
expect(image.exif.orientation, equals(i));
File('out/jpg/landscape_$i.png')
..createSync(recursive: true)
..writeAsBytesSync(PngEncoder().encodeImage(bakeOrientation(image)));
});
test('exif/orientation_$i/portrait', () {
Image image = JpegDecoder().decodeImage(
File('test/res/jpg/portrait_$i.jpg').readAsBytesSync());
expect(image.exif.hasOrientation, equals(true));
expect(image.exif.orientation, equals(i));
File('out/jpg/portrait_$i.png')
..createSync(recursive: true)
..writeAsBytesSync(PngEncoder().encodeImage(bakeOrientation(image)));
});
}
});
}