| 1234567891011121314151617181920212223242526272829303132 |
- // Synthetic header/allocation regressions; complete image decoding is intentionally separate.
- import assert from 'node:assert/strict'
- import { rasterDimensions, rasterAllocation, tileRasterBudget, rasterLimits } from './raster-budget.mjs'
- function png(width, height) {
- const bytes = Buffer.alloc(33)
- Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]).copy(bytes)
- bytes.writeUInt32BE(13, 8); bytes.write('IHDR', 12); bytes.writeUInt32BE(width, 16); bytes.writeUInt32BE(height, 20)
- return { bytes, mimeType: 'image/png' }
- }
- function jpeg(marker, width, height) {
- const bytes = Buffer.from([255, 216, 255, 224, 0, 4, 0, 0, 255, marker, 0, 11, 8, 0, 0, 0, 0, 1, 1, 17, 0])
- bytes.writeUInt16BE(height, 13); bytes.writeUInt16BE(width, 15)
- return bytes
- }
- assert.deepEqual(rasterDimensions(png(256, 32).bytes, 'image/png'), { width: 256, height: 32 })
- for (const marker of [192, 194]) assert.deepEqual(rasterDimensions(jpeg(marker, 300, 200), 'image/jpeg'), { width: 300, height: 200 })
- assert.equal(rasterAllocation(png(1, 1).bytes, 'image/png').mipBytes, 4)
- assert.equal(rasterAllocation(png(3, 5).bytes, 'image/png').mipBytes, 72)
- const image = png(512, 512)
- const one = tileRasterBudget([image], [{ source: 0 }])
- assert.equal(one.rgbaBytes, 1048576); assert.equal(one.decodedTextureBytes, 1398100)
- assert.equal(tileRasterBudget([image], [{ source: 0 }, { source: 0 }]).decodedTextureBytes, 2796200)
- assert.equal(tileRasterBudget([], []).decodedTextureBytes, 0)
- assert.throws(() => rasterAllocation(png(65536, 65536).bytes, 'image/png'), /dimension budget/)
- assert.throws(() => rasterDimensions(png(0, 256).bytes, 'image/png'), /dimensions/)
- assert.throws(() => rasterDimensions(image.bytes.subarray(0, 20), 'image/png'), /header/)
- assert.throws(() => rasterDimensions(jpeg(194, 30, 20).subarray(0, 15), 'image/jpeg'), /truncated/)
- assert.throws(() => rasterDimensions(Buffer.from([255, 216, 255, 218]), 'image/jpeg'), /missing JPEG SOF/)
- assert.throws(() => tileRasterBudget([image], [{ source: 1 }]), /image index/)
- assert.throws(() => tileRasterBudget([image], [{ source: 0 }], { ...rasterLimits, maxRasterBytes: 32 }), /embedded raster/)
- assert.throws(() => tileRasterBudget([image], Array.from({ length: 4 }, () => ({ source: 0 }))), /decoded texture budget/)
- console.log(JSON.stringify({ passed: true, scope: 'synthetic PNG/JPEG headers, dimensions, RGBA8 mip allocations, rejection paths' }))
|