resize.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. package imaging
  2. import (
  3. "image"
  4. "math"
  5. )
  6. // Resize resizes the image to the specified width and height using the specified resampling
  7. // filter and returns the transformed image. If one of width or height is 0, the image aspect
  8. // ratio is preserved.
  9. //
  10. // Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
  11. // CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
  12. //
  13. // Usage example:
  14. //
  15. // dstImage := imaging.Resize(srcImage, 800, 600, imaging.Lanczos)
  16. //
  17. func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA {
  18. dstW, dstH := width, height
  19. if dstW < 0 || dstH < 0 {
  20. return &image.NRGBA{}
  21. }
  22. if dstW == 0 && dstH == 0 {
  23. return &image.NRGBA{}
  24. }
  25. src := toNRGBA(img)
  26. srcW := src.Bounds().Max.X
  27. srcH := src.Bounds().Max.Y
  28. if srcW <= 0 || srcH <= 0 {
  29. return &image.NRGBA{}
  30. }
  31. // if new width or height is 0 then preserve aspect ratio, minimum 1px
  32. if dstW == 0 {
  33. tmpW := float64(dstH) * float64(srcW) / float64(srcH)
  34. dstW = int(math.Max(1.0, math.Floor(tmpW+0.5)))
  35. }
  36. if dstH == 0 {
  37. tmpH := float64(dstW) * float64(srcH) / float64(srcW)
  38. dstH = int(math.Max(1.0, math.Floor(tmpH+0.5)))
  39. }
  40. var dst *image.NRGBA
  41. if filter.Support <= 0.0 {
  42. // nearest-neighbor special case
  43. dst = resizeNearest(src, dstW, dstH)
  44. } else {
  45. // two-pass resize
  46. if srcW != dstW {
  47. dst = resizeHorizontal(src, dstW, filter)
  48. } else {
  49. dst = src
  50. }
  51. if srcH != dstH {
  52. dst = resizeVertical(dst, dstH, filter)
  53. }
  54. }
  55. return dst
  56. }
  57. func resizeHorizontal(src *image.NRGBA, width int, filter ResampleFilter) *image.NRGBA {
  58. srcBounds := src.Bounds()
  59. srcW := srcBounds.Max.X
  60. srcH := srcBounds.Max.Y
  61. dstW := width
  62. dstH := srcH
  63. dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
  64. dX := float64(srcW) / float64(dstW)
  65. scaleX := math.Max(dX, 1.0)
  66. rX := math.Ceil(scaleX * filter.Support)
  67. parallel(dstW, func(partStart, partEnd int) {
  68. weights := make([]float64, int(rX+2)*2)
  69. for dstX := partStart; dstX < partEnd; dstX++ {
  70. fX := (float64(dstX)+0.5)*dX - 0.5
  71. startX := int(math.Ceil(fX - rX))
  72. if startX < 0 {
  73. startX = 0
  74. }
  75. endX := int(math.Floor(fX + rX))
  76. if endX > srcW-1 {
  77. endX = srcW - 1
  78. }
  79. // cache weights
  80. weightSum := 0.0
  81. for x := startX; x <= endX; x++ {
  82. w := filter.Kernel((float64(x) - fX) / scaleX)
  83. weightSum += w
  84. weights[x-startX] = w
  85. }
  86. for dstY := 0; dstY < dstH; dstY++ {
  87. r, g, b, a := 0.0, 0.0, 0.0, 0.0
  88. for x := startX; x <= endX; x++ {
  89. weight := weights[x-startX]
  90. i := dstY*src.Stride + x*4
  91. r += float64(src.Pix[i+0]) * weight
  92. g += float64(src.Pix[i+1]) * weight
  93. b += float64(src.Pix[i+2]) * weight
  94. a += float64(src.Pix[i+3]) * weight
  95. }
  96. r = math.Min(math.Max(r/weightSum, 0.0), 255.0)
  97. g = math.Min(math.Max(g/weightSum, 0.0), 255.0)
  98. b = math.Min(math.Max(b/weightSum, 0.0), 255.0)
  99. a = math.Min(math.Max(a/weightSum, 0.0), 255.0)
  100. j := dstY*dst.Stride + dstX*4
  101. dst.Pix[j+0] = uint8(r + 0.5)
  102. dst.Pix[j+1] = uint8(g + 0.5)
  103. dst.Pix[j+2] = uint8(b + 0.5)
  104. dst.Pix[j+3] = uint8(a + 0.5)
  105. }
  106. }
  107. })
  108. return dst
  109. }
  110. func resizeVertical(src *image.NRGBA, height int, filter ResampleFilter) *image.NRGBA {
  111. srcBounds := src.Bounds()
  112. srcW := srcBounds.Max.X
  113. srcH := srcBounds.Max.Y
  114. dstW := srcW
  115. dstH := height
  116. dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
  117. dY := float64(srcH) / float64(dstH)
  118. scaleY := math.Max(dY, 1.0)
  119. rY := math.Ceil(scaleY * filter.Support)
  120. parallel(dstH, func(partStart, partEnd int) {
  121. weights := make([]float64, int(rY+2)*2)
  122. for dstY := partStart; dstY < partEnd; dstY++ {
  123. fY := (float64(dstY)+0.5)*dY - 0.5
  124. startY := int(math.Ceil(fY - rY))
  125. if startY < 0 {
  126. startY = 0
  127. }
  128. endY := int(math.Floor(fY + rY))
  129. if endY > srcH-1 {
  130. endY = srcH - 1
  131. }
  132. // cache weights
  133. weightSum := 0.0
  134. for y := startY; y <= endY; y++ {
  135. w := filter.Kernel((float64(y) - fY) / scaleY)
  136. weightSum += w
  137. weights[y-startY] = w
  138. }
  139. for dstX := 0; dstX < dstW; dstX++ {
  140. r, g, b, a := 0.0, 0.0, 0.0, 0.0
  141. for y := startY; y <= endY; y++ {
  142. weight := weights[y-startY]
  143. i := y*src.Stride + dstX*4
  144. r += float64(src.Pix[i+0]) * weight
  145. g += float64(src.Pix[i+1]) * weight
  146. b += float64(src.Pix[i+2]) * weight
  147. a += float64(src.Pix[i+3]) * weight
  148. }
  149. r = math.Min(math.Max(r/weightSum, 0.0), 255.0)
  150. g = math.Min(math.Max(g/weightSum, 0.0), 255.0)
  151. b = math.Min(math.Max(b/weightSum, 0.0), 255.0)
  152. a = math.Min(math.Max(a/weightSum, 0.0), 255.0)
  153. j := dstY*dst.Stride + dstX*4
  154. dst.Pix[j+0] = uint8(r + 0.5)
  155. dst.Pix[j+1] = uint8(g + 0.5)
  156. dst.Pix[j+2] = uint8(b + 0.5)
  157. dst.Pix[j+3] = uint8(a + 0.5)
  158. }
  159. }
  160. })
  161. return dst
  162. }
  163. // fast nearest-neighbor resize, no filtering
  164. func resizeNearest(src *image.NRGBA, width, height int) *image.NRGBA {
  165. dstW, dstH := width, height
  166. srcBounds := src.Bounds()
  167. srcW := srcBounds.Max.X
  168. srcH := srcBounds.Max.Y
  169. dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
  170. dx := float64(srcW) / float64(dstW)
  171. dy := float64(srcH) / float64(dstH)
  172. parallel(dstH, func(partStart, partEnd int) {
  173. for dstY := partStart; dstY < partEnd; dstY++ {
  174. fy := (float64(dstY)+0.5)*dy - 0.5
  175. for dstX := 0; dstX < dstW; dstX++ {
  176. fx := (float64(dstX)+0.5)*dx - 0.5
  177. srcX := int(math.Min(math.Max(math.Floor(fx+0.5), 0.0), float64(srcW)))
  178. srcY := int(math.Min(math.Max(math.Floor(fy+0.5), 0.0), float64(srcH)))
  179. srcOff := srcY*src.Stride + srcX*4
  180. dstOff := dstY*dst.Stride + dstX*4
  181. copy(dst.Pix[dstOff:dstOff+4], src.Pix[srcOff:srcOff+4])
  182. }
  183. }
  184. })
  185. return dst
  186. }
  187. // Fit scales down the image using the specified resample filter to fit the specified
  188. // maximum width and height and returns the transformed image.
  189. //
  190. // Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
  191. // CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
  192. //
  193. // Usage example:
  194. //
  195. // dstImage := imaging.Fit(srcImage, 800, 600, imaging.Lanczos)
  196. //
  197. func Fit(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA {
  198. maxW, maxH := width, height
  199. if maxW <= 0 || maxH <= 0 {
  200. return &image.NRGBA{}
  201. }
  202. srcBounds := img.Bounds()
  203. srcW := srcBounds.Dx()
  204. srcH := srcBounds.Dy()
  205. if srcW <= 0 || srcH <= 0 {
  206. return &image.NRGBA{}
  207. }
  208. if srcW <= maxW && srcH <= maxH {
  209. return Clone(img)
  210. }
  211. srcAspectRatio := float64(srcW) / float64(srcH)
  212. maxAspectRatio := float64(maxW) / float64(maxH)
  213. var newW, newH int
  214. if srcAspectRatio > maxAspectRatio {
  215. newW = maxW
  216. newH = int(float64(newW) / srcAspectRatio)
  217. } else {
  218. newH = maxH
  219. newW = int(float64(newH) * srcAspectRatio)
  220. }
  221. return Resize(img, newW, newH, filter)
  222. }
  223. // Thumbnail scales the image up or down using the specified resample filter, crops it
  224. // to the specified width and hight and returns the transformed image.
  225. //
  226. // Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
  227. // CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
  228. //
  229. // Usage example:
  230. //
  231. // dstImage := imaging.Fit(srcImage, 100, 100, imaging.Lanczos)
  232. //
  233. func Thumbnail(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA {
  234. thumbW, thumbH := width, height
  235. if thumbW <= 0 || thumbH <= 0 {
  236. return &image.NRGBA{}
  237. }
  238. srcBounds := img.Bounds()
  239. srcW := srcBounds.Dx()
  240. srcH := srcBounds.Dy()
  241. if srcW <= 0 || srcH <= 0 {
  242. return &image.NRGBA{}
  243. }
  244. srcAspectRatio := float64(srcW) / float64(srcH)
  245. thumbAspectRatio := float64(thumbW) / float64(thumbH)
  246. var tmp image.Image
  247. if srcAspectRatio > thumbAspectRatio {
  248. tmp = Resize(img, 0, thumbH, filter)
  249. } else {
  250. tmp = Resize(img, thumbW, 0, filter)
  251. }
  252. return CropCenter(tmp, thumbW, thumbH)
  253. }
  254. // Resample filter struct. It can be used to make custom filters.
  255. //
  256. // Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
  257. // CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
  258. //
  259. // General filter recommendations:
  260. //
  261. // - Lanczos
  262. // Probably the best resampling filter for photographic images yielding sharp results,
  263. // but it's slower than cubic filters (see below).
  264. //
  265. // - CatmullRom
  266. // A sharp cubic filter. It's a good filter for both upscaling and downscaling if sharp results are needed.
  267. //
  268. // - MitchellNetravali
  269. // A high quality cubic filter that produces smoother results with less ringing than CatmullRom.
  270. //
  271. // - BSpline
  272. // A good filter if a very smooth output is needed.
  273. //
  274. // - Linear
  275. // Bilinear interpolation filter, produces reasonably good, smooth output. It's faster than cubic filters.
  276. //
  277. // - Box
  278. // Simple and fast resampling filter appropriate for downscaling.
  279. // When upscaling it's similar to NearestNeighbor.
  280. //
  281. // - NearestNeighbor
  282. // Fastest resample filter, no antialiasing at all. Rarely used.
  283. //
  284. type ResampleFilter struct {
  285. Support float64
  286. Kernel func(float64) float64
  287. }
  288. // Nearest-neighbor filter, no anti-aliasing.
  289. var NearestNeighbor ResampleFilter
  290. // Box filter (averaging pixels).
  291. var Box ResampleFilter
  292. // Linear filter.
  293. var Linear ResampleFilter
  294. // Hermite cubic spline filter (BC-spline; B=0; C=0).
  295. var Hermite ResampleFilter
  296. // Mitchell-Netravali cubic filter (BC-spline; B=1/3; C=1/3).
  297. var MitchellNetravali ResampleFilter
  298. // Catmull-Rom - sharp cubic filter (BC-spline; B=0; C=0.5).
  299. var CatmullRom ResampleFilter
  300. // Cubic B-spline - smooth cubic filter (BC-spline; B=1; C=0).
  301. var BSpline ResampleFilter
  302. // Gaussian Blurring Filter.
  303. var Gaussian ResampleFilter
  304. // Bartlett-windowed sinc filter (3 lobes).
  305. var Bartlett ResampleFilter
  306. // Lanczos filter (3 lobes).
  307. var Lanczos ResampleFilter
  308. // Hann-windowed sinc filter (3 lobes).
  309. var Hann ResampleFilter
  310. // Hamming-windowed sinc filter (3 lobes).
  311. var Hamming ResampleFilter
  312. // Blackman-windowed sinc filter (3 lobes).
  313. var Blackman ResampleFilter
  314. // Welch-windowed sinc filter (parabolic window, 3 lobes).
  315. var Welch ResampleFilter
  316. // Cosine-windowed sinc filter (3 lobes).
  317. var Cosine ResampleFilter
  318. func bcspline(x, b, c float64) float64 {
  319. x = math.Abs(x)
  320. if x < 1.0 {
  321. return ((12-9*b-6*c)*x*x*x + (-18+12*b+6*c)*x*x + (6 - 2*b)) / 6
  322. }
  323. if x < 2.0 {
  324. return ((-b-6*c)*x*x*x + (6*b+30*c)*x*x + (-12*b-48*c)*x + (8*b + 24*c)) / 6
  325. }
  326. return 0
  327. }
  328. func sinc(x float64) float64 {
  329. if x == 0 {
  330. return 1
  331. }
  332. return math.Sin(math.Pi*x) / (math.Pi * x)
  333. }
  334. func init() {
  335. NearestNeighbor = ResampleFilter{
  336. Support: 0.0, // special case - not applying the filter
  337. }
  338. Box = ResampleFilter{
  339. Support: 0.5,
  340. Kernel: func(x float64) float64 {
  341. x = math.Abs(x)
  342. if x <= 0.5 {
  343. return 1.0
  344. }
  345. return 0
  346. },
  347. }
  348. Linear = ResampleFilter{
  349. Support: 1.0,
  350. Kernel: func(x float64) float64 {
  351. x = math.Abs(x)
  352. if x < 1.0 {
  353. return 1.0 - x
  354. }
  355. return 0
  356. },
  357. }
  358. Hermite = ResampleFilter{
  359. Support: 1.0,
  360. Kernel: func(x float64) float64 {
  361. x = math.Abs(x)
  362. if x < 1.0 {
  363. return bcspline(x, 0.0, 0.0)
  364. }
  365. return 0
  366. },
  367. }
  368. MitchellNetravali = ResampleFilter{
  369. Support: 2.0,
  370. Kernel: func(x float64) float64 {
  371. x = math.Abs(x)
  372. if x < 2.0 {
  373. return bcspline(x, 1.0/3.0, 1.0/3.0)
  374. }
  375. return 0
  376. },
  377. }
  378. CatmullRom = ResampleFilter{
  379. Support: 2.0,
  380. Kernel: func(x float64) float64 {
  381. x = math.Abs(x)
  382. if x < 2.0 {
  383. return bcspline(x, 0.0, 0.5)
  384. }
  385. return 0
  386. },
  387. }
  388. BSpline = ResampleFilter{
  389. Support: 2.0,
  390. Kernel: func(x float64) float64 {
  391. x = math.Abs(x)
  392. if x < 2.0 {
  393. return bcspline(x, 1.0, 0.0)
  394. }
  395. return 0
  396. },
  397. }
  398. Gaussian = ResampleFilter{
  399. Support: 2.0,
  400. Kernel: func(x float64) float64 {
  401. x = math.Abs(x)
  402. if x < 2.0 {
  403. return math.Exp(-2 * x * x)
  404. }
  405. return 0
  406. },
  407. }
  408. Bartlett = ResampleFilter{
  409. Support: 3.0,
  410. Kernel: func(x float64) float64 {
  411. x = math.Abs(x)
  412. if x < 3.0 {
  413. return sinc(x) * (3.0 - x) / 3.0
  414. }
  415. return 0
  416. },
  417. }
  418. Lanczos = ResampleFilter{
  419. Support: 3.0,
  420. Kernel: func(x float64) float64 {
  421. x = math.Abs(x)
  422. if x < 3.0 {
  423. return sinc(x) * sinc(x/3.0)
  424. }
  425. return 0
  426. },
  427. }
  428. Hann = ResampleFilter{
  429. Support: 3.0,
  430. Kernel: func(x float64) float64 {
  431. x = math.Abs(x)
  432. if x < 3.0 {
  433. return sinc(x) * (0.5 + 0.5*math.Cos(math.Pi*x/3.0))
  434. }
  435. return 0
  436. },
  437. }
  438. Hamming = ResampleFilter{
  439. Support: 3.0,
  440. Kernel: func(x float64) float64 {
  441. x = math.Abs(x)
  442. if x < 3.0 {
  443. return sinc(x) * (0.54 + 0.46*math.Cos(math.Pi*x/3.0))
  444. }
  445. return 0
  446. },
  447. }
  448. Blackman = ResampleFilter{
  449. Support: 3.0,
  450. Kernel: func(x float64) float64 {
  451. x = math.Abs(x)
  452. if x < 3.0 {
  453. return sinc(x) * (0.42 - 0.5*math.Cos(math.Pi*x/3.0+math.Pi) + 0.08*math.Cos(2.0*math.Pi*x/3.0))
  454. }
  455. return 0
  456. },
  457. }
  458. Welch = ResampleFilter{
  459. Support: 3.0,
  460. Kernel: func(x float64) float64 {
  461. x = math.Abs(x)
  462. if x < 3.0 {
  463. return sinc(x) * (1.0 - (x * x / 9.0))
  464. }
  465. return 0
  466. },
  467. }
  468. Cosine = ResampleFilter{
  469. Support: 3.0,
  470. Kernel: func(x float64) float64 {
  471. x = math.Abs(x)
  472. if x < 3.0 {
  473. return sinc(x) * math.Cos((math.Pi/2.0)*(x/3.0))
  474. }
  475. return 0
  476. },
  477. }
  478. }