process.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package main
  2. /*
  3. #cgo pkg-config: vips
  4. #cgo LDFLAGS: -s -w
  5. #include "vips.h"
  6. */
  7. import "C"
  8. import (
  9. "errors"
  10. "log"
  11. "math"
  12. "runtime"
  13. "unsafe"
  14. )
  15. type imageType int
  16. const (
  17. UNKNOWN imageType = iota
  18. JPEG
  19. PNG
  20. WEBP
  21. GIF
  22. )
  23. var imageTypes = map[string]imageType{
  24. "jpeg": JPEG,
  25. "jpg": JPEG,
  26. "png": PNG,
  27. "webp": WEBP,
  28. "gif": GIF,
  29. }
  30. type gravityType int
  31. const (
  32. CENTER gravityType = iota
  33. NORTH
  34. EAST
  35. SOUTH
  36. WEST
  37. SMART
  38. )
  39. var gravityTypes = map[string]gravityType{
  40. "ce": CENTER,
  41. "no": NORTH,
  42. "ea": EAST,
  43. "so": SOUTH,
  44. "we": WEST,
  45. "sm": SMART,
  46. }
  47. type resizeType int
  48. const (
  49. FIT resizeType = iota
  50. FILL
  51. CROP
  52. )
  53. var resizeTypes = map[string]resizeType{
  54. "fit": FIT,
  55. "fill": FILL,
  56. "crop": CROP,
  57. }
  58. type processingOptions struct {
  59. resize resizeType
  60. width int
  61. height int
  62. gravity gravityType
  63. enlarge bool
  64. format imageType
  65. }
  66. var vipsSupportSmartcrop bool
  67. func initVips() {
  68. runtime.LockOSThread()
  69. defer runtime.UnlockOSThread()
  70. if err := C.vips_initialize(); err != 0 {
  71. C.vips_shutdown()
  72. log.Fatalln("unable to start vips!")
  73. }
  74. C.vips_concurrency_set(1)
  75. C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100Mb
  76. C.vips_cache_set_max(500)
  77. vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
  78. }
  79. func vipsTypeSupportedLoad(imgtype imageType) bool {
  80. switch imgtype {
  81. case JPEG:
  82. return int(C.vips_type_find_load_go(C.JPEG)) != 0
  83. case PNG:
  84. return int(C.vips_type_find_load_go(C.PNG)) != 0
  85. case WEBP:
  86. return int(C.vips_type_find_load_go(C.WEBP)) != 0
  87. case GIF:
  88. return int(C.vips_type_find_load_go(C.GIF)) != 0
  89. }
  90. return false
  91. }
  92. func vipsTypeSupportedSave(imgtype imageType) bool {
  93. switch imgtype {
  94. case JPEG:
  95. return int(C.vips_type_find_save_go(C.JPEG)) != 0
  96. case PNG:
  97. return int(C.vips_type_find_save_go(C.PNG)) != 0
  98. case WEBP:
  99. return int(C.vips_type_find_save_go(C.WEBP)) != 0
  100. }
  101. return false
  102. }
  103. func round(f float64) int {
  104. return int(f + .5)
  105. }
  106. func calcScale(width, height int, po processingOptions) float64 {
  107. if (po.width == width && po.height == height) || (po.resize != FILL && po.resize != FIT) {
  108. return 1
  109. }
  110. fsw, fsh, fow, foh := float64(width), float64(height), float64(po.width), float64(po.height)
  111. wr := fow / fsw
  112. hr := foh / fsh
  113. if po.resize == FIT {
  114. return math.Min(wr, hr)
  115. }
  116. return math.Max(wr, hr)
  117. }
  118. func calcCrop(width, height int, po processingOptions) (left, top int) {
  119. left = (width - po.width + 1) / 2
  120. top = (height - po.height + 1) / 2
  121. if po.gravity == NORTH {
  122. top = 0
  123. }
  124. if po.gravity == EAST {
  125. left = width - po.width
  126. }
  127. if po.gravity == SOUTH {
  128. top = height - po.height
  129. }
  130. if po.gravity == WEST {
  131. left = 0
  132. }
  133. return
  134. }
  135. func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte, error) {
  136. defer keepAlive(data)
  137. err := C.int(0)
  138. var img *C.struct__VipsImage
  139. defer C.vips_cleanup()
  140. // Load the image
  141. switch imgtype {
  142. case JPEG:
  143. err = C.vips_jpegload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  144. case PNG:
  145. err = C.vips_pngload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  146. case GIF:
  147. err = C.vips_gifload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  148. case WEBP:
  149. err = C.vips_webpload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img)
  150. }
  151. if err != 0 {
  152. return nil, vipsError()
  153. }
  154. imgWidth := int(img.Xsize)
  155. imgHeight := int(img.Ysize)
  156. // Ensure we won't crop out of bounds
  157. if !po.enlarge || po.resize == CROP {
  158. if imgWidth < po.width {
  159. po.width = imgWidth
  160. }
  161. if imgHeight < po.height {
  162. po.height = imgHeight
  163. }
  164. }
  165. if po.width != imgWidth || po.height != imgHeight {
  166. var (
  167. pResize, pCrop int
  168. pScale float64
  169. pSmart int
  170. pLeft, pTop, pWidth, pHeight int
  171. )
  172. if po.resize == FILL || po.resize == FIT {
  173. pResize = 1
  174. pScale = calcScale(imgWidth, imgHeight, po)
  175. } else {
  176. pScale = 1.0
  177. }
  178. if po.resize == FILL || po.resize == CROP {
  179. pCrop = 1
  180. pWidth, pHeight = po.width, po.height
  181. if po.gravity == SMART && vipsSupportSmartcrop {
  182. pSmart = 1
  183. } else {
  184. pLeft, pTop = calcCrop(round(float64(imgWidth)*pScale), round(float64(imgHeight)*pScale), po)
  185. }
  186. }
  187. err = C.vips_process_image(&img, C.int(pResize), C.double(pScale), C.int(pCrop), C.int(pSmart), C.int(pLeft), C.int(pTop), C.int(pWidth), C.int(pHeight))
  188. if err != 0 {
  189. return nil, vipsError()
  190. }
  191. }
  192. // Finally, save
  193. imgsize := C.size_t(0)
  194. var ptr unsafe.Pointer
  195. switch po.format {
  196. case JPEG:
  197. err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
  198. case PNG:
  199. err = C.vips_pngsave_go(img, &ptr, &imgsize)
  200. case WEBP:
  201. err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality))
  202. }
  203. if err != 0 {
  204. return nil, vipsError()
  205. }
  206. C.g_object_unref(C.gpointer(img))
  207. buf := C.GoBytes(ptr, C.int(imgsize))
  208. C.g_free(C.gpointer(ptr))
  209. return buf, nil
  210. }
  211. func vipsError() error {
  212. return errors.New(C.GoString(C.vips_error_buffer()))
  213. }