helpers_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package imaging
  2. import (
  3. "bytes"
  4. "image"
  5. "image/color"
  6. "testing"
  7. )
  8. func compareNRGBA(img1, img2 *image.NRGBA, delta int) bool {
  9. if !img1.Rect.Eq(img2.Rect) {
  10. return false
  11. }
  12. if len(img1.Pix) != len(img2.Pix) {
  13. return false
  14. }
  15. for i := 0; i < len(img1.Pix); i++ {
  16. if absint(int(img1.Pix[i])-int(img2.Pix[i])) > delta {
  17. return false
  18. }
  19. }
  20. return true
  21. }
  22. func TestEncodeDecode(t *testing.T) {
  23. imgWithAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3))
  24. imgWithAlpha.Pix = []uint8{
  25. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  26. 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
  27. 244, 245, 246, 247, 248, 249, 250, 252, 252, 253, 254, 255,
  28. }
  29. imgWithoutAlpha := image.NewNRGBA(image.Rect(0, 0, 3, 3))
  30. imgWithoutAlpha.Pix = []uint8{
  31. 0, 1, 2, 255, 4, 5, 6, 255, 8, 9, 10, 255,
  32. 127, 128, 129, 255, 131, 132, 133, 255, 135, 136, 137, 255,
  33. 244, 245, 246, 255, 248, 249, 250, 255, 252, 253, 254, 255,
  34. }
  35. for _, format := range []Format{JPEG, PNG, GIF, BMP, TIFF} {
  36. img := imgWithoutAlpha
  37. if format == PNG {
  38. img = imgWithAlpha
  39. }
  40. buf := &bytes.Buffer{}
  41. err := Encode(buf, img, format)
  42. if err != nil {
  43. t.Errorf("fail encoding format %s", format)
  44. continue
  45. }
  46. img2, err := Decode(buf)
  47. if err != nil {
  48. t.Errorf("fail decoding format %s", format)
  49. continue
  50. }
  51. img2cloned := Clone(img2)
  52. delta := 0
  53. if format == JPEG {
  54. delta = 3
  55. } else if format == GIF {
  56. delta = 16
  57. }
  58. if !compareNRGBA(img, img2cloned, delta) {
  59. t.Errorf("test [DecodeEncode %s] failed: %#v %#v", format, img, img2cloned)
  60. continue
  61. }
  62. }
  63. buf := &bytes.Buffer{}
  64. err := Encode(buf, imgWithAlpha, Format(100))
  65. if err != ErrUnsupportedFormat {
  66. t.Errorf("expected ErrUnsupportedFormat")
  67. }
  68. }
  69. func TestNew(t *testing.T) {
  70. td := []struct {
  71. desc string
  72. w, h int
  73. c color.Color
  74. dstBounds image.Rectangle
  75. dstPix []uint8
  76. }{
  77. {
  78. "New 1x1 black",
  79. 1, 1,
  80. color.NRGBA{0, 0, 0, 0},
  81. image.Rect(0, 0, 1, 1),
  82. []uint8{0x00, 0x00, 0x00, 0x00},
  83. },
  84. {
  85. "New 1x2 red",
  86. 1, 2,
  87. color.NRGBA{255, 0, 0, 255},
  88. image.Rect(0, 0, 1, 2),
  89. []uint8{0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff},
  90. },
  91. {
  92. "New 2x1 white",
  93. 2, 1,
  94. color.NRGBA{255, 255, 255, 255},
  95. image.Rect(0, 0, 2, 1),
  96. []uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  97. },
  98. }
  99. for _, d := range td {
  100. got := New(d.w, d.h, d.c)
  101. want := image.NewNRGBA(d.dstBounds)
  102. want.Pix = d.dstPix
  103. if !compareNRGBA(got, want, 0) {
  104. t.Errorf("test [%s] failed: %#v", d.desc, got)
  105. }
  106. }
  107. }
  108. func TestClone(t *testing.T) {
  109. td := []struct {
  110. desc string
  111. src image.Image
  112. want *image.NRGBA
  113. }{
  114. {
  115. "Clone NRGBA",
  116. &image.NRGBA{
  117. Rect: image.Rect(-1, -1, 0, 1),
  118. Stride: 1 * 4,
  119. Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff},
  120. },
  121. &image.NRGBA{
  122. Rect: image.Rect(0, 0, 1, 2),
  123. Stride: 1 * 4,
  124. Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff},
  125. },
  126. },
  127. {
  128. "Clone NRGBA64",
  129. &image.NRGBA64{
  130. Rect: image.Rect(-1, -1, 0, 1),
  131. Stride: 1 * 8,
  132. Pix: []uint8{
  133. 0x00, 0x00, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
  134. 0xcc, 0xcc, 0xdd, 0xdd, 0xee, 0xee, 0xff, 0xff,
  135. },
  136. },
  137. &image.NRGBA{
  138. Rect: image.Rect(0, 0, 1, 2),
  139. Stride: 1 * 4,
  140. Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff},
  141. },
  142. },
  143. {
  144. "Clone RGBA",
  145. &image.RGBA{
  146. Rect: image.Rect(-1, -1, 0, 1),
  147. Stride: 1 * 4,
  148. Pix: []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff},
  149. },
  150. &image.NRGBA{
  151. Rect: image.Rect(0, 0, 1, 2),
  152. Stride: 1 * 4,
  153. Pix: []uint8{0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff},
  154. },
  155. },
  156. {
  157. "Clone RGBA64",
  158. &image.RGBA64{
  159. Rect: image.Rect(-1, -1, 0, 1),
  160. Stride: 1 * 8,
  161. Pix: []uint8{
  162. 0x00, 0x00, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
  163. 0xcc, 0xcc, 0xdd, 0xdd, 0xee, 0xee, 0xff, 0xff,
  164. },
  165. },
  166. &image.NRGBA{
  167. Rect: image.Rect(0, 0, 1, 2),
  168. Stride: 1 * 4,
  169. Pix: []uint8{0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff},
  170. },
  171. },
  172. {
  173. "Clone Gray",
  174. &image.Gray{
  175. Rect: image.Rect(-1, -1, 0, 1),
  176. Stride: 1 * 1,
  177. Pix: []uint8{0x11, 0xee},
  178. },
  179. &image.NRGBA{
  180. Rect: image.Rect(0, 0, 1, 2),
  181. Stride: 1 * 4,
  182. Pix: []uint8{0x11, 0x11, 0x11, 0xff, 0xee, 0xee, 0xee, 0xff},
  183. },
  184. },
  185. {
  186. "Clone Gray16",
  187. &image.Gray16{
  188. Rect: image.Rect(-1, -1, 0, 1),
  189. Stride: 1 * 2,
  190. Pix: []uint8{0x11, 0x11, 0xee, 0xee},
  191. },
  192. &image.NRGBA{
  193. Rect: image.Rect(0, 0, 1, 2),
  194. Stride: 1 * 4,
  195. Pix: []uint8{0x11, 0x11, 0x11, 0xff, 0xee, 0xee, 0xee, 0xff},
  196. },
  197. },
  198. {
  199. "Clone Alpha",
  200. &image.Alpha{
  201. Rect: image.Rect(-1, -1, 0, 1),
  202. Stride: 1 * 1,
  203. Pix: []uint8{0x11, 0xee},
  204. },
  205. &image.NRGBA{
  206. Rect: image.Rect(0, 0, 1, 2),
  207. Stride: 1 * 4,
  208. Pix: []uint8{0xff, 0xff, 0xff, 0x11, 0xff, 0xff, 0xff, 0xee},
  209. },
  210. },
  211. {
  212. "Clone YCbCr",
  213. &image.YCbCr{
  214. Rect: image.Rect(-1, -1, 5, 0),
  215. SubsampleRatio: image.YCbCrSubsampleRatio444,
  216. YStride: 6,
  217. CStride: 6,
  218. Y: []uint8{0x00, 0xff, 0x7f, 0x26, 0x4b, 0x0e},
  219. Cb: []uint8{0x80, 0x80, 0x80, 0x6b, 0x56, 0xc0},
  220. Cr: []uint8{0x80, 0x80, 0x80, 0xc0, 0x4b, 0x76},
  221. },
  222. &image.NRGBA{
  223. Rect: image.Rect(0, 0, 6, 1),
  224. Stride: 6 * 4,
  225. Pix: []uint8{
  226. 0x00, 0x00, 0x00, 0xff,
  227. 0xff, 0xff, 0xff, 0xff,
  228. 0x7f, 0x7f, 0x7f, 0xff,
  229. 0x7f, 0x00, 0x00, 0xff,
  230. 0x00, 0x7f, 0x00, 0xff,
  231. 0x00, 0x00, 0x7f, 0xff,
  232. },
  233. },
  234. },
  235. {
  236. "Clone Paletted",
  237. &image.Paletted{
  238. Rect: image.Rect(-1, -1, 5, 0),
  239. Stride: 6 * 1,
  240. Palette: color.Palette{
  241. color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff},
  242. color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
  243. color.NRGBA{R: 0x7f, G: 0x7f, B: 0x7f, A: 0xff},
  244. color.NRGBA{R: 0x7f, G: 0x00, B: 0x00, A: 0xff},
  245. color.NRGBA{R: 0x00, G: 0x7f, B: 0x00, A: 0xff},
  246. color.NRGBA{R: 0x00, G: 0x00, B: 0x7f, A: 0xff},
  247. },
  248. Pix: []uint8{0x0, 0x1, 0x2, 0x3, 0x4, 0x5},
  249. },
  250. &image.NRGBA{
  251. Rect: image.Rect(0, 0, 6, 1),
  252. Stride: 6 * 4,
  253. Pix: []uint8{
  254. 0x00, 0x00, 0x00, 0xff,
  255. 0xff, 0xff, 0xff, 0xff,
  256. 0x7f, 0x7f, 0x7f, 0xff,
  257. 0x7f, 0x00, 0x00, 0xff,
  258. 0x00, 0x7f, 0x00, 0xff,
  259. 0x00, 0x00, 0x7f, 0xff,
  260. },
  261. },
  262. },
  263. }
  264. for _, d := range td {
  265. got := Clone(d.src)
  266. want := d.want
  267. delta := 0
  268. if _, ok := d.src.(*image.YCbCr); ok {
  269. delta = 1
  270. }
  271. if !compareNRGBA(got, want, delta) {
  272. t.Errorf("test [%s] failed: %#v", d.desc, got)
  273. }
  274. }
  275. }