asciiraster_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package imageutil
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "image"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "strings"
  10. "testing"
  11. "devt.de/krotik/common/fileutil"
  12. )
  13. import (
  14. "image/color"
  15. "image/jpeg"
  16. "image/png"
  17. )
  18. func TestRenderSymbols(t *testing.T) {
  19. reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data))
  20. m, err := jpeg.Decode(reader)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. imageFile := "test.png"
  25. if res, _ := fileutil.PathExists(imageFile); res {
  26. os.Remove(imageFile)
  27. }
  28. colBlack := color.RGBA{0, 0, 0, 255}
  29. _, err = RenderSymbols(m, image.Point{5, 2}, "c", colBlack,
  30. testCharMap)
  31. if err.Error() != "Cannot find mapping for rune: 'c'" {
  32. t.Error("Unexpected error response: ", err)
  33. return
  34. }
  35. m, err = RenderSymbols(m, image.Point{5, 2}, "aba a", colBlack,
  36. testCharMap)
  37. if err != nil {
  38. t.Error(err)
  39. return
  40. }
  41. // Check that at least some pixels were set correctly
  42. if m.At(7, 2) == colBlack {
  43. t.Error("Expected color was not found")
  44. }
  45. if m.At(8, 2) != colBlack {
  46. t.Error("Expected color was not found")
  47. }
  48. if m.At(13, 5) != colBlack {
  49. t.Error("Expected color was not found")
  50. }
  51. if m.At(14, 5) == colBlack {
  52. t.Error("Expected color was not found")
  53. }
  54. var buf bytes.Buffer
  55. png.Encode(&buf, m)
  56. ioutil.WriteFile(imageFile, buf.Bytes(), 0644)
  57. if res, _ := fileutil.PathExists(imageFile); res {
  58. os.Remove(imageFile)
  59. } else {
  60. t.Error("Expected image file did not exist")
  61. }
  62. }
  63. // Test character
  64. var testCharMap = map[rune]string{
  65. 'a': `
  66. ****
  67. ** **
  68. ********
  69. ** **
  70. ** **
  71. `[1:],
  72. 'b': `
  73. ******
  74. ** *
  75. ******
  76. ** *
  77. ******
  78. `[1:],
  79. }
  80. // Test image
  81. const data = `
  82. /9j/4AAQSkZJRgABAQIAHAAcAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdA
  83. SFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2Nj
  84. Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCABnAJYDASIAAhEBAxEB/8QA
  85. HwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIh
  86. MUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVW
  87. V1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXG
  88. x8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQF
  89. BgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAV
  90. YnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOE
  91. hYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq
  92. 8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDlwKMD0pwzSiuK57QzGDxS7D6in8Y5ximnAPUfSlcq4m3ilUYp
  93. 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gigknWI43xLu6jjIHTjtXqfkpPGVYsBkghTikgsYIN/lhgXb
  119. cxLkknp/ShczQ7xtY8vtEmhkj8yGRBuCnehUcnHcVtmwfJ/fQ8e7f/E12txZW91C0U6b42xlST2OR/Ko
  120. Bo1gM/uW55/1jf41nOipu7LhV5FZHIGzI6zwj/vr/Ck+yr3uYf8Ax7/CutbQdMb71tn/ALaN/jSf8I/p
  121. X/PoP++2/wAan6rAr6wzkWt0II+1Rc/7Lf4Vd1eeCSKBbdZDdShYoiZNoyfY10P/AAj2lf8APmP++2/x
  122. oPh/SjKspsozIuNrZORjp3qo0FHYPb3OZt7ae3SzjuItsiRSAgnccl/UA+3Q1yNjKLR4ZZYY5VD7tkv3
  123. WwO/+e1evPp9nI257aJm6bioz1z1+tY+s6Hplnot9PbWMMcqwOFcLyOO1bJWMZSTOPHi+9w3mosrlyd2
  124. 9lCj02g9P/1e9a3hzxAbl2ikZRcdQueHHt7j864Y8Z4I4oRzG6urFWU5BHBB7HNJxTFGbR6he6Vpmtgm
  125. eLy5zwZI/lb8fX8azIvBUUTHdfSFP4QsYB/HNZ+k+KEnRY75hHOvAk6K/v7H9K6yyvlnQBmDZ6GsnzR0
  126. N0oy1RzOtaN/Y1tHNFO06u+zYy4I4Jzx9KKveJblXuordSGES5b6n/62PzorKVdp2LjQTVyWz8UWEWlq
  127. jSgyxfJt6EgdDzWTdeLIZGO7zHI/hVajGmWWP+PWL8qwlAIURrhpMAHHJA71pRcZrToZzcoEuo6heakA
  128. GHk245CZ6/X1qPTLq40q+W5t2QybSpDAkEEc55/zilk5k2r91eKhLDzWz2rpsczbbuemeD76fUNG865I
  129. MiysmQMZAAwa3a5j4ftu0ByP+fh/5CulkLLG7INzhSVHqe1Fh3uOoqn9qQQxyhndmHIxwOmSR2xQ13KD
  130. KoiBZOV9JBnt707MVy5RWdNdy7wRGf3bfMinnO1jg+vY03WXLaJO3mhQ20b0zwpYf0qlG7S7icrJs08U
  131. VwumgC+YiQyeVtZH567hzj8aSL949oGhE/2v5pJCDkksQwBHC4/+vXQ8LZ2uYxxCavY7us/xCcaBfn0h
  132. b+VP0bnSrb94ZMJgOecj1rl/GfidUE2k2gy5+SeQjgA/wj3rlas2jdao48qrjLAGkSKPk4Gc1WMj92I+
  133. lIJnU8OfxPWo5inBokmtQTmM4OOh71b0q6vbFmWCbaxHyqQGAP0PT8KhSTzVyo5ocSKA5VfTOTmqsmRd
  134. pl99XjPzThzK3zOeOSeveirNmkgg/fIpYsTkYORxRXmzlTjJqx6EVUcU7mhkKCzdAK59QI9zYxtG1fYU
  135. UVtgtmY4nZEa8Ak9aqFv3rfSiiu1nMeifDv/AJF+T/r4f+QrqqKKQwzQenNFFMCOKFIgNuThdoJ5OPSk
  136. ubeK6t3gnXdG4wwziiii/UTKMOg6dbzJLFE4dSCP3rEdeOM8805tDsGMvySgSsS6rM6gk9eAcUUVftZt
  137. 3uyVGNthuq3Eei6DK8H7sRR7YuMgHtXkc8rzTNLM26RyWY+p70UVnLY0iEsUipG7rhZBlDkc1HgYoorM
  138. 0HwyBXGeRjmrcUhMg2ghezd//rUUVcTKW5s2jZtY/QDaOKKKK8ip8bPRj8KP/9k=
  139. `