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()
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 {
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)
@ -202,7 +202,7 @@ func (rq *wrpReq) captureScreenshot() {
sSize = fmt.Sprintf("%.0f KB", float32(len(gifBuf.Bytes()))/1024.0)
iW = i.Bounds().Max.X
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":
i, err := png.Decode(bytes.NewReader(pngCap))
if err != nil {
@ -212,7 +212,7 @@ func (rq *wrpReq) captureScreenshot() {
}
st := time.Now()
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 {
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)
@ -222,7 +222,7 @@ func (rq *wrpReq) captureScreenshot() {
sSize = fmt.Sprintf("%.0f KB", float32(len(jpgBuf.Bytes()))/1024.0)
iW = i.Bounds().Max.X
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{
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
// - BUG: markdown table errors
// 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
import (
@ -196,7 +196,14 @@ func (rq *wrpReq) captureMarkdown() {
return
}
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(
goldmark.WithExtensions(extension.GFM),
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")
wrpMode = flag.String("m", "ismap", "WRP Mode: ismap|html")
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")
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")
@ -72,6 +72,7 @@ type uiData struct {
URL string
BgColor string
NColors int64
JQual int64
Width int64
Height int64
Zoom float64
@ -100,19 +101,19 @@ type uiParams struct {
// WRP Request
type wrpReq struct {
url string // url
width int64 // width
height int64 // height
zoom float64 // zoom/scale
colors int64 // #colors
mouseX int64 // mouseX
mouseY int64 // mouseY
keys string // keys to send
buttons string // Fn buttons
imgType string // imgtype
wrpMode string // mode ismap/html
maxSize int64 // image max size for html mode
imgOpt int64
url string
width int64
height int64
zoom float64
nColors int64
jQual int64
mouseX int64
mouseY int64
keys string
buttons string
imgType string
wrpMode string
maxSize int64
w http.ResponseWriter
r *http.Request
}
@ -138,9 +139,19 @@ func (rq *wrpReq) parseForm() {
if rq.zoom < 0.1 {
rq.zoom = 1.0
}
rq.colors, _ = strconv.ParseInt(rq.r.FormValue("c"), 10, 64) // TODO: this needs to be jpeg quality as well
if rq.colors < 2 || rq.colors > 256 { // ... but maybe not because of this?
rq.colors = defGeom.c
rq.imgType = rq.r.FormValue("t")
switch rq.imgType {
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.buttons = rq.r.FormValue("Fn")
@ -148,17 +159,6 @@ func (rq *wrpReq) parseForm() {
if rq.maxSize == 0 {
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)
}
@ -177,7 +177,8 @@ func (rq *wrpReq) printUI(p uiParams) {
BgColor: p.bgColor,
Width: rq.width,
Height: rq.height,
NColors: rq.colors, // TODO: this needs to be also jpeg quality
NColors: rq.nColors,
JQual: rq.jQual,
Zoom: rq.zoom,
MaxSize: rq.maxSize,
ImgType: rq.imgType,

View File

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