imageprocessing

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 17 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 the smaller of PNG and JPEG at the configured quality, then lower 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 DecodeImage

func DecodeImage(data []byte) (image.Image, error)

DecodeImage decodes the bytes Pi hands to Photon's PhotonImage.new_from_byteslice: what photonimage.Decode decodes, as image 0.24.9 decodes it. That includes the 16-bit and BI_BITFIELDS bitmaps that Windows places on the clipboard as CF_DIB for Print Screen and GDI captures, and every other layout of image's BMP decoder. upstream: packages/coding-agent/src/utils/image-convert.ts:convertImageBytesToPng

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, error)

DetectSupportedImageMimeTypeFromFile is upstream detectSupportedImageMimeTypeFromFile (utils/mime.ts:25): it reads the sniff window from the start of a file and returns the supported still-image MIME type, or "" when unsupported or empty. A file that cannot be opened or read is the error.

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.

type ResizedImage

type ResizedImage struct {
	Data           string
	MimeType       string
	OriginalWidth  int
	OriginalHeight int
	Width          int
	Height         int
	WasResized     bool
}

ResizedImage is upstream's ResizedImage (image-resize-core.ts:9). Data is the image's base64 text, as upstream's `data`; Bytes returns the decoded image.

func ResizeImage

func ResizeImage(in []byte, mimeType string, options *ai.ModelImageResizeOptions) (ResizedImage, error)

ResizeImage fits an image within the option limits and the encoded size limit, as upstream resizeImage (image-resize.ts:85) does. It reports an error where upstream returns null. An image already within every limit returns the input bytes under the declared mimeType, or image/png when none is declared (image-resize-core.ts:80).

func (ResizedImage) Bytes

func (r ResizedImage) Bytes() []byte

Bytes returns the image Data encodes. Data is always the base64 text this package produced, so it decodes.

Jump to

Keyboard shortcuts

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