package renderpage import ( "bytes" "errors" "fmt" "image" "image/color" "image/draw" "image/png" "io" "math" "amadan.net/oficina/equipo/internal/runner/render" ) // Capture bounds. A capture is decoded only after its IHDR says it is // within MaxWidth by MaxHeight and maxPixels, and only from a file of at // most maxInput bytes; what the runner writes is its own encoding, at // most MaxCaptureBytes. const ( // MaxWidth is the widest viewport (desktop) and MaxHeight a full // page's clip (spec: full pages capped at 10,000 px). MaxWidth = 1440 MaxHeight = render.MaxCaptureHeight // maxPixels caps the decoded image on its own (about 58 MB as 8-bit // RGBA), whatever the two above become. maxPixels = MaxWidth * MaxHeight // maxInput caps the browser's file, as the CDP frame cap does. maxInput = 100 << 20 // MaxCaptureBytes is the most one written capture holds (spec: at // most 2 MB). MaxCaptureBytes = 2 << 20 // maxScaleSteps bounds the downscaling: each step at least a tenth // smaller, so far fewer suffice for any capture within the bounds. maxScaleSteps = 24 ) // errCapture is a capture the runner refused: the call is crashed. var errCapture = errors.New("render_page: the capture is not a PNG within the bounds") // decodeCapture reads one PNG the browser made, bounded: the file at // most maxInput bytes; IHDR's dimensions checked before any pixel is // decoded; then fully decoded (CRCs and the zlib stream checked by // image/png), its bounds the declared ones. The pixels are copied into // an NRGBA image the runner owns. func decodeCapture(r io.Reader) (*image.NRGBA, error) { raw, err := io.ReadAll(io.LimitReader(r, maxInput+1)) if err != nil { return nil, err } if len(raw) == 0 || len(raw) > maxInput { return nil, fmt.Errorf("%w: %d bytes", errCapture, len(raw)) } cfg, err := png.DecodeConfig(bytes.NewReader(raw)) if err != nil { return nil, fmt.Errorf("%w: %v", errCapture, err) } if cfg.Width <= 0 || cfg.Height <= 0 || cfg.Width > MaxWidth || cfg.Height > MaxHeight || int64(cfg.Width)*int64(cfg.Height) > maxPixels { return nil, fmt.Errorf("%w: %dx%d", errCapture, cfg.Width, cfg.Height) } img, err := png.Decode(bytes.NewReader(raw)) if err != nil { return nil, fmt.Errorf("%w: %v", errCapture, err) } b := img.Bounds() if b.Min != (image.Point{}) || b.Dx() != cfg.Width || b.Dy() != cfg.Height { return nil, fmt.Errorf("%w: decoded %v", errCapture, b) } out := image.NewNRGBA(b) draw.Draw(out, b, img, image.Point{}, draw.Src) return out, nil } // encodeCapture is the runner's encoding of img: scaled down, keeping // its shape, until the PNG is at most MaxCaptureBytes. It returns the // bytes and their dimensions. func encodeCapture(img *image.NRGBA) ([]byte, int, int, error) { for step := 0; ; step++ { var b bytes.Buffer if err := png.Encode(&b, img); err != nil { return nil, 0, 0, err } w, h := img.Bounds().Dx(), img.Bounds().Dy() if b.Len() <= MaxCaptureBytes { return b.Bytes(), w, h, nil } if step == maxScaleSteps || w == 1 && h == 1 { return nil, 0, 0, fmt.Errorf("%w: still %d bytes at %dx%d", errCapture, b.Len(), w, h) } // PNG size follows the pixel count: aim a little under the cap, // never less than a quarter or more than nine tenths a side. f := math.Sqrt(float64(MaxCaptureBytes)/float64(b.Len())) * 0.95 f = min(max(f, 0.25), 0.9) img = downscale(img, max(1, int(float64(w)*f)), max(1, int(float64(h)*f))) } } // downscale is img at w by h, each pixel the mean of the source pixels // its box covers. func downscale(img *image.NRGBA, w, h int) *image.NRGBA { sw, sh := img.Bounds().Dx(), img.Bounds().Dy() out := image.NewNRGBA(image.Rect(0, 0, w, h)) for y := range h { y0, y1 := y*sh/h, max((y+1)*sh/h, y*sh/h+1) for x := range w { x0, x1 := x*sw/w, max((x+1)*sw/w, x*sw/w+1) var sum [4]int for sy := y0; sy < y1; sy++ { row := img.Pix[sy*img.Stride+x0*4 : sy*img.Stride+x1*4] for i := 0; i < len(row); i += 4 { sum[0] += int(row[i]) sum[1] += int(row[i+1]) sum[2] += int(row[i+2]) sum[3] += int(row[i+3]) } } n := (y1 - y0) * (x1 - x0) o := out.PixOffset(x, y) for c := range 4 { out.Pix[o+c] = uint8(sum[c] / n) } } } return out } // diffMark is a changed pixel in a diff image. var diffMark = color.NRGBA{0xff, 0x00, 0x40, 0xff} // compare pairs one page's base and work captures: the share of pixels // that differ, over the larger extent of the two (a pixel only one has // is changed), and a diff image of that extent with each changed pixel // marked and the rest the work capture faded. func compare(base, work *image.NRGBA) (float64, *image.NRGBA) { bb, wb := base.Bounds(), work.Bounds() w, h := max(bb.Dx(), wb.Dx()), max(bb.Dy(), wb.Dy()) diff := image.NewNRGBA(image.Rect(0, 0, w, h)) changed := 0 for y := range h { for x := range w { p := image.Point{x, y} inB, inW := p.In(bb), p.In(wb) o := diff.PixOffset(x, y) if inB && inW { bo, wo := base.PixOffset(x, y), work.PixOffset(x, y) if bytes.Equal(base.Pix[bo:bo+4], work.Pix[wo:wo+4]) { c := work.Pix[wo : wo+4] grey := (299*int(c[0]) + 587*int(c[1]) + 114*int(c[2])) / 1000 v := uint8(192 + grey*63/255) copy(diff.Pix[o:o+4], []byte{v, v, v, 0xff}) continue } } changed++ copy(diff.Pix[o:o+4], []byte{diffMark.R, diffMark.G, diffMark.B, diffMark.A}) } } return float64(changed) / float64(w*h), diff }