formats.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package images
  3. import (
  4. "fmt"
  5. "image"
  6. "image/gif"
  7. "image/jpeg"
  8. "image/png"
  9. "io"
  10. "golang.org/x/image/bmp"
  11. "golang.org/x/image/tiff"
  12. _ "golang.org/x/image/webp"
  13. )
  14. var _ = fmt.Print
  15. var DecodableImageTypes = map[string]bool{
  16. "image/jpeg": true, "image/png": true, "image/bmp": true, "image/tiff": true, "image/webp": true, "image/gif": true,
  17. }
  18. var EncodableImageTypes = map[string]bool{
  19. "image/jpeg": true, "image/png": true, "image/bmp": true, "image/tiff": true, "image/gif": true,
  20. }
  21. func Encode(output io.Writer, img image.Image, format_mime string) (err error) {
  22. switch format_mime {
  23. case "image/png":
  24. return png.Encode(output, img)
  25. case "image/jpeg":
  26. return jpeg.Encode(output, img, nil)
  27. case "image/bmp":
  28. return bmp.Encode(output, img)
  29. case "image/gif":
  30. return gif.Encode(output, img, nil)
  31. case "image/tiff":
  32. return tiff.Encode(output, img, nil)
  33. }
  34. err = fmt.Errorf("Unsupported output image MIME type %s", format_mime)
  35. return
  36. }