Skip to content

Commit

Permalink
Merge pull request #94 from rramphal/feat/djvu
Browse files Browse the repository at this point in the history
feat: add support for DjVu image format
  • Loading branch information
bojand authored May 31, 2024
2 parents e95a5fb + aa9a9d1 commit 1006136
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
[![documentation](https://docs.rs/infer/badge.svg)](https://docs.rs/infer)

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

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

Does not require magic file database (i.e. `/etc/magic`).
Does not require magic file database (i.e. `/etc/magic`).

## Features

Expand Down Expand Up @@ -88,7 +88,7 @@ assert!(infer::is_image(&buf));
```

### Adds a custom file type matcher

```rust
fn custom_matcher(buf: &[u8]) -> bool {
return buf.len() >= 3 && buf[0] == 0x10 && buf[1] == 0x11 && buf[2] == 0x12;
Expand Down Expand Up @@ -121,6 +121,7 @@ assert_eq!(kind.extension(), "foo");
- **psd** - `image/vnd.adobe.photoshop`
- **ico** - `image/vnd.microsoft.icon`
- **ora** - `image/openraster`
- **djvu** - `image/vnd.djvu`

#### Video

Expand Down
6 changes: 6 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ matcher_map!(
"ora",
matchers::image::is_ora
),
(
MatcherType::Image,
"image/vnd.djvu",
"djvu",
matchers::image::is_djvu
),
// Video
(
MatcherType::Video,
Expand Down
16 changes: 16 additions & 0 deletions src/matchers/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ pub fn is_ora(buf: &[u8]) -> bool {
&& buf[53] == 0x72
}

/// Returns whether a buffer is DjVu image data.
pub fn is_djvu(buf: &[u8]) -> bool {
buf.len() > 14
&& buf[0] == 0x41
&& buf[1] == 0x54
&& buf[2] == 0x26
&& buf[3] == 0x54
&& buf[4] == 0x46
&& buf[5] == 0x4F
&& buf[6] == 0x52
&& buf[7] == 0x4D
&& buf[12] == 0x44
&& buf[13] == 0x4A
&& buf[14] == 0x56
}

// GetFtyp returns the major brand, minor version and compatible brands of the ISO-BMFF data
fn get_ftyp(buf: &[u8]) -> Option<(&[u8], &[u8], impl Iterator<Item = &[u8]>)> {
if buf.len() < 16 {
Expand Down
Binary file added testdata/sample_multi.djvu
Binary file not shown.
Binary file added testdata/sample_single.djvu
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ test_format!(Image, "image/avif", "avif", avif, "sample.avif");
test_format!(Image, "image/jxl", "jxl", jxl, "spline_on_first_frame.jxl");

test_format!(Image, "image/openraster", "ora", ora, "sample.ora");

test_format!(Image, "image/vnd.djvu", "djvu", djvu1, "sample_single.djvu");

test_format!(Image, "image/vnd.djvu", "djvu", djvu2, "sample_multi.djvu");

0 comments on commit 1006136

Please sign in to comment.