Cotton code
169 lines · 4.9 KB · GoRaw
package mainimport (	"bytes"	"encoding/binary"	"image"	"image/color"	"image/jpeg"	_ "image/png")// The shape WhatsApp is told for a picture (owner, 24 Nov 2026: a photo sent from Comms showed on WhatsApp Web with// the wrong aspect ratio). WhatsApp draws a picture's bubble from the width and height the message carries and its// thumbnail, before the file itself is fetched. Those came from the phone's outbox row: its width and height can be// the camera's stored pixels before the photo's EXIF rotation (a portrait photo stored on its side), and its preview// is Cotton's own tiny one. So both are read here from the very file that goes: its size as it is seen, rotation// applied, and a thumbnail of it, upright, of the same shape. A file Go cannot read (WebP) keeps the phone's values.// thumbSide is the longest side of the thumbnail WhatsApp gets, in pixels.const thumbSide = 100// pictureShape answers the picture's width and height as it is seen and an upright JPEG thumbnail of it; ok is false// when the file is not a JPEG or PNG Go can read. thumb is nil when the size could be read and the pixels not.func pictureShape(data []byte) (width, height uint32, thumb []byte, ok bool) {	cfg, _, err := image.DecodeConfig(bytes.NewReader(data))	if err != nil || cfg.Width <= 0 || cfg.Height <= 0 {		return 0, 0, nil, false	}	orientation := jpegOrientation(data)	w, h := cfg.Width, cfg.Height	if orientation >= 5 {		w, h = h, w	}	img, _, err := image.Decode(bytes.NewReader(data))	if err != nil {		return uint32(w), uint32(h), nil, true	}	small := oriented(shrunk(img, thumbSide), orientation)	var out bytes.Buffer	if err := jpeg.Encode(&out, small, &jpeg.Options{Quality: 70}); err != nil {		return uint32(w), uint32(h), nil, true	}	return uint32(w), uint32(h), out.Bytes(), true}// jpegOrientation reads a JPEG's EXIF orientation (1 to 8; 1, as stored, when there is none or the file is no JPEG).func jpegOrientation(data []byte) int {	if len(data) < 4 || data[0] != 0xFF || data[1] != 0xD8 {		return 1	}	at := 2	for at+4 <= len(data) && data[at] == 0xFF {		marker := data[at+1]		if marker == 0xDA || marker == 0xD9 {			break		}		length := int(binary.BigEndian.Uint16(data[at+2:]))		if length < 2 || at+2+length > len(data) {			break		}		segment := data[at+4 : at+2+length]		if marker == 0xE1 && len(segment) > 14 && string(segment[:6]) == "Exif\x00\x00" {			return tiffOrientation(segment[6:])		}		at += 2 + length	}	return 1}// tiffOrientation reads tag 0x0112 from the first directory of an EXIF TIFF block.func tiffOrientation(tiff []byte) int {	var order binary.ByteOrder	switch string(tiff[:2]) {	case "II":		order = binary.LittleEndian	case "MM":		order = binary.BigEndian	default:		return 1	}	ifd := int(order.Uint32(tiff[4:]))	if ifd+2 > len(tiff) {		return 1	}	count := int(order.Uint16(tiff[ifd:]))	for i := 0; i < count; i++ {		entry := ifd + 2 + 12*i		if entry+12 > len(tiff) {			return 1		}		if order.Uint16(tiff[entry:]) == 0x0112 {			if v := int(order.Uint16(tiff[entry+8:])); v >= 1 && v <= 8 {				return v			}			return 1		}	}	return 1}// shrunk is img made at most side pixels on its longest side, each pixel the average of a few under it, over white// where the picture is see-through.func shrunk(img image.Image, side int) *image.RGBA {	b := img.Bounds()	w, h := b.Dx(), b.Dy()	tw, th := w, h	if w >= h && w > side {		tw, th = side, max(1, h*side/w)	} else if h > w && h > side {		tw, th = max(1, w*side/h), side	}	out := image.NewRGBA(image.Rect(0, 0, tw, th))	const taps = 4	for y := 0; y < th; y++ {		for x := 0; x < tw; x++ {			var r, g, bl, n uint32			for j := 0; j < taps; j++ {				for i := 0; i < taps; i++ {					// Evenly across the box of the picture under this pixel.					sx := b.Min.X + (2*taps*x*w+(2*i+1)*w)/(2*taps*tw)					sy := b.Min.Y + (2*taps*y*h+(2*j+1)*h)/(2*taps*th)					sx, sy = min(sx, b.Max.X-1), min(sy, b.Max.Y-1)					cr, cg, cb, ca := img.At(sx, sy).RGBA()					// Over white.					cr, cg, cb = cr+(0xFFFF-ca), cg+(0xFFFF-ca), cb+(0xFFFF-ca)					r, g, bl, n = r+cr, g+cg, bl+cb, n+1				}			}			out.SetRGBA(x, y, color.RGBA{uint8(r / n >> 8), uint8(g / n >> 8), uint8(bl / n >> 8), 0xFF})		}	}	return out}// oriented is img turned as its EXIF orientation says it is seen.func oriented(img *image.RGBA, orientation int) *image.RGBA {	if orientation <= 1 || orientation > 8 {		return img	}	w, h := img.Bounds().Dx(), img.Bounds().Dy()	dw, dh := w, h	if orientation >= 5 {		dw, dh = h, w	}	out := image.NewRGBA(image.Rect(0, 0, dw, dh))	for y := 0; y < dh; y++ {		for x := 0; x < dw; x++ {			var sx, sy int			switch orientation {			case 2:				sx, sy = w-1-x, y			case 3:				sx, sy = w-1-x, h-1-y			case 4:				sx, sy = x, h-1-y			case 5:				sx, sy = y, x			case 6:				sx, sy = y, h-1-x			case 7:				sx, sy = w-1-y, h-1-x			case 8:				sx, sy = w-1-y, x			}			out.SetRGBA(x, y, img.RGBAAt(sx, sy))		}	}	return out}