process.go 5.5 KB

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