adjust_test.go 16 KB

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