resample.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. }