adjust.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package imaging
  2. import (
  3. "image"
  4. "image/color"
  5. "math"
  6. )
  7. // Grayscale produces a grayscale version of the image.
  8. func Grayscale(img image.Image) *image.NRGBA {
  9. src := newScanner(img)
  10. dst := image.NewNRGBA(image.Rect(0, 0, src.w, src.h))
  11. parallel(0, src.h, func(ys <-chan int) {
  12. for y := range ys {
  13. i := y * dst.Stride
  14. src.scan(0, y, src.w, y+1, dst.Pix[i:i+src.w*4])
  15. for x := 0; x < src.w; x++ {
  16. d := dst.Pix[i : i+3 : i+3]
  17. r := d[0]
  18. g := d[1]
  19. b := d[2]
  20. f := 0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)
  21. y := uint8(f + 0.5)
  22. d[0] = y
  23. d[1] = y
  24. d[2] = y
  25. i += 4
  26. }
  27. }
  28. })
  29. return dst
  30. }
  31. // Invert produces an inverted (negated) version of the image.
  32. func Invert(img image.Image) *image.NRGBA {
  33. src := newScanner(img)
  34. dst := image.NewNRGBA(image.Rect(0, 0, src.w, src.h))
  35. parallel(0, src.h, func(ys <-chan int) {
  36. for y := range ys {
  37. i := y * dst.Stride
  38. src.scan(0, y, src.w, y+1, dst.Pix[i:i+src.w*4])
  39. for x := 0; x < src.w; x++ {
  40. d := dst.Pix[i : i+3 : i+3]
  41. d[0] = 255 - d[0]
  42. d[1] = 255 - d[1]
  43. d[2] = 255 - d[2]
  44. i += 4
  45. }
  46. }
  47. })
  48. return dst
  49. }
  50. // AdjustSaturation changes the saturation of the image using the percentage parameter and returns the adjusted image.
  51. // The percentage must be in the range (-100, 100).
  52. // The percentage = 0 gives the original image.
  53. // The percentage = 100 gives the image with the saturation value doubled for each pixel.
  54. // The percentage = -100 gives the image with the saturation value zeroed for each pixel (grayscale).
  55. //
  56. // Examples:
  57. // dstImage = imaging.AdjustSaturation(srcImage, 25) // Increase image saturation by 25%.
  58. // dstImage = imaging.AdjustSaturation(srcImage, -10) // Decrease image saturation by 10%.
  59. //
  60. func AdjustSaturation(img image.Image, percentage float64) *image.NRGBA {
  61. if percentage == 0 {
  62. return Clone(img)
  63. }
  64. percentage = math.Min(math.Max(percentage, -100), 100)
  65. multiplier := 1 + percentage/100
  66. return AdjustFunc(img, func(c color.NRGBA) color.NRGBA {
  67. h, s, l := rgbToHSL(c.R, c.G, c.B)
  68. s *= multiplier
  69. if s > 1 {
  70. s = 1
  71. }
  72. r, g, b := hslToRGB(h, s, l)
  73. return color.NRGBA{r, g, b, c.A}
  74. })
  75. }
  76. // AdjustContrast changes the contrast of the image using the percentage parameter and returns the adjusted image.
  77. // The percentage must be in range (-100, 100). The percentage = 0 gives the original image.
  78. // The percentage = -100 gives solid gray image.
  79. //
  80. // Examples:
  81. //
  82. // dstImage = imaging.AdjustContrast(srcImage, -10) // Decrease image contrast by 10%.
  83. // dstImage = imaging.AdjustContrast(srcImage, 20) // Increase image contrast by 20%.
  84. //
  85. func AdjustContrast(img image.Image, percentage float64) *image.NRGBA {
  86. if percentage == 0 {
  87. return Clone(img)
  88. }
  89. percentage = math.Min(math.Max(percentage, -100.0), 100.0)
  90. lut := make([]uint8, 256)
  91. v := (100.0 + percentage) / 100.0
  92. for i := 0; i < 256; i++ {
  93. switch {
  94. case 0 <= v && v <= 1:
  95. lut[i] = clamp((0.5 + (float64(i)/255.0-0.5)*v) * 255.0)
  96. case 1 < v && v < 2:
  97. lut[i] = clamp((0.5 + (float64(i)/255.0-0.5)*(1/(2.0-v))) * 255.0)
  98. default:
  99. lut[i] = uint8(float64(i)/255.0+0.5) * 255
  100. }
  101. }
  102. return adjustLUT(img, lut)
  103. }
  104. // AdjustBrightness changes the brightness of the image using the percentage parameter and returns the adjusted image.
  105. // The percentage must be in range (-100, 100). The percentage = 0 gives the original image.
  106. // The percentage = -100 gives solid black image. The percentage = 100 gives solid white image.
  107. //
  108. // Examples:
  109. //
  110. // dstImage = imaging.AdjustBrightness(srcImage, -15) // Decrease image brightness by 15%.
  111. // dstImage = imaging.AdjustBrightness(srcImage, 10) // Increase image brightness by 10%.
  112. //
  113. func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA {
  114. if percentage == 0 {
  115. return Clone(img)
  116. }
  117. percentage = math.Min(math.Max(percentage, -100.0), 100.0)
  118. lut := make([]uint8, 256)
  119. shift := 255.0 * percentage / 100.0
  120. for i := 0; i < 256; i++ {
  121. lut[i] = clamp(float64(i) + shift)
  122. }
  123. return adjustLUT(img, lut)
  124. }
  125. // AdjustGamma performs a gamma correction on the image and returns the adjusted image.
  126. // Gamma parameter must be positive. Gamma = 1.0 gives the original image.
  127. // Gamma less than 1.0 darkens the image and gamma greater than 1.0 lightens it.
  128. //
  129. // Example:
  130. //
  131. // dstImage = imaging.AdjustGamma(srcImage, 0.7)
  132. //
  133. func AdjustGamma(img image.Image, gamma float64) *image.NRGBA {
  134. if gamma == 1 {
  135. return Clone(img)
  136. }
  137. e := 1.0 / math.Max(gamma, 0.0001)
  138. lut := make([]uint8, 256)
  139. for i := 0; i < 256; i++ {
  140. lut[i] = clamp(math.Pow(float64(i)/255.0, e) * 255.0)
  141. }
  142. return adjustLUT(img, lut)
  143. }
  144. // AdjustSigmoid changes the contrast of the image using a sigmoidal function and returns the adjusted image.
  145. // It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.
  146. // The midpoint parameter is the midpoint of contrast that must be between 0 and 1, typically 0.5.
  147. // The factor parameter indicates how much to increase or decrease the contrast, typically in range (-10, 10).
  148. // If the factor parameter is positive the image contrast is increased otherwise the contrast is decreased.
  149. //
  150. // Examples:
  151. //
  152. // dstImage = imaging.AdjustSigmoid(srcImage, 0.5, 3.0) // Increase the contrast.
  153. // dstImage = imaging.AdjustSigmoid(srcImage, 0.5, -3.0) // Decrease the contrast.
  154. //
  155. func AdjustSigmoid(img image.Image, midpoint, factor float64) *image.NRGBA {
  156. if factor == 0 {
  157. return Clone(img)
  158. }
  159. lut := make([]uint8, 256)
  160. a := math.Min(math.Max(midpoint, 0.0), 1.0)
  161. b := math.Abs(factor)
  162. sig0 := sigmoid(a, b, 0)
  163. sig1 := sigmoid(a, b, 1)
  164. e := 1.0e-6
  165. if factor > 0 {
  166. for i := 0; i < 256; i++ {
  167. x := float64(i) / 255.0
  168. sigX := sigmoid(a, b, x)
  169. f := (sigX - sig0) / (sig1 - sig0)
  170. lut[i] = clamp(f * 255.0)
  171. }
  172. } else {
  173. for i := 0; i < 256; i++ {
  174. x := float64(i) / 255.0
  175. arg := math.Min(math.Max((sig1-sig0)*x+sig0, e), 1.0-e)
  176. f := a - math.Log(1.0/arg-1.0)/b
  177. lut[i] = clamp(f * 255.0)
  178. }
  179. }
  180. return adjustLUT(img, lut)
  181. }
  182. func sigmoid(a, b, x float64) float64 {
  183. return 1 / (1 + math.Exp(b*(a-x)))
  184. }
  185. // adjustLUT applies the given lookup table to the colors of the image.
  186. func adjustLUT(img image.Image, lut []uint8) *image.NRGBA {
  187. src := newScanner(img)
  188. dst := image.NewNRGBA(image.Rect(0, 0, src.w, src.h))
  189. lut = lut[0:256]
  190. parallel(0, src.h, func(ys <-chan int) {
  191. for y := range ys {
  192. i := y * dst.Stride
  193. src.scan(0, y, src.w, y+1, dst.Pix[i:i+src.w*4])
  194. for x := 0; x < src.w; x++ {
  195. d := dst.Pix[i : i+3 : i+3]
  196. d[0] = lut[d[0]]
  197. d[1] = lut[d[1]]
  198. d[2] = lut[d[2]]
  199. i += 4
  200. }
  201. }
  202. })
  203. return dst
  204. }
  205. // AdjustFunc applies the fn function to each pixel of the img image and returns the adjusted image.
  206. //
  207. // Example:
  208. //
  209. // dstImage = imaging.AdjustFunc(
  210. // srcImage,
  211. // func(c color.NRGBA) color.NRGBA {
  212. // // Shift the red channel by 16.
  213. // r := int(c.R) + 16
  214. // if r > 255 {
  215. // r = 255
  216. // }
  217. // return color.NRGBA{uint8(r), c.G, c.B, c.A}
  218. // }
  219. // )
  220. //
  221. func AdjustFunc(img image.Image, fn func(c color.NRGBA) color.NRGBA) *image.NRGBA {
  222. src := newScanner(img)
  223. dst := image.NewNRGBA(image.Rect(0, 0, src.w, src.h))
  224. parallel(0, src.h, func(ys <-chan int) {
  225. for y := range ys {
  226. i := y * dst.Stride
  227. src.scan(0, y, src.w, y+1, dst.Pix[i:i+src.w*4])
  228. for x := 0; x < src.w; x++ {
  229. d := dst.Pix[i : i+4 : i+4]
  230. r := d[0]
  231. g := d[1]
  232. b := d[2]
  233. a := d[3]
  234. c := fn(color.NRGBA{r, g, b, a})
  235. d[0] = c.R
  236. d[1] = c.G
  237. d[2] = c.B
  238. d[3] = c.A
  239. i += 4
  240. }
  241. }
  242. })
  243. return dst
  244. }