Guest User

Untitled

a guest
Feb 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "image"
  5. "image/color"
  6. "image/draw"
  7. "time"
  8.  
  9. "github.com/faiface/pixel"
  10. "github.com/faiface/pixel/pixelgl"
  11. "github.com/peterhellberg/plasma"
  12. "github.com/peterhellberg/plasma/palette"
  13. )
  14.  
  15. func run() {
  16. const w, h, d = 512, 256, 64 * time.Millisecond
  17.  
  18. win, err := pixelgl.NewWindow(pixelgl.WindowConfig{
  19. Bounds: pixel.R(0, 0, float64(w), float64(h)),
  20. VSync: true,
  21. Undecorated: true,
  22. })
  23. if err != nil {
  24. panic(err)
  25. }
  26.  
  27. win.SetSmooth(false)
  28.  
  29. c := win.Bounds().Center()
  30. t := time.NewTicker(d)
  31.  
  32. var i int
  33.  
  34. for !win.Closed() {
  35. win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ))
  36.  
  37. select {
  38. case <-t.C:
  39. i++
  40. p := atkinsonPlasmaPicture(w/4, h/4, 12, i)
  41. s := pixel.NewSprite(p, p.Bounds())
  42. s.Draw(win, pixel.IM.Moved(c).Scaled(c, 4))
  43. default:
  44. }
  45.  
  46. win.Update()
  47. }
  48. }
  49.  
  50. func main() {
  51. pixelgl.Run(run)
  52. }
  53.  
  54. func atkinsonPlasmaPicture(w, h int, s float64, i int) *pixel.PictureData {
  55. return pixel.PictureDataFromImage(atkinsonDither(
  56. plasma.New(w, h, s).Image(w, h, i, palette.DefaultGradient),
  57. ))
  58. }
  59.  
  60. func atkinsonDither(src image.Image) image.Image {
  61. bounds, dst := src.Bounds(), image.NewGray(src.Bounds())
  62. draw.Draw(dst, bounds, src, image.ZP, draw.Src)
  63.  
  64. s := 1
  65.  
  66. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  67. for x := bounds.Min.X; x < bounds.Max.X; x++ {
  68. gray := dst.At(x, y).(color.Gray)
  69.  
  70. var m = mono(gray.Y)
  71.  
  72. quant_err := int((float64(gray.Y) - float64(m)) / 8)
  73.  
  74. dst.SetGray(x, y, color.Gray{m})
  75.  
  76. for _, p := range []image.Point{
  77. {x + s, y},
  78. {x - s, y + s},
  79. {x, y + s},
  80. {x + s, y + s},
  81. {x + 2*s, y},
  82. {x, y + 2*s},
  83. } {
  84. gray := dst.At(p.X, p.Y).(color.Gray)
  85. dst.SetGray(p.X, p.Y, color.Gray{uint8(int16(gray.Y) + int16(quant_err))})
  86. }
  87. }
  88. }
  89.  
  90. return dst
  91. }
  92.  
  93. func mono(l uint8) uint8 {
  94. if l < 128 {
  95. return 0
  96. } else {
  97. return 255
  98. }
  99. }
Add Comment
Please, Sign In to add comment