improve fastgif

This commit is contained in:
Hill Ma 2022-11-10 20:39:41 -08:00
parent fec812bc32
commit bf7e7bfb2c
1 changed files with 23 additions and 9 deletions

32
wrp.go
View File

@ -283,7 +283,7 @@ func (rq *wrpReq) capture() {
return return
} }
seq := rand.Intn(9999) seq := rand.Intn(9999)
imgpath := fmt.Sprintf("/img/%04d.%s", seq, rq.imgType) var imgpath string
mappath := fmt.Sprintf("/map/%04d.map", seq) mappath := fmt.Sprintf("/map/%04d.map", seq)
ismap[mappath] = *rq ismap[mappath] = *rq
var ssize string var ssize string
@ -292,6 +292,7 @@ func (rq *wrpReq) capture() {
case "fastgif": case "fastgif":
fallthrough fallthrough
case "gif": case "gif":
imgpath = fmt.Sprintf("/img/%04d.gif", seq)
i, err := png.Decode(bytes.NewReader(pngcap)) i, err := png.Decode(bytes.NewReader(pngcap))
if err != nil { if err != nil {
log.Printf("%s Failed to decode screenshot: %s\n", rq.r.RemoteAddr, err) log.Printf("%s Failed to decode screenshot: %s\n", rq.r.RemoteAddr, err)
@ -309,14 +310,26 @@ func (rq *wrpReq) capture() {
r := i.Bounds() r := i.Bounds()
// NOTE: the color index computation below works only for palette.WebSafe! // NOTE: the color index computation below works only for palette.WebSafe!
p = image.NewPaletted(r, palette.WebSafe) p = image.NewPaletted(r, palette.WebSafe)
for y := 0; y != r.Dy(); y++ { if i64, ok := i.(image.RGBA64Image); ok {
for x := 0; x != r.Dx(); x++ { for y := r.Min.Y; y < r.Max.Y; y++ {
c := i.At(x, y) for x := r.Min.X; x < r.Max.X; x++ {
r, g, b, _ := c.RGBA() c := i64.RGBA64At(x, y)
r6 := FastGifLut[r&0xff] r6 := FastGifLut[c.R>>8]
g6 := FastGifLut[g&0xff] g6 := FastGifLut[c.G>>8]
b6 := FastGifLut[b&0xff] b6 := FastGifLut[c.B>>8]
p.SetColorIndex(x, y, uint8(36*r6+6*g6+b6)) p.SetColorIndex(x, y, uint8(36*r6+6*g6+b6))
}
}
} else {
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
c := i.At(x, y)
r, g, b, _ := c.RGBA()
r6 := FastGifLut[r&0xff]
g6 := FastGifLut[g&0xff]
b6 := FastGifLut[b&0xff]
p.SetColorIndex(x, y, uint8(36*r6+6*g6+b6))
}
} }
} }
} else { } else {
@ -335,6 +348,7 @@ func (rq *wrpReq) capture() {
ih = i.Bounds().Max.Y ih = i.Bounds().Max.Y
log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgpath, ssize, rq.colors, iw, ih, time.Since(st).Milliseconds()) log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgpath, ssize, rq.colors, iw, ih, time.Since(st).Milliseconds())
case "png": case "png":
imgpath = fmt.Sprintf("/img/%04d.png", seq)
pngbuf := bytes.NewBuffer(pngcap) pngbuf := bytes.NewBuffer(pngcap)
img[imgpath] = *pngbuf img[imgpath] = *pngbuf
cfg, _, _ := image.DecodeConfig(pngbuf) cfg, _, _ := image.DecodeConfig(pngbuf)