package attach import ( "bytes" "image" "image/color" "image/draw" "image/jpeg" ) // Renditions. const ( // PreviewEdge bounds a feed thumbnail's longer side. PreviewEdge = 1024 // ModelEdge and ModelBytes bound an image sent to a model: the API // scales anything longer than 1568 px itself, and a frame carries five. ModelEdge = 1568 ModelBytes = 1_500_000 ) // Preview is a thumbnail for the feed: a JPEG at most PreviewEdge on its // longer side, upright. ok is false when the original serves as is (small // and upright, an animated GIF, a WebP). func Preview(mediaType string, b []byte) (out []byte, ok bool, err error) { if mediaType != JPEG && mediaType != PNG { return nil, false, nil } w, h := Dimensions(mediaType, b) if w == 0 || TooBig(w, h) { return nil, false, ErrPixels } turn := 1 if mediaType == JPEG { turn = Orientation(b) } if max(w, h) <= PreviewEdge && turn == 1 { return nil, false, nil } out, err = scaled(b, turn, PreviewEdge, 82) return out, err == nil, err } // ForModel is an image a model can take: the original when it is small // enough and upright, else a scaled JPEG. ok is false when it cannot be // made small enough (a large WebP: there is no decoder here). func ForModel(mediaType string, b []byte) (out []byte, outType string, ok bool) { w, h := Dimensions(mediaType, b) turn := 1 if mediaType == JPEG { turn = Orientation(b) } if w > 0 && max(w, h) <= ModelEdge && len(b) <= ModelBytes && turn == 1 { return b, mediaType, true } if mediaType == WebP || w == 0 || TooBig(w, h) { return nil, "", false } out, err := scaled(b, turn, ModelEdge, 85) if err != nil || len(out) > ModelBytes { return nil, "", false } return out, JPEG, true } // scaled decodes, turns upright, fits within edge and encodes as JPEG on // white (a transparent PNG would otherwise go black). func scaled(b []byte, turn, edge, quality int) ([]byte, error) { src, _, err := image.Decode(bytes.NewReader(b)) if err != nil { return nil, err } r := src.Bounds() rgba := image.NewRGBA(image.Rect(0, 0, r.Dx(), r.Dy())) draw.Draw(rgba, rgba.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src) draw.Draw(rgba, rgba.Bounds(), src, r.Min, draw.Over) img := upright(rgba, turn) if w, h := img.Rect.Dx(), img.Rect.Dy(); max(w, h) > edge { if w >= h { img = shrink(img, edge, max(1, h*edge/w)) } else { img = shrink(img, max(1, w*edge/h), edge) } } var out bytes.Buffer if err := jpeg.Encode(&out, img, &jpeg.Options{Quality: quality}); err != nil { return nil, err } return out.Bytes(), nil } // shrink is an area average: each output pixel is the mean of the source // pixels it covers. Good for downscaling, which is all it does. func shrink(src *image.RGBA, w, h int) *image.RGBA { sw, sh := src.Rect.Dx(), src.Rect.Dy() dst := image.NewRGBA(image.Rect(0, 0, w, h)) for y := 0; y < h; y++ { y0, y1 := y*sh/h, max((y+1)*sh/h, y*sh/h+1) for x := 0; x < w; x++ { x0, x1 := x*sw/w, max((x+1)*sw/w, x*sw/w+1) var rs, gs, bs, n uint64 for sy := y0; sy < y1; sy++ { row := src.Pix[sy*src.Stride:] for sx := x0; sx < x1; sx++ { p := row[sx*4:] rs, gs, bs = rs+uint64(p[0]), gs+uint64(p[1]), bs+uint64(p[2]) n++ } } d := dst.Pix[y*dst.Stride+x*4:] d[0], d[1], d[2], d[3] = uint8(rs/n), uint8(gs/n), uint8(bs/n), 255 } } return dst } // upright applies an EXIF orientation (1 to 8). func upright(src *image.RGBA, turn int) *image.RGBA { if turn <= 1 || turn > 8 { return src } w, h := src.Rect.Dx(), src.Rect.Dy() dw, dh := w, h if turn >= 5 { dw, dh = h, w } dst := image.NewRGBA(image.Rect(0, 0, dw, dh)) for y := 0; y < h; y++ { for x := 0; x < w; x++ { var dx, dy int switch turn { case 2: dx, dy = w-1-x, y case 3: dx, dy = w-1-x, h-1-y case 4: dx, dy = x, h-1-y case 5: dx, dy = y, x case 6: dx, dy = h-1-y, x case 7: dx, dy = h-1-y, w-1-x case 8: dx, dy = y, w-1-x } copy(dst.Pix[dy*dst.Stride+dx*4:dy*dst.Stride+dx*4+4], src.Pix[y*src.Stride+x*4:y*src.Stride+x*4+4]) } } return dst }