adjust.go 7.2 KB

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