Skip to content

Commit 1006136

Browse files
authored
Merge pull request #94 from rramphal/feat/djvu
feat: add support for DjVu image format
2 parents e95a5fb + aa9a9d1 commit 1006136

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
[![documentation](https://docs.rs/infer/badge.svg)](https://docs.rs/infer)
66

77
Small crate to infer file and MIME type by checking the
8-
[magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)) signature.
8+
[magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)) signature.
99

10-
Adaptation of [filetype](https://github.com/h2non/filetype) Go package ported to Rust.
10+
Adaptation of [filetype](https://github.com/h2non/filetype) Go package ported to Rust.
1111

12-
Does not require magic file database (i.e. `/etc/magic`).
12+
Does not require magic file database (i.e. `/etc/magic`).
1313

1414
## Features
1515

@@ -88,7 +88,7 @@ assert!(infer::is_image(&buf));
8888
```
8989

9090
### Adds a custom file type matcher
91-
91+
9292
```rust
9393
fn custom_matcher(buf: &[u8]) -> bool {
9494
return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
@@ -121,6 +121,7 @@ assert_eq!(kind.extension(), "foo");
121121
- **psd** - `image/vnd.adobe.photoshop`
122122
- **ico** - `image/vnd.microsoft.icon`
123123
- **ora** - `image/openraster`
124+
- **djvu** - `image/vnd.djvu`
124125

125126
#### Video
126127

src/map.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ matcher_map!(
209209
"ora",
210210
matchers::image::is_ora
211211
),
212+
(
213+
MatcherType::Image,
214+
"image/vnd.djvu",
215+
"djvu",
216+
matchers::image::is_djvu
217+
),
212218
// Video
213219
(
214220
MatcherType::Video,

src/matchers/image.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ pub fn is_ora(buf: &[u8]) -> bool {
194194
&& buf[53] == 0x72
195195
}
196196

197+
/// Returns whether a buffer is DjVu image data.
198+
pub fn is_djvu(buf: &[u8]) -> bool {
199+
buf.len() > 14
200+
&& buf[0] == 0x41
201+
&& buf[1] == 0x54
202+
&& buf[2] == 0x26
203+
&& buf[3] == 0x54
204+
&& buf[4] == 0x46
205+
&& buf[5] == 0x4F
206+
&& buf[6] == 0x52
207+
&& buf[7] == 0x4D
208+
&& buf[12] == 0x44
209+
&& buf[13] == 0x4A
210+
&& buf[14] == 0x56
211+
}
212+
197213
// GetFtyp returns the major brand, minor version and compatible brands of the ISO-BMFF data
198214
fn get_ftyp(buf: &[u8]) -> Option<(&[u8], &[u8], impl Iterator<Item = &[u8]>)> {
199215
if buf.len() < 16 {

testdata/sample_multi.djvu

1.13 KB
Binary file not shown.

testdata/sample_single.djvu

372 Bytes
Binary file not shown.

tests/image.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ test_format!(Image, "image/avif", "avif", avif, "sample.avif");
2929
test_format!(Image, "image/jxl", "jxl", jxl, "spline_on_first_frame.jxl");
3030

3131
test_format!(Image, "image/openraster", "ora", ora, "sample.ora");
32+
33+
test_format!(Image, "image/vnd.djvu", "djvu", djvu1, "sample_single.djvu");
34+
35+
test_format!(Image, "image/vnd.djvu", "djvu", djvu2, "sample_multi.djvu");

0 commit comments

Comments
 (0)