estimateImageTokens method
Estimates the token cost of an image based on its dimensions and detail level.
Uses the tile-based formula from OpenAI / Anthropic documentation:
lowdetail: fixed 85 tokenshighdetail: 170 tokens per 512x512 tile + 85 base
Implementation
int estimateImageTokens(int width, int height, {String detail = 'auto'}) {
if (detail == 'low') return 85;
// Scale down to fit within 2048x2048 then tile at 512x512.
var w = width.toDouble();
var h = height.toDouble();
if (w > 2048 || h > 2048) {
final scale = 2048 / math.max(w, h);
w = (w * scale).ceilToDouble();
h = (h * scale).ceilToDouble();
}
// Scale shortest side to 768.
final minSide = math.min(w, h);
if (minSide > 768) {
final scale = 768 / minSide;
w = (w * scale).ceilToDouble();
h = (h * scale).ceilToDouble();
}
final tilesX = (w / 512).ceil();
final tilesY = (h / 512).ceil();
return 170 * tilesX * tilesY + 85;
}