process.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 = C.UNKNOWN
  19. JPEG = C.JPEG
  20. PNG = C.PNG
  21. WEBP = C.WEBP
  22. GIF = C.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 extractMeta(img *C.VipsImage) (int, int, int, int) {
  118. width := int(img.Xsize)
  119. height := int(img.Ysize)
  120. angle := C.VIPS_ANGLE_D0
  121. flip := C.FALSE
  122. orientation := C.vips_get_exif_orientation(img)
  123. if orientation >= 5 && orientation <= 8 {
  124. width, height = height, width
  125. }
  126. if orientation == 3 || orientation == 4 {
  127. angle = C.VIPS_ANGLE_D180
  128. }
  129. if orientation == 5 || orientation == 6 {
  130. angle = C.VIPS_ANGLE_D90
  131. }
  132. if orientation == 7 || orientation == 8 {
  133. angle = C.VIPS_ANGLE_D270
  134. }
  135. if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
  136. flip = C.TRUE
  137. }
  138. return width, height, angle, flip
  139. }
  140. func calcScale(width, height int, po processingOptions) float64 {
  141. if (po.width == width && po.height == height) || (po.resize != FILL && po.resize != FIT) {
  142. return 1
  143. }
  144. fsw, fsh, fow, foh := float64(width), float64(height), float64(po.width), float64(po.height)
  145. wr := fow / fsw
  146. hr := foh / fsh
  147. if po.resize == FIT {
  148. return math.Min(wr, hr)
  149. }
  150. return math.Max(wr, hr)
  151. }
  152. func calcCrop(width, height int, po processingOptions) (left, top int) {
  153. left = (width - po.width + 1) / 2
  154. top = (height - po.height + 1) / 2
  155. if po.gravity == NORTH {
  156. top = 0
  157. }
  158. if po.gravity == EAST {
  159. left = width - po.width
  160. }
  161. if po.gravity == SOUTH {
  162. top = height - po.height
  163. }
  164. if po.gravity == WEST {
  165. left = 0
  166. }
  167. return
  168. }
  169. func processImage(data []byte, imgtype imageType, po processingOptions, t *timer) ([]byte, error) {
  170. defer keepAlive(data)
  171. if po.gravity == SMART && !vipsSupportSmartcrop {
  172. return nil, errors.New("Smart crop is not supported by used version of libvips")
  173. }
  174. err := C.int(0)
  175. var img *C.struct__VipsImage
  176. defer C.clear_image(&img)
  177. defer C.vips_cleanup()
  178. // Load the image
  179. err = C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), &img)
  180. if err != 0 {
  181. return nil, vipsError()
  182. }
  183. t.Check()
  184. imgWidth, imgHeight, angle, flip := extractMeta(img)
  185. // Ensure we won't crop out of bounds
  186. if !po.enlarge || po.resize == CROP {
  187. if imgWidth < po.width {
  188. po.width = imgWidth
  189. }
  190. if imgHeight < po.height {
  191. po.height = imgHeight
  192. }
  193. }
  194. if po.width != imgWidth || po.height != imgHeight {
  195. pResize, pCrop, pSmart := C.FALSE, C.FALSE, C.FALSE
  196. var (
  197. pScale float64
  198. pLeft, pTop, pWidth, pHeight int
  199. )
  200. if po.resize == FILL || po.resize == FIT {
  201. pResize = 1
  202. pScale = calcScale(imgWidth, imgHeight, po)
  203. } else {
  204. pScale = 1.0
  205. }
  206. if po.resize == FILL || po.resize == CROP {
  207. pCrop = 1
  208. pWidth, pHeight = po.width, po.height
  209. if po.gravity == SMART {
  210. pSmart = 1
  211. } else {
  212. pLeft, pTop = calcCrop(round(float64(imgWidth)*pScale), round(float64(imgHeight)*pScale), po)
  213. }
  214. }
  215. err = C.vips_process_image(&img, C.gboolean(pResize), C.double(pScale), C.gboolean(pCrop), C.gboolean(pSmart), C.int(pLeft), C.int(pTop), C.int(pWidth), C.int(pHeight), C.VipsAngle(angle), C.gboolean(flip))
  216. if err != 0 {
  217. return nil, vipsError()
  218. }
  219. }
  220. t.Check()
  221. // Finally, save
  222. var ptr unsafe.Pointer
  223. defer C.g_free_go(&ptr)
  224. imgsize := C.size_t(0)
  225. switch po.format {
  226. case JPEG:
  227. err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
  228. case PNG:
  229. err = C.vips_pngsave_go(img, &ptr, &imgsize)
  230. case WEBP:
  231. err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality))
  232. }
  233. if err != 0 {
  234. return nil, vipsError()
  235. }
  236. t.Check()
  237. buf := C.GoBytes(ptr, C.int(imgsize))
  238. return buf, nil
  239. }
  240. func vipsError() error {
  241. return errors.New(C.GoString(C.vips_error_buffer()))
  242. }