process.go 5.2 KB

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