Skip to content

Commit

Permalink
Merge pull request #127 from krlv/add_image_avif_support
Browse files Browse the repository at this point in the history
feat: Add image/avif support
  • Loading branch information
h2non committed Jan 23, 2023
2 parents 06ca62d + bcb25e2 commit cfcd7d0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func main() {
- **psd** - `image/vnd.adobe.photoshop`
- **ico** - `image/vnd.microsoft.icon`
- **dwg** - `image/vnd.dwg`
- **avif** - `image/avif`

#### Video

Expand Down
Binary file added fixtures/sample.avif
Binary file not shown.
1 change: 1 addition & 0 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestMatchFile(t *testing.T) {
{"dwg"},
{"zst"},
{"exr"},
{"avif"},
}

for _, test := range cases {
Expand Down
23 changes: 23 additions & 0 deletions matchers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
TypeHeif = newType("heif", "image/heif")
TypeDwg = newType("dwg", "image/vnd.dwg")
TypeExr = newType("exr", "image/x-exr")
TypeAvif = newType("avif", "image/avif")
)

var Image = Map{
Expand All @@ -34,6 +35,7 @@ var Image = Map{
TypeHeif: Heif,
TypeDwg: Dwg,
TypeExr: Exr,
TypeAvif: Avif,
}

func Jpeg(buf []byte) bool {
Expand Down Expand Up @@ -149,3 +151,24 @@ func Exr(buf []byte) bool {
buf[0] == 0x76 && buf[1] == 0x2f &&
buf[2] == 0x31 && buf[3] == 0x01
}

func Avif(buf []byte) bool {
if !isobmff.IsISOBMFF(buf) {
return false
}

majorBrand, _, compatibleBrands := isobmff.GetFtyp(buf)
if majorBrand == "avif" {
return true
}

if majorBrand == "mif1" || majorBrand == "msf1" {
for _, compatibleBrand := range compatibleBrands {
if compatibleBrand == "avif" {
return true
}
}
}

return false
}

0 comments on commit cfcd7d0

Please sign in to comment.