metadata.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package bimg
  2. /*
  3. #cgo pkg-config: vips
  4. #include "vips/vips.h"
  5. */
  6. import "C"
  7. // ImageSize represents the image width and height values
  8. type ImageSize struct {
  9. Width int
  10. Height int
  11. }
  12. // ImageMetadata represents the basic metadata fields
  13. type ImageMetadata struct {
  14. Orientation int
  15. Channels int
  16. Alpha bool
  17. Profile bool
  18. Type string
  19. Space string
  20. Colourspace string
  21. Size ImageSize
  22. }
  23. // Size returns the image size by width and height pixels.
  24. func Size(buf []byte) (ImageSize, error) {
  25. metadata, err := Metadata(buf)
  26. if err != nil {
  27. return ImageSize{}, err
  28. }
  29. return ImageSize{
  30. Width: int(metadata.Size.Width),
  31. Height: int(metadata.Size.Height),
  32. }, nil
  33. }
  34. // ColourspaceIsSupported checks if the image colourspace is supported by libvips.
  35. func ColourspaceIsSupported(buf []byte) (bool, error) {
  36. return vipsColourspaceIsSupportedBuffer(buf)
  37. }
  38. // ImageInterpretation returns the image interpretation type.
  39. // See: http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/VipsImage.html#VipsInterpretation
  40. func ImageInterpretation(buf []byte) (Interpretation, error) {
  41. return vipsInterpretationBuffer(buf)
  42. }
  43. // Metadata returns the image metadata (size, type, alpha channel, profile, EXIF orientation...).
  44. func Metadata(buf []byte) (ImageMetadata, error) {
  45. defer C.vips_thread_shutdown()
  46. image, imageType, err := vipsRead(buf)
  47. if err != nil {
  48. return ImageMetadata{}, err
  49. }
  50. defer C.g_object_unref(C.gpointer(image))
  51. size := ImageSize{
  52. Width: int(image.Xsize),
  53. Height: int(image.Ysize),
  54. }
  55. metadata := ImageMetadata{
  56. Size: size,
  57. Channels: int(image.Bands),
  58. Orientation: vipsExifOrientation(image),
  59. Alpha: vipsHasAlpha(image),
  60. Profile: vipsHasProfile(image),
  61. Space: vipsSpace(image),
  62. Type: ImageTypeName(imageType),
  63. }
  64. return metadata, nil
  65. }