Skip to content

Commit

Permalink
Merge branch 'ibraimgm-jpg2000'
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Oct 6, 2019
2 parents 84e752a + b5b85da commit d39c541
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
26 changes: 26 additions & 0 deletions internal/matchers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ func Jpg(in []byte) bool {
return bytes.HasPrefix(in, []byte{0xFF, 0xD8, 0xFF})
}

// isJpeg2k matches a generic JPEG2000 file.
func isJpeg2k(in []byte) bool {
if len(in) < 24 {
return false
}

signature := in[4:8]
return bytes.Equal(signature, []byte{0x6A, 0x50, 0x20, 0x20}) ||
bytes.Equal(signature, []byte{0x6A, 0x50, 0x32, 0x20})
}

// Jp2 matches a JPEG 2000 Image file (ISO 15444-1)
func Jp2(in []byte) bool {
return isJpeg2k(in) && bytes.Equal(in[20:24], []byte{0x6a, 0x70, 0x32, 0x20})
}

// Jpx matches a JPEG 2000 Image file (ISO 15444-2)
func Jpx(in []byte) bool {
return isJpeg2k(in) && bytes.Equal(in[20:24], []byte{0x6a, 0x70, 0x78, 0x20})
}

// Jpm matches a JPEG 2000 Image file (ISO 15444-6)
func Jpm(in []byte) bool {
return isJpeg2k(in) && bytes.Equal(in[20:24], []byte{0x6a, 0x70, 0x6D, 0x20})
}

// Gif matches a Graphics Interchange Format file.
func Gif(in []byte) bool {
return bytes.HasPrefix(in, []byte("GIF87a")) ||
Expand Down
3 changes: 3 additions & 0 deletions mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ var files = map[string]*node{
// images
"png.png": png,
"jpg.jpg": jpg,
"jp2.jp2": jp2,
"jpf.jpf": jpx,
"jpm.jpm": jpm,
"psd.psd": psd,
"webp.webp": webp,
"tif.tif": tiff,
Expand Down
5 changes: 4 additions & 1 deletion supported_mimes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 119 Supported MIME types
## 122 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type
Expand Down Expand Up @@ -31,6 +31,9 @@ Extension | MIME type
**ogv** | video/ogg
**png** | image/png
**jpg** | image/jpeg
**jp2** | image/jp2
**jpf** | image/jpx
**jpm** | image/jpm
**gif** | image/gif
**webp** | image/webp
**exe** | application/vnd.microsoft.portable-executable
Expand Down
Binary file added testdata/jp2.jp2
Binary file not shown.
Binary file added testdata/jpf.jpf
Binary file not shown.
Binary file added testdata/jpm.jpm
Binary file not shown.
5 changes: 4 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "github.com/gabriel-vasile/mimetype/internal/matchers"
// When a matcher passes the check, the children matchers
// are tried in order to find a more accurate mime type.
var root = newNode("application/octet-stream", "", matchers.True,
sevenZ, zip, pdf, xls, ppt, doc, ps, psd, ogg, png, jpg, gif, webp, exe, elf,
sevenZ, zip, pdf, xls, ppt, doc, ps, psd, ogg, png, jpg, jp2, jpx, jpm, gif, webp, exe, elf,
ar, tar, xar, bz2, fits, tiff, bmp, ico, mp3, flac, midi, ape, musePack, amr,
wav, aiff, au, mpeg, quickTime, mqv, mp4, webM, threeGP, threeG2, avi, flv,
mkv, asf, aac, voc, aMp4, m4a, txt, gzip, class, swf, crx, woff, woff2, otf,
Expand Down Expand Up @@ -67,6 +67,9 @@ var (
threemf = newNode("application/vnd.ms-package.3dmanufacturing-3dmodel+xml", "3mf", matchers.Threemf)
png = newNode("image/png", "png", matchers.Png)
jpg = newNode("image/jpeg", "jpg", matchers.Jpg)
jp2 = newNode("image/jp2", "jp2", matchers.Jp2)
jpx = newNode("image/jpx", "jpf", matchers.Jpx)
jpm = newNode("image/jpm", "jpm", matchers.Jpm)
bpg = newNode("image/bpg", "bpg", matchers.Bpg)
gif = newNode("image/gif", "gif", matchers.Gif)
webp = newNode("image/webp", "webp", matchers.Webp)
Expand Down

0 comments on commit d39c541

Please sign in to comment.