imageprocessing

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Image resize + re-encode policy for CLI/LLM image attachments, plus the EXIF orientation reader (upstream exif-orientation.ts). PNG conversion for Kitty (upstream image-convert.ts) lives in image_convert.go.

Mirrors upstream packages/coding-agent/src/utils/image-resize.ts using the Go standard library plus golang.org/x/image in place of Photon:

  1. Decode + apply EXIF orientation once.
  2. If needed, resize to fit within 2000x2000.
  3. Try PNG first, then JPEG quality steps until the base64 payload fits under 4.5 MB.
  4. If still too large, shrink dimensions by 25% and retry.

Index

Constants

View Source
const (
	MaxLongestSide  = 2000
	MaxEncodedBytes = int(4.5 * 1024 * 1024) // base64 payload bytes
	JPEGQuality     = 80
)
View Source
const ImageTypeSniffBytes = 4100

ImageTypeSniffBytes mirrors upstream IMAGE_TYPE_SNIFF_BYTES: detection looks only at a file's first 4100 bytes.

Variables

View Source
var ErrImageTooLarge = errors.New("image: could not be resized below inline image size limit")

Functions

func ConvertImageBytesToPng

func ConvertImageBytesToPng(data []byte) []byte

ConvertImageBytesToPng applies EXIF orientation before encoding PNG, as upstream image-convert.ts does through Photon. Failure returns nil.

func DecodeNodeBase64

func DecodeNodeBase64(data string) []byte

DecodeNodeBase64 mirrors Node's Buffer.from(data, "base64"): it accepts the standard and URL-safe alphabets, skips every other character (whitespace included), stops at the first '=', and decodes a trailing partial quantum of two or three characters to one or two bytes.

func DetectSupportedImageMimeType

func DetectSupportedImageMimeType(buffer []byte) string

DetectSupportedImageMimeType mirrors upstream mime.ts and returns a supported still-image MIME type for the provided bytes, or "" when unsupported.

The formats named here are also the only decoders this package links, so a format outside the allowlist has no decoder to reach. Adding a MIME type here or to tools.SupportedImageMime without adding its decoder import decodes nothing; adding the decoder import widens the attack surface of the image pipeline to that format's parser.

func DetectSupportedImageMimeTypeFromFile

func DetectSupportedImageMimeTypeFromFile(filePath string) string

DetectSupportedImageMimeTypeFromFile reads the upstream sniff window from a file and returns the supported still-image MIME type, or "" when unsupported.

func NormalizeToolResultImages

func NormalizeToolResultImages(result agent.AgentToolResult, autoResize bool) agent.AgentToolResult

NormalizeToolResultImages processes tool images before history. Each conversion or resize hint follows its image; failures and unchanged results retain the original blocks and content slice. Ports packages/coding-agent/src/utils/tool-result-images.ts.

func NormalizeToolResultImagesWithOptions

func NormalizeToolResultImagesWithOptions(result agent.AgentToolResult, autoResize bool, options *ai.ModelImageResizeOptions) agent.AgentToolResult

NormalizeToolResultImagesWithOptions applies the active model profile after extension hooks. Failed image processing retains the original image block.

func PrepareCLIImageAttachment

func PrepareCLIImageAttachment(in []byte, inputMIME string) ([]byte, string, string, error)

PrepareCLIImageAttachment processes one CLI image file into the attached image payload plus the upstream dimension note (empty when not resized).

func ProcessImage

func ProcessImage(in []byte, inputMIME string, autoResize bool, options *ai.ModelImageResizeOptions) ([]byte, string, string, error)

ProcessImage normalizes unsupported formats before applying a model's resize profile. A false autoResize flag still converts unsupported formats. Errors carry upstream's omission hint; callers decide whether to keep a tool image.

func ResizeImageForLLM

func ResizeImageForLLM(in []byte) ([]byte, string, error)

ResizeImageForLLM applies the upstream CLI image policy and returns the processed bytes plus the resulting MIME type.

Types

type ProcessImageFunc

type ProcessImageFunc func([]byte, string, bool, *ai.ModelImageResizeOptions) ([]byte, string, string, error)

ProcessImageFunc is the selected image processor at the model-input boundary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL