Load loads the frames of the GIF animation.
()
| 34 | |
| 35 | // Load loads the frames of the GIF animation. |
| 36 | func (gifAnim *GifAnimation) Load() { |
| 37 | |
| 38 | for index, img := range gifAnim.Data.Image { |
| 39 | |
| 40 | // After decoding, we have to manually create a new image and plot each frame of the GIF because transparent GIFs |
| 41 | // can only have frames that account for changed pixels (i.e. if you have a 320x240 GIF, but on frame |
| 42 | // 17 only one pixel changes, the image generated for frame 17 will be 1x1 for Bounds.Size()). |
| 43 | |
| 44 | prev := 0 |
| 45 | |
| 46 | disposalMode := byte(gif.DisposalNone) |
| 47 | if index > 0 { |
| 48 | disposalMode = gifAnim.Data.Disposal[index-1] |
| 49 | } |
| 50 | |
| 51 | // empty := sdl.RGBA8888{0, 0, 0, 0} |
| 52 | |
| 53 | w := gifAnim.frameImg.W |
| 54 | h := gifAnim.frameImg.H |
| 55 | |
| 56 | for y := int32(0); y < h; y++ { |
| 57 | |
| 58 | for x := int32(0); x < w; x++ { |
| 59 | |
| 60 | // We clear each pixel of each frame, but only the pixels within the rectangle specified by the frame is plotted below, as |
| 61 | // some frames of GIFs can have a "changed rectangle", indicating which pixels in which rectangle need to actually change. |
| 62 | |
| 63 | if disposalMode == gif.DisposalBackground || disposalMode == gif.DisposalPrevious { |
| 64 | a, r, g, b := gifAnim.Data.Image[prev].At(int(x), int(y)).RGBA() |
| 65 | gifAnim.frameImg.WritePixel(x, y, uint8(r), uint8(g), uint8(b), uint8(a)) |
| 66 | } else if disposalMode == gif.DisposalNone { |
| 67 | r, g, b, a := gifAnim.Data.Image[0].At(int(x), int(y)).RGBA() |
| 68 | gifAnim.frameImg.WritePixel(x, y, uint8(r), uint8(g), uint8(b), uint8(a)) |
| 69 | } |
| 70 | |
| 71 | if disposalMode != gif.DisposalPrevious { |
| 72 | prev = index |
| 73 | } |
| 74 | |
| 75 | color := img.RGBA64At(int(x), int(y)) |
| 76 | |
| 77 | if x >= int32(img.Rect.Min.X) && x < int32(img.Rect.Max.X) && y >= int32(img.Rect.Min.Y) && y < int32(img.Rect.Max.Y) { |
| 78 | |
| 79 | // alpha is 65535 max |
| 80 | if _, _, _, alpha := color.RGBA(); alpha > 128 { |
| 81 | r, g, b, a := color.RGBA() |
| 82 | gifAnim.frameImg.WritePixel(x, y, uint8(r), uint8(g), uint8(b), uint8(a)) |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | newSurf, err := gifAnim.frameImg.Duplicate() |
| 92 | if err != nil { |
| 93 | panic(err) |
no test coverage detected