scale_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package draw
  5. import (
  6. "bytes"
  7. "flag"
  8. "fmt"
  9. "image"
  10. "image/color"
  11. "image/png"
  12. "math/rand"
  13. "os"
  14. "reflect"
  15. "testing"
  16. "golang.org/x/image/math/f64"
  17. _ "image/jpeg"
  18. )
  19. var genGoldenFiles = flag.Bool("gen_golden_files", false, "whether to generate the TestXxx golden files.")
  20. var transformMatrix = func(scale, tx, ty float64) f64.Aff3 {
  21. const cos30, sin30 = 0.866025404, 0.5
  22. return f64.Aff3{
  23. +scale * cos30, -scale * sin30, tx,
  24. +scale * sin30, +scale * cos30, ty,
  25. }
  26. }
  27. func encode(filename string, m image.Image) error {
  28. f, err := os.Create(filename)
  29. if err != nil {
  30. return fmt.Errorf("Create: %v", err)
  31. }
  32. defer f.Close()
  33. if err := png.Encode(f, m); err != nil {
  34. return fmt.Errorf("Encode: %v", err)
  35. }
  36. return nil
  37. }
  38. // testInterp tests that interpolating the source image gives the exact
  39. // destination image. This is to ensure that any refactoring or optimization of
  40. // the interpolation code doesn't change the behavior. Changing the actual
  41. // algorithm or kernel used by any particular quality setting will obviously
  42. // change the resultant pixels. In such a case, use the gen_golden_files flag
  43. // to regenerate the golden files.
  44. func testInterp(t *testing.T, w int, h int, direction, prefix, suffix string) {
  45. f, err := os.Open("../testdata/" + prefix + suffix)
  46. if err != nil {
  47. t.Fatalf("Open: %v", err)
  48. }
  49. defer f.Close()
  50. src, _, err := image.Decode(f)
  51. if err != nil {
  52. t.Fatalf("Decode: %v", err)
  53. }
  54. op, scale := Src, 3.75
  55. if prefix == "tux" {
  56. op, scale = Over, 0.125
  57. }
  58. green := image.NewUniform(color.RGBA{0x00, 0x22, 0x11, 0xff})
  59. testCases := map[string]Interpolator{
  60. "nn": NearestNeighbor,
  61. "ab": ApproxBiLinear,
  62. "bl": BiLinear,
  63. "cr": CatmullRom,
  64. }
  65. for name, q := range testCases {
  66. goldenFilename := fmt.Sprintf("../testdata/%s-%s-%s.png", prefix, direction, name)
  67. got := image.NewRGBA(image.Rect(0, 0, w, h))
  68. Copy(got, image.Point{}, green, got.Bounds(), Src, nil)
  69. if direction == "rotate" {
  70. q.Transform(got, transformMatrix(scale, 40, 10), src, src.Bounds(), op, nil)
  71. } else {
  72. q.Scale(got, got.Bounds(), src, src.Bounds(), op, nil)
  73. }
  74. if *genGoldenFiles {
  75. if err := encode(goldenFilename, got); err != nil {
  76. t.Error(err)
  77. }
  78. continue
  79. }
  80. g, err := os.Open(goldenFilename)
  81. if err != nil {
  82. t.Errorf("Open: %v", err)
  83. continue
  84. }
  85. defer g.Close()
  86. wantRaw, err := png.Decode(g)
  87. if err != nil {
  88. t.Errorf("Decode: %v", err)
  89. continue
  90. }
  91. // convert wantRaw to RGBA.
  92. want, ok := wantRaw.(*image.RGBA)
  93. if !ok {
  94. b := wantRaw.Bounds()
  95. want = image.NewRGBA(b)
  96. Draw(want, b, wantRaw, b.Min, Src)
  97. }
  98. if !reflect.DeepEqual(got, want) {
  99. t.Errorf("%s: actual image differs from golden image", goldenFilename)
  100. continue
  101. }
  102. }
  103. }
  104. func TestScaleDown(t *testing.T) { testInterp(t, 100, 100, "down", "go-turns-two", "-280x360.jpeg") }
  105. func TestScaleUp(t *testing.T) { testInterp(t, 75, 100, "up", "go-turns-two", "-14x18.png") }
  106. func TestTformSrc(t *testing.T) { testInterp(t, 100, 100, "rotate", "go-turns-two", "-14x18.png") }
  107. func TestTformOver(t *testing.T) { testInterp(t, 100, 100, "rotate", "tux", ".png") }
  108. // TestSimpleTransforms tests Scale and Transform calls that simplify to Copy
  109. // or Scale calls.
  110. func TestSimpleTransforms(t *testing.T) {
  111. f, err := os.Open("../testdata/testpattern.png") // A 100x100 image.
  112. if err != nil {
  113. t.Fatalf("Open: %v", err)
  114. }
  115. defer f.Close()
  116. src, _, err := image.Decode(f)
  117. if err != nil {
  118. t.Fatalf("Decode: %v", err)
  119. }
  120. dst0 := image.NewRGBA(image.Rect(0, 0, 120, 150))
  121. dst1 := image.NewRGBA(image.Rect(0, 0, 120, 150))
  122. for _, op := range []string{"scale/copy", "tform/copy", "tform/scale"} {
  123. for _, epsilon := range []float64{0, 1e-50, 1e-1} {
  124. Copy(dst0, image.Point{}, image.Transparent, dst0.Bounds(), Src, nil)
  125. Copy(dst1, image.Point{}, image.Transparent, dst1.Bounds(), Src, nil)
  126. switch op {
  127. case "scale/copy":
  128. dr := image.Rect(10, 30, 10+100, 30+100)
  129. if epsilon > 1e-10 {
  130. dr.Max.X++
  131. }
  132. Copy(dst0, image.Point{10, 30}, src, src.Bounds(), Src, nil)
  133. ApproxBiLinear.Scale(dst1, dr, src, src.Bounds(), Src, nil)
  134. case "tform/copy":
  135. Copy(dst0, image.Point{10, 30}, src, src.Bounds(), Src, nil)
  136. ApproxBiLinear.Transform(dst1, f64.Aff3{
  137. 1, 0 + epsilon, 10,
  138. 0, 1, 30,
  139. }, src, src.Bounds(), Src, nil)
  140. case "tform/scale":
  141. ApproxBiLinear.Scale(dst0, image.Rect(10, 50, 10+50, 50+50), src, src.Bounds(), Src, nil)
  142. ApproxBiLinear.Transform(dst1, f64.Aff3{
  143. 0.5, 0.0 + epsilon, 10,
  144. 0.0, 0.5, 50,
  145. }, src, src.Bounds(), Src, nil)
  146. }
  147. differ := !bytes.Equal(dst0.Pix, dst1.Pix)
  148. if epsilon > 1e-10 {
  149. if !differ {
  150. t.Errorf("%s yielded same pixels, want different pixels: epsilon=%v", op, epsilon)
  151. }
  152. } else {
  153. if differ {
  154. t.Errorf("%s yielded different pixels, want same pixels: epsilon=%v", op, epsilon)
  155. }
  156. }
  157. }
  158. }
  159. }
  160. func BenchmarkSimpleScaleCopy(b *testing.B) {
  161. dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
  162. src := image.NewRGBA(image.Rect(0, 0, 400, 300))
  163. b.ResetTimer()
  164. for i := 0; i < b.N; i++ {
  165. ApproxBiLinear.Scale(dst, image.Rect(10, 20, 10+400, 20+300), src, src.Bounds(), Src, nil)
  166. }
  167. }
  168. func BenchmarkSimpleTransformCopy(b *testing.B) {
  169. dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
  170. src := image.NewRGBA(image.Rect(0, 0, 400, 300))
  171. b.ResetTimer()
  172. for i := 0; i < b.N; i++ {
  173. ApproxBiLinear.Transform(dst, f64.Aff3{
  174. 1, 0, 10,
  175. 0, 1, 20,
  176. }, src, src.Bounds(), Src, nil)
  177. }
  178. }
  179. func BenchmarkSimpleTransformScale(b *testing.B) {
  180. dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
  181. src := image.NewRGBA(image.Rect(0, 0, 400, 300))
  182. b.ResetTimer()
  183. for i := 0; i < b.N; i++ {
  184. ApproxBiLinear.Transform(dst, f64.Aff3{
  185. 0.5, 0.0, 10,
  186. 0.0, 0.5, 20,
  187. }, src, src.Bounds(), Src, nil)
  188. }
  189. }
  190. func TestOps(t *testing.T) {
  191. blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
  192. testCases := map[Op]color.RGBA{
  193. Over: color.RGBA{0x7f, 0x00, 0x80, 0xff},
  194. Src: color.RGBA{0x7f, 0x00, 0x00, 0x7f},
  195. }
  196. for op, want := range testCases {
  197. dst := image.NewRGBA(image.Rect(0, 0, 2, 2))
  198. Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
  199. src := image.NewRGBA(image.Rect(0, 0, 1, 1))
  200. src.SetRGBA(0, 0, color.RGBA{0x7f, 0x00, 0x00, 0x7f})
  201. NearestNeighbor.Scale(dst, dst.Bounds(), src, src.Bounds(), op, nil)
  202. if got := dst.RGBAAt(0, 0); got != want {
  203. t.Errorf("op=%v: got %v, want %v", op, got, want)
  204. }
  205. }
  206. }
  207. // TestNegativeWeights tests that scaling by a kernel that produces negative
  208. // weights, such as the Catmull-Rom kernel, doesn't produce an invalid color
  209. // according to Go's alpha-premultiplied model.
  210. func TestNegativeWeights(t *testing.T) {
  211. check := func(m *image.RGBA) error {
  212. b := m.Bounds()
  213. for y := b.Min.Y; y < b.Max.Y; y++ {
  214. for x := b.Min.X; x < b.Max.X; x++ {
  215. if c := m.RGBAAt(x, y); c.R > c.A || c.G > c.A || c.B > c.A {
  216. return fmt.Errorf("invalid color.RGBA at (%d, %d): %v", x, y, c)
  217. }
  218. }
  219. }
  220. return nil
  221. }
  222. src := image.NewRGBA(image.Rect(0, 0, 16, 16))
  223. for y := 0; y < 16; y++ {
  224. for x := 0; x < 16; x++ {
  225. a := y * 0x11
  226. src.Set(x, y, color.RGBA{
  227. R: uint8(x * 0x11 * a / 0xff),
  228. A: uint8(a),
  229. })
  230. }
  231. }
  232. if err := check(src); err != nil {
  233. t.Fatalf("src image: %v", err)
  234. }
  235. dst := image.NewRGBA(image.Rect(0, 0, 32, 32))
  236. CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), Over, nil)
  237. if err := check(dst); err != nil {
  238. t.Fatalf("dst image: %v", err)
  239. }
  240. }
  241. func fillPix(r *rand.Rand, pixs ...[]byte) {
  242. for _, pix := range pixs {
  243. for i := range pix {
  244. pix[i] = uint8(r.Intn(256))
  245. }
  246. }
  247. }
  248. func TestInterpClipCommute(t *testing.T) {
  249. src := image.NewNRGBA(image.Rect(0, 0, 20, 20))
  250. fillPix(rand.New(rand.NewSource(0)), src.Pix)
  251. outer := image.Rect(1, 1, 8, 5)
  252. inner := image.Rect(2, 3, 6, 5)
  253. qs := []Interpolator{
  254. NearestNeighbor,
  255. ApproxBiLinear,
  256. CatmullRom,
  257. }
  258. for _, transform := range []bool{false, true} {
  259. for _, q := range qs {
  260. dst0 := image.NewRGBA(image.Rect(1, 1, 10, 10))
  261. dst1 := image.NewRGBA(image.Rect(1, 1, 10, 10))
  262. for i := range dst0.Pix {
  263. dst0.Pix[i] = uint8(i / 4)
  264. dst1.Pix[i] = uint8(i / 4)
  265. }
  266. var interp func(dst *image.RGBA)
  267. if transform {
  268. interp = func(dst *image.RGBA) {
  269. q.Transform(dst, transformMatrix(3.75, 2, 1), src, src.Bounds(), Over, nil)
  270. }
  271. } else {
  272. interp = func(dst *image.RGBA) {
  273. q.Scale(dst, outer, src, src.Bounds(), Over, nil)
  274. }
  275. }
  276. // Interpolate then clip.
  277. interp(dst0)
  278. dst0 = dst0.SubImage(inner).(*image.RGBA)
  279. // Clip then interpolate.
  280. dst1 = dst1.SubImage(inner).(*image.RGBA)
  281. interp(dst1)
  282. loop:
  283. for y := inner.Min.Y; y < inner.Max.Y; y++ {
  284. for x := inner.Min.X; x < inner.Max.X; x++ {
  285. if c0, c1 := dst0.RGBAAt(x, y), dst1.RGBAAt(x, y); c0 != c1 {
  286. t.Errorf("q=%T: at (%d, %d): c0=%v, c1=%v", q, x, y, c0, c1)
  287. break loop
  288. }
  289. }
  290. }
  291. }
  292. }
  293. }
  294. // translatedImage is an image m translated by t.
  295. type translatedImage struct {
  296. m image.Image
  297. t image.Point
  298. }
  299. func (t *translatedImage) At(x, y int) color.Color { return t.m.At(x-t.t.X, y-t.t.Y) }
  300. func (t *translatedImage) Bounds() image.Rectangle { return t.m.Bounds().Add(t.t) }
  301. func (t *translatedImage) ColorModel() color.Model { return t.m.ColorModel() }
  302. // TestSrcTranslationInvariance tests that Scale and Transform are invariant
  303. // under src translations. Specifically, when some source pixels are not in the
  304. // bottom-right quadrant of src coordinate space, we consistently round down,
  305. // not round towards zero.
  306. func TestSrcTranslationInvariance(t *testing.T) {
  307. f, err := os.Open("../testdata/testpattern.png")
  308. if err != nil {
  309. t.Fatalf("Open: %v", err)
  310. }
  311. defer f.Close()
  312. src, _, err := image.Decode(f)
  313. if err != nil {
  314. t.Fatalf("Decode: %v", err)
  315. }
  316. sr := image.Rect(2, 3, 16, 12)
  317. if !sr.In(src.Bounds()) {
  318. t.Fatalf("src bounds too small: got %v", src.Bounds())
  319. }
  320. qs := []Interpolator{
  321. NearestNeighbor,
  322. ApproxBiLinear,
  323. CatmullRom,
  324. }
  325. deltas := []image.Point{
  326. {+0, +0},
  327. {+0, +5},
  328. {+0, -5},
  329. {+5, +0},
  330. {-5, +0},
  331. {+8, +8},
  332. {+8, -8},
  333. {-8, +8},
  334. {-8, -8},
  335. }
  336. m00 := transformMatrix(3.75, 0, 0)
  337. for _, transform := range []bool{false, true} {
  338. for _, q := range qs {
  339. want := image.NewRGBA(image.Rect(0, 0, 20, 20))
  340. if transform {
  341. q.Transform(want, m00, src, sr, Over, nil)
  342. } else {
  343. q.Scale(want, want.Bounds(), src, sr, Over, nil)
  344. }
  345. for _, delta := range deltas {
  346. tsrc := &translatedImage{src, delta}
  347. got := image.NewRGBA(image.Rect(0, 0, 20, 20))
  348. if transform {
  349. m := matMul(&m00, &f64.Aff3{
  350. 1, 0, -float64(delta.X),
  351. 0, 1, -float64(delta.Y),
  352. })
  353. q.Transform(got, m, tsrc, sr.Add(delta), Over, nil)
  354. } else {
  355. q.Scale(got, got.Bounds(), tsrc, sr.Add(delta), Over, nil)
  356. }
  357. if !bytes.Equal(got.Pix, want.Pix) {
  358. t.Errorf("pix differ for delta=%v, transform=%t, q=%T", delta, transform, q)
  359. }
  360. }
  361. }
  362. }
  363. }
  364. func TestSrcMask(t *testing.T) {
  365. srcMask := image.NewRGBA(image.Rect(0, 0, 23, 1))
  366. srcMask.SetRGBA(19, 0, color.RGBA{0x00, 0x00, 0x00, 0x7f})
  367. srcMask.SetRGBA(20, 0, color.RGBA{0x00, 0x00, 0x00, 0xff})
  368. srcMask.SetRGBA(21, 0, color.RGBA{0x00, 0x00, 0x00, 0x3f})
  369. srcMask.SetRGBA(22, 0, color.RGBA{0x00, 0x00, 0x00, 0x00})
  370. red := image.NewUniform(color.RGBA{0xff, 0x00, 0x00, 0xff})
  371. blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
  372. dst := image.NewRGBA(image.Rect(0, 0, 6, 1))
  373. Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
  374. NearestNeighbor.Scale(dst, dst.Bounds(), red, image.Rect(0, 0, 3, 1), Over, &Options{
  375. SrcMask: srcMask,
  376. SrcMaskP: image.Point{20, 0},
  377. })
  378. got := [6]color.RGBA{
  379. dst.RGBAAt(0, 0),
  380. dst.RGBAAt(1, 0),
  381. dst.RGBAAt(2, 0),
  382. dst.RGBAAt(3, 0),
  383. dst.RGBAAt(4, 0),
  384. dst.RGBAAt(5, 0),
  385. }
  386. want := [6]color.RGBA{
  387. {0xff, 0x00, 0x00, 0xff},
  388. {0xff, 0x00, 0x00, 0xff},
  389. {0x3f, 0x00, 0xc0, 0xff},
  390. {0x3f, 0x00, 0xc0, 0xff},
  391. {0x00, 0x00, 0xff, 0xff},
  392. {0x00, 0x00, 0xff, 0xff},
  393. }
  394. if got != want {
  395. t.Errorf("\ngot %v\nwant %v", got, want)
  396. }
  397. }
  398. func TestDstMask(t *testing.T) {
  399. dstMask := image.NewRGBA(image.Rect(0, 0, 23, 1))
  400. dstMask.SetRGBA(19, 0, color.RGBA{0x00, 0x00, 0x00, 0x7f})
  401. dstMask.SetRGBA(20, 0, color.RGBA{0x00, 0x00, 0x00, 0xff})
  402. dstMask.SetRGBA(21, 0, color.RGBA{0x00, 0x00, 0x00, 0x3f})
  403. dstMask.SetRGBA(22, 0, color.RGBA{0x00, 0x00, 0x00, 0x00})
  404. red := image.NewRGBA(image.Rect(0, 0, 1, 1))
  405. red.SetRGBA(0, 0, color.RGBA{0xff, 0x00, 0x00, 0xff})
  406. blue := image.NewUniform(color.RGBA{0x00, 0x00, 0xff, 0xff})
  407. qs := []Interpolator{
  408. NearestNeighbor,
  409. ApproxBiLinear,
  410. CatmullRom,
  411. }
  412. for _, q := range qs {
  413. dst := image.NewRGBA(image.Rect(0, 0, 3, 1))
  414. Copy(dst, image.Point{}, blue, dst.Bounds(), Src, nil)
  415. q.Scale(dst, dst.Bounds(), red, red.Bounds(), Over, &Options{
  416. DstMask: dstMask,
  417. DstMaskP: image.Point{20, 0},
  418. })
  419. got := [3]color.RGBA{
  420. dst.RGBAAt(0, 0),
  421. dst.RGBAAt(1, 0),
  422. dst.RGBAAt(2, 0),
  423. }
  424. want := [3]color.RGBA{
  425. {0xff, 0x00, 0x00, 0xff},
  426. {0x3f, 0x00, 0xc0, 0xff},
  427. {0x00, 0x00, 0xff, 0xff},
  428. }
  429. if got != want {
  430. t.Errorf("q=%T:\ngot %v\nwant %v", q, got, want)
  431. }
  432. }
  433. }
  434. func TestRectDstMask(t *testing.T) {
  435. f, err := os.Open("../testdata/testpattern.png")
  436. if err != nil {
  437. t.Fatalf("Open: %v", err)
  438. }
  439. defer f.Close()
  440. src, _, err := image.Decode(f)
  441. if err != nil {
  442. t.Fatalf("Decode: %v", err)
  443. }
  444. m00 := transformMatrix(1, 0, 0)
  445. bounds := image.Rect(0, 0, 50, 50)
  446. dstOutside := image.NewRGBA(bounds)
  447. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  448. for x := bounds.Min.X; x < bounds.Max.X; x++ {
  449. dstOutside.SetRGBA(x, y, color.RGBA{uint8(5 * x), uint8(5 * y), 0x00, 0xff})
  450. }
  451. }
  452. mk := func(q Transformer, dstMask image.Image, dstMaskP image.Point) *image.RGBA {
  453. m := image.NewRGBA(bounds)
  454. Copy(m, bounds.Min, dstOutside, bounds, Src, nil)
  455. q.Transform(m, m00, src, src.Bounds(), Over, &Options{
  456. DstMask: dstMask,
  457. DstMaskP: dstMaskP,
  458. })
  459. return m
  460. }
  461. qs := []Interpolator{
  462. NearestNeighbor,
  463. ApproxBiLinear,
  464. CatmullRom,
  465. }
  466. dstMaskPs := []image.Point{
  467. {0, 0},
  468. {5, 7},
  469. {-3, 0},
  470. }
  471. rect := image.Rect(10, 10, 30, 40)
  472. for _, q := range qs {
  473. for _, dstMaskP := range dstMaskPs {
  474. dstInside := mk(q, nil, image.Point{})
  475. for _, wrap := range []bool{false, true} {
  476. // TODO: replace "rectImage(rect)" with "rect" once Go 1.5 is
  477. // released, where an image.Rectangle implements image.Image.
  478. dstMask := image.Image(rectImage(rect))
  479. if wrap {
  480. dstMask = srcWrapper{dstMask}
  481. }
  482. dst := mk(q, dstMask, dstMaskP)
  483. nError := 0
  484. loop:
  485. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  486. for x := bounds.Min.X; x < bounds.Max.X; x++ {
  487. which := dstOutside
  488. if (image.Point{x, y}).Add(dstMaskP).In(rect) {
  489. which = dstInside
  490. }
  491. if got, want := dst.RGBAAt(x, y), which.RGBAAt(x, y); got != want {
  492. if nError == 10 {
  493. t.Errorf("q=%T dmp=%v wrap=%v: ...and more errors", q, dstMaskP, wrap)
  494. break loop
  495. }
  496. nError++
  497. t.Errorf("q=%T dmp=%v wrap=%v: x=%3d y=%3d: got %v, want %v",
  498. q, dstMaskP, wrap, x, y, got, want)
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. }
  506. // TODO: delete this wrapper type once Go 1.5 is released, where an
  507. // image.Rectangle implements image.Image.
  508. type rectImage image.Rectangle
  509. func (r rectImage) ColorModel() color.Model { return color.Alpha16Model }
  510. func (r rectImage) Bounds() image.Rectangle { return image.Rectangle(r) }
  511. func (r rectImage) At(x, y int) color.Color {
  512. if (image.Point{x, y}).In(image.Rectangle(r)) {
  513. return color.Opaque
  514. }
  515. return color.Transparent
  516. }
  517. // The fooWrapper types wrap the dst or src image to avoid triggering the
  518. // type-specific fast path implementations.
  519. type (
  520. dstWrapper struct{ Image }
  521. srcWrapper struct{ image.Image }
  522. )
  523. func srcGray(boundsHint image.Rectangle) (image.Image, error) {
  524. m := image.NewGray(boundsHint)
  525. fillPix(rand.New(rand.NewSource(0)), m.Pix)
  526. return m, nil
  527. }
  528. func srcNRGBA(boundsHint image.Rectangle) (image.Image, error) {
  529. m := image.NewNRGBA(boundsHint)
  530. fillPix(rand.New(rand.NewSource(1)), m.Pix)
  531. return m, nil
  532. }
  533. func srcRGBA(boundsHint image.Rectangle) (image.Image, error) {
  534. m := image.NewRGBA(boundsHint)
  535. fillPix(rand.New(rand.NewSource(2)), m.Pix)
  536. // RGBA is alpha-premultiplied, so the R, G and B values should
  537. // be <= the A values.
  538. for i := 0; i < len(m.Pix); i += 4 {
  539. m.Pix[i+0] = uint8(uint32(m.Pix[i+0]) * uint32(m.Pix[i+3]) / 0xff)
  540. m.Pix[i+1] = uint8(uint32(m.Pix[i+1]) * uint32(m.Pix[i+3]) / 0xff)
  541. m.Pix[i+2] = uint8(uint32(m.Pix[i+2]) * uint32(m.Pix[i+3]) / 0xff)
  542. }
  543. return m, nil
  544. }
  545. func srcUnif(boundsHint image.Rectangle) (image.Image, error) {
  546. return image.NewUniform(color.RGBA64{0x1234, 0x5555, 0x9181, 0xbeef}), nil
  547. }
  548. func srcYCbCr(boundsHint image.Rectangle) (image.Image, error) {
  549. m := image.NewYCbCr(boundsHint, image.YCbCrSubsampleRatio420)
  550. fillPix(rand.New(rand.NewSource(3)), m.Y, m.Cb, m.Cr)
  551. return m, nil
  552. }
  553. func srcLarge(boundsHint image.Rectangle) (image.Image, error) {
  554. // 3072 x 2304 is over 7 million pixels at 4:3, comparable to a
  555. // 2015 smart-phone camera's output.
  556. return srcYCbCr(image.Rect(0, 0, 3072, 2304))
  557. }
  558. func srcTux(boundsHint image.Rectangle) (image.Image, error) {
  559. // tux.png is a 386 x 395 image.
  560. f, err := os.Open("../testdata/tux.png")
  561. if err != nil {
  562. return nil, fmt.Errorf("Open: %v", err)
  563. }
  564. defer f.Close()
  565. src, err := png.Decode(f)
  566. if err != nil {
  567. return nil, fmt.Errorf("Decode: %v", err)
  568. }
  569. return src, nil
  570. }
  571. func benchScale(b *testing.B, w int, h int, op Op, srcf func(image.Rectangle) (image.Image, error), q Interpolator) {
  572. dst := image.NewRGBA(image.Rect(0, 0, w, h))
  573. src, err := srcf(image.Rect(0, 0, 1024, 768))
  574. if err != nil {
  575. b.Fatal(err)
  576. }
  577. dr, sr := dst.Bounds(), src.Bounds()
  578. scaler := Scaler(q)
  579. if n, ok := q.(interface {
  580. NewScaler(int, int, int, int) Scaler
  581. }); ok {
  582. scaler = n.NewScaler(dr.Dx(), dr.Dy(), sr.Dx(), sr.Dy())
  583. }
  584. b.ReportAllocs()
  585. b.ResetTimer()
  586. for i := 0; i < b.N; i++ {
  587. scaler.Scale(dst, dr, src, sr, op, nil)
  588. }
  589. }
  590. func benchTform(b *testing.B, w int, h int, op Op, srcf func(image.Rectangle) (image.Image, error), q Interpolator) {
  591. dst := image.NewRGBA(image.Rect(0, 0, w, h))
  592. src, err := srcf(image.Rect(0, 0, 1024, 768))
  593. if err != nil {
  594. b.Fatal(err)
  595. }
  596. sr := src.Bounds()
  597. m := transformMatrix(3.75, 40, 10)
  598. b.ReportAllocs()
  599. b.ResetTimer()
  600. for i := 0; i < b.N; i++ {
  601. q.Transform(dst, m, src, sr, op, nil)
  602. }
  603. }
  604. func BenchmarkScaleNNLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, NearestNeighbor) }
  605. func BenchmarkScaleABLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, ApproxBiLinear) }
  606. func BenchmarkScaleBLLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, BiLinear) }
  607. func BenchmarkScaleCRLargeDown(b *testing.B) { benchScale(b, 200, 150, Src, srcLarge, CatmullRom) }
  608. func BenchmarkScaleNNDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, NearestNeighbor) }
  609. func BenchmarkScaleABDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, ApproxBiLinear) }
  610. func BenchmarkScaleBLDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, BiLinear) }
  611. func BenchmarkScaleCRDown(b *testing.B) { benchScale(b, 120, 80, Src, srcTux, CatmullRom) }
  612. func BenchmarkScaleNNUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, NearestNeighbor) }
  613. func BenchmarkScaleABUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, ApproxBiLinear) }
  614. func BenchmarkScaleBLUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, BiLinear) }
  615. func BenchmarkScaleCRUp(b *testing.B) { benchScale(b, 800, 600, Src, srcTux, CatmullRom) }
  616. func BenchmarkScaleNNSrcRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcRGBA, NearestNeighbor) }
  617. func BenchmarkScaleNNSrcUnif(b *testing.B) { benchScale(b, 200, 150, Src, srcUnif, NearestNeighbor) }
  618. func BenchmarkScaleNNOverRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcRGBA, NearestNeighbor) }
  619. func BenchmarkScaleNNOverUnif(b *testing.B) { benchScale(b, 200, 150, Over, srcUnif, NearestNeighbor) }
  620. func BenchmarkTformNNSrcRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcRGBA, NearestNeighbor) }
  621. func BenchmarkTformNNSrcUnif(b *testing.B) { benchTform(b, 200, 150, Src, srcUnif, NearestNeighbor) }
  622. func BenchmarkTformNNOverRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcRGBA, NearestNeighbor) }
  623. func BenchmarkTformNNOverUnif(b *testing.B) { benchTform(b, 200, 150, Over, srcUnif, NearestNeighbor) }
  624. func BenchmarkScaleABSrcGray(b *testing.B) { benchScale(b, 200, 150, Src, srcGray, ApproxBiLinear) }
  625. func BenchmarkScaleABSrcNRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcNRGBA, ApproxBiLinear) }
  626. func BenchmarkScaleABSrcRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcRGBA, ApproxBiLinear) }
  627. func BenchmarkScaleABSrcYCbCr(b *testing.B) { benchScale(b, 200, 150, Src, srcYCbCr, ApproxBiLinear) }
  628. func BenchmarkScaleABOverGray(b *testing.B) { benchScale(b, 200, 150, Over, srcGray, ApproxBiLinear) }
  629. func BenchmarkScaleABOverNRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcNRGBA, ApproxBiLinear) }
  630. func BenchmarkScaleABOverRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcRGBA, ApproxBiLinear) }
  631. func BenchmarkScaleABOverYCbCr(b *testing.B) { benchScale(b, 200, 150, Over, srcYCbCr, ApproxBiLinear) }
  632. func BenchmarkTformABSrcGray(b *testing.B) { benchTform(b, 200, 150, Src, srcGray, ApproxBiLinear) }
  633. func BenchmarkTformABSrcNRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcNRGBA, ApproxBiLinear) }
  634. func BenchmarkTformABSrcRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcRGBA, ApproxBiLinear) }
  635. func BenchmarkTformABSrcYCbCr(b *testing.B) { benchTform(b, 200, 150, Src, srcYCbCr, ApproxBiLinear) }
  636. func BenchmarkTformABOverGray(b *testing.B) { benchTform(b, 200, 150, Over, srcGray, ApproxBiLinear) }
  637. func BenchmarkTformABOverNRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcNRGBA, ApproxBiLinear) }
  638. func BenchmarkTformABOverRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcRGBA, ApproxBiLinear) }
  639. func BenchmarkTformABOverYCbCr(b *testing.B) { benchTform(b, 200, 150, Over, srcYCbCr, ApproxBiLinear) }
  640. func BenchmarkScaleCRSrcGray(b *testing.B) { benchScale(b, 200, 150, Src, srcGray, CatmullRom) }
  641. func BenchmarkScaleCRSrcNRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcNRGBA, CatmullRom) }
  642. func BenchmarkScaleCRSrcRGBA(b *testing.B) { benchScale(b, 200, 150, Src, srcRGBA, CatmullRom) }
  643. func BenchmarkScaleCRSrcYCbCr(b *testing.B) { benchScale(b, 200, 150, Src, srcYCbCr, CatmullRom) }
  644. func BenchmarkScaleCROverGray(b *testing.B) { benchScale(b, 200, 150, Over, srcGray, CatmullRom) }
  645. func BenchmarkScaleCROverNRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcNRGBA, CatmullRom) }
  646. func BenchmarkScaleCROverRGBA(b *testing.B) { benchScale(b, 200, 150, Over, srcRGBA, CatmullRom) }
  647. func BenchmarkScaleCROverYCbCr(b *testing.B) { benchScale(b, 200, 150, Over, srcYCbCr, CatmullRom) }
  648. func BenchmarkTformCRSrcGray(b *testing.B) { benchTform(b, 200, 150, Src, srcGray, CatmullRom) }
  649. func BenchmarkTformCRSrcNRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcNRGBA, CatmullRom) }
  650. func BenchmarkTformCRSrcRGBA(b *testing.B) { benchTform(b, 200, 150, Src, srcRGBA, CatmullRom) }
  651. func BenchmarkTformCRSrcYCbCr(b *testing.B) { benchTform(b, 200, 150, Src, srcYCbCr, CatmullRom) }
  652. func BenchmarkTformCROverGray(b *testing.B) { benchTform(b, 200, 150, Over, srcGray, CatmullRom) }
  653. func BenchmarkTformCROverNRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcNRGBA, CatmullRom) }
  654. func BenchmarkTformCROverRGBA(b *testing.B) { benchTform(b, 200, 150, Over, srcRGBA, CatmullRom) }
  655. func BenchmarkTformCROverYCbCr(b *testing.B) { benchTform(b, 200, 150, Over, srcYCbCr, CatmullRom) }