From f03714d29865576bf449ac45197ee796b654e57d Mon Sep 17 00:00:00 2001 From: ci-check Date: Wed, 11 Nov 2026 18:05:39 +0800 Subject: [PATCH] fix: read BMP BITMAPCOREHEADER 16-bit dims by DIB header size (#6179) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bmpDims always used the 32-bit BITMAPINFOHEADER layout, so a CORE header (bfSize==12) fused height+planes into width and read the bit-count field as height — a valid 64×128 CORE BMP billed 765 tokens instead of the true 1105. Select the dimension layout by the DIB size field: 12 → 16-bit dims at offsets 16/18; >=40 → 32-bit signed dims at 18/22 (top-down magnitude preserved, negative width rejected); any other short/unknown layout → undefined fallback to flat cost. Closes #6179 --- src/image-tokens.ts | 20 +++++++++- tests/issue767-pixel-billing.test.ts | 59 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/image-tokens.ts b/src/image-tokens.ts index 91d85e16b9..00a7302ff6 100644 --- a/src/image-tokens.ts +++ b/src/image-tokens.ts @@ -85,8 +85,24 @@ function gifDims(b: Buffer): Dims | undefined { function bmpDims(b: Buffer): Dims | undefined { if (b.length < 26 || b[0] !== 0x42 || b[1] !== 0x4d) return undefined; // "BM" - const w = b.readUInt32LE(18), h = Math.abs(b.readInt32LE(22)); - return w > 0 && h > 0 ? { w, h } : undefined; + // The DIB header follows the 14-byte file header; its leading size field + // selects the dimension layout (#6179): BITMAPCOREHEADER (size==12) stores + // 16-bit dims at offsets 16 (width) / 18 (height), while BITMAPINFOHEADER + // and every V2–V5 extension (size>=40) store 32-bit signed dims at 18/22. + // Reading the low 16 bits discriminates both cleanly — all valid sizes carry + // zero high bits, so it is exact for the WORD-sized CORE field too. + const dibSize = b.readUInt16LE(14); + if (dibSize === 12) { + const w = b.readUInt16LE(16), h = b.readUInt16LE(18); + return w > 0 && h > 0 ? { w, h } : undefined; + } + if (dibSize >= 40) { + // Negative height encodes top-down scan order — take its magnitude. A + // negative width is invalid and rejected by the w > 0 gate below. + const w = b.readInt32LE(18), h = Math.abs(b.readInt32LE(22)); + return w > 0 && h > 0 ? { w, h } : undefined; + } + return undefined; // unsupported short/unknown DIB layout } function webpDims(b: Buffer): Dims | undefined { diff --git a/tests/issue767-pixel-billing.test.ts b/tests/issue767-pixel-billing.test.ts index 6cf2958767..c376d0e36e 100644 --- a/tests/issue767-pixel-billing.test.ts +++ b/tests/issue767-pixel-billing.test.ts @@ -64,6 +64,45 @@ function bmpB64(w: number, h: number): string { return b.toString("base64"); } +// #6179: BITMAPCOREHEADER variant — bfSize==12 marker at offset 14 with 16-bit +// dims at offsets 16 (w) / 18 (h). Distinct from bmpB64 above (INFO, biSize==40). +function bmpCoreB64(w: number, h: number): string { + const b = Buffer.alloc(26); + b.write("BM", 0, "ascii"); + b.writeUInt32LE(26, 2); + b.writeUInt32LE(26, 10); + b.writeUInt16LE(12, 14); // bfSize = 12 → CORE layout + b.writeUInt16LE(w, 16); + b.writeUInt16LE(h, 18); + b.writeUInt16LE(1, 20); // planes + b.writeUInt16LE(24, 22); // bit count + return b.toString("base64"); +} + +// #6179: an unsupported short DIB size (neither 12 nor >=40) must be rejected. +function bmpShortDibB64(dibSize: number): string { + const b = Buffer.alloc(26); + b.write("BM", 0, "ascii"); + b.writeUInt32LE(26, 2); + b.writeUInt32LE(26, 10); + b.writeUInt16LE(dibSize, 14); + b.writeUInt16LE(100, 16); + b.writeUInt16LE(100, 18); + return b.toString("base64"); +} + +// #6179: an INFO header with a negative width is invalid and must be rejected. +function bmpNegWidthInfoB64(): string { + const b = Buffer.alloc(40); + b.write("BM", 0, "ascii"); + b.writeUInt32LE(40, 2); + b.writeUInt32LE(40, 10); + b.writeUInt32LE(40, 14); // biSize = 40 → INFO layout + b.writeInt32LE(-1, 18); // negative width + b.writeInt32LE(100, 22); + return b.toString("base64"); +} + function webpVp8B64(w: number, h: number): string { const b = Buffer.alloc(32); b.write("RIFF", 0, "ascii"); @@ -145,6 +184,26 @@ test("#767 unit: decodeImageDims parses container headers without full decode", assert.equal(decodeImageDims(Buffer.from([0x89, 0x50, 0x4e, 0x47]).toString("base64")), undefined, "truncated header → fallback"); }); +test("#6179 unit: BITMAPCOREHEADER reads 16-bit dims — no fused-width misparse", () => { + // A valid 64×128 CORE-header BMP used to parse as {w:65664,h:24}: the old reader + // always used the 32-bit INFO layout, fusing height+planes into width and reading + // the bit-count field as height → billed 765 tokens instead of the true 1105. + assert.deepEqual(decodeImageDims(bmpCoreB64(64, 128)), { w: 64, h: 128 }, "CORE 64×128 reads true dims"); + assert.deepEqual(decodeImageDims(bmpCoreB64(32, 16)), { w: 32, h: 16 }, "CORE small image"); + assert.deepEqual(decodeImageDims(bmpCoreB64(1000, 500)), { w: 1000, h: 500 }, "CORE large image"); + // The INFO path is unchanged: bottom-up (bmpB64) and top-down magnitude preserved. + assert.deepEqual(decodeImageDims(bmpB64(640, 480)), { w: 640, h: 480 }, "INFO bottom-up unchanged"); + assert.deepEqual(decodeImageDims(bmpB64(100, -50)), { w: 100, h: 50 }, "INFO top-down magnitude preserved"); + // Reject invalid / unsupported layouts rather than guessing dimensions. + assert.equal(decodeImageDims(bmpNegWidthInfoB64()), undefined, "negative INFO width rejected"); + assert.equal(decodeImageDims(bmpShortDibB64(8)), undefined, "unsupported short DIB (size 8) rejected"); + assert.equal(decodeImageDims(bmpShortDibB64(11)), undefined, "unsupported short DIB (size 11) rejected"); + // End-to-end: the 64×128 CORE BMP bills its TRUE dims through pixels mode, + // not the misparsed fused-width estimate. + const coreBody = { input: [{ type: "message", role: "user", content: [{ type: "input_image", image_url: `data:image/bmp;base64,${bmpCoreB64(64, 128)}` }] }] }; + assert.equal(imageTokensInParsedBody("responses", coreBody, "pixels"), pixelTileEstimate(64, 128), "64×128 CORE bills its true tile cost"); +}); + test("#767 unit: pixelTileEstimate matches the OpenAI high-detail tile model", () => { assert.equal(pixelTileEstimate(100, 100), 765, "small square scales up to 768×768 = 4 tiles"); assert.equal(pixelTileEstimate(2048, 2048), 2805, "max-size square = 16 tiles (the ceiling)");