See the images you paste into Claude Code: thumbnails above the prompt instead of bare [Image #1] tags

A Claude Code mod that shows the images you paste, so you see thumbnails above your prompt instead of bare [Image #1] tags.

Inside Claude Code, run:
/plugin marketplace add jarrodwatts/claude-image-view
/plugin install image-view
/reload-plugins
That's it. Paste an image into the prompt and its thumbnail appears above the input.
claude plugin marketplace add jarrodwatts/claude-image-view
claude plugin install image-view@claude-image-view
Then run /reload-plugins inside a session, or start a new one.
Paste one or more images and a row of thumbnails sits above the prompt, each labelled with the number of its tag:
╭────────────────────────╮ ╭────────────╮
│ │ │ │
│ (screenshot) │ │ (photo) │
│ │ │ │
│ #1 │ │ #2 │
╰────────────────────────╯ ╰────────────╯
❯ why is the header misaligned here [Image #1] vs [Image #2]
+N for the rest.Claude Code saves every pasted image to a cache folder for the session, as <tmp>/<project>/<session>/images/<n>.<ext> in the format it was pasted (.png, .webp, .jpg, ...), and puts an [Image #n] tag in the prompt. Claude Image View is a mod:
[Image #n] tags. It checks on a timer because pasting an image doesn't raise an edit event.sips to <session>/image-view-<n>.png. It reads the size from the PNG header.Image element, and the terminal reads the file itself, so the image data never passes through the mod.▀) with Claude Code's Raster element. It's lower resolution than a real picture, but it's the picture.Claude Code, not the mod, decides whether a terminal gets real pictures. It draws them in kitty (0.28+) and Ghostty, and turns them off inside tmux and screen and in background sessions. Every other terminal gets half-block thumbnails.
| Terminal | Thumbnail |
|---|---|
| Ghostty, kitty | Real picture |
| iTerm2, Terminal.app, WezTerm, VS Code, Windows Terminal, Orca, most others | Half blocks |
| Inside tmux or screen, even in Ghostty or kitty | Half blocks |
| Agent view and background sessions | Half blocks, or real pictures with CLAUDE_CODE_FORCE_TERMINAL_IMAGES (below) |
Some terminals speak the kitty graphics protocol but still get half blocks, because Claude Code trusts terminals by name (anthropics/claude-code#99566), or because it doesn't wrap images for tmux passthrough yet (anthropics/claude-code#100146). Those are Claude Code limits, so a fix there brings real pictures to the mod with no change here.
CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 makes Claude Code draw pictures in any terminal. Only set it if your terminal supports the kitty protocol's Unicode placeholders. VS Code's terminal, for one, doesn't yet, and shows a grid of colored squares instead (xtermjs/xterm.js#5711).
Claude Image View is local-only. It makes no network requests. It reads the prompt box, lists Claude Code's temp folder to find the current session's image cache, and reads each pasted image (up to Claude Code's 4 MiB read limit). For a non-PNG paste it runs sips and writes one PNG thumbnail into that session's temp folder. On macOS and Linux it runs id -u once to find that temp folder. It also reads a few terminal variables (TERM, TERM_PROGRAM, TMUX and similar) to tell which kind of thumbnail Claude Code can draw.
Run claude plugin validate on the repo to see every event it hooks and every call it makes.
The Claude Desktop app already previews pasted images, so the mod draws nothing there.
Nothing appears when I paste. Run /plugin and check the dim line under the tabs lists image-view as an active mod. If it isn't listed, run /reload-plugins. If it still isn't, look for an image-view: hooks module did not load line above the first prompt of a new session: that means your Claude Code is too old for the mod, so update it.
The tile says "no preview". The mod couldn't find the cached file, or the paste isn't a PNG and you're not on macOS, where sips converts it. Claude Code may have moved where it stores pasted images. Please open an issue with your Claude Code version.
The tile is blocky. Claude Code doesn't draw pictures in your terminal, so the mod draws the picture in colored half blocks. See Terminal Support.
The tile shows [Image #1] text instead of the picture. The mod expected Claude Code to draw a picture, but it didn't. That happens in terminals that look like Ghostty or kitty but aren't, such as Herdr (anthropics/claude-code#101306), or in kitty older than 0.28. Set CLAUDE_IMAGE_VIEW_RENDERER=blocks in the environment you start Claude Code from. CLAUDE_IMAGE_VIEW_RENDERER=image forces real pictures the other way.
The tile is a grid of colored squares. CLAUDE_CODE_FORCE_TERMINAL_IMAGES is set in a terminal that can't draw Unicode placeholders, such as VS Code's. Unset it to get half-block thumbnails.
I want real pictures in agent view or a background session. Claude Code turns terminal images off there. If you attach from Ghostty or kitty, turn them back on in the env block of ~/.claude/settings.json, then start a new session:
"env": { "CLAUDE_CODE_FORCE_TERMINAL_IMAGES": "1" }
git clone https://github.com/jarrodwatts/claude-image-view
cd claude-image-view
# Load it for one session without installing
claude --plugin-dir .
# Check it and run the tests
claude plugin validate .
claude plugin test .
Claude Code writes the API types into .claude-plugin/types/ the first time it loads the mod, and tsc -p . type-checks it from then on.
MIT. See LICENSE.
hooks/register.tsx 205 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { PastedImage } from '../types'
5import { blockCells, drawsPictures, fitRow, imageNumbers, pngSize } from './layout'
6import type { Size } from './layout'
7import { decodeThumb } from './png'
8import type { Thumb } from './png'
9
10// Pasting an image raises no prompt.edit (the tag only shows up on the next keystroke),
11// so the draft is polled instead.
12const POLL_MS = 200
13
14const images = atom({ plugin: 'image-view', key: 'images' } as const, [] as PastedImage[])
15
16let tmpRoot: string | undefined
17let found: { sessionId: string; dir: string } | undefined
18// The image numbers last drawn, so an unchanged draft doesn't rewrite state; undefined
19// while a drawn image's file is still missing, so the next poll looks again.
20let shownKey: string | undefined
21// The list last written, so a poll that finds the same tiles doesn't redraw the band.
22let written: string | undefined
23let isChecking = false
24const sizes = new Map<string, Size | null>()
25// Files whose first bytes aren't a PNG's: they never will be, so they aren't read again.
26const notPng = new Set<string>()
27// Source files sips couldn't convert, so a broken paste isn't retried on every poll.
28const unconvertible = new Set<string>()
29// Small decoded copies for terminals that can't draw pictures, which get colored blocks instead.
30const thumbs = new Map<string, Thumb | null>()
31let pictures: boolean | undefined
32
33// Claude Code doesn't tell mods whether its Image draws a picture, so go by the environment
34// it decides from.
35async function canDrawPictures($: EngineInterface): Promise<boolean> {
36 if (pictures === undefined) {
37 pictures = drawsPictures({
38 TERM: await $.env.get('TERM'),
39 TERM_PROGRAM: await $.env.get('TERM_PROGRAM'),
40 TMUX: await $.env.get('TMUX'),
41 STY: await $.env.get('STY'),
42 KITTY_WINDOW_ID: await $.env.get('KITTY_WINDOW_ID'),
43 GHOSTTY_RESOURCES_DIR: await $.env.get('GHOSTTY_RESOURCES_DIR'),
44 CLAUDE_CODE_FORCE_TERMINAL_IMAGES: await $.env.get('CLAUDE_CODE_FORCE_TERMINAL_IMAGES'),
45 CLAUDE_CODE_SESSION_KIND: await $.env.get('CLAUDE_CODE_SESSION_KIND'),
46 CLAUDE_IMAGE_VIEW_RENDERER: await $.env.get('CLAUDE_IMAGE_VIEW_RENDERER'),
47 })
48 }
49 return pictures
50}
51
52// Claude Code keeps its temp files in <base>/claude-<uid>, or <base>\claude on Windows. The base
53// is CLAUDE_CODE_TMPDIR when set, else /tmp (%TEMP% on Windows): the variable names the folder
54// Claude Code nests its own in, not that folder itself.
55async function tempRoot($: EngineInterface): Promise<string | undefined> {
56 const fromEnv = await $.env.get('CLAUDE_CODE_TMPDIR')
57 if ((await $.env.get('OS')) === 'Windows_NT') {
58 const base = fromEnv ?? (await $.env.get('TEMP'))
59 return base === undefined ? undefined : `${base.replace(/[\\/]+$/, '')}/claude`
60 }
61 const uid = (await $.process.run(['id', '-u'])).stdout.trim()
62 return `${(fromEnv ?? '/tmp').replace(/\/+$/, '')}/claude-${uid}`
63}
64
65// Claude Code caches each paste as <tmp>/<project>/<session>/images/<n>.png. The project
66// folder is named after a working directory that may since have moved, so find it by the
67// session id instead of rebuilding it.
68async function imagesDir($: EngineInterface): Promise<string | undefined> {
69 const sessionId = await $.session.id()
70 if (found?.sessionId === sessionId) return found.dir
71 tmpRoot ??= await tempRoot($)
72 if (tmpRoot === undefined) return undefined
73 const entries = await $.fs.list(tmpRoot).catch(() => [])
74 for (const entry of entries) {
75 const dir = `${tmpRoot}/${entry.name}/${sessionId}/images`
76 if (entry.kind === 'dir' && (await $.fs.exists(dir))) {
77 found = { sessionId, dir }
78 return dir
79 }
80 }
81 return undefined
82}
83
84// Pastes are cached as <n>.png or <n>.webp (or another format), but the terminal only
85// decodes PNG, so anything else is converted once to <session>/image-view-<n>.png.
86// sips ships with macOS only, so on Linux a non-PNG paste still shows "no preview".
87async function pngFor($: EngineInterface, dir: string, n: number): Promise<string | undefined> {
88 const png = `${dir}/${n}.png`
89 if (await $.fs.exists(png)) return png
90 const out = `${dir.replace(/\/images$/, '')}/image-view-${n}.png`
91 if (await $.fs.exists(out)) return out
92 const entries = await $.fs.list(dir).catch(() => [])
93 const source = entries.find(entry => entry.kind === 'file' && entry.name.startsWith(`${n}.`))
94 if (source === undefined) return undefined
95 const sourcePath = `${dir}/${source.name}`
96 if (unconvertible.has(sourcePath)) return undefined
97 const { exitCode } = await $.process
98 .run(['sips', '-s', 'format', 'png', '-Z', '800', sourcePath, '--out', out])
99 .catch(() => ({ exitCode: 1 }))
100 if (exitCode === 0) return out
101 unconvertible.add(sourcePath)
102 return undefined
103}
104
105async function describe($: EngineInterface, dir: string | undefined, n: number): Promise<PastedImage> {
106 const path = dir === undefined ? undefined : await pngFor($, dir, n)
107 if (path === undefined || notPng.has(path)) return { n, path: null, size: null }
108 if (!sizes.has(path)) {
109 const base64 = await $.fs.read(path, { as: 'bytes' }).then(
110 bytes => bytes.base64,
111 () => undefined, // too big to read: still drawable, just without its aspect ratio
112 )
113 const head = base64 === undefined ? undefined : pngSize(base64)
114 if (head === null) {
115 // A file no longer than the 24-byte header may still be being written; a longer one is settled.
116 if (base64!.length > 32) notPng.add(path)
117 return { n, path: null, size: null }
118 }
119 sizes.set(path, head ?? null)
120 if (base64 !== undefined && !(await canDrawPictures($))) thumbs.set(path, decodeThumb(Uint8Array.fromBase64(base64)))
121 }
122 return { n, path, size: sizes.get(path) ?? null }
123}
124
125async function show($: EngineInterface, draft: string) {
126 const numbers = imageNumbers(draft)
127 const key = numbers.join(',')
128 if (key === shownKey) return
129 const dir = numbers.length > 0 ? await imagesDir($) : undefined
130 const list: PastedImage[] = []
131 for (const n of numbers) list.push(await describe($, dir, n))
132 shownKey = list.every(image => image.path !== null) ? key : undefined
133 const json = JSON.stringify(list)
134 if (json === written) return
135 await update($, images, () => list)
136 written = json
137}
138
139async function check($: EngineInterface) {
140 if (isChecking) return
141 isChecking = true
142 try {
143 await show($, (await $.prompt.read()).text)
144 } finally {
145 isChecking = false
146 }
147}
148
149export const register: Register = on => {
150 on('session.start', async ($, e, next) => {
151 pictures = undefined
152 $.clock.every(POLL_MS, () => check($))
153 return next(e)
154 })
155
156 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
157 if (e.surface !== 'terminal' || e.props.hasSurvey) return next(e)
158 const list = await read($, images)
159 if (list.length === 0) return next(e)
160
161 const { Box, Image, Raster, Text } = $.ui.resolve(e)
162 const cells = fitRow(list.map(image => image.size), e.props.maxRows, e.props.bodyColumns)
163 if (cells.length === 0) return next(e)
164 const hidden = list.length - cells.length
165 const below = await next(e)
166
167 return (
168 <Box flexDirection="column">
169 <Box flexDirection="row" columnGap={1}>
170 {list.slice(0, cells.length).map((image, i) => {
171 const { columns, rows } = cells[i] ?? { columns: 4, rows: 1 }
172 return (
173 <Box key={`tile-${image.n}`} flexDirection="column" alignItems="center" borderStyle="round" borderDimColor>
174 {image.path === null ? (
175 <Box width={columns} height={rows} alignItems="center" justifyContent="center">
176 <Text dimColor wrap="truncate">no preview</Text>
177 </Box>
178 ) : thumbs.get(image.path) ? (
179 <Raster
180 key={`blocks-${image.n}`}
181 columns={columns}
182 rows={rows}
183 cells={blockCells(thumbs.get(image.path)!, columns, rows)}
184 />
185 ) : (
186 <Image
187 key={`image-${image.n}`}
188 source={{ file: image.path, format: 'png' }}
189 columns={columns}
190 rows={rows}
191 alt={`[Image #${image.n}]`}
192 />
193 )}
194 <Text dimColor>#{image.n}</Text>
195 </Box>
196 )
197 })}
198 {hidden > 0 && <Text dimColor>+{hidden}</Text>}
199 </Box>
200 {below}
201 </Box>
202 )
203 })
204}
205hooks/layout.ts 132 lines1export type Size = { width: number; height: number }
2export type Cells = { columns: number; rows: number }
3
4const TILE_ROWS = 6
5const MAX_COLUMNS = 32
6const MIN_COLUMNS = 4
7// A terminal cell is about twice as tall as it is wide.
8const CELL_ASPECT = 2
9// Used when the size is unknown (file over $.fs.read's 4 MiB cap, or no file).
10const FALLBACK: Size = { width: 16, height: 10 }
11// Each tile adds a border on every side and a label row under the picture.
12const TILE_CHROME_ROWS = 3
13const TILE_CHROME_COLUMNS = 2
14const GAP = 1
15
16/** The distinct image numbers a draft references, in the order they first appear. */
17export function imageNumbers(draft: string): number[] {
18 const seen = new Set<number>()
19 for (const match of draft.matchAll(/\[Image #(\d+)\]/g)) seen.add(Number(match[1]))
20 return [...seen]
21}
22
23/** Width and height from a PNG's IHDR chunk, or null when the bytes aren't a PNG. */
24export function pngSize(base64: string): Size | null {
25 // 24 bytes cover the signature and IHDR's width and height; 32 base64 chars decode to exactly 24.
26 const head = Uint8Array.from(atob(base64.slice(0, 32)), char => char.charCodeAt(0))
27 const signature = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
28 if (head.length < 24 || signature.some((byte, i) => head[i] !== byte)) return null
29 const view = new DataView(head.buffer, head.byteOffset, head.byteLength)
30 const width = view.getUint32(16)
31 const height = view.getUint32(20)
32 return width > 0 && height > 0 ? { width, height } : null
33}
34
35/** A picture box `rows` tall that keeps the picture's aspect ratio. */
36export function fitCells(size: Size | null, tileRows = TILE_ROWS): Cells {
37 const { width, height } = size ?? FALLBACK
38 let rows = tileRows
39 let columns = Math.round((rows * CELL_ASPECT * width) / height)
40 if (columns > MAX_COLUMNS) {
41 columns = MAX_COLUMNS
42 rows = Math.max(1, Math.round((MAX_COLUMNS * height) / (CELL_ASPECT * width)))
43 }
44 return { columns: Math.max(MIN_COLUMNS, columns), rows: Math.min(rows, tileRows) }
45}
46
47/**
48 * Picture boxes for one row of tiles that fits the band whole, so it never scrolls:
49 * the tallest tiles whose chrome fits in `maxRows` and whose total width fits in `bodyColumns`.
50 * When even one-row tiles are too wide, only the first ones that fit beside a `+N` label for
51 * the rest; none when the band is too small for one tile.
52 */
53export function fitRow(sizes: readonly (Size | null)[], maxRows: number, bodyColumns: number): Cells[] {
54 if (maxRows < TILE_CHROME_ROWS + 1) return []
55 const tallest = Math.min(TILE_ROWS, maxRows - TILE_CHROME_ROWS)
56 for (let tileRows = tallest; tileRows > 1; tileRows--) {
57 const cells = sizes.map(size => fitCells(size, tileRows))
58 if (rowWidth(cells, 0) <= bodyColumns) return cells
59 }
60 const cells = sizes.map(size => fitCells(size, 1))
61 let count = cells.length
62 while (count > 0 && rowWidth(cells.slice(0, count), cells.length - count) > bodyColumns) count--
63 return cells.slice(0, count)
64}
65
66function rowWidth(cells: readonly Cells[], hidden: number): number {
67 const tiles = cells.reduce((sum, c) => sum + c.columns + TILE_CHROME_COLUMNS, 0) + GAP * Math.max(0, cells.length - 1)
68 return hidden > 0 ? tiles + GAP + `+${hidden}`.length : tiles
69}
70
71type Env = Readonly<Record<string, string | undefined>>
72
73/**
74 * Whether Claude Code will draw `Image` as a real picture. It decides that itself and doesn't tell
75 * mods, so this mirrors its rule as far as the environment shows it: always when
76 * CLAUDE_CODE_FORCE_TERMINAL_IMAGES is set; otherwise never inside tmux or screen or in a
77 * background session, and only when the terminal is kitty or Ghostty (it goes by the terminal's
78 * name, so other kitty-protocol terminals such as WezTerm still get the `alt` text).
79 * `CLAUDE_IMAGE_VIEW_RENDERER` overrides the guess: `image` for the picture, `blocks` for colored half blocks.
80 */
81export function drawsPictures(env: Env): boolean {
82 const forced = env.CLAUDE_IMAGE_VIEW_RENDERER
83 if (forced === 'image' || forced === 'blocks') return forced === 'image'
84 if (env.CLAUDE_CODE_FORCE_TERMINAL_IMAGES) return true
85 if (env.TMUX || env.STY || env.CLAUDE_CODE_SESSION_KIND === 'bg') return false
86 return (
87 Boolean(env.KITTY_WINDOW_ID || env.GHOSTTY_RESOURCES_DIR) ||
88 /kitty|ghostty/i.test(env.TERM ?? '') ||
89 /^ghostty$/i.test(env.TERM_PROGRAM ?? '')
90 )
91}
92
93/**
94 * The cells of a `Raster` that draws `rgb` (a `width` x `height` thumbnail) in `columns` x `rows`
95 * cells, base64. Each cell is an upper half block, so it holds two pixels: the top as its
96 * foreground and the bottom as its background.
97 */
98export function blockCells(thumb: { width: number; height: number; rgb: Uint8Array }, columns: number, rows: number): string {
99 const pixelRows = rows * 2
100 const average = (px: number, py: number): number => {
101 // The box of source pixels under output pixel (px, py), at least one pixel wide and tall.
102 const x0 = Math.floor((px * thumb.width) / columns)
103 const x1 = Math.max(x0 + 1, Math.floor(((px + 1) * thumb.width) / columns))
104 const y0 = Math.floor((py * thumb.height) / pixelRows)
105 const y1 = Math.max(y0 + 1, Math.floor(((py + 1) * thumb.height) / pixelRows))
106 let r = 0
107 let g = 0
108 let b = 0
109 let n = 0
110 for (let y = y0; y < y1 && y < thumb.height; y++) {
111 for (let x = x0; x < x1 && x < thumb.width; x++, n++) {
112 const at = (y * thumb.width + x) * 3
113 r += thumb.rgb[at]!
114 g += thumb.rgb[at + 1]!
115 b += thumb.rgb[at + 2]!
116 }
117 }
118 n = n || 1
119 return (Math.round(r / n) << 16) | (Math.round(g / n) << 8) | Math.round(b / n)
120 }
121 const words = new Uint32Array(columns * rows * 3)
122 for (let row = 0; row < rows; row++) {
123 for (let column = 0; column < columns; column++) {
124 const at = (row * columns + column) * 3
125 words[at] = 0x2580
126 words[at + 1] = average(column, row * 2)
127 words[at + 2] = average(column, row * 2 + 1)
128 }
129 }
130 return (new Uint8Array(words.buffer) as Uint8Array & { toBase64(): string }).toBase64()
131}
132hooks/png.ts 289 lines1// A small PNG decoder for terminals that can't draw pictures. A mod has no zlib, so this
2// carries its own inflate. It box-averages the picture down to a thumbnail as it unfilters,
3// so a screenshot never has to sit in memory as RGBA.
4
5export type Thumb = { width: number; height: number; rgb: Uint8Array }
6
7// The longest side of the stored thumbnail, in pixels: more than any tile can show.
8const THUMB_SIDE = 96
9// What transparent pixels are composited over (the dark of most terminal themes).
10const BACKDROP = 0x1e
11// Refuse a picture whose raw scanlines would be bigger than this (an Adam7-free 8k x 8k RGBA).
12const MAX_RAW_BYTES = 256 * 1024 * 1024
13
14const LENGTH_BASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]
15const LENGTH_EXTRA = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]
16const DISTANCE_BASE = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]
17const DISTANCE_EXTRA = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]
18const CODE_LENGTH_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]
19
20type Huffman = { table: Uint32Array; bits: number }
21
22/** A lookup table indexed by the next `bits` input bits: (code length << 16) | symbol. */
23function huffman(lengths: ArrayLike<number>): Huffman {
24 let bits = 0
25 const count = new Array<number>(16).fill(0)
26 for (let i = 0; i < lengths.length; i++) {
27 count[lengths[i]!]!++
28 if (lengths[i]! > bits) bits = lengths[i]!
29 }
30 count[0] = 0
31 const next = new Array<number>(16).fill(0)
32 for (let len = 1, code = 0; len < 16; len++) {
33 code = (code + count[len - 1]!) << 1
34 next[len] = code
35 }
36 const size = 1 << Math.max(bits, 1)
37 const table = new Uint32Array(size)
38 for (let symbol = 0; symbol < lengths.length; symbol++) {
39 const len = lengths[symbol]!
40 if (len === 0) continue
41 let code = next[len]!++
42 let reversed = 0
43 for (let i = 0; i < len; i++, code >>= 1) reversed = (reversed << 1) | (code & 1)
44 for (let at = reversed; at < size; at += 1 << len) table[at] = (len << 16) | symbol
45 }
46 return { table, bits: Math.max(bits, 1) }
47}
48
49let fixedLiterals: Huffman | undefined
50let fixedDistances: Huffman | undefined
51
52/** Inflate a zlib stream into exactly `size` bytes; null when it is malformed or short. */
53export function inflate(input: Uint8Array, size: number): Uint8Array | null {
54 if (input.length < 2 || (input[0]! & 0x0f) !== 8) return null
55 const out = new Uint8Array(size)
56 let pos = 2
57 let written = 0
58 let buffer = 0
59 let held = 0
60
61 const need = (n: number) => {
62 while (held < n) {
63 buffer |= (pos < input.length ? input[pos]! : 0) << held
64 pos++
65 held += 8
66 }
67 }
68 const take = (n: number) => {
69 if (n === 0) return 0
70 need(n)
71 const value = buffer & ((1 << n) - 1)
72 buffer >>>= n
73 held -= n
74 return value
75 }
76 const decode = ({ table, bits }: Huffman) => {
77 need(bits)
78 const entry = table[buffer & ((1 << bits) - 1)]!
79 const len = entry >>> 16
80 if (len === 0) return -1
81 buffer >>>= len
82 held -= len
83 return entry & 0xffff
84 }
85
86 let last = false
87 while (!last) {
88 if (pos > input.length + 4) return null
89 last = take(1) === 1
90 const type = take(2)
91 if (type === 0) {
92 take(held & 7)
93 const len = take(16)
94 take(16)
95 for (let i = 0; i < len && written < size; i++) out[written++] = take(8)
96 continue
97 }
98 let literals: Huffman
99 let distances: Huffman
100 if (type === 1) {
101 if (!fixedLiterals || !fixedDistances) {
102 const lengths = new Uint8Array(288)
103 lengths.fill(8, 0, 144).fill(9, 144, 256).fill(7, 256, 280).fill(8, 280, 288)
104 fixedLiterals = huffman(lengths)
105 fixedDistances = huffman(new Uint8Array(30).fill(5))
106 }
107 literals = fixedLiterals
108 distances = fixedDistances
109 } else if (type === 2) {
110 const nLiterals = take(5) + 257
111 const nDistances = take(5) + 1
112 const nCodes = take(4) + 4
113 const codeLengths = new Uint8Array(19)
114 for (let i = 0; i < nCodes; i++) codeLengths[CODE_LENGTH_ORDER[i]!] = take(3)
115 const codes = huffman(codeLengths)
116 const lengths = new Uint8Array(nLiterals + nDistances)
117 for (let i = 0; i < lengths.length; ) {
118 const symbol = decode(codes)
119 if (symbol < 0) return null
120 if (symbol < 16) {
121 lengths[i++] = symbol
122 continue
123 }
124 const repeat = symbol === 16 ? 3 + take(2) : symbol === 17 ? 3 + take(3) : 11 + take(7)
125 const value = symbol === 16 ? lengths[i - 1]! : 0
126 if (symbol === 16 && i === 0) return null
127 for (let r = 0; r < repeat && i < lengths.length; r++) lengths[i++] = value
128 }
129 literals = huffman(lengths.subarray(0, nLiterals))
130 distances = huffman(lengths.subarray(nLiterals))
131 } else {
132 return null
133 }
134
135 for (;;) {
136 const symbol = decode(literals)
137 if (symbol < 0) return null
138 if (symbol < 256) {
139 if (written >= size) return null
140 out[written++] = symbol
141 continue
142 }
143 if (symbol === 256) break
144 const lengthIndex = symbol - 257
145 if (lengthIndex >= 29) return null
146 const length = LENGTH_BASE[lengthIndex]! + take(LENGTH_EXTRA[lengthIndex]!)
147 const distanceSymbol = decode(distances)
148 if (distanceSymbol < 0 || distanceSymbol >= 30) return null
149 const distance = DISTANCE_BASE[distanceSymbol]! + take(DISTANCE_EXTRA[distanceSymbol]!)
150 if (distance > written || written + length > size) return null
151 for (let i = 0; i < length; i++, written++) out[written] = out[written - distance]!
152 }
153 }
154 return written === size ? out : null
155}
156
157const SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
158// Samples per pixel by PNG color type: gray, -, RGB, palette, gray + alpha, -, RGBA.
159const CHANNELS = [1, 0, 3, 1, 2, 0, 4]
160
161/**
162 * Decode a PNG into a small RGB thumbnail (longest side at most THUMB_SIDE), or null when it
163 * isn't a PNG, is interlaced, or is malformed. Transparent pixels are composited over a dark backdrop.
164 */
165export function decodeThumb(png: Uint8Array): Thumb | null {
166 if (png.length < 33 || SIGNATURE.some((byte, i) => png[i] !== byte)) return null
167 const view = new DataView(png.buffer, png.byteOffset, png.byteLength)
168
169 let width = 0
170 let height = 0
171 let depth = 0
172 let colorType = 0
173 let palette = new Uint8Array(0)
174 let paletteAlpha = new Uint8Array(0)
175 const data: Uint8Array[] = []
176 for (let at = 8; at + 8 <= png.length; ) {
177 const length = view.getUint32(at)
178 const name = String.fromCharCode(png[at + 4]!, png[at + 5]!, png[at + 6]!, png[at + 7]!)
179 const body = png.subarray(at + 8, at + 8 + length)
180 if (body.length < length) return null
181 if (name === 'IHDR' && length >= 13) {
182 width = view.getUint32(at + 8)
183 height = view.getUint32(at + 12)
184 depth = body[8]!
185 colorType = body[9]!
186 if (body[10] !== 0 || body[11] !== 0 || body[12] !== 0) return null // not deflate, or interlaced
187 } else if (name === 'PLTE') palette = body
188 else if (name === 'tRNS') paletteAlpha = body
189 else if (name === 'IDAT') data.push(body)
190 else if (name === 'IEND') break
191 at += 12 + length
192 }
193 const channels = CHANNELS[colorType] ?? 0
194 if (width === 0 || height === 0 || channels === 0 || ![1, 2, 4, 8, 16].includes(depth)) return null
195 if (colorType === 3 && (palette.length === 0 || depth === 16)) return null
196
197 const bitsPerPixel = channels * depth
198 const stride = Math.ceil((width * bitsPerPixel) / 8)
199 const step = Math.max(1, bitsPerPixel >> 3)
200 if ((stride + 1) * height > MAX_RAW_BYTES) return null
201
202 const joined = new Uint8Array(data.reduce((sum, chunk) => sum + chunk.length, 0))
203 for (let i = 0, offset = 0; i < data.length; offset += data[i]!.length, i++) joined.set(data[i]!, offset)
204 const raw = inflate(joined, (stride + 1) * height)
205 if (!raw) return null
206
207 const scale = Math.min(1, THUMB_SIDE / Math.max(width, height))
208 const tw = Math.max(1, Math.round(width * scale))
209 const th = Math.max(1, Math.round(height * scale))
210 const sums = new Float64Array(tw * th * 3)
211 const counts = new Uint32Array(tw * th)
212 const columnOf = new Uint16Array(width)
213 for (let x = 0; x < width; x++) columnOf[x] = Math.min(tw - 1, Math.floor((x * tw) / width))
214
215 const max = (1 << Math.min(depth, 8)) - 1
216 const unit = depth === 16 ? 1 : 255 / max
217 let prior = new Uint8Array(stride)
218 let line = new Uint8Array(stride)
219 const sample = (x: number, channel: number) => {
220 if (depth === 8) return line[x * channels + channel]!
221 if (depth === 16) return line[(x * channels + channel) * 2]!
222 const bit = (x * channels + channel) * depth
223 return (line[bit >> 3]! >> (8 - depth - (bit & 7))) & max
224 }
225
226 for (let y = 0; y < height; y++) {
227 const filter = raw[y * (stride + 1)]!
228 const source = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1))
229 for (let i = 0; i < stride; i++) {
230 const left = i >= step ? line[i - step]! : 0
231 const up = prior[i]!
232 const upLeft = i >= step ? prior[i - step]! : 0
233 let predicted = 0
234 if (filter === 1) predicted = left
235 else if (filter === 2) predicted = up
236 else if (filter === 3) predicted = (left + up) >> 1
237 else if (filter === 4) {
238 const p = left + up - upLeft
239 const pa = Math.abs(p - left)
240 const pb = Math.abs(p - up)
241 const pc = Math.abs(p - upLeft)
242 predicted = pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft
243 } else if (filter !== 0) return null
244 line[i] = (source[i]! + predicted) & 0xff
245 }
246
247 const row = Math.min(th - 1, Math.floor((y * th) / height))
248 for (let x = 0; x < width; x++) {
249 let r: number
250 let g: number
251 let b: number
252 let a = 255
253 if (colorType === 2 || colorType === 6) {
254 r = sample(x, 0)
255 g = sample(x, 1)
256 b = sample(x, 2)
257 if (colorType === 6) a = sample(x, 3)
258 } else if (colorType === 3) {
259 const index = sample(x, 0)
260 r = palette[index * 3] ?? 0
261 g = palette[index * 3 + 1] ?? 0
262 b = palette[index * 3 + 2] ?? 0
263 a = paletteAlpha[index] ?? 255
264 } else {
265 r = g = b = Math.round(sample(x, 0) * unit)
266 if (colorType === 4) a = sample(x, 1)
267 }
268 if (a < 255) {
269 r = (r * a + BACKDROP * (255 - a)) / 255
270 g = (g * a + BACKDROP * (255 - a)) / 255
271 b = (b * a + BACKDROP * (255 - a)) / 255
272 }
273 const cell = row * tw + columnOf[x]!
274 sums[cell * 3] += r
275 sums[cell * 3 + 1] += g
276 sums[cell * 3 + 2] += b
277 counts[cell]!++
278 }
279 ;[prior, line] = [line, prior]
280 }
281
282 const rgb = new Uint8Array(tw * th * 3)
283 for (let cell = 0; cell < tw * th; cell++) {
284 const n = counts[cell] || 1
285 for (let c = 0; c < 3; c++) rgb[cell * 3 + c] = Math.round(sums[cell * 3 + c]! / n)
286 }
287 return { width: tw, height: th, rgb }
288}
289types/index.d.ts 14 lines1export type PastedImage = {
2 n: number
3 /** Absolute path of the PNG to draw (the cached paste or its converted copy); null when it can't be found. */
4 path: string | null
5 /** Pixel size; null when unknown, and the tile falls back to a default shape. */
6 size: { width: number; height: number } | null
7}
8
9declare module 'claude-code' {
10 interface PluginState {
11 'image-view': { images: PastedImage[] }
12 }
13}
14