mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(browser): return a fitting JPEG redraw without decoding it
This commit is contained in:
parent
9a98f89938
commit
22902fc4d1
3 changed files with 46 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { expect, test } from "bun:test"
|
||||
import { captureCoordinator, cropPixels, pngFits, undrawable, undrawableMessage } from "./capture"
|
||||
import { captureCoordinator, cropPixels, jpegFits, pngFits, undrawable, undrawableMessage } from "./capture"
|
||||
|
||||
const shown = { minimized: false, visible: true, appHidden: false }
|
||||
|
||||
|
|
@ -95,6 +95,28 @@ test("reuse the redraw's PNG only when its actual dimensions and transfer size f
|
|||
expect(pngFits(png, 800, 32)).toBe(false)
|
||||
})
|
||||
|
||||
test("reuse the redraw's JPEG only when its frame header dimensions and transfer size fit", () => {
|
||||
const segment = (marker: number, body: number[]) => [0xff, marker, (body.length + 2) >> 8, (body.length + 2) & 0xff, ...body]
|
||||
|
||||
const frame = (marker: number, width: number, height: number) =>
|
||||
segment(marker, [8, height >> 8, height & 0xff, width >> 8, width & 0xff, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1])
|
||||
|
||||
const jpeg = (...segments: number[][]) => Buffer.from([0xff, 0xd8, ...segments.flat(), 0xff, 0xd9])
|
||||
// JFIF and a Huffman table (0xc4, in the frame-marker range) precede the frame header; the scan follows it.
|
||||
const app = segment(0xe0, [0x4a, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0])
|
||||
const table = segment(0xc4, Array(20).fill(0))
|
||||
const scan = segment(0xda, [1, 1, 0, 0, 0x3f, 0])
|
||||
const image = jpeg(app, table, frame(0xc0, 800, 600), scan)
|
||||
expect(jpegFits(image, 800, image.length)).toBe(true)
|
||||
expect(jpegFits(image, 799, image.length)).toBe(false)
|
||||
expect(jpegFits(image, 800, image.length - 1)).toBe(false)
|
||||
expect(jpegFits(jpeg(app, frame(0xc2, 800, 600), scan), 800, 1_000)).toBe(true)
|
||||
expect(jpegFits(jpeg(app, frame(0xc0, 800, 0), scan), 800, 1_000)).toBe(false)
|
||||
expect(jpegFits(jpeg(app, scan, frame(0xc0, 800, 600)), 800, 1_000)).toBe(false)
|
||||
expect(jpegFits(image.subarray(0, 30), 800, 1_000)).toBe(false)
|
||||
expect(jpegFits(Buffer.from([0x89, 0x50, 0x4e, 0x47]), 800, 1_000)).toBe(false)
|
||||
})
|
||||
|
||||
test.each([
|
||||
{ platform: "darwin", window: shown, expected: undefined },
|
||||
{ platform: "darwin", window: { ...shown, minimized: true, visible: false }, expected: "minimized" },
|
||||
|
|
|
|||
|
|
@ -87,6 +87,26 @@ export function pngFits(png: Buffer, maxWidth: number, maxBytes: number) {
|
|||
return width > 0 && width <= maxWidth && png.readUInt32BE(20) > 0
|
||||
}
|
||||
|
||||
/** CDP produced a JPEG. Read its frame header dimensions, as for a PNG, so the final encoding needs no decode. */
|
||||
export function jpegFits(jpeg: Buffer, maxWidth: number, maxBytes: number) {
|
||||
if (jpeg.length < 4 || jpeg.length > maxBytes || jpeg.readUInt16BE(0) !== 0xffd8) return false
|
||||
|
||||
for (let at = 2; at + 9 <= jpeg.length; at += 2 + jpeg.readUInt16BE(at + 2)) {
|
||||
const marker = jpeg[at + 1]
|
||||
|
||||
if (jpeg[at] !== 0xff || marker === 0xd9 || marker === 0xda) return false
|
||||
|
||||
// Start-of-frame markers; 0xc4, 0xc8 and 0xcc share the range but are not frames.
|
||||
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
|
||||
const width = jpeg.readUInt16BE(at + 7)
|
||||
|
||||
return width > 0 && width <= maxWidth && jpeg.readUInt16BE(at + 5) > 0
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* macOS stops drawing a minimized window and every window of a hidden app, and a copy of the tab's frame then never
|
||||
* answers. Windows keeps drawing a minimized window, and Linux is left to the capture deadline.
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import electron, { type BrowserWindow, type WebContents } from "electron"
|
|||
import type { Protocol } from "devtools-protocol"
|
||||
import { Schema } from "effect"
|
||||
import type { Embeds } from "@opencode/plugin/gui/main"
|
||||
import { captureCoordinator, cropPixels, pngFits, undrawable, undrawableMessage } from "./capture"
|
||||
import { captureCoordinator, cropPixels, jpegFits, pngFits, undrawable, undrawableMessage } from "./capture"
|
||||
import { createCdp, abortError, waitFor } from "./cdp"
|
||||
import { navigationDeadline } from "./navigation-deadline"
|
||||
import { attachChildren, releaseWorker } from "./child-targets"
|
||||
|
|
@ -1947,7 +1947,8 @@ export function createBrowserPage(
|
|||
abortError(signal)
|
||||
const bytes = Buffer.from(shot.data, "base64")
|
||||
|
||||
if (!clip && format === "png" && pngFits(bytes, maxWidth, Browser.MAX_FILE_BYTES)) return { format, data: bytes }
|
||||
if (!clip && encoding === format && (format === "png" ? pngFits : jpegFits)(bytes, maxWidth, Browser.MAX_FILE_BYTES))
|
||||
return { format, data: bytes }
|
||||
|
||||
const viewportImage = electron.nativeImage.createFromBuffer(bytes)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue