jpeg quality via form value etc

This commit is contained in:
Antoni Sawicki 2024-07-09 01:45:03 -07:00
parent eb38499280
commit 0d998af68c
4 changed files with 48 additions and 35 deletions

8
cdp.go
View File

@ -192,7 +192,7 @@ func (rq *wrpReq) captureScreenshot() {
} }
st := time.Now() st := time.Now()
var gifBuf bytes.Buffer var gifBuf bytes.Buffer
err = gif.Encode(&gifBuf, gifPalette(i, rq.imgOpt), &gif.Options{}) err = gif.Encode(&gifBuf, gifPalette(i, rq.nColors), &gif.Options{})
if err != nil { if err != nil {
log.Printf("%s Failed to encode GIF: %s\n", rq.r.RemoteAddr, err) log.Printf("%s Failed to encode GIF: %s\n", rq.r.RemoteAddr, err)
fmt.Fprintf(rq.w, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err) fmt.Fprintf(rq.w, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
@ -202,7 +202,7 @@ func (rq *wrpReq) captureScreenshot() {
sSize = fmt.Sprintf("%.0f KB", float32(len(gifBuf.Bytes()))/1024.0) sSize = fmt.Sprintf("%.0f KB", float32(len(gifBuf.Bytes()))/1024.0)
iW = i.Bounds().Max.X iW = i.Bounds().Max.X
iH = i.Bounds().Max.Y iH = i.Bounds().Max.Y
log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, rq.imgOpt, iW, iH, time.Since(st).Milliseconds()) log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, rq.nColors, iW, iH, time.Since(st).Milliseconds())
case "jpg": case "jpg":
i, err := png.Decode(bytes.NewReader(pngCap)) i, err := png.Decode(bytes.NewReader(pngCap))
if err != nil { if err != nil {
@ -212,7 +212,7 @@ func (rq *wrpReq) captureScreenshot() {
} }
st := time.Now() st := time.Now()
var jpgBuf bytes.Buffer var jpgBuf bytes.Buffer
err = jpeg.Encode(&jpgBuf, i, &jpeg.Options{Quality: *jpgQual}) err = jpeg.Encode(&jpgBuf, i, &jpeg.Options{Quality: int(rq.jQual)})
if err != nil { if err != nil {
log.Printf("%s Failed to encode JPG: %s\n", rq.r.RemoteAddr, err) log.Printf("%s Failed to encode JPG: %s\n", rq.r.RemoteAddr, err)
fmt.Fprintf(rq.w, "<BR>Unable to encode JPG:<BR>%s<BR>\n", err) fmt.Fprintf(rq.w, "<BR>Unable to encode JPG:<BR>%s<BR>\n", err)
@ -222,7 +222,7 @@ func (rq *wrpReq) captureScreenshot() {
sSize = fmt.Sprintf("%.0f KB", float32(len(jpgBuf.Bytes()))/1024.0) sSize = fmt.Sprintf("%.0f KB", float32(len(jpgBuf.Bytes()))/1024.0)
iW = i.Bounds().Max.X iW = i.Bounds().Max.X
iH = i.Bounds().Max.Y iH = i.Bounds().Max.Y
log.Printf("%s Encoded JPG image: %s, Size: %s, Quality: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, *jpgQual, iW, iH, time.Since(st).Milliseconds()) log.Printf("%s Encoded JPG image: %s, Size: %s, Quality: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, *defJpgQual, iW, iH, time.Since(st).Milliseconds())
} }
rq.printUI(uiParams{ rq.printUI(uiParams{
bgColor: fmt.Sprintf("#%02X%02X%02X", r, g, b), bgColor: fmt.Sprintf("#%02X%02X%02X", r, g, b),

11
txt.go
View File

@ -14,7 +14,7 @@ package main
// reproduces on vsi vms docs // reproduces on vsi vms docs
// - BUG: markdown table errors // - BUG: markdown table errors
// reproduces on hacker news // reproduces on hacker news
// - BUG: captcha errors using html to markdown, perhaps use cdp inner html // - BUG: captcha errors using html to markdown, perhaps use cdp inner html + downloaded images
// reproduces on https://www.cnn.com/cnn-underscored/electronics // reproduces on https://www.cnn.com/cnn-underscored/electronics
import ( import (
@ -196,7 +196,14 @@ func (rq *wrpReq) captureMarkdown() {
return return
} }
log.Printf("Got %v bytes md from %v", len(md), rq.url) log.Printf("Got %v bytes md from %v", len(md), rq.url)
t := &astTransformer{imgType: rq.imgType, maxSize: int(rq.maxSize), imgOpt: int(rq.imgOpt)} // TODO: maxSize still doesn't work var imgOpt int
switch rq.imgType {
case "jpg":
imgOpt = int(rq.jQual)
case "gif":
imgOpt = int(rq.nColors)
}
t := &astTransformer{imgType: rq.imgType, maxSize: int(rq.maxSize), imgOpt: imgOpt}
gm := goldmark.New( gm := goldmark.New(
goldmark.WithExtensions(extension.GFM), goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(parser.WithASTTransformers(util.Prioritized(t, 100))), goldmark.WithParserOptions(parser.WithASTTransformers(util.Prioritized(t, 100))),

59
wrp.go
View File

@ -36,7 +36,7 @@ var (
defType = flag.String("t", "gif", "Image type: png|gif|jpg") defType = flag.String("t", "gif", "Image type: png|gif|jpg")
wrpMode = flag.String("m", "ismap", "WRP Mode: ismap|html") wrpMode = flag.String("m", "ismap", "WRP Mode: ismap|html")
defImgSize = flag.Int64("is", 200, "html mode default image size") defImgSize = flag.Int64("is", 200, "html mode default image size")
jpgQual = flag.Int("q", 75, "Jpeg image quality, default 75%") // TODO: this should be form dropdown when jpeg is selected as image type defJpgQual = flag.Int64("q", 75, "Jpeg image quality, default 75%") // TODO: this should be form dropdown when jpeg is selected as image type
fgeom = flag.String("g", "1152x600x216", "Geometry: width x height x colors, height can be 0 for unlimited") fgeom = flag.String("g", "1152x600x216", "Geometry: width x height x colors, height can be 0 for unlimited")
htmFnam = flag.String("ui", "wrp.html", "HTML template file for the UI") htmFnam = flag.String("ui", "wrp.html", "HTML template file for the UI")
delay = flag.Duration("s", 2*time.Second, "Delay/sleep after page is rendered and before screenshot is taken") delay = flag.Duration("s", 2*time.Second, "Delay/sleep after page is rendered and before screenshot is taken")
@ -72,6 +72,7 @@ type uiData struct {
URL string URL string
BgColor string BgColor string
NColors int64 NColors int64
JQual int64
Width int64 Width int64
Height int64 Height int64
Zoom float64 Zoom float64
@ -100,19 +101,19 @@ type uiParams struct {
// WRP Request // WRP Request
type wrpReq struct { type wrpReq struct {
url string // url url string
width int64 // width width int64
height int64 // height height int64
zoom float64 // zoom/scale zoom float64
colors int64 // #colors nColors int64
mouseX int64 // mouseX jQual int64
mouseY int64 // mouseY mouseX int64
keys string // keys to send mouseY int64
buttons string // Fn buttons keys string
imgType string // imgtype buttons string
wrpMode string // mode ismap/html imgType string
maxSize int64 // image max size for html mode wrpMode string
imgOpt int64 maxSize int64
w http.ResponseWriter w http.ResponseWriter
r *http.Request r *http.Request
} }
@ -138,9 +139,19 @@ func (rq *wrpReq) parseForm() {
if rq.zoom < 0.1 { if rq.zoom < 0.1 {
rq.zoom = 1.0 rq.zoom = 1.0
} }
rq.colors, _ = strconv.ParseInt(rq.r.FormValue("c"), 10, 64) // TODO: this needs to be jpeg quality as well rq.imgType = rq.r.FormValue("t")
if rq.colors < 2 || rq.colors > 256 { // ... but maybe not because of this? switch rq.imgType {
rq.colors = defGeom.c case "png", "gif", "jpg":
default:
rq.imgType = *defType
}
rq.nColors, _ = strconv.ParseInt(rq.r.FormValue("c"), 10, 64)
if rq.nColors < 2 || rq.nColors > 256 {
rq.nColors = defGeom.c
}
rq.jQual, _ = strconv.ParseInt(rq.r.FormValue("q"), 10, 64)
if rq.jQual < 1 || rq.jQual > 100 {
rq.jQual = *defJpgQual
} }
rq.keys = rq.r.FormValue("k") rq.keys = rq.r.FormValue("k")
rq.buttons = rq.r.FormValue("Fn") rq.buttons = rq.r.FormValue("Fn")
@ -148,17 +159,6 @@ func (rq *wrpReq) parseForm() {
if rq.maxSize == 0 { if rq.maxSize == 0 {
rq.maxSize = *defImgSize rq.maxSize = *defImgSize
} }
rq.imgType = rq.r.FormValue("t")
switch rq.imgType {
case "png":
case "gif":
rq.imgOpt = defGeom.c
case "jpg":
rq.imgOpt = int64(*jpgQual)
default:
rq.imgType = *defType
rq.imgOpt = 80 // TODO: fixme, this needs to be different based on image type
}
log.Printf("%s WrpReq from UI Form: %+v\n", rq.r.RemoteAddr, rq) log.Printf("%s WrpReq from UI Form: %+v\n", rq.r.RemoteAddr, rq)
} }
@ -177,7 +177,8 @@ func (rq *wrpReq) printUI(p uiParams) {
BgColor: p.bgColor, BgColor: p.bgColor,
Width: rq.width, Width: rq.width,
Height: rq.height, Height: rq.height,
NColors: rq.colors, // TODO: this needs to be also jpeg quality NColors: rq.nColors,
JQual: rq.jQual,
Zoom: rq.zoom, Zoom: rq.zoom,
MaxSize: rq.maxSize, MaxSize: rq.maxSize,
ImgType: rq.imgType, ImgType: rq.imgType,

View File

@ -41,6 +41,7 @@
<OPTION VALUE="gif" {{ if eq .ImgType "gif"}}SELECTED{{end}}>GIF</OPTION> <OPTION VALUE="gif" {{ if eq .ImgType "gif"}}SELECTED{{end}}>GIF</OPTION>
<OPTION VALUE="jpg" {{ if eq .ImgType "jpg"}}SELECTED{{end}}>JPG</OPTION> <OPTION VALUE="jpg" {{ if eq .ImgType "jpg"}}SELECTED{{end}}>JPG</OPTION>
</SELECT> </SELECT>
{{ if eq .ImgType "gif" }}
C <SELECT NAME="c"> C <SELECT NAME="c">
<OPTION DISABLED>Ncol</OPTION> <OPTION DISABLED>Ncol</OPTION>
<OPTION VALUE="256" {{ if eq .NColors 256}}SELECTED{{end}}>256</OPTION> <OPTION VALUE="256" {{ if eq .NColors 256}}SELECTED{{end}}>256</OPTION>
@ -50,6 +51,10 @@
<OPTION VALUE="16" {{ if eq .NColors 16}}SELECTED{{end}}>16</OPTION> <OPTION VALUE="16" {{ if eq .NColors 16}}SELECTED{{end}}>16</OPTION>
<OPTION VALUE="2" {{ if eq .NColors 2}}SELECTED{{end}}>2</OPTION> <OPTION VALUE="2" {{ if eq .NColors 2}}SELECTED{{end}}>2</OPTION>
</SELECT> </SELECT>
{{ end }}
{{ if eq .ImgType "jpg" }}
Q <INPUT TYPE="TEXT" NAME="q" VALUE="{{.JQual}}" SIZE="2">%
{{ end }}
{{ if eq .WrpMode "ismap" }} {{ if eq .WrpMode "ismap" }}
K <INPUT TYPE="TEXT" NAME="k" VALUE="" SIZE="4"> K <INPUT TYPE="TEXT" NAME="k" VALUE="" SIZE="4">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Bs"> <INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Bs">