package pipeline import ( "encoding/binary" "image" "io" ) // jpegOrientation returns the EXIF orientation (1..8) of a JPEG, or 1 when the // file has no usable EXIF block. Go's image/jpeg intentionally ignores EXIF, // so the worker applies the transform after decoding. The original bytes are // never rewritten. func jpegOrientation(r io.ReadSeeker) int { defer func() { _, _ = r.Seek(0, 0) }() if _, err := r.Seek(0, 0); err != nil { return 1 } var soi [2]byte if _, err := io.ReadFull(r, soi[:]); err != nil || soi[0] != 0xff || soi[1] != 0xd8 { return 1 } for { var prefix [1]byte if _, err := io.ReadFull(r, prefix[:]); err != nil || prefix[0] != 0xff { return 1 } marker := byte(0xff) for marker == 0xff { if _, err := io.ReadFull(r, prefix[:]); err != nil { return 1 } marker = prefix[0] } // Markers without a length field, and start-of-scan where metadata // after it cannot be an APP1 segment we care about. if marker == 0xd8 || marker == 0xd9 || marker == 0xda || marker == 0x01 || (marker >= 0xd0 && marker <= 0xd7) { return 1 } var lengthBytes [2]byte if _, err := io.ReadFull(r, lengthBytes[:]); err != nil { return 1 } length := int(binary.BigEndian.Uint16(lengthBytes[:])) if length < 2 { return 1 } payloadLength := length - 2 if marker != 0xe1 || payloadLength < 6 { if _, err := r.Seek(int64(payloadLength), io.SeekCurrent); err != nil { return 1 } continue } payload := make([]byte, payloadLength) if _, err := io.ReadFull(r, payload); err != nil { return 1 } if string(payload[:6]) == "Exif\x00\x00" { return parseTIFFOrientation(payload[6:]) } } } func parseTIFFOrientation(data []byte) int { if len(data) < 8 { return 1 } var order binary.ByteOrder switch string(data[:2]) { case "II": order = binary.LittleEndian case "MM": order = binary.BigEndian default: return 1 } if order.Uint16(data[2:4]) != 42 { return 1 } ifdOffset := int(order.Uint32(data[4:8])) if ifdOffset < 8 || ifdOffset+2 > len(data) { return 1 } count := int(order.Uint16(data[ifdOffset : ifdOffset+2])) position := ifdOffset + 2 for index := 0; index < count; index++ { if position+12 > len(data) { return 1 } entry := data[position : position+12] tag := order.Uint16(entry[0:2]) fieldType := order.Uint16(entry[2:4]) items := order.Uint32(entry[4:8]) if tag == 0x0112 && fieldType == 3 && items >= 1 { if items == 1 { value := int(order.Uint16(entry[8:10])) if value >= 1 && value <= 8 { return value } return 1 } offset := int(order.Uint32(entry[8:12])) if offset+2 > len(data) { return 1 } value := int(order.Uint16(data[offset : offset+2])) if value >= 1 && value <= 8 { return value } return 1 } position += 12 } return 1 } // orient applies the eight EXIF orientation transforms. Every branch returns a // new image except orientation 1, so the caller can treat the result as // read-only. func orient(src image.Image, orientation int) image.Image { if orientation <= 1 || orientation > 8 { return src } bounds := src.Bounds() width, height := bounds.Dx(), bounds.Dy() swap := orientation >= 5 outWidth, outHeight := width, height if swap { outWidth, outHeight = height, width } dst := image.NewRGBA(image.Rect(0, 0, outWidth, outHeight)) for y := 0; y < height; y++ { for x := 0; x < width; x++ { var nx, ny int switch orientation { case 2: // mirror horizontal nx, ny = width-1-x, y case 3: // rotate 180 nx, ny = width-1-x, height-1-y case 4: // mirror vertical nx, ny = x, height-1-y case 5: // transpose nx, ny = y, x case 6: // rotate 90 clockwise nx, ny = height-1-y, x case 7: // transverse nx, ny = height-1-y, width-1-x case 8: // rotate 270 clockwise nx, ny = y, width-1-x } dst.Set(nx, ny, src.At(bounds.Min.X+x, bounds.Min.Y+y)) } } return dst }