...

Source file src/github.com/gabriel-vasile/mimetype/internal/magic/image.go

Documentation: github.com/gabriel-vasile/mimetype/internal/magic

     1  package magic
     2  
     3  import "bytes"
     4  
     5  var (
     6  	// Png matches a Portable Network Graphics file.
     7  	// https://www.w3.org/TR/PNG/
     8  	Png = prefix([]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A})
     9  	// Apng matches an Animated Portable Network Graphics file.
    10  	// https://wiki.mozilla.org/APNG_Specification
    11  	Apng = offset([]byte("acTL"), 37)
    12  	// Jpg matches a Joint Photographic Experts Group file.
    13  	Jpg = prefix([]byte{0xFF, 0xD8, 0xFF})
    14  	// Jp2 matches a JPEG 2000 Image file (ISO 15444-1).
    15  	Jp2 = jpeg2k([]byte{0x6a, 0x70, 0x32, 0x20})
    16  	// Jpx matches a JPEG 2000 Image file (ISO 15444-2).
    17  	Jpx = jpeg2k([]byte{0x6a, 0x70, 0x78, 0x20})
    18  	// Jpm matches a JPEG 2000 Image file (ISO 15444-6).
    19  	Jpm = jpeg2k([]byte{0x6a, 0x70, 0x6D, 0x20})
    20  	// Gif matches a Graphics Interchange Format file.
    21  	Gif = prefix([]byte("GIF87a"), []byte("GIF89a"))
    22  	// Bmp matches a bitmap image file.
    23  	Bmp = prefix([]byte{0x42, 0x4D})
    24  	// Ps matches a PostScript file.
    25  	Ps = prefix([]byte("%!PS-Adobe-"))
    26  	// Psd matches a Photoshop Document file.
    27  	Psd = prefix([]byte("8BPS"))
    28  	// Ico matches an ICO file.
    29  	Ico = prefix([]byte{0x00, 0x00, 0x01, 0x00}, []byte{0x00, 0x00, 0x02, 0x00})
    30  	// Icns matches an ICNS (Apple Icon Image format) file.
    31  	Icns = prefix([]byte("icns"))
    32  	// Tiff matches a Tagged Image File Format file.
    33  	Tiff = prefix([]byte{0x49, 0x49, 0x2A, 0x00}, []byte{0x4D, 0x4D, 0x00, 0x2A})
    34  	// Bpg matches a Better Portable Graphics file.
    35  	Bpg = prefix([]byte{0x42, 0x50, 0x47, 0xFB})
    36  	// Xcf matches GIMP image data.
    37  	Xcf = prefix([]byte("gimp xcf"))
    38  	// Pat matches GIMP pattern data.
    39  	Pat = offset([]byte("GPAT"), 20)
    40  	// Gbr matches GIMP brush data.
    41  	Gbr = offset([]byte("GIMP"), 20)
    42  	// Hdr matches Radiance HDR image.
    43  	// https://web.archive.org/web/20060913152809/http://local.wasp.uwa.edu.au/~pbourke/dataformats/pic/
    44  	Hdr = prefix([]byte("#?RADIANCE\n"))
    45  	// Xpm matches X PixMap image data.
    46  	Xpm = prefix([]byte{0x2F, 0x2A, 0x20, 0x58, 0x50, 0x4D, 0x20, 0x2A, 0x2F})
    47  	// Jxs matches a JPEG XS coded image file (ISO/IEC 21122-3).
    48  	Jxs = prefix([]byte{0x00, 0x00, 0x00, 0x0C, 0x4A, 0x58, 0x53, 0x20, 0x0D, 0x0A, 0x87, 0x0A})
    49  	// Jxr matches Microsoft HD JXR photo file.
    50  	Jxr = prefix([]byte{0x49, 0x49, 0xBC, 0x01})
    51  )
    52  
    53  func jpeg2k(sig []byte) Detector {
    54  	return func(raw []byte, _ uint32) bool {
    55  		if len(raw) < 24 {
    56  			return false
    57  		}
    58  
    59  		if !bytes.Equal(raw[4:8], []byte{0x6A, 0x50, 0x20, 0x20}) &&
    60  			!bytes.Equal(raw[4:8], []byte{0x6A, 0x50, 0x32, 0x20}) {
    61  			return false
    62  		}
    63  		return bytes.Equal(raw[20:24], sig)
    64  	}
    65  }
    66  
    67  // Webp matches a WebP file.
    68  func Webp(raw []byte, _ uint32) bool {
    69  	return len(raw) > 12 &&
    70  		bytes.Equal(raw[0:4], []byte("RIFF")) &&
    71  		bytes.Equal(raw[8:12], []byte{0x57, 0x45, 0x42, 0x50})
    72  }
    73  
    74  // Dwg matches a CAD drawing file.
    75  func Dwg(raw []byte, _ uint32) bool {
    76  	if len(raw) < 6 || raw[0] != 0x41 || raw[1] != 0x43 {
    77  		return false
    78  	}
    79  	dwgVersions := [][]byte{
    80  		{0x31, 0x2E, 0x34, 0x30},
    81  		{0x31, 0x2E, 0x35, 0x30},
    82  		{0x32, 0x2E, 0x31, 0x30},
    83  		{0x31, 0x30, 0x30, 0x32},
    84  		{0x31, 0x30, 0x30, 0x33},
    85  		{0x31, 0x30, 0x30, 0x34},
    86  		{0x31, 0x30, 0x30, 0x36},
    87  		{0x31, 0x30, 0x30, 0x39},
    88  		{0x31, 0x30, 0x31, 0x32},
    89  		{0x31, 0x30, 0x31, 0x34},
    90  		{0x31, 0x30, 0x31, 0x35},
    91  		{0x31, 0x30, 0x31, 0x38},
    92  		{0x31, 0x30, 0x32, 0x31},
    93  		{0x31, 0x30, 0x32, 0x34},
    94  		{0x31, 0x30, 0x33, 0x32},
    95  	}
    96  
    97  	for _, d := range dwgVersions {
    98  		if bytes.Equal(raw[2:6], d) {
    99  			return true
   100  		}
   101  	}
   102  
   103  	return false
   104  }
   105  
   106  // Jxl matches JPEG XL image file.
   107  func Jxl(raw []byte, _ uint32) bool {
   108  	return bytes.HasPrefix(raw, []byte{0xFF, 0x0A}) ||
   109  		bytes.HasPrefix(raw, []byte("\x00\x00\x00\x0cJXL\x20\x0d\x0a\x87\x0a"))
   110  }
   111  

View as plain text