package transform import ( "context" "encoding/binary" "errors" "image" "image/color" ) var errInvalidEXIF = errors.New("invalid EXIF metadata") func jpegOrientation(ctx context.Context, data []byte) (int, error) { if len(data) < 2 || data[0] != 0xff || data[1] != 0xd8 { return 0, errInvalidEXIF } for offset := 2; offset < len(data); { if err := contextError(ctx); err != nil { return 0, err } if data[offset] != 0xff { return 0, errInvalidEXIF } for offset < len(data) && data[offset] == 0xff { offset++ } if offset >= len(data) { return 0, errInvalidEXIF } marker := data[offset] offset++ if marker == 0xd9 || marker == 0xda { return 1, nil } if marker == 0x01 || marker >= 0xd0 && marker <= 0xd7 { continue } if offset+2 > len(data) { return 0, errInvalidEXIF } length := int(binary.BigEndian.Uint16(data[offset : offset+2])) if length < 2 || offset+length > len(data) { return 0, errInvalidEXIF } payload := data[offset+2 : offset+length] offset += length if marker != 0xe1 || len(payload) < 6 || string(payload[:6]) != "Exif\x00\x00" { continue } return tiffOrientation(ctx, payload[6:]) } return 1, nil } func tiffOrientation(ctx context.Context, data []byte) (int, error) { if len(data) < 8 { return 0, errInvalidEXIF } var order binary.ByteOrder switch string(data[:2]) { case "II": order = binary.LittleEndian case "MM": order = binary.BigEndian default: return 0, errInvalidEXIF } if order.Uint16(data[2:4]) != 42 { return 0, errInvalidEXIF } ifdOffset := uint64(order.Uint32(data[4:8])) if ifdOffset > uint64(len(data)-2) { return 0, errInvalidEXIF } count := uint64(order.Uint16(data[ifdOffset : ifdOffset+2])) entriesStart := ifdOffset + 2 if count > (uint64(len(data))-entriesStart)/12 { return 0, errInvalidEXIF } for index := uint64(0); index < count; index++ { if err := contextError(ctx); err != nil { return 0, err } entry := data[entriesStart+index*12 : entriesStart+(index+1)*12] if order.Uint16(entry[:2]) != 0x0112 { continue } if order.Uint16(entry[2:4]) != 3 || order.Uint32(entry[4:8]) != 1 { return 0, errInvalidEXIF } orientation := int(order.Uint16(entry[8:10])) if orientation < 1 || orientation > 8 { return 0, errInvalidEXIF } return orientation, nil } return 1, nil } func applyOrientation(ctx context.Context, source image.Image, orientation int) (*image.NRGBA, error) { bounds := source.Bounds() width, height := bounds.Dx(), bounds.Dy() outputWidth, outputHeight := width, height if orientation >= 5 { outputWidth, outputHeight = height, width } output := image.NewNRGBA(image.Rect(0, 0, outputWidth, outputHeight)) for y := 0; y < outputHeight; y++ { if err := contextError(ctx); err != nil { return nil, err } for x := 0; x < outputWidth; x++ { if x&255 == 0 { if err := contextError(ctx); err != nil { return nil, err } } sourceX, sourceY := orientedSourcePoint(x, y, width, height, orientation) output.Set(x, y, source.At(bounds.Min.X+sourceX, bounds.Min.Y+sourceY)) } } return output, nil } func orientedSourcePoint(x, y, width, height, orientation int) (int, int) { switch orientation { case 2: return width - 1 - x, y case 3: return width - 1 - x, height - 1 - y case 4: return x, height - 1 - y case 5: return y, x case 6: return y, height - 1 - x case 7: return width - 1 - y, height - 1 - x case 8: return width - 1 - y, x default: return x, y } } func flattenWhite(ctx context.Context, source image.Image) (*image.NRGBA, error) { bounds := source.Bounds() output := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy())) for y := 0; y < bounds.Dy(); y++ { if err := contextError(ctx); err != nil { return nil, err } for x := 0; x < bounds.Dx(); x++ { if x&255 == 0 { if err := contextError(ctx); err != nil { return nil, err } } r, g, b, a := source.At(bounds.Min.X+x, bounds.Min.Y+y).RGBA() output.SetNRGBA(x, y, color.NRGBA{ R: uint8((r + 0xffff - a) / 0x101), G: uint8((g + 0xffff - a) / 0x101), B: uint8((b + 0xffff - a) / 0x101), A: 0xff, }) } } return output, nil }