| 1 | package attach |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "image" |
| 6 | "image/color" |
| 7 | "image/draw" |
| 8 | "image/jpeg" |
| 9 | ) |
| 10 | |
| 11 | // Renditions. |
| 12 | const ( |
| 13 | // PreviewEdge bounds a feed thumbnail's longer side. |
| 14 | PreviewEdge = 1024 |
| 15 | // ModelEdge and ModelBytes bound an image sent to a model: the API |
| 16 | // scales anything longer than 1568 px itself, and a frame carries five. |
| 17 | ModelEdge = 1568 |
| 18 | ModelBytes = 1_500_000 |
| 19 | ) |
| 20 | |
| 21 | // Preview is a thumbnail for the feed: a JPEG at most PreviewEdge on its |
| 22 | // longer side, upright. ok is false when the original serves as is (small |
| 23 | // and upright, an animated GIF, a WebP). |
| 24 | func Preview(mediaType string, b []byte) (out []byte, ok bool, err error) { |
| 25 | if mediaType != JPEG && mediaType != PNG { |
| 26 | return nil, false, nil |
| 27 | } |
| 28 | w, h := Dimensions(mediaType, b) |
| 29 | if w == 0 || TooBig(w, h) { |
| 30 | return nil, false, ErrPixels |
| 31 | } |
| 32 | turn := 1 |
| 33 | if mediaType == JPEG { |
| 34 | turn = Orientation(b) |
| 35 | } |
| 36 | if max(w, h) <= PreviewEdge && turn == 1 { |
| 37 | return nil, false, nil |
| 38 | } |
| 39 | out, err = scaled(b, turn, PreviewEdge, 82) |
| 40 | return out, err == nil, err |
| 41 | } |
| 42 | |
| 43 | // ForModel is an image a model can take: the original when it is small |
| 44 | // enough and upright, else a scaled JPEG. ok is false when it cannot be |
| 45 | // made small enough (a large WebP: there is no decoder here). |
| 46 | func ForModel(mediaType string, b []byte) (out []byte, outType string, ok bool) { |
| 47 | w, h := Dimensions(mediaType, b) |
| 48 | turn := 1 |
| 49 | if mediaType == JPEG { |
| 50 | turn = Orientation(b) |
| 51 | } |
| 52 | if w > 0 && max(w, h) <= ModelEdge && len(b) <= ModelBytes && turn == 1 { |
| 53 | return b, mediaType, true |
| 54 | } |
| 55 | if mediaType == WebP || w == 0 || TooBig(w, h) { |
| 56 | return nil, "", false |
| 57 | } |
| 58 | out, err := scaled(b, turn, ModelEdge, 85) |
| 59 | if err != nil || len(out) > ModelBytes { |
| 60 | return nil, "", false |
| 61 | } |
| 62 | return out, JPEG, true |
| 63 | } |
| 64 | |
| 65 | // scaled decodes, turns upright, fits within edge and encodes as JPEG on |
| 66 | // white (a transparent PNG would otherwise go black). |
| 67 | func scaled(b []byte, turn, edge, quality int) ([]byte, error) { |
| 68 | src, _, err := image.Decode(bytes.NewReader(b)) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | r := src.Bounds() |
| 73 | rgba := image.NewRGBA(image.Rect(0, 0, r.Dx(), r.Dy())) |
| 74 | draw.Draw(rgba, rgba.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src) |
| 75 | draw.Draw(rgba, rgba.Bounds(), src, r.Min, draw.Over) |
| 76 | img := upright(rgba, turn) |
| 77 | if w, h := img.Rect.Dx(), img.Rect.Dy(); max(w, h) > edge { |
| 78 | if w >= h { |
| 79 | img = shrink(img, edge, max(1, h*edge/w)) |
| 80 | } else { |
| 81 | img = shrink(img, max(1, w*edge/h), edge) |
| 82 | } |
| 83 | } |
| 84 | var out bytes.Buffer |
| 85 | if err := jpeg.Encode(&out, img, &jpeg.Options{Quality: quality}); err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | return out.Bytes(), nil |
| 89 | } |
| 90 | |
| 91 | // shrink is an area average: each output pixel is the mean of the source |
| 92 | // pixels it covers. Good for downscaling, which is all it does. |
| 93 | func shrink(src *image.RGBA, w, h int) *image.RGBA { |
| 94 | sw, sh := src.Rect.Dx(), src.Rect.Dy() |
| 95 | dst := image.NewRGBA(image.Rect(0, 0, w, h)) |
| 96 | for y := 0; y < h; y++ { |
| 97 | y0, y1 := y*sh/h, max((y+1)*sh/h, y*sh/h+1) |
| 98 | for x := 0; x < w; x++ { |
| 99 | x0, x1 := x*sw/w, max((x+1)*sw/w, x*sw/w+1) |
| 100 | var rs, gs, bs, n uint64 |
| 101 | for sy := y0; sy < y1; sy++ { |
| 102 | row := src.Pix[sy*src.Stride:] |
| 103 | for sx := x0; sx < x1; sx++ { |
| 104 | p := row[sx*4:] |
| 105 | rs, gs, bs = rs+uint64(p[0]), gs+uint64(p[1]), bs+uint64(p[2]) |
| 106 | n++ |
| 107 | } |
| 108 | } |
| 109 | d := dst.Pix[y*dst.Stride+x*4:] |
| 110 | d[0], d[1], d[2], d[3] = uint8(rs/n), uint8(gs/n), uint8(bs/n), 255 |
| 111 | } |
| 112 | } |
| 113 | return dst |
| 114 | } |
| 115 | |
| 116 | // upright applies an EXIF orientation (1 to 8). |
| 117 | func upright(src *image.RGBA, turn int) *image.RGBA { |
| 118 | if turn <= 1 || turn > 8 { |
| 119 | return src |
| 120 | } |
| 121 | w, h := src.Rect.Dx(), src.Rect.Dy() |
| 122 | dw, dh := w, h |
| 123 | if turn >= 5 { |
| 124 | dw, dh = h, w |
| 125 | } |
| 126 | dst := image.NewRGBA(image.Rect(0, 0, dw, dh)) |
| 127 | for y := 0; y < h; y++ { |
| 128 | for x := 0; x < w; x++ { |
| 129 | var dx, dy int |
| 130 | switch turn { |
| 131 | case 2: |
| 132 | dx, dy = w-1-x, y |
| 133 | case 3: |
| 134 | dx, dy = w-1-x, h-1-y |
| 135 | case 4: |
| 136 | dx, dy = x, h-1-y |
| 137 | case 5: |
| 138 | dx, dy = y, x |
| 139 | case 6: |
| 140 | dx, dy = h-1-y, x |
| 141 | case 7: |
| 142 | dx, dy = h-1-y, w-1-x |
| 143 | case 8: |
| 144 | dx, dy = y, w-1-x |
| 145 | } |
| 146 | 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]) |
| 147 | } |
| 148 | } |
| 149 | return dst |
| 150 | } |
| 151 | |