Skip to content

Commit

Permalink
fix(drawlots): draw gif noise
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed May 2, 2024
1 parent c29b123 commit 68fee35
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions plugin/drawlots/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"bytes"
"errors"
"image"
"image/color"
"image/draw"
"image/gif"
"io"
"math/rand"
"os"
"strconv"
Expand Down Expand Up @@ -235,20 +236,41 @@ func randGif(gifName string) (image.Image, error) {
return nil, err
}
im, err := gif.DecodeAll(file)
_ = file.Close()
if err != nil {
return nil, err
}
/*
firstImg, err := imgfactory.Load(name)
if err != nil {
return nil, err
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
config, err := gif.DecodeConfig(file)
if err != nil {
return nil, err
}
_ = file.Close()
// https://zhuanlan.zhihu.com/p/27718135
rect := image.Rect(0, 0, config.Width, config.Height)
if rect.Min == rect.Max {
var max image.Point
for _, frame := range im.Image {
maxF := frame.Bounds().Max
if max.X < maxF.X {
max.X = maxF.X
}
if max.Y < maxF.Y {
max.Y = maxF.Y
}
}
v := im.Image[rand.Intn(len(im.Image))]
return imgfactory.Size(firstImg, firstImg.Bounds().Dx(), firstImg.Bounds().Dy()).InsertUpC(v, 0, 0, firstImg.Bounds().Dx()/2, firstImg.Bounds().Dy()/2).Clone().Image(),err
/*/
// 如果gif图片出现信息缺失请使用上面注释掉的代码,把下面注释了(上面代码部分图存在bug)
v := im.Image[rand.Intn(len(im.Image))]
return imgfactory.NewFactoryBG(v.Rect.Dx(), v.Rect.Dy(), color.NRGBA{0, 0, 0, 255}).InsertUp(v, 0, 0, 0, 0).Clone().Image(), err
// */
rect.Max = max
}
img := image.NewRGBA(rect)
b := rand.Intn(len(im.Image)) + 1
a := 0
if b > 8 {
a = b - 8
}
for _, srcimg := range im.Image[a:b] {
draw.Draw(img, srcimg.Bounds(), srcimg, srcimg.Rect.Min, draw.Src)
}
return img, err
}

0 comments on commit 68fee35

Please sign in to comment.