Skip to content

Commit 8a34d61

Browse files
authored
refactoring code (#17)
- Add document - Version to 2.0.0 - Support bmp format - Support custom decoder - Remove AsyncImageSizeGetter
1 parent 2f8d5b6 commit 8a34d61

35 files changed

+1038
-576
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"*.m3u8": "m3u",
4+
"*.bt": "c"
5+
}
6+
}

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
# 元数据总结
22

33
- [元数据总结](#元数据总结)
4+
- [关于 Big Endian 和 Little Endian](#关于-big-endian-和-little-endian)
45
- [JPEG](#jpeg)
56
- [PNG](#png)
67
- [Webp](#webp)
78
- [Gif](#gif)
8-
- [关于 Big Endian 和 Little Endian](#关于-big-endian-和-little-endian)
9+
10+
## 关于 Big Endian 和 Little Endian
11+
12+
写在前面
13+
14+
计算机中储存数值有 2 种方式,一种是大端存储(Big Endian),一种是小端存储(Little Endian)。
15+
16+
以 uint32 数值的储存为例来举例:
17+
18+
所谓大端储存就是指存储的数值是从高位到低位的顺序,,就是数值占用 4 个字节,比如:
19+
20+
0x00 0x00 0x00 0x01 转为十进制,结果是 1
21+
22+
小端储存就不同了,同样的数,如果是小端储存,高位存储在低地址,低位存储在高地址,比如:
23+
24+
0x00 0x00 0x00 0x01 转为十进制,结果是 256^3。等于大端储存的 0x01 0x00 0x00 0x00
925

1026
## JPEG
1127

1228
因为历史原因,也叫 JPG
1329

14-
储存方式有如下的规则,其中一般使用开头和结尾的各 2 个字节来判断是否为 JPEG 格式,关于数字的 2 种储存方式查看 [关于 Big Endian 和 Little Endian](#关于-big-endian-和-little-endian)
30+
储存方式有如下的规则,其中一般使用开头和结尾的各 2 个字节来判断是否为 JPEG 格式
1531

1632
开头: 0xFF 0xD8
1733
结尾: 0xFF 0xD9
@@ -73,17 +89,3 @@ VP8 chunk: 找到 VP8 的标识 根据内容不同, 表示也不尽相同
7389
结尾: `0x3B`
7490

7591
从偏移量 `+6~+7` 两位标识宽度 `+8~+9`标识高度, 这里也是倒序, 示例中图片的宽度对应的十六进制为: `[0xB0,0x02] (688)`, 高度为`[0x2E,0x05](1326)`
76-
77-
## 关于 Big Endian 和 Little Endian
78-
79-
计算机中储存数值有 2 种方式,一种是大端存储,一种是小端存储。
80-
81-
以 uint32 数值的储存为例来举例:
82-
83-
所谓大端储存就是指存储的数值是从高位到低位的顺序,,就是数值占用 4 个字节,比如:
84-
85-
0x00 0x00 0x00 0x01 转为十进制,结果是 1
86-
87-
小端储存就不同了,同样的数,如果是小端储存,高位存储在低地址,低位存储在高地址,比如:
88-
89-
0x00 0x00 0x00 0x01 转为十进制,结果是 256^3。等于大端储存的 0x01 0x00 0x00 0x00

example/asset/demo.bmp

192 KB
Binary file not shown.

image_size_getter_http_input/pubspec.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ environment:
88
sdk: ">=2.16.2 <3.0.0"
99

1010
dependencies:
11-
# image_size_getter: ^1.1.0
1211
http: ^0.13.4
13-
# image_size_getter:
14-
# path: ../library
15-
image_size_getter: ^1.1.0
12+
image_size_getter:
13+
path: ../library
14+
# image_size_getter: ^1.1.0
1615
# path: ^1.8.0
1716

1817
dev_dependencies:

image_size_getter_http_input/test/image_size_getter_http_input_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Future<void> main() async {
4343
test('Test get size', () async {
4444
final width = 2554;
4545
final height = 824;
46-
final size = await AsyncImageSizeGetter.getSize(input);
46+
final size = await ImageSizeGetter.getSizeAsync(input);
4747

4848
expect(size.width, width);
4949
expect(size.height, height);

library/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# CHANGELOG
22

33
- [CHANGELOG](#changelog)
4+
- [2.0.0](#200)
45
- [1.1.0](#110)
56
- [What's Changed](#whats-changed)
67
- [New Contributors](#new-contributors)
@@ -11,12 +12,18 @@
1112
- [0.1.1](#011)
1213
- [0.1.0](#010)
1314

15+
## 2.0.0
16+
17+
New feature:
18+
19+
- Refactored the code and removed the `AsyncImageSizeGetter` class.
20+
- We can customize the decoder.
21+
1422
## 1.1.0
1523

1624
### What's Changed
1725

1826
- Support webp extended format by @nkming2 in <https://github.com/CaiJingLong/dart_image_size_getter/pull/11>
19-
2027
- Http input by @CaiJingLong in <https://github.com/CaiJingLong/dart_image_size_getter/pull/14>
2128

2229
### New Contributors

library/README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Do not completely decode the image file, just read the metadata to get the image width and height.
44

5-
Just support jpeg, gif, png, webp.
5+
Just support jpeg, gif, png, webp, bmp.
66

77
## Usage
88

@@ -43,6 +43,140 @@ void foo(Uint8List image){
4343
}
4444
```
4545

46+
## AsyncImageInput
47+
48+
If your data source is read asynchronously, consider using `AsyncImageInput`.
49+
50+
A typical use case is [http_input](https://pub.dev/packages/image_size_getter_http_input).
51+
52+
## Custom
53+
54+
We can implement our own input or decoder.
55+
56+
In addition to several built-in implementations, subsequent implementations will also be added to the project through plugin.
57+
58+
### Custom Input
59+
60+
Such as: [http_input](https://github.com/CaiJingLong/dart_image_size_getter/tree/master/image_size_getter_http_input).
61+
62+
In addition, if your picture has verification, for example, you need to use the request header to access it, or you need a post request to get it, you need to customize input.
63+
64+
### Custom Decoder
65+
66+
Such as bmp decoder
67+
68+
Check the file type:
69+
70+
![VPMMfA](https://cdn.jsdelivr.net/gh/kikt-blog/image@branch-2/uPic/VPMMfA.png)
71+
72+
The width and height:
73+
74+
![AZnx9I](https://cdn.jsdelivr.net/gh/kikt-blog/image@branch-2/uPic/AZnx9I.png)
75+
76+
So, we can write code with:
77+
78+
```dart
79+
import 'package:image_size_getter/image_size_getter.dart';
80+
81+
class BmpDecoder extends BaseDecoder {
82+
const BmpDecoder();
83+
84+
@override
85+
String get decoderName => 'bmp';
86+
87+
@override
88+
Size getSize(ImageInput input) {
89+
final widthList = input.getRange(0x12, 0x16);
90+
final heightList = input.getRange(0x16, 0x1a);
91+
92+
final width = convertRadix16ToInt(widthList, reverse: true);
93+
final height = convertRadix16ToInt(heightList, reverse: true);
94+
return Size(width, height);
95+
}
96+
97+
@override
98+
Future<Size> getSizeAsync(AsyncImageInput input) async {
99+
final widthList = await input.getRange(0x12, 0x16);
100+
final heightList = await input.getRange(0x16, 0x1a);
101+
102+
final width = convertRadix16ToInt(widthList, reverse: true);
103+
final height = convertRadix16ToInt(heightList, reverse: true);
104+
return Size(width, height);
105+
}
106+
107+
@override
108+
bool isValid(ImageInput input) {
109+
final list = input.getRange(0, 2);
110+
return _isBmp(list);
111+
}
112+
113+
@override
114+
Future<bool> isValidAsync(AsyncImageInput input) async {
115+
final list = await input.getRange(0, 2);
116+
return _isBmp(list);
117+
}
118+
119+
bool _isBmp(List<int> startList) {
120+
return startList[0] == 66 && startList[1] == 77;
121+
}
122+
}
123+
124+
```
125+
126+
Use it:
127+
128+
```dart
129+
final bmp = File('../example/asset/demo.bmp');
130+
131+
const BmpDecoder decoder = BmpDecoder();
132+
final input = FileInput(bmp);
133+
134+
assert(decoder.isValid(input));
135+
expect(decoder.getSize(input), Size(256, 256));
136+
```
137+
138+
#### Register custom decoder to image size getter
139+
140+
```dart
141+
ImageSizeGetter.registerDecoder(const BmpDecoder());
142+
```
143+
144+
The method can also be used to replace the default decoder.
145+
146+
For example, you think the existing JPEG format is not rigorous enough.
147+
148+
```dart
149+
ImageSizeGetter.registerDecoder(const MyJpegDecoder());
150+
```
151+
152+
#### Use decoder alone
153+
154+
Each decoder can be used alone.
155+
156+
```dart
157+
void decodeWithImageInput(ImageInput input) {
158+
BaseDecoder decoder = const GifDecoder();
159+
final isGif = decoder.isValid(input);
160+
print('isGif: $isGif');
161+
162+
if (isGif) {
163+
final size = decoder.getSize(input);
164+
print('size: $size');
165+
}
166+
}
167+
168+
void decodeWithAsyncImageInput(AsyncImageInput input) async {
169+
BaseDecoder decoder = const PngDecoder();
170+
final isPng = await decoder.isValidAsync(input);
171+
print('isPng: $isPng');
172+
173+
if (isPng) {
174+
final size = await decoder.getSizeAsync(input);
175+
print('size: $size');
176+
}
177+
}
178+
```
179+
46180
## migrate
47181

48182
See [migrate](https://github.com/CaiJingLong/dart_image_size_getter/blob/master/library/migrate.md)

library/lib/file_input.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ import 'dart:io';
33
import 'package:image_size_getter/src/core/input.dart';
44
import 'package:image_size_getter/src/utils/file_utils.dart';
55

6+
///
7+
/// {@template image_size_getter.file_input}
8+
///
9+
/// [ImageInput] using file as input source.
10+
///
11+
/// {@endtemplate}
12+
///
613
class FileInput extends ImageInput {
7-
final File file;
14+
/// {@macro image_size_getter.file_input}
15+
const FileInput(this.file);
816

9-
FileInput(this.file);
17+
final File file;
1018

1119
@override
1220
List<int> getRange(int start, int end) {

library/lib/image_size_getter.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
export 'src/core/size.dart';
1+
export 'src/entity/size.dart';
22
export 'src/core/memory_input.dart';
33
export 'src/image_size_getter.dart';
4+
export 'src/decoder/decoder.dart';
5+
export 'src/decoder/impl/gif_decoder.dart';
6+
export 'src/decoder/impl/jpeg_decoder.dart';
7+
export 'src/decoder/impl/png_decoder.dart';
8+
export 'src/decoder/impl/webp_decoder.dart';
9+
export 'src/decoder/impl/bmp_decoder.dart';

0 commit comments

Comments
 (0)