From fec812bc326eea6eda30a57f6feee90728926d05 Mon Sep 17 00:00:00 2001 From: Hill Ma Date: Wed, 9 Nov 2022 21:29:58 -0800 Subject: [PATCH] Add "fastgif" image type that provides much faster (but lower quality) GIF quantization. The default image type is changed to PNG as it is the most "trouble free" choice. --- fastgif_lut.go | 260 +++++++++++++++++++++++++++++++++++++++++++++++++ wrp.go | 26 ++++- wrp.html | 1 + 3 files changed, 284 insertions(+), 3 deletions(-) create mode 100644 fastgif_lut.go diff --git a/fastgif_lut.go b/fastgif_lut.go new file mode 100644 index 0000000..9cd4b11 --- /dev/null +++ b/fastgif_lut.go @@ -0,0 +1,260 @@ +package main + +var FastGifLut = [256]int{ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, +} diff --git a/wrp.go b/wrp.go index 49d1931..85be069 100644 --- a/wrp.go +++ b/wrp.go @@ -15,6 +15,7 @@ import ( "fmt" "html/template" "image" + "image/color/palette" "image/gif" "image/png" "io/ioutil" @@ -129,7 +130,7 @@ func (rq *wrpReq) parseForm() { rq.keys = rq.r.FormValue("k") rq.buttons = rq.r.FormValue("Fn") rq.imgType = rq.r.FormValue("t") - if rq.imgType != "gif" && rq.imgType != "png" { + if rq.imgType != "fastgif" && rq.imgType != "gif" && rq.imgType != "png" { rq.imgType = defType } log.Printf("%s WrpReq from UI Form: %+v\n", rq.r.RemoteAddr, rq) @@ -288,6 +289,8 @@ func (rq *wrpReq) capture() { var ssize string var iw, ih int switch rq.imgType { + case "fastgif": + fallthrough case "gif": i, err := png.Decode(bytes.NewReader(pngcap)) if err != nil { @@ -301,8 +304,25 @@ func (rq *wrpReq) capture() { } var gifbuf bytes.Buffer st := time.Now() - q := median.Quantizer(rq.colors) - p := q.Paletted(i) + var p *image.Paletted + if rq.imgType == "fastgif" { + r := i.Bounds() + // NOTE: the color index computation below works only for palette.WebSafe! + p = image.NewPaletted(r, palette.WebSafe) + for y := 0; y != r.Dy(); y++ { + for x := 0; x != r.Dx(); 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 { + q := median.Quantizer(rq.colors) + p = q.Paletted(i) + } err = gif.Encode(&gifbuf, p, &gif.Options{}) if err != nil { log.Printf("%s Failed to encode GIF: %s\n", rq.r.RemoteAddr, err) diff --git a/wrp.html b/wrp.html index 58b4094..cbd72e3 100644 --- a/wrp.html +++ b/wrp.html @@ -21,6 +21,7 @@ T