webp_preset.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package vips
  2. /*
  3. #include "vips.h"
  4. */
  5. import "C"
  6. import (
  7. "log/slog"
  8. "strconv"
  9. )
  10. // WebpPreset represents WebP preset to use when saving WebP images
  11. type WebpPreset C.VipsForeignWebpPreset
  12. const (
  13. WebpPresetDefault = C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT
  14. WebpPresetPhoto = C.VIPS_FOREIGN_WEBP_PRESET_PHOTO
  15. WebpPresetPicture = C.VIPS_FOREIGN_WEBP_PRESET_PICTURE
  16. WebpPresetDrawing = C.VIPS_FOREIGN_WEBP_PRESET_DRAWING
  17. WebpPresetIcon = C.VIPS_FOREIGN_WEBP_PRESET_ICON
  18. WebpPresetText = C.VIPS_FOREIGN_WEBP_PRESET_TEXT
  19. )
  20. // WebpPresets maps string representations to WebpPreset values
  21. var WebpPresets = map[string]WebpPreset{
  22. "default": WebpPresetDefault,
  23. "photo": WebpPresetPhoto,
  24. "picture": WebpPresetPicture,
  25. "drawing": WebpPresetDrawing,
  26. "icon": WebpPresetIcon,
  27. "text": WebpPresetText,
  28. }
  29. // C converts WebpPreset to C.VipsForeignWebpPreset
  30. func (wp WebpPreset) C() C.VipsForeignWebpPreset {
  31. return C.VipsForeignWebpPreset(wp)
  32. }
  33. // String returns the string representation of the WebpPreset
  34. func (wp WebpPreset) String() string {
  35. for k, v := range WebpPresets {
  36. if v == wp {
  37. return k
  38. }
  39. }
  40. return "unknown"
  41. }
  42. // MarshalJSON implements the json.Marshaler interface
  43. func (wp WebpPreset) MarshalJSON() ([]byte, error) {
  44. return strconv.AppendQuote(nil, wp.String()), nil
  45. }
  46. // LogValue implements the slog.LogValuer interface
  47. func (wp WebpPreset) LogValue() slog.Value {
  48. return slog.StringValue(wp.String())
  49. }