main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "fmt"
  4. "image"
  5. "image/color"
  6. "image/gif"
  7. "log"
  8. "sort"
  9. "git.red-green.com/david/point2d"
  10. )
  11. type HitList []point2d.Point
  12. func (h HitList) Len() int {
  13. return len(h)
  14. }
  15. func (h HitList) Swap(i, j int) {
  16. h[i], h[j] = h[j], h[i]
  17. }
  18. func (h HitList) Less(i, j int) bool {
  19. return h[i].LessThan(&h[j])
  20. }
  21. func Scan(filename string) (hits []HitList, err error) {
  22. var (
  23. gif *gif.GIF
  24. size image.Point
  25. pix color.Color
  26. r uint32
  27. g uint32
  28. b uint32
  29. hitframe HitList
  30. )
  31. gif, err = DecodeGif(filename)
  32. if err != nil {
  33. return nil, fmt.Errorf("DecodeGif(\"%s\") > %v", filename, err)
  34. }
  35. for _, frame := range gif.Image {
  36. size = frame.Bounds().Size()
  37. hitframe = HitList{}
  38. for y := range make([]byte, size.Y) {
  39. for x := range make([]byte, size.X) {
  40. pix = frame.At(x, y)
  41. r, g, b, _ = pix.RGBA()
  42. r /= 255
  43. g /= 255
  44. b /= 255
  45. // Now to determine if this pixel is considerable as rain
  46. if (r <= 47 && g >= 90 && b <= 51) || (r >= 240 && g >= 240 && b <= 10) || (r >= 240 && g <= 10 && b <= 10) {
  47. //fmt.Printf("{(%d, %d) [%d, %d, %d, %s]}\r\n", x, y, r, g, b, rgbToHex(r, g, b))
  48. frame.Set(x, y, color.RGBA{R: 255, G: 0, B: 0, A: 255})
  49. hitframe = append(hitframe, *point2d.AsPoint(x, y))
  50. }
  51. }
  52. }
  53. hits = append(hits, hitframe)
  54. }
  55. err = EncodeGif(filename+".out", gif)
  56. if err != nil {
  57. return nil, fmt.Errorf("EncodeGif(\"%s.out\", gif) > %v", filename, err)
  58. }
  59. return hits, nil
  60. }
  61. func main() {
  62. var (
  63. err error
  64. hits []HitList
  65. diff point2d.Point
  66. )
  67. /*
  68. err = GrabPascoGif()
  69. if err != nil {
  70. log.Panic("GrabPascoGif() >", err)
  71. }
  72. */
  73. hits, err = Scan("pasco.gif")
  74. if err != nil {
  75. log.Panic(err)
  76. }
  77. for _, frame := range hits {
  78. sort.Sort(&frame)
  79. }
  80. for _, pos := range hits[0] {
  81. if diff.IsZero() {
  82. diff = pos
  83. } else {
  84. dist := diff.DistanceTo(&pos)
  85. if dist > 1 {
  86. fmt.Printf("0] (%s) diff=(%s) dist=%d\r\n", pos.String(), diff.String(), dist)
  87. }
  88. if dist > 1 {
  89. diff = pos
  90. }
  91. }
  92. }
  93. }