vendor/github.com/go-rod/rod/lib/utils/imageutil.go

134 lines · 3031 bytes

  1package utils
  2
  3import (
  4	"bytes"
  5	"fmt"
  6	"image"
  7	"image/jpeg"
  8	"image/png"
  9	"io"
 10
 11	"github.com/go-rod/rod/lib/proto"
 12)
 13
 14// ImgWithBox is a image with a box, if the box is nil, it means the whole image.
 15type ImgWithBox struct {
 16	Img []byte
 17	Box *image.Rectangle
 18}
 19
 20// ImgOption is the option for image processing.
 21type ImgOption struct {
 22	Quality int
 23}
 24
 25// ImgProcessor is the interface for image processing.
 26type ImgProcessor interface {
 27	Encode(img image.Image, opt *ImgOption) ([]byte, error)
 28	Decode(file io.Reader) (image.Image, error)
 29}
 30
 31type jpegProcessor struct{}
 32
 33func (p jpegProcessor) Encode(img image.Image, opt *ImgOption) ([]byte, error) {
 34	var buf bytes.Buffer
 35	var jpegOpt *jpeg.Options
 36	if opt != nil {
 37		jpegOpt = &jpeg.Options{Quality: opt.Quality}
 38	}
 39	err := jpeg.Encode(&buf, img, jpegOpt)
 40	return buf.Bytes(), err
 41}
 42
 43func (p jpegProcessor) Decode(file io.Reader) (image.Image, error) {
 44	return jpeg.Decode(file)
 45}
 46
 47type pngProcessor struct{}
 48
 49func (p pngProcessor) Encode(img image.Image, _ *ImgOption) ([]byte, error) {
 50	var buf bytes.Buffer
 51	err := png.Encode(&buf, img)
 52	return buf.Bytes(), err
 53}
 54
 55func (p pngProcessor) Decode(file io.Reader) (image.Image, error) {
 56	return png.Decode(file)
 57}
 58
 59// NewImgProcessor create a ImgProcessor by the format.
 60func NewImgProcessor(format proto.PageCaptureScreenshotFormat) (ImgProcessor, error) {
 61	switch format {
 62	case proto.PageCaptureScreenshotFormatJpeg:
 63		return &jpegProcessor{}, nil
 64	case "", proto.PageCaptureScreenshotFormatPng:
 65		return &pngProcessor{}, nil
 66	default:
 67		return nil, fmt.Errorf("not support format: %v", format)
 68	}
 69}
 70
 71// SplicePngVertical splice png vertically, if there is only one image, it will return the image directly.
 72// Only support png and jpeg format yet, webP is not supported because no suitable processing
 73// library was found in golang.
 74func SplicePngVertical(files []ImgWithBox, format proto.PageCaptureScreenshotFormat, opt *ImgOption) ([]byte, error) {
 75	if len(files) == 0 {
 76		return nil, nil
 77	}
 78	if len(files) == 1 {
 79		return files[0].Img, nil
 80	}
 81
 82	var width, height int
 83
 84	processor, err := NewImgProcessor(format)
 85	if err != nil {
 86		return nil, err
 87	}
 88
 89	var images []image.Image
 90	for _, file := range files {
 91		img, err := processor.Decode(bytes.NewReader(file.Img))
 92		if err != nil {
 93			return nil, err
 94		}
 95
 96		images = append(images, img)
 97		if file.Box != nil {
 98			width = file.Box.Dx()
 99			height += file.Box.Dy()
100		} else {
101			width = img.Bounds().Dx()
102			height += img.Bounds().Dy()
103		}
104	}
105
106	spliceImg := image.NewRGBA(image.Rect(0, 0, width, height))
107
108	var destY int
109	for i, file := range files {
110		img := images[i]
111		bounds := img.Bounds()
112
113		if file.Box != nil {
114			bounds = *file.Box
115		}
116		start := bounds.Min
117		end := bounds.Max
118		for y := start.Y; y < end.Y; y++ {
119			for x := start.X; x < end.X; x++ {
120				color := img.At(x, y)
121				spliceImg.Set(x, y-start.Y+destY, color)
122			}
123		}
124
125		destY += bounds.Dy()
126	}
127
128	bs, err := processor.Encode(spliceImg, opt)
129	if err != nil {
130		return nil, err
131	}
132
133	return bs, nil
134}