forked from h2non/bimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
77 lines (65 loc) · 1.8 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package bimg
/*
#cgo pkg-config: vips
#include "vips/vips.h"
*/
import "C"
// ImageSize represents the image width and height values
type ImageSize struct {
Width int
Height int
}
// ImageMetadata represents the basic metadata fields
type ImageMetadata struct {
Orientation int
Channels int
Alpha bool
Profile bool
Type string
Space string
Colourspace string
Size ImageSize
}
// Size returns the image size by width and height pixels.
func Size(buf []byte) (ImageSize, error) {
metadata, err := Metadata(buf)
if err != nil {
return ImageSize{}, err
}
return ImageSize{
Width: int(metadata.Size.Width),
Height: int(metadata.Size.Height),
}, nil
}
// ColourspaceIsSupported checks if the image colourspace is supported by libvips.
func ColourspaceIsSupported(buf []byte) (bool, error) {
return vipsColourspaceIsSupportedBuffer(buf)
}
// ImageInterpretation returns the image interpretation type.
// See: https://jcupitt.github.io/libvips/API/current/VipsImage.html#VipsInterpretation
func ImageInterpretation(buf []byte) (Interpretation, error) {
return vipsInterpretationBuffer(buf)
}
// Metadata returns the image metadata (size, type, alpha channel, profile, EXIF orientation...).
func Metadata(buf []byte) (ImageMetadata, error) {
defer C.vips_thread_shutdown()
image, imageType, err := vipsRead(buf)
if err != nil {
return ImageMetadata{}, err
}
defer C.g_object_unref(C.gpointer(image))
size := ImageSize{
Width: int(image.Xsize),
Height: int(image.Ysize),
}
metadata := ImageMetadata{
Size: size,
Channels: int(image.Bands),
Orientation: vipsExifOrientation(image),
Alpha: vipsHasAlpha(image),
Profile: vipsHasProfile(image),
Space: vipsSpace(image),
Type: ImageTypeName(imageType),
}
return metadata, nil
}