main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "image"
  18. _ "image/jpeg"
  19. "log"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  22. )
  23. const (
  24. screenWidth = 640
  25. screenHeight = 480
  26. )
  27. var (
  28. gophersImage *ebiten.Image
  29. )
  30. type Game struct{}
  31. func (g *Game) Update() error {
  32. return nil
  33. }
  34. func (g *Game) Draw(screen *ebiten.Image) {
  35. op := &ebiten.DrawImageOptions{}
  36. op.GeoM.Translate(0, 0)
  37. screen.DrawImage(gophersImage, op)
  38. // Box blur (7x7)
  39. // https://en.wikipedia.org/wiki/Box_blur
  40. //
  41. // Note that this is a fixed function implementation of a box blur - more
  42. // efficiency can be gained by using a separable blur
  43. // (blurring horizontally and vertically separately, or for large blurs,
  44. // even multiple horizontal or vertical passes), ideally combined with
  45. // doing the summing up in a fragment shader (Kage can be used here).
  46. //
  47. // So this implementation only serves to demonstrate use of alpha blending.
  48. layers := 0
  49. for j := -3; j <= 3; j++ {
  50. for i := -3; i <= 3; i++ {
  51. op := &ebiten.DrawImageOptions{}
  52. op.GeoM.Translate(float64(i), 244+float64(j))
  53. // This is a blur based on the source-over blend mode,
  54. // which is basically (GL_ONE, GL_ONE_MINUS_SRC_ALPHA). ColorM acts
  55. // on unpremultiplied colors, but all Ebitengine internal colors are
  56. // premultiplied, meaning this mode is regular alpha blending,
  57. // computing each destination pixel as srcPix * alpha + dstPix * (1 - alpha).
  58. //
  59. // This means that the final color is affected by the destination color when BlendSourceOver is used.
  60. // This blend mode is the default mode. See how this is calculated at the doc:
  61. // https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#Blend
  62. //
  63. // So if using the same alpha every time, the end result will sure be biased towards the last layer.
  64. //
  65. // Correct averaging works based on
  66. // Let A_n := (a_1 + ... + a_n) / n
  67. // A_{n+1} = (a_1 + ... + a_{n+1}) / (n + 1)
  68. // A_{n+1} = (n * A_n + a_{n+1)) / (n + 1)
  69. // A_{n+1} = A_n * (1 - 1/(n+1)) + a_{n+1} * 1/(n+1)
  70. // which is precisely what an alpha blend with alpha 1/(n+1) does.
  71. layers++
  72. op.ColorScale.ScaleAlpha(1 / float32(layers))
  73. screen.DrawImage(gophersImage, op)
  74. }
  75. }
  76. }
  77. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  78. return screenWidth, screenHeight
  79. }
  80. func main() {
  81. // Decode an image from the image file's byte slice.
  82. img, _, err := image.Decode(bytes.NewReader(images.FiveYears_jpg))
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. gophersImage = ebiten.NewImageFromImage(img)
  87. ebiten.SetWindowSize(screenWidth, screenHeight)
  88. ebiten.SetWindowTitle("Blur (Ebitengine Demo)")
  89. if err := ebiten.RunGame(&Game{}); err != nil {
  90. log.Fatal(err)
  91. }
  92. }