plugin.go 775 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "image"
  4. "image/color"
  5. )
  6. func contrast(v uint32) uint32 {
  7. vf := float64(v) / 255
  8. if vf < 0.5 {
  9. vf = vf * vf * 2
  10. } else {
  11. vf = 1 - vf
  12. vf = 1 - vf*vf*2
  13. }
  14. return uint32(255 * vf)
  15. // if v < 128 {
  16. // return v * v * 2
  17. // }
  18. // v = 128 - v
  19. // return 128 - v*v*2
  20. }
  21. // Convert to binary image
  22. func Transform(src image.Image) image.Image {
  23. bounds := src.Bounds()
  24. w, h := bounds.Max.X, bounds.Max.Y
  25. ret := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{w, h}})
  26. for x := 0; x < w; x++ {
  27. for y := 0; y < h; y++ {
  28. r, g, b, _ := src.At(x, y).RGBA()
  29. r, g, b = r/256, g/256, b/256
  30. r = contrast(r)
  31. g = contrast(g)
  32. b = contrast(b)
  33. ret.Set(x, y, color.RGBA{uint8(r), uint8(g), uint8(b), 0})
  34. }
  35. }
  36. return ret
  37. }