process.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. "context"
  10. "errors"
  11. "log"
  12. "math"
  13. "os"
  14. "runtime"
  15. "unsafe"
  16. )
  17. var (
  18. vipsSupportSmartcrop bool
  19. vipsTypeSupportLoad = make(map[imageType]bool)
  20. vipsTypeSupportSave = make(map[imageType]bool)
  21. errSmartCropNotSupported = errors.New("Smart crop is not supported by used version of libvips")
  22. )
  23. type cConfig struct {
  24. Quality C.int
  25. JpegProgressive C.int
  26. PngInterlaced C.int
  27. }
  28. var cConf cConfig
  29. func initVips() {
  30. runtime.LockOSThread()
  31. defer runtime.UnlockOSThread()
  32. if err := C.vips_initialize(); err != 0 {
  33. C.vips_shutdown()
  34. log.Fatalln("unable to start vips!")
  35. }
  36. // Disable libvips cache. Since processing pipeline is fine tuned, we won't get much profit from it.
  37. // Enabled cache can cause SIGSEGV on Musl-based systems like Alpine.
  38. C.vips_cache_set_max_mem(0)
  39. C.vips_cache_set_max(0)
  40. if len(os.Getenv("IMGPROXY_VIPS_LEAK_CHECK")) > 0 {
  41. C.vips_leak_set(C.gboolean(1))
  42. }
  43. if len(os.Getenv("IMGPROXY_VIPS_CACHE_TRACE")) > 0 {
  44. C.vips_cache_set_trace(C.gboolean(1))
  45. }
  46. vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
  47. if int(C.vips_type_find_load_go(imageTypeJPEG)) != 0 {
  48. vipsTypeSupportLoad[imageTypeJPEG] = true
  49. }
  50. if int(C.vips_type_find_load_go(imageTypePNG)) != 0 {
  51. vipsTypeSupportLoad[imageTypePNG] = true
  52. }
  53. if int(C.vips_type_find_load_go(imageTypeWEBP)) != 0 {
  54. vipsTypeSupportLoad[imageTypeWEBP] = true
  55. }
  56. if int(C.vips_type_find_load_go(imageTypeGIF)) != 0 {
  57. vipsTypeSupportLoad[imageTypeGIF] = true
  58. }
  59. if int(C.vips_type_find_save_go(imageTypeJPEG)) != 0 {
  60. vipsTypeSupportSave[imageTypeJPEG] = true
  61. }
  62. if int(C.vips_type_find_save_go(imageTypePNG)) != 0 {
  63. vipsTypeSupportSave[imageTypePNG] = true
  64. }
  65. if int(C.vips_type_find_save_go(imageTypeWEBP)) != 0 {
  66. vipsTypeSupportSave[imageTypeWEBP] = true
  67. }
  68. cConf.Quality = C.int(conf.Quality)
  69. if conf.JpegProgressive {
  70. cConf.JpegProgressive = C.int(1)
  71. }
  72. if conf.PngInterlaced {
  73. cConf.PngInterlaced = C.int(1)
  74. }
  75. }
  76. func shutdownVips() {
  77. C.vips_shutdown()
  78. }
  79. func extractMeta(img *C.VipsImage) (int, int, int, bool) {
  80. width := int(img.Xsize)
  81. height := int(img.Ysize)
  82. angle := C.VIPS_ANGLE_D0
  83. flip := false
  84. orientation := C.vips_get_exif_orientation(img)
  85. if orientation >= 5 && orientation <= 8 {
  86. width, height = height, width
  87. }
  88. if orientation == 3 || orientation == 4 {
  89. angle = C.VIPS_ANGLE_D180
  90. }
  91. if orientation == 5 || orientation == 6 {
  92. angle = C.VIPS_ANGLE_D90
  93. }
  94. if orientation == 7 || orientation == 8 {
  95. angle = C.VIPS_ANGLE_D270
  96. }
  97. if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
  98. flip = true
  99. }
  100. return width, height, angle, flip
  101. }
  102. func needToScale(width, height int, po *processingOptions) bool {
  103. return ((po.Width != 0 && po.Width != width) || (po.Height != 0 && po.Height != height)) &&
  104. (po.Resize == resizeFill || po.Resize == resizeFit)
  105. }
  106. func needToCrop(width, height int, po *processingOptions) bool {
  107. return (po.Width != width || po.Height != height) &&
  108. (po.Resize == resizeFill || po.Resize == resizeCrop)
  109. }
  110. func calcScale(width, height int, po *processingOptions) float64 {
  111. fsw, fsh, fow, foh := float64(width), float64(height), float64(po.Width), float64(po.Height)
  112. wr := fow / fsw
  113. hr := foh / fsh
  114. if po.Width == 0 {
  115. return hr
  116. }
  117. if po.Height == 0 {
  118. return wr
  119. }
  120. if po.Resize == resizeFit {
  121. return math.Min(wr, hr)
  122. }
  123. return math.Max(wr, hr)
  124. }
  125. func calcShink(scale float64, imgtype imageType) int {
  126. switch imgtype {
  127. case imageTypeWEBP:
  128. return int(1.0 / scale)
  129. case imageTypeJPEG:
  130. shrink := int(1.0 / scale)
  131. switch {
  132. case shrink >= 16:
  133. return 8
  134. case shrink >= 8:
  135. return 4
  136. case shrink >= 4:
  137. return 2
  138. }
  139. }
  140. return 1
  141. }
  142. func calcCrop(width, height int, po *processingOptions) (left, top int) {
  143. left = (width - po.Width + 1) / 2
  144. top = (height - po.Height + 1) / 2
  145. if po.Gravity.Type == gravityNorth {
  146. top = 0
  147. }
  148. if po.Gravity.Type == gravityEast {
  149. left = width - po.Width
  150. }
  151. if po.Gravity.Type == gravitySouth {
  152. top = height - po.Height
  153. }
  154. if po.Gravity.Type == gravityWest {
  155. left = 0
  156. }
  157. if po.Gravity.Type == gravityFocusPoint {
  158. pointX := int(float64(width) * po.Gravity.X)
  159. pointY := int(float64(height) * po.Gravity.Y)
  160. left = maxInt(0, minInt(pointX-po.Width/2, width-po.Width))
  161. top = maxInt(0, minInt(pointY-po.Height/2, height-po.Height))
  162. }
  163. return
  164. }
  165. func processImage(ctx context.Context) ([]byte, error) {
  166. defer C.vips_cleanup()
  167. data := getImageData(ctx).Bytes()
  168. po := getProcessingOptions(ctx)
  169. imgtype := getImageType(ctx)
  170. if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop {
  171. return nil, errSmartCropNotSupported
  172. }
  173. img, err := vipsLoadImage(data, imgtype, 1)
  174. if err != nil {
  175. return nil, err
  176. }
  177. defer C.clear_image(&img)
  178. checkTimeout(ctx)
  179. imgWidth, imgHeight, angle, flip := extractMeta(img)
  180. // Ensure we won't crop out of bounds
  181. if !po.Enlarge || po.Resize == resizeCrop {
  182. if imgWidth < po.Width {
  183. po.Width = imgWidth
  184. }
  185. if imgHeight < po.Height {
  186. po.Height = imgHeight
  187. }
  188. }
  189. hasAlpha := vipsImageHasAlpha(img)
  190. if needToScale(imgWidth, imgHeight, po) {
  191. scale := calcScale(imgWidth, imgHeight, po)
  192. // Do some shrink-on-load
  193. if scale < 1.0 {
  194. if shrink := calcShink(scale, imgtype); shrink != 1 {
  195. scale = scale * float64(shrink)
  196. if tmp, e := vipsLoadImage(data, imgtype, shrink); e == nil {
  197. C.swap_and_clear(&img, tmp)
  198. } else {
  199. return nil, e
  200. }
  201. }
  202. }
  203. premultiplied := false
  204. var bandFormat C.VipsBandFormat
  205. if hasAlpha {
  206. if bandFormat, err = vipsPremultiply(&img); err != nil {
  207. return nil, err
  208. }
  209. premultiplied = true
  210. }
  211. if err = vipsResize(&img, scale); err != nil {
  212. return nil, err
  213. }
  214. // Update actual image size after resize
  215. imgWidth, imgHeight, _, _ = extractMeta(img)
  216. if premultiplied {
  217. if err = vipsUnpremultiply(&img, bandFormat); err != nil {
  218. return nil, err
  219. }
  220. }
  221. }
  222. if err = vipsImportColourProfile(&img); err != nil {
  223. return nil, err
  224. }
  225. if err = vipsFixColourspace(&img); err != nil {
  226. return nil, err
  227. }
  228. checkTimeout(ctx)
  229. if angle != C.VIPS_ANGLE_D0 || flip {
  230. if err = vipsImageCopyMemory(&img); err != nil {
  231. return nil, err
  232. }
  233. if angle != C.VIPS_ANGLE_D0 {
  234. if err = vipsRotate(&img, angle); err != nil {
  235. return nil, err
  236. }
  237. }
  238. if flip {
  239. if err = vipsFlip(&img); err != nil {
  240. return nil, err
  241. }
  242. }
  243. }
  244. checkTimeout(ctx)
  245. if po.Width == 0 {
  246. po.Width = imgWidth
  247. }
  248. if po.Height == 0 {
  249. po.Height = imgHeight
  250. }
  251. if needToCrop(imgWidth, imgHeight, po) {
  252. if po.Gravity.Type == gravitySmart {
  253. if err = vipsImageCopyMemory(&img); err != nil {
  254. return nil, err
  255. }
  256. if err = vipsSmartCrop(&img, po.Width, po.Height); err != nil {
  257. return nil, err
  258. }
  259. // Applying additional modifications after smart crop causes SIGSEGV on Alpine
  260. // so we have to copy memory after it
  261. if err = vipsImageCopyMemory(&img); err != nil {
  262. return nil, err
  263. }
  264. } else {
  265. left, top := calcCrop(imgWidth, imgHeight, po)
  266. if err = vipsCrop(&img, left, top, po.Width, po.Height); err != nil {
  267. return nil, err
  268. }
  269. }
  270. checkTimeout(ctx)
  271. }
  272. if hasAlpha && po.Flatten {
  273. if err = vipsFlatten(&img, po.Background); err != nil {
  274. return nil, err
  275. }
  276. }
  277. if po.Blur > 0 {
  278. if err = vipsBlur(&img, po.Blur); err != nil {
  279. return nil, err
  280. }
  281. }
  282. if po.Sharpen > 0 {
  283. if err = vipsSharpen(&img, po.Sharpen); err != nil {
  284. return nil, err
  285. }
  286. }
  287. checkTimeout(ctx)
  288. return vipsSaveImage(img, po.Format)
  289. }
  290. func vipsLoadImage(data []byte, imgtype imageType, shrink int) (*C.struct__VipsImage, error) {
  291. var img *C.struct__VipsImage
  292. if C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), C.int(shrink), &img) != 0 {
  293. return nil, vipsError()
  294. }
  295. return img, nil
  296. }
  297. func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error) {
  298. var ptr unsafe.Pointer
  299. defer C.g_free_go(&ptr)
  300. err := C.int(0)
  301. imgsize := C.size_t(0)
  302. switch imgtype {
  303. case imageTypeJPEG:
  304. err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, cConf.Quality, cConf.JpegProgressive)
  305. case imageTypePNG:
  306. err = C.vips_pngsave_go(img, &ptr, &imgsize, cConf.PngInterlaced)
  307. case imageTypeWEBP:
  308. err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, cConf.Quality)
  309. }
  310. if err != 0 {
  311. return nil, vipsError()
  312. }
  313. return C.GoBytes(ptr, C.int(imgsize)), nil
  314. }
  315. func vipsImageHasAlpha(img *C.struct__VipsImage) bool {
  316. return C.vips_image_hasalpha_go(img) > 0
  317. }
  318. func vipsPremultiply(img **C.struct__VipsImage) (C.VipsBandFormat, error) {
  319. var tmp *C.struct__VipsImage
  320. format := C.vips_band_format(*img)
  321. if C.vips_premultiply_go(*img, &tmp) != 0 {
  322. return 0, vipsError()
  323. }
  324. C.swap_and_clear(img, tmp)
  325. return format, nil
  326. }
  327. func vipsUnpremultiply(img **C.struct__VipsImage, format C.VipsBandFormat) error {
  328. var tmp *C.struct__VipsImage
  329. if C.vips_unpremultiply_go(*img, &tmp) != 0 {
  330. return vipsError()
  331. }
  332. C.swap_and_clear(img, tmp)
  333. if C.vips_cast_go(*img, &tmp, format) != 0 {
  334. return vipsError()
  335. }
  336. C.swap_and_clear(img, tmp)
  337. return nil
  338. }
  339. func vipsResize(img **C.struct__VipsImage, scale float64) error {
  340. var tmp *C.struct__VipsImage
  341. if C.vips_resize_go(*img, &tmp, C.double(scale)) != 0 {
  342. return vipsError()
  343. }
  344. C.swap_and_clear(img, tmp)
  345. return nil
  346. }
  347. func vipsRotate(img **C.struct__VipsImage, angle int) error {
  348. var tmp *C.struct__VipsImage
  349. if C.vips_rot_go(*img, &tmp, C.VipsAngle(angle)) != 0 {
  350. return vipsError()
  351. }
  352. C.swap_and_clear(img, tmp)
  353. return nil
  354. }
  355. func vipsFlip(img **C.struct__VipsImage) error {
  356. var tmp *C.struct__VipsImage
  357. if C.vips_flip_horizontal_go(*img, &tmp) != 0 {
  358. return vipsError()
  359. }
  360. C.swap_and_clear(img, tmp)
  361. return nil
  362. }
  363. func vipsCrop(img **C.struct__VipsImage, left, top, width, height int) error {
  364. var tmp *C.struct__VipsImage
  365. if C.vips_extract_area_go(*img, &tmp, C.int(left), C.int(top), C.int(width), C.int(height)) != 0 {
  366. return vipsError()
  367. }
  368. C.swap_and_clear(img, tmp)
  369. return nil
  370. }
  371. func vipsSmartCrop(img **C.struct__VipsImage, width, height int) error {
  372. var tmp *C.struct__VipsImage
  373. if C.vips_smartcrop_go(*img, &tmp, C.int(width), C.int(height)) != 0 {
  374. return vipsError()
  375. }
  376. C.swap_and_clear(img, tmp)
  377. return nil
  378. }
  379. func vipsFlatten(img **C.struct__VipsImage, bg color) error {
  380. var tmp *C.struct__VipsImage
  381. if C.vips_flatten_go(*img, &tmp, C.double(bg.R), C.double(bg.G), C.double(bg.B)) != 0 {
  382. return vipsError()
  383. }
  384. C.swap_and_clear(img, tmp)
  385. return nil
  386. }
  387. func vipsBlur(img **C.struct__VipsImage, sigma float32) error {
  388. var tmp *C.struct__VipsImage
  389. if C.vips_gaussblur_go(*img, &tmp, C.double(sigma)) != 0 {
  390. return vipsError()
  391. }
  392. C.swap_and_clear(img, tmp)
  393. return nil
  394. }
  395. func vipsSharpen(img **C.struct__VipsImage, sigma float32) error {
  396. var tmp *C.struct__VipsImage
  397. if C.vips_sharpen_go(*img, &tmp, C.double(sigma)) != 0 {
  398. return vipsError()
  399. }
  400. C.swap_and_clear(img, tmp)
  401. return nil
  402. }
  403. func vipsImportColourProfile(img **C.struct__VipsImage) error {
  404. var tmp *C.struct__VipsImage
  405. if C.vips_need_icc_import(*img) > 0 {
  406. profile, err := cmykProfilePath()
  407. if err != nil {
  408. return err
  409. }
  410. if C.vips_icc_import_go(*img, &tmp, C.CString(profile)) != 0 {
  411. return vipsError()
  412. }
  413. C.swap_and_clear(img, tmp)
  414. }
  415. return nil
  416. }
  417. func vipsFixColourspace(img **C.struct__VipsImage) error {
  418. var tmp *C.struct__VipsImage
  419. if C.vips_image_guess_interpretation(*img) != C.VIPS_INTERPRETATION_sRGB {
  420. if C.vips_colourspace_go(*img, &tmp, C.VIPS_INTERPRETATION_sRGB) != 0 {
  421. return vipsError()
  422. }
  423. C.swap_and_clear(img, tmp)
  424. }
  425. return nil
  426. }
  427. func vipsImageCopyMemory(img **C.struct__VipsImage) error {
  428. var tmp *C.struct__VipsImage
  429. if tmp = C.vips_image_copy_memory(*img); tmp == nil {
  430. return vipsError()
  431. }
  432. C.swap_and_clear(img, tmp)
  433. return nil
  434. }
  435. func vipsError() error {
  436. return errors.New(C.GoString(C.vips_error_buffer()))
  437. }