process.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. func initVips() {
  69. runtime.LockOSThread()
  70. defer runtime.UnlockOSThread()
  71. if err := C.vips_initialize(); err != 0 {
  72. C.vips_shutdown()
  73. log.Fatalln("unable to start vips!")
  74. }
  75. C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100Mb
  76. C.vips_cache_set_max(500)
  77. if len(os.Getenv("IMGPROXY_DEBUG_VIPS")) > 0 {
  78. C.vips_cache_set_dump(C.gboolean(1))
  79. C.vips_cache_set_trace(C.gboolean(1))
  80. }
  81. vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
  82. }
  83. func randomAccessRequired(po processingOptions) int {
  84. if po.gravity == SMART {
  85. return 1
  86. }
  87. return 0
  88. }
  89. func vipsTypeSupportedLoad(imgtype imageType) bool {
  90. switch imgtype {
  91. case JPEG:
  92. return int(C.vips_type_find_load_go(C.JPEG)) != 0
  93. case PNG:
  94. return int(C.vips_type_find_load_go(C.PNG)) != 0
  95. case WEBP:
  96. return int(C.vips_type_find_load_go(C.WEBP)) != 0
  97. case GIF:
  98. return int(C.vips_type_find_load_go(C.GIF)) != 0
  99. }
  100. return false
  101. }
  102. func vipsTypeSupportedSave(imgtype imageType) bool {
  103. switch imgtype {
  104. case JPEG:
  105. return int(C.vips_type_find_save_go(C.JPEG)) != 0
  106. case PNG:
  107. return int(C.vips_type_find_save_go(C.PNG)) != 0
  108. case WEBP:
  109. return int(C.vips_type_find_save_go(C.WEBP)) != 0
  110. }
  111. return false
  112. }
  113. func round(f float64) int {
  114. return int(f + .5)
  115. }
  116. func calcScale(width, height int, po processingOptions) float64 {
  117. if (po.width == width && po.height == height) || (po.resize != FILL && po.resize != FIT) {
  118. return 1
  119. }
  120. fsw, fsh, fow, foh := float64(width), float64(height), float64(po.width), float64(po.height)
  121. wr := fow / fsw
  122. hr := foh / fsh
  123. if po.resize == FIT {
  124. return math.Min(wr, hr)
  125. }
  126. return math.Max(wr, hr)
  127. }
  128. func calcCrop(width, height int, po processingOptions) (left, top int) {
  129. left = (width - po.width + 1) / 2
  130. top = (height - po.height + 1) / 2
  131. if po.gravity == NORTH {
  132. top = 0
  133. }
  134. if po.gravity == EAST {
  135. left = width - po.width
  136. }
  137. if po.gravity == SOUTH {
  138. top = height - po.height
  139. }
  140. if po.gravity == WEST {
  141. left = 0
  142. }
  143. return
  144. }
  145. func processImage(data []byte, imgtype imageType, po processingOptions) ([]byte, error) {
  146. defer keepAlive(data)
  147. if po.gravity == SMART && !vipsSupportSmartcrop {
  148. return nil, errors.New("Smart crop is not supported by used version of libvips")
  149. }
  150. err := C.int(0)
  151. var img *C.struct__VipsImage
  152. defer C.clear_image(&img)
  153. defer C.vips_cleanup()
  154. // Load the image
  155. switch imgtype {
  156. case JPEG:
  157. err = C.vips_jpegload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
  158. case PNG:
  159. err = C.vips_pngload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
  160. case GIF:
  161. err = C.vips_gifload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
  162. case WEBP:
  163. err = C.vips_webpload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
  164. }
  165. if err != 0 {
  166. return nil, vipsError()
  167. }
  168. imgWidth := int(img.Xsize)
  169. imgHeight := int(img.Ysize)
  170. // Ensure we won't crop out of bounds
  171. if !po.enlarge || po.resize == CROP {
  172. if imgWidth < po.width {
  173. po.width = imgWidth
  174. }
  175. if imgHeight < po.height {
  176. po.height = imgHeight
  177. }
  178. }
  179. if po.width != imgWidth || po.height != imgHeight {
  180. var (
  181. pResize, pCrop int
  182. pScale float64
  183. pSmart int
  184. pLeft, pTop, pWidth, pHeight int
  185. )
  186. if po.resize == FILL || po.resize == FIT {
  187. pResize = 1
  188. pScale = calcScale(imgWidth, imgHeight, po)
  189. } else {
  190. pScale = 1.0
  191. }
  192. if po.resize == FILL || po.resize == CROP {
  193. pCrop = 1
  194. pWidth, pHeight = po.width, po.height
  195. if po.gravity == SMART {
  196. pSmart = 1
  197. } else {
  198. pLeft, pTop = calcCrop(round(float64(imgWidth)*pScale), round(float64(imgHeight)*pScale), po)
  199. }
  200. }
  201. 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))
  202. if err != 0 {
  203. return nil, vipsError()
  204. }
  205. }
  206. // Finally, save
  207. var ptr unsafe.Pointer
  208. defer C.g_free(C.gpointer(ptr))
  209. imgsize := C.size_t(0)
  210. switch po.format {
  211. case JPEG:
  212. err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
  213. case PNG:
  214. err = C.vips_pngsave_go(img, &ptr, &imgsize)
  215. case WEBP:
  216. err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality))
  217. }
  218. if err != 0 {
  219. return nil, vipsError()
  220. }
  221. buf := C.GoBytes(ptr, C.int(imgsize))
  222. return buf, nil
  223. }
  224. func vipsError() error {
  225. return errors.New(C.GoString(C.vips_error_buffer()))
  226. }