adjust.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. percentage = math.Min(math.Max(percentage, -100), 100)
  62. multiplier := 1 + percentage/100
  63. return AdjustFunc(img, func(c color.NRGBA) color.NRGBA {
  64. h, s, l := rgbToHSL(c.R, c.G, c.B)
  65. s *= multiplier
  66. if s > 1 {
  67. s = 1
  68. }
  69. r, g, b := hslToRGB(h, s, l)
  70. return color.NRGBA{r, g, b, c.A}
  71. })
  72. }
  73. // AdjustContrast changes the contrast of the image using the percentage parameter and returns the adjusted image.
  74. // The percentage must be in range (-100, 100). The percentage = 0 gives the original image.
  75. // The percentage = -100 gives solid gray image.
  76. //
  77. // Examples:
  78. //
  79. // dstImage = imaging.AdjustContrast(srcImage, -10) // Decrease image contrast by 10%.
  80. // dstImage = imaging.AdjustContrast(srcImage, 20) // Increase image contrast by 20%.
  81. //
  82. func AdjustContrast(img image.Image, percentage float64) *image.NRGBA {
  83. percentage = math.Min(math.Max(percentage, -100.0), 100.0)
  84. lut := make([]uint8, 256)
  85. v := (100.0 + percentage) / 100.0
  86. for i := 0; i < 256; i++ {
  87. if 0 <= v && v <= 1 {
  88. lut[i] = clamp((0.5 + (float64(i)/255.0-0.5)*v) * 255.0)
  89. } else if 1 < v && v < 2 {
  90. lut[i] = clamp((0.5 + (float64(i)/255.0-0.5)*(1/(2.0-v))) * 255.0)
  91. } else {
  92. lut[i] = uint8(float64(i)/255.0+0.5) * 255
  93. }
  94. }
  95. return adjustLUT(img, lut)
  96. }
  97. // AdjustBrightness changes the brightness of the image using the percentage parameter and returns the adjusted image.
  98. // The percentage must be in range (-100, 100). The percentage = 0 gives the original image.
  99. // The percentage = -100 gives solid black image. The percentage = 100 gives solid white image.
  100. //
  101. // Examples:
  102. //
  103. // dstImage = imaging.AdjustBrightness(srcImage, -15) // Decrease image brightness by 15%.
  104. // dstImage = imaging.AdjustBrightness(srcImage, 10) // Increase image brightness by 10%.
  105. //
  106. func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA {
  107. percentage = math.Min(math.Max(percentage, -100.0), 100.0)
  108. lut := make([]uint8, 256)
  109. shift := 255.0 * percentage / 100.0
  110. for i := 0; i < 256; i++ {
  111. lut[i] = clamp(float64(i) + shift)
  112. }
  113. return adjustLUT(img, lut)
  114. }
  115. // AdjustGamma performs a gamma correction on the image and returns the adjusted image.
  116. // Gamma parameter must be positive. Gamma = 1.0 gives the original image.
  117. // Gamma less than 1.0 darkens the image and gamma greater than 1.0 lightens it.
  118. //
  119. // Example:
  120. //
  121. // dstImage = imaging.AdjustGamma(srcImage, 0.7)
  122. //
  123. func AdjustGamma(img image.Image, gamma float64) *image.NRGBA {
  124. e := 1.0 / math.Max(gamma, 0.0001)
  125. lut := make([]uint8, 256)
  126. for i := 0; i < 256; i++ {
  127. lut[i] = clamp(math.Pow(float64(i)/255.0, e) * 255.0)
  128. }
  129. return adjustLUT(img, lut)
  130. }
  131. // AdjustSigmoid changes the contrast of the image using a sigmoidal function and returns the adjusted image.
  132. // It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.
  133. // The midpoint parameter is the midpoint of contrast that must be between 0 and 1, typically 0.5.
  134. // The factor parameter indicates how much to increase or decrease the contrast, typically in range (-10, 10).
  135. // If the factor parameter is positive the image contrast is increased otherwise the contrast is decreased.
  136. //
  137. // Examples:
  138. //
  139. // dstImage = imaging.AdjustSigmoid(srcImage, 0.5, 3.0) // Increase the contrast.
  140. // dstImage = imaging.AdjustSigmoid(srcImage, 0.5, -3.0) // Decrease the contrast.
  141. //
  142. func AdjustSigmoid(img image.Image, midpoint, factor float64) *image.NRGBA {
  143. if factor == 0 {
  144. return Clone(img)
  145. }
  146. lut := make([]uint8, 256)
  147. a := math.Min(math.Max(midpoint, 0.0), 1.0)
  148. b := math.Abs(factor)
  149. sig0 := sigmoid(a, b, 0)
  150. sig1 := sigmoid(a, b, 1)
  151. e := 1.0e-6
  152. if factor > 0 {
  153. for i := 0; i < 256; i++ {
  154. x := float64(i) / 255.0
  155. sigX := sigmoid(a, b, x)
  156. f := (sigX - sig0) / (sig1 - sig0)
  157. lut[i] = clamp(f * 255.0)
  158. }
  159. } else {
  160. for i := 0; i < 256; i++ {
  161. x := float64(i) / 255.0
  162. arg := math.Min(math.Max((sig1-sig0)*x+sig0, e), 1.0-e)
  163. f := a - math.Log(1.0/arg-1.0)/b
  164. lut[i] = clamp(f * 255.0)
  165. }
  166. }
  167. return adjustLUT(img, lut)
  168. }
  169. func sigmoid(a, b, x float64) float64 {
  170. return 1 / (1 + math.Exp(b*(a-x)))
  171. }
  172. // adjustLUT applies the given lookup table to the colors of the image.
  173. func adjustLUT(img image.Image, lut []uint8) *image.NRGBA {
  174. src := newScanner(img)
  175. dst := image.NewNRGBA(image.Rect(0, 0, src.w, src.h))
  176. lut = lut[0:256]
  177. parallel(0, src.h, func(ys <-chan int) {
  178. for y := range ys {
  179. i := y * dst.Stride
  180. src.scan(0, y, src.w, y+1, dst.Pix[i:i+src.w*4])
  181. for x := 0; x < src.w; x++ {
  182. d := dst.Pix[i : i+3 : i+3]
  183. d[0] = lut[d[0]]
  184. d[1] = lut[d[1]]
  185. d[2] = lut[d[2]]
  186. i += 4
  187. }
  188. }
  189. })
  190. return dst
  191. }
  192. // AdjustFunc applies the fn function to each pixel of the img image and returns the adjusted image.
  193. //
  194. // Example:
  195. //
  196. // dstImage = imaging.AdjustFunc(
  197. // srcImage,
  198. // func(c color.NRGBA) color.NRGBA {
  199. // // Shift the red channel by 16.
  200. // r := int(c.R) + 16
  201. // if r > 255 {
  202. // r = 255
  203. // }
  204. // return color.NRGBA{uint8(r), c.G, c.B, c.A}
  205. // }
  206. // )
  207. //
  208. func AdjustFunc(img image.Image, fn func(c color.NRGBA) color.NRGBA) *image.NRGBA {
  209. src := newScanner(img)
  210. dst := image.NewNRGBA(image.Rect(0, 0, src.w, src.h))
  211. parallel(0, src.h, func(ys <-chan int) {
  212. for y := range ys {
  213. i := y * dst.Stride
  214. src.scan(0, y, src.w, y+1, dst.Pix[i:i+src.w*4])
  215. for x := 0; x < src.w; x++ {
  216. d := dst.Pix[i : i+4 : i+4]
  217. r := d[0]
  218. g := d[1]
  219. b := d[2]
  220. a := d[3]
  221. c := fn(color.NRGBA{r, g, b, a})
  222. d[0] = c.R
  223. d[1] = c.G
  224. d[2] = c.B
  225. d[3] = c.A
  226. i += 4
  227. }
  228. }
  229. })
  230. return dst
  231. }