adjust_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. package imaging
  2. import (
  3. "image"
  4. "testing"
  5. )
  6. func TestGrayscale(t *testing.T) {
  7. td := []struct {
  8. desc string
  9. src image.Image
  10. want *image.NRGBA
  11. }{
  12. {
  13. "Grayscale 3x3",
  14. &image.NRGBA{
  15. Rect: image.Rect(-1, -1, 2, 2),
  16. Stride: 3 * 4,
  17. Pix: []uint8{
  18. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  19. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  20. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  21. },
  22. },
  23. &image.NRGBA{
  24. Rect: image.Rect(0, 0, 3, 3),
  25. Stride: 3 * 4,
  26. Pix: []uint8{
  27. 0x3d, 0x3d, 0x3d, 0x01, 0x78, 0x78, 0x78, 0x02, 0x17, 0x17, 0x17, 0x03,
  28. 0x1f, 0x1f, 0x1f, 0xff, 0x25, 0x25, 0x25, 0xff, 0x66, 0x66, 0x66, 0xff,
  29. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  30. },
  31. },
  32. },
  33. }
  34. for _, d := range td {
  35. got := Grayscale(d.src)
  36. want := d.want
  37. if !compareNRGBA(got, want, 0) {
  38. t.Errorf("test [%s] failed: %#v", d.desc, got)
  39. }
  40. }
  41. }
  42. func TestInvert(t *testing.T) {
  43. td := []struct {
  44. desc string
  45. src image.Image
  46. want *image.NRGBA
  47. }{
  48. {
  49. "Invert 3x3",
  50. &image.NRGBA{
  51. Rect: image.Rect(-1, -1, 2, 2),
  52. Stride: 3 * 4,
  53. Pix: []uint8{
  54. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  55. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  56. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  57. },
  58. },
  59. &image.NRGBA{
  60. Rect: image.Rect(0, 0, 3, 3),
  61. Stride: 3 * 4,
  62. Pix: []uint8{
  63. 0x33, 0xff, 0xff, 0x01, 0xff, 0x33, 0xff, 0x02, 0xff, 0xff, 0x33, 0x03,
  64. 0xee, 0xdd, 0xcc, 0xff, 0xcc, 0xdd, 0xee, 0xff, 0x55, 0xcc, 0x44, 0xff,
  65. 0xff, 0xff, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0xff, 0x00, 0x00, 0x00, 0xff,
  66. },
  67. },
  68. },
  69. }
  70. for _, d := range td {
  71. got := Invert(d.src)
  72. want := d.want
  73. if !compareNRGBA(got, want, 0) {
  74. t.Errorf("test [%s] failed: %#v", d.desc, got)
  75. }
  76. }
  77. }
  78. func TestAdjustContrast(t *testing.T) {
  79. td := []struct {
  80. desc string
  81. src image.Image
  82. p float64
  83. want *image.NRGBA
  84. }{
  85. {
  86. "AdjustContrast 3x3 10",
  87. &image.NRGBA{
  88. Rect: image.Rect(-1, -1, 2, 2),
  89. Stride: 3 * 4,
  90. Pix: []uint8{
  91. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  92. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  93. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  94. },
  95. },
  96. 10,
  97. &image.NRGBA{
  98. Rect: image.Rect(0, 0, 3, 3),
  99. Stride: 3 * 4,
  100. Pix: []uint8{
  101. 0xd5, 0x00, 0x00, 0x01, 0x00, 0xd5, 0x00, 0x02, 0x00, 0x00, 0xd5, 0x03,
  102. 0x05, 0x18, 0x2b, 0xff, 0x2b, 0x18, 0x05, 0xff, 0xaf, 0x2b, 0xc2, 0xff,
  103. 0x00, 0x00, 0x00, 0xff, 0x2b, 0x2b, 0x2b, 0xff, 0xff, 0xff, 0xff, 0xff,
  104. },
  105. },
  106. },
  107. {
  108. "AdjustContrast 3x3 100",
  109. &image.NRGBA{
  110. Rect: image.Rect(-1, -1, 2, 2),
  111. Stride: 3 * 4,
  112. Pix: []uint8{
  113. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  114. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  115. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  116. },
  117. },
  118. 100,
  119. &image.NRGBA{
  120. Rect: image.Rect(0, 0, 3, 3),
  121. Stride: 3 * 4,
  122. Pix: []uint8{
  123. 0xff, 0x00, 0x00, 0x01, 0x00, 0xff, 0x00, 0x02, 0x00, 0x00, 0xff, 0x03,
  124. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff,
  125. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  126. },
  127. },
  128. },
  129. {
  130. "AdjustContrast 3x3 -10",
  131. &image.NRGBA{
  132. Rect: image.Rect(-1, -1, 2, 2),
  133. Stride: 3 * 4,
  134. Pix: []uint8{
  135. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  136. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  137. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  138. },
  139. },
  140. -10,
  141. &image.NRGBA{
  142. Rect: image.Rect(0, 0, 3, 3),
  143. Stride: 3 * 4,
  144. Pix: []uint8{
  145. 0xc4, 0x0d, 0x0d, 0x01, 0x0d, 0xc4, 0x0d, 0x02, 0x0d, 0x0d, 0xc4, 0x03,
  146. 0x1c, 0x2b, 0x3b, 0xff, 0x3b, 0x2b, 0x1c, 0xff, 0xa6, 0x3b, 0xb5, 0xff,
  147. 0x0d, 0x0d, 0x0d, 0xff, 0x3b, 0x3b, 0x3b, 0xff, 0xf2, 0xf2, 0xf2, 0xff,
  148. },
  149. },
  150. },
  151. {
  152. "AdjustContrast 3x3 -100",
  153. &image.NRGBA{
  154. Rect: image.Rect(-1, -1, 2, 2),
  155. Stride: 3 * 4,
  156. Pix: []uint8{
  157. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  158. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  159. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  160. },
  161. },
  162. -100,
  163. &image.NRGBA{
  164. Rect: image.Rect(0, 0, 3, 3),
  165. Stride: 3 * 4,
  166. Pix: []uint8{
  167. 0x80, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x02, 0x80, 0x80, 0x80, 0x03,
  168. 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff,
  169. 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff, 0x80, 0x80, 0x80, 0xff,
  170. },
  171. },
  172. },
  173. {
  174. "AdjustContrast 3x3 0",
  175. &image.NRGBA{
  176. Rect: image.Rect(-1, -1, 2, 2),
  177. Stride: 3 * 4,
  178. Pix: []uint8{
  179. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  180. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  181. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  182. },
  183. },
  184. 0,
  185. &image.NRGBA{
  186. Rect: image.Rect(0, 0, 3, 3),
  187. Stride: 3 * 4,
  188. Pix: []uint8{
  189. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  190. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  191. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  192. },
  193. },
  194. },
  195. }
  196. for _, d := range td {
  197. got := AdjustContrast(d.src, d.p)
  198. want := d.want
  199. if !compareNRGBA(got, want, 0) {
  200. t.Errorf("test [%s] failed: %#v", d.desc, got)
  201. }
  202. }
  203. }
  204. func TestAdjustContrastGolden(t *testing.T) {
  205. src, err := Open("testdata/lena_128.png")
  206. if err != nil {
  207. t.Errorf("Open: %v", err)
  208. }
  209. for name, p := range map[string]float64{
  210. "out_contrast_m10.png": -10,
  211. "out_contrast_p10.png": 10,
  212. } {
  213. got := AdjustContrast(src, p)
  214. want, err := Open("testdata/" + name)
  215. if err != nil {
  216. t.Errorf("Open: %v", err)
  217. }
  218. if !compareNRGBA(got, toNRGBA(want), 0) {
  219. t.Errorf("resulting image differs from golden: %s", name)
  220. }
  221. }
  222. }
  223. func TestAdjustBrightness(t *testing.T) {
  224. td := []struct {
  225. desc string
  226. src image.Image
  227. p float64
  228. want *image.NRGBA
  229. }{
  230. {
  231. "AdjustBrightness 3x3 10",
  232. &image.NRGBA{
  233. Rect: image.Rect(-1, -1, 2, 2),
  234. Stride: 3 * 4,
  235. Pix: []uint8{
  236. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  237. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  238. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  239. },
  240. },
  241. 10,
  242. &image.NRGBA{
  243. Rect: image.Rect(0, 0, 3, 3),
  244. Stride: 3 * 4,
  245. Pix: []uint8{
  246. 0xe6, 0x1a, 0x1a, 0x01, 0x1a, 0xe6, 0x1a, 0x02, 0x1a, 0x1a, 0xe6, 0x03,
  247. 0x2b, 0x3c, 0x4d, 0xff, 0x4d, 0x3c, 0x2b, 0xff, 0xc4, 0x4d, 0xd5, 0xff,
  248. 0x1a, 0x1a, 0x1a, 0xff, 0x4d, 0x4d, 0x4d, 0xff, 0xff, 0xff, 0xff, 0xff,
  249. },
  250. },
  251. },
  252. {
  253. "AdjustBrightness 3x3 100",
  254. &image.NRGBA{
  255. Rect: image.Rect(-1, -1, 2, 2),
  256. Stride: 3 * 4,
  257. Pix: []uint8{
  258. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  259. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  260. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  261. },
  262. },
  263. 100,
  264. &image.NRGBA{
  265. Rect: image.Rect(0, 0, 3, 3),
  266. Stride: 3 * 4,
  267. Pix: []uint8{
  268. 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0x02, 0xff, 0xff, 0xff, 0x03,
  269. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  270. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  271. },
  272. },
  273. },
  274. {
  275. "AdjustBrightness 3x3 -10",
  276. &image.NRGBA{
  277. Rect: image.Rect(-1, -1, 2, 2),
  278. Stride: 3 * 4,
  279. Pix: []uint8{
  280. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  281. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  282. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  283. },
  284. },
  285. -10,
  286. &image.NRGBA{
  287. Rect: image.Rect(0, 0, 3, 3),
  288. Stride: 3 * 4,
  289. Pix: []uint8{
  290. 0xb3, 0x00, 0x00, 0x01, 0x00, 0xb3, 0x00, 0x02, 0x00, 0x00, 0xb3, 0x03,
  291. 0x00, 0x09, 0x1a, 0xff, 0x1a, 0x09, 0x00, 0xff, 0x91, 0x1a, 0xa2, 0xff,
  292. 0x00, 0x00, 0x00, 0xff, 0x1a, 0x1a, 0x1a, 0xff, 0xe6, 0xe6, 0xe6, 0xff,
  293. },
  294. },
  295. },
  296. {
  297. "AdjustBrightness 3x3 -100",
  298. &image.NRGBA{
  299. Rect: image.Rect(-1, -1, 2, 2),
  300. Stride: 3 * 4,
  301. Pix: []uint8{
  302. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  303. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  304. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  305. },
  306. },
  307. -100,
  308. &image.NRGBA{
  309. Rect: image.Rect(0, 0, 3, 3),
  310. Stride: 3 * 4,
  311. Pix: []uint8{
  312. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
  313. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
  314. 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
  315. },
  316. },
  317. },
  318. {
  319. "AdjustBrightness 3x3 0",
  320. &image.NRGBA{
  321. Rect: image.Rect(-1, -1, 2, 2),
  322. Stride: 3 * 4,
  323. Pix: []uint8{
  324. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  325. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  326. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  327. },
  328. },
  329. 0,
  330. &image.NRGBA{
  331. Rect: image.Rect(0, 0, 3, 3),
  332. Stride: 3 * 4,
  333. Pix: []uint8{
  334. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  335. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  336. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  337. },
  338. },
  339. },
  340. }
  341. for _, d := range td {
  342. got := AdjustBrightness(d.src, d.p)
  343. want := d.want
  344. if !compareNRGBA(got, want, 0) {
  345. t.Errorf("test [%s] failed: %#v", d.desc, got)
  346. }
  347. }
  348. }
  349. func TestAdjustBrightnessGolden(t *testing.T) {
  350. src, err := Open("testdata/lena_128.png")
  351. if err != nil {
  352. t.Errorf("Open: %v", err)
  353. }
  354. for name, p := range map[string]float64{
  355. "out_brightness_m10.png": -10,
  356. "out_brightness_p10.png": 10,
  357. } {
  358. got := AdjustBrightness(src, p)
  359. want, err := Open("testdata/" + name)
  360. if err != nil {
  361. t.Errorf("Open: %v", err)
  362. }
  363. if !compareNRGBA(got, toNRGBA(want), 0) {
  364. t.Errorf("resulting image differs from golden: %s", name)
  365. }
  366. }
  367. }
  368. func TestAdjustGamma(t *testing.T) {
  369. td := []struct {
  370. desc string
  371. src image.Image
  372. p float64
  373. want *image.NRGBA
  374. }{
  375. {
  376. "AdjustGamma 3x3 0.75",
  377. &image.NRGBA{
  378. Rect: image.Rect(-1, -1, 2, 2),
  379. Stride: 3 * 4,
  380. Pix: []uint8{
  381. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  382. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  383. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  384. },
  385. },
  386. 0.75,
  387. &image.NRGBA{
  388. Rect: image.Rect(0, 0, 3, 3),
  389. Stride: 3 * 4,
  390. Pix: []uint8{
  391. 0xbd, 0x00, 0x00, 0x01, 0x00, 0xbd, 0x00, 0x02, 0x00, 0x00, 0xbd, 0x03,
  392. 0x07, 0x11, 0x1e, 0xff, 0x1e, 0x11, 0x07, 0xff, 0x95, 0x1e, 0xa9, 0xff,
  393. 0x00, 0x00, 0x00, 0xff, 0x1e, 0x1e, 0x1e, 0xff, 0xff, 0xff, 0xff, 0xff,
  394. },
  395. },
  396. },
  397. {
  398. "AdjustGamma 3x3 1.5",
  399. &image.NRGBA{
  400. Rect: image.Rect(-1, -1, 2, 2),
  401. Stride: 3 * 4,
  402. Pix: []uint8{
  403. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  404. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  405. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  406. },
  407. },
  408. 1.5,
  409. &image.NRGBA{
  410. Rect: image.Rect(0, 0, 3, 3),
  411. Stride: 3 * 4,
  412. Pix: []uint8{
  413. 0xdc, 0x00, 0x00, 0x01, 0x00, 0xdc, 0x00, 0x02, 0x00, 0x00, 0xdc, 0x03,
  414. 0x2a, 0x43, 0x57, 0xff, 0x57, 0x43, 0x2a, 0xff, 0xc3, 0x57, 0xcf, 0xff,
  415. 0x00, 0x00, 0x00, 0xff, 0x57, 0x57, 0x57, 0xff, 0xff, 0xff, 0xff, 0xff,
  416. },
  417. },
  418. },
  419. {
  420. "AdjustGamma 3x3 1.0",
  421. &image.NRGBA{
  422. Rect: image.Rect(-1, -1, 2, 2),
  423. Stride: 3 * 4,
  424. Pix: []uint8{
  425. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  426. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  427. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  428. },
  429. },
  430. 1.0,
  431. &image.NRGBA{
  432. Rect: image.Rect(0, 0, 3, 3),
  433. Stride: 3 * 4,
  434. Pix: []uint8{
  435. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  436. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  437. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  438. },
  439. },
  440. },
  441. }
  442. for _, d := range td {
  443. got := AdjustGamma(d.src, d.p)
  444. want := d.want
  445. if !compareNRGBA(got, want, 0) {
  446. t.Errorf("test [%s] failed: %#v", d.desc, got)
  447. }
  448. }
  449. }
  450. func TestAdjustGammaGolden(t *testing.T) {
  451. src, err := Open("testdata/lena_128.png")
  452. if err != nil {
  453. t.Errorf("Open: %v", err)
  454. }
  455. for name, g := range map[string]float64{
  456. "out_gamma_0.75.png": 0.75,
  457. "out_gamma_1.25.png": 1.25,
  458. } {
  459. got := AdjustGamma(src, g)
  460. want, err := Open("testdata/" + name)
  461. if err != nil {
  462. t.Errorf("Open: %v", err)
  463. }
  464. if !compareNRGBA(got, toNRGBA(want), 0) {
  465. t.Errorf("resulting image differs from golden: %s", name)
  466. }
  467. }
  468. }
  469. func TestAdjustSigmoid(t *testing.T) {
  470. td := []struct {
  471. desc string
  472. src image.Image
  473. m float64
  474. p float64
  475. want *image.NRGBA
  476. }{
  477. {
  478. "AdjustSigmoid 3x3 0.5 3.0",
  479. &image.NRGBA{
  480. Rect: image.Rect(-1, -1, 2, 2),
  481. Stride: 3 * 4,
  482. Pix: []uint8{
  483. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  484. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  485. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  486. },
  487. },
  488. 0.5,
  489. 3.0,
  490. &image.NRGBA{
  491. Rect: image.Rect(0, 0, 3, 3),
  492. Stride: 3 * 4,
  493. Pix: []uint8{
  494. 0xd4, 0x00, 0x00, 0x01, 0x00, 0xd4, 0x00, 0x02, 0x00, 0x00, 0xd4, 0x03,
  495. 0x0d, 0x1b, 0x2b, 0xff, 0x2b, 0x1b, 0x0d, 0xff, 0xb1, 0x2b, 0xc3, 0xff,
  496. 0x00, 0x00, 0x00, 0xff, 0x2b, 0x2b, 0x2b, 0xff, 0xff, 0xff, 0xff, 0xff,
  497. },
  498. },
  499. },
  500. {
  501. "AdjustSigmoid 3x3 0.5 -3.0",
  502. &image.NRGBA{
  503. Rect: image.Rect(-1, -1, 2, 2),
  504. Stride: 3 * 4,
  505. Pix: []uint8{
  506. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  507. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  508. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  509. },
  510. },
  511. 0.5,
  512. -3.0,
  513. &image.NRGBA{
  514. Rect: image.Rect(0, 0, 3, 3),
  515. Stride: 3 * 4,
  516. Pix: []uint8{
  517. 0xc4, 0x00, 0x00, 0x01, 0x00, 0xc4, 0x00, 0x02, 0x00, 0x00, 0xc4, 0x03,
  518. 0x16, 0x2a, 0x3b, 0xff, 0x3b, 0x2a, 0x16, 0xff, 0xa4, 0x3b, 0xb3, 0xff,
  519. 0x00, 0x00, 0x00, 0xff, 0x3b, 0x3b, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff,
  520. },
  521. },
  522. },
  523. {
  524. "AdjustSigmoid 3x3 0.5 0.0",
  525. &image.NRGBA{
  526. Rect: image.Rect(-1, -1, 2, 2),
  527. Stride: 3 * 4,
  528. Pix: []uint8{
  529. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  530. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  531. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  532. },
  533. },
  534. 0.5,
  535. 0.0,
  536. &image.NRGBA{
  537. Rect: image.Rect(0, 0, 3, 3),
  538. Stride: 3 * 4,
  539. Pix: []uint8{
  540. 0xcc, 0x00, 0x00, 0x01, 0x00, 0xcc, 0x00, 0x02, 0x00, 0x00, 0xcc, 0x03,
  541. 0x11, 0x22, 0x33, 0xff, 0x33, 0x22, 0x11, 0xff, 0xaa, 0x33, 0xbb, 0xff,
  542. 0x00, 0x00, 0x00, 0xff, 0x33, 0x33, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
  543. },
  544. },
  545. },
  546. }
  547. for _, d := range td {
  548. got := AdjustSigmoid(d.src, d.m, d.p)
  549. want := d.want
  550. if !compareNRGBA(got, want, 0) {
  551. t.Errorf("test [%s] failed: %#v", d.desc, got)
  552. }
  553. }
  554. }