{
  "version": 3,
  "sources": ["../src/heic-parser.ts"],
  "sourcesContent": ["/* eslint-disable no-bitwise */\n\n/**\n * Lightweight HEIC (ISOBMFF) container parser.\n *\n * Extracts the HEVC decoder configuration and compressed image data from\n * HEIC files, including grid/tiled images (the common iPhone format).\n * This enables decoding via the WebCodecs VideoDecoder API without\n * shipping our own HEVC codec.\n *\n * Only the container format is parsed here (no patented codec involved).\n * Actual HEVC decoding is delegated to the browser's platform decoder.\n */\n\n/** A single tile to decode and draw. */\nexport interface HeicTile {\n\t/** Raw HEVC bitstream for this tile. */\n\tdata: Uint8Array;\n\t/** X position on the output canvas. */\n\tx: number;\n\t/** Y position on the output canvas. */\n\ty: number;\n}\n\nexport interface HeicImageData {\n\t/** HEVC codec string for VideoDecoder (e.g. 'hvc1.1.6.L93.B0'). */\n\tcodecString: string;\n\t/** Raw HEVCDecoderConfigurationRecord bytes for VideoDecoder description. */\n\tdescription: Uint8Array;\n\t/** Tiles to decode (1 for single-image, many for grid). */\n\ttiles: HeicTile[];\n\t/** Width of each HEVC tile in pixels. */\n\ttileWidth: number;\n\t/** Height of each HEVC tile in pixels. */\n\ttileHeight: number;\n\t/** Final output width in pixels. */\n\toutputWidth: number;\n\t/** Final output height in pixels. */\n\toutputHeight: number;\n\t/** Rotation angle in degrees counter-clockwise (0, 90, 180, 270). */\n\trotation: number;\n\t/**\n\t * EXIF orientation (1-8) to apply when there is no native `irot`/`imir`\n\t * transform.\n\t *\n\t * libheif applies the `irot`/`imir` boxes but ignores EXIF orientation for\n\t * HEIF-family inputs, so this captures the orientation for files that carry\n\t * it in an EXIF tag instead. It is 1 (no change) whenever a native transform\n\t * is present, so the two are never applied together. This mirrors\n\t * `getUnappliedExifOrientation` so the canvas decode path and the sub-size\n\t * generation path agree on mirror-only inputs.\n\t */\n\texifOrientation: number;\n}\n\n// ---------------------------------------------------------------------------\n// Binary reader\n// ---------------------------------------------------------------------------\n\nclass Reader {\n\treadonly view: DataView;\n\treadonly buffer: ArrayBuffer;\n\tpos: number;\n\n\tconstructor( buffer: ArrayBuffer, offset = 0 ) {\n\t\tthis.buffer = buffer;\n\t\tthis.view = new DataView( buffer );\n\t\tthis.pos = offset;\n\t}\n\n\tu8(): number {\n\t\tconst v = this.view.getUint8( this.pos );\n\t\tthis.pos += 1;\n\t\treturn v;\n\t}\n\n\tu16(): number {\n\t\tconst v = this.view.getUint16( this.pos );\n\t\tthis.pos += 2;\n\t\treturn v;\n\t}\n\n\tu32(): number {\n\t\tconst v = this.view.getUint32( this.pos );\n\t\tthis.pos += 4;\n\t\treturn v;\n\t}\n\n\tu64(): number {\n\t\tconst hi = this.view.getUint32( this.pos );\n\t\tconst lo = this.view.getUint32( this.pos + 4 );\n\t\tthis.pos += 8;\n\t\treturn hi * 0x100000000 + lo;\n\t}\n\n\t/**\n\t * Read a variable-width unsigned integer (0, 4 or 8 bytes).\n\t *\n\t * @param size Byte width to read (0, 4, or 8).\n\t */\n\tuN( size: number ): number {\n\t\tif ( size === 0 ) {\n\t\t\treturn 0;\n\t\t}\n\t\tif ( size === 4 ) {\n\t\t\treturn this.u32();\n\t\t}\n\t\tif ( size === 8 ) {\n\t\t\treturn this.u64();\n\t\t}\n\t\tthrow new Error( `Unsupported uint size: ${ size }` );\n\t}\n\n\tstr( len: number ): string {\n\t\tlet s = '';\n\t\tfor ( let i = 0; i < len; i++ ) {\n\t\t\ts += String.fromCharCode( this.view.getUint8( this.pos + i ) );\n\t\t}\n\t\tthis.pos += len;\n\t\treturn s;\n\t}\n\n\tbytes( len: number ): Uint8Array {\n\t\tconst b = new Uint8Array( this.buffer, this.pos, len );\n\t\tthis.pos += len;\n\t\treturn new Uint8Array( b ); // copy to avoid detach issues\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// ISOBMFF box helpers\n// ---------------------------------------------------------------------------\n\ninterface BoxInfo {\n\ttype: string;\n\toffset: number;\n\tsize: number;\n\theaderSize: number;\n}\n\nfunction readBox( r: Reader ): BoxInfo | null {\n\tif ( r.pos + 8 > r.view.byteLength ) {\n\t\treturn null;\n\t}\n\tconst offset = r.pos;\n\tlet size: number = r.u32();\n\tconst type = r.str( 4 );\n\tlet headerSize = 8;\n\n\tif ( size === 1 ) {\n\t\tsize = r.u64();\n\t\theaderSize = 16;\n\t} else if ( size === 0 ) {\n\t\tsize = r.view.byteLength - offset;\n\t}\n\n\treturn { type, offset, size, headerSize };\n}\n\nfunction findBoxes( r: Reader, start: number, end: number ): BoxInfo[] {\n\tconst boxes: BoxInfo[] = [];\n\tr.pos = start;\n\twhile ( r.pos < end ) {\n\t\tconst box = readBox( r );\n\t\tif ( ! box || box.size < 8 ) {\n\t\t\tbreak;\n\t\t}\n\t\tboxes.push( box );\n\t\tr.pos = box.offset + box.size;\n\t}\n\treturn boxes;\n}\n\nfunction findBox(\n\tr: Reader,\n\tstart: number,\n\tend: number,\n\ttype: string\n): BoxInfo | undefined {\n\tr.pos = start;\n\twhile ( r.pos < end ) {\n\t\tconst box = readBox( r );\n\t\tif ( ! box || box.size < 8 ) {\n\t\t\tbreak;\n\t\t}\n\t\tif ( box.type === type ) {\n\t\t\treturn box;\n\t\t}\n\t\tr.pos = box.offset + box.size;\n\t}\n\treturn undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Specific box parsers\n// ---------------------------------------------------------------------------\n\n/**\n * Parse Primary Item Box → primary item ID.\n *\n * @param r   Binary reader.\n * @param box BoxInfo for the pitm box.\n */\nfunction parsePitm( r: Reader, box: BoxInfo ): number {\n\tr.pos = box.offset + box.headerSize;\n\tconst version = r.u8();\n\tr.pos += 3; // flags\n\treturn version === 0 ? r.u16() : r.u32();\n}\n\ninterface ItemExtent {\n\toffset: number;\n\tlength: number;\n}\n\ninterface ItemLocation {\n\t/** 0 = file offset (mdat), 1 = idat offset. */\n\tconstructionMethod: number;\n\textents: ItemExtent[];\n}\n\n/**\n * Parse Item Location Box → map of item ID to data extents.\n *\n * @param r   Binary reader.\n * @param box BoxInfo for the iloc box.\n */\nfunction parseIloc( r: Reader, box: BoxInfo ): Map< number, ItemLocation > {\n\tr.pos = box.offset + box.headerSize;\n\tconst version = r.u8();\n\tr.pos += 3; // flags\n\n\tconst byte1 = r.u8();\n\tconst offsetSize = ( byte1 >> 4 ) & 0xf;\n\tconst lengthSize = byte1 & 0xf;\n\n\tconst byte2 = r.u8();\n\tconst baseOffsetSize = ( byte2 >> 4 ) & 0xf;\n\tconst indexSize = version >= 1 ? byte2 & 0xf : 0;\n\n\tconst itemCount = version < 2 ? r.u16() : r.u32();\n\tconst items = new Map< number, ItemLocation >();\n\n\tfor ( let i = 0; i < itemCount; i++ ) {\n\t\tconst itemId = version < 2 ? r.u16() : r.u32();\n\n\t\tlet constructionMethod = 0;\n\t\tif ( version === 1 || version === 2 ) {\n\t\t\tconst cm = r.u16();\n\t\t\tconstructionMethod = cm & 0xf; // lower 4 bits\n\t\t}\n\n\t\tr.u16(); // data_reference_index\n\t\tconst baseOffset = r.uN( baseOffsetSize );\n\t\tconst extentCount = r.u16();\n\t\tconst extents: ItemExtent[] = [];\n\n\t\tfor ( let j = 0; j < extentCount; j++ ) {\n\t\t\tif ( version >= 1 ) {\n\t\t\t\tr.uN( indexSize ); // extent_index — skip\n\t\t\t}\n\t\t\tconst extOffset = r.uN( offsetSize );\n\t\t\tconst extLength = r.uN( lengthSize );\n\t\t\textents.push( {\n\t\t\t\toffset: baseOffset + extOffset,\n\t\t\t\tlength: extLength,\n\t\t\t} );\n\t\t}\n\n\t\titems.set( itemId, { constructionMethod, extents } );\n\t}\n\n\treturn items;\n}\n\n/**\n * Parse Item Property Association Box → map of item ID to 1-based property indices.\n *\n * @param r   Binary reader.\n * @param box BoxInfo for the ipma box.\n */\nfunction parseIpma( r: Reader, box: BoxInfo ): Map< number, number[] > {\n\tr.pos = box.offset + box.headerSize;\n\tconst vf = r.u32(); // version (8 bits) + flags (24 bits)\n\tconst version = vf >>> 24;\n\tconst flags = vf & 0xffffff;\n\tconst largeIndex = ( flags & 1 ) !== 0;\n\n\tconst entryCount = r.u32();\n\tconst associations = new Map< number, number[] >();\n\n\tfor ( let i = 0; i < entryCount; i++ ) {\n\t\tconst itemId = version < 1 ? r.u16() : r.u32();\n\t\tconst assocCount = r.u8();\n\t\tconst indices: number[] = [];\n\n\t\tfor ( let j = 0; j < assocCount; j++ ) {\n\t\t\tif ( largeIndex ) {\n\t\t\t\tindices.push( r.u16() & 0x7fff ); // strip essential bit\n\t\t\t} else {\n\t\t\t\tindices.push( r.u8() & 0x7f ); // strip essential bit\n\t\t\t}\n\t\t}\n\n\t\tassociations.set( itemId, indices );\n\t}\n\n\treturn associations;\n}\n\n/**\n * Parse Image Spatial Extents → width & height.\n *\n * @param r   Binary reader.\n * @param box BoxInfo for the ispe box.\n */\nfunction parseIspe(\n\tr: Reader,\n\tbox: BoxInfo\n): { width: number; height: number } {\n\t// ispe is a FullBox: skip version (1) + flags (3).\n\tr.pos = box.offset + box.headerSize + 4;\n\treturn { width: r.u32(), height: r.u32() };\n}\n\n/**\n * Parse Image Rotation box → rotation angle in degrees CCW.\n *\n * Format: 1 byte with reserved (6 bits) + angle (2 bits).\n * angle * 90 = rotation in degrees counter-clockwise.\n *\n * @param r   Binary reader.\n * @param box BoxInfo for the irot box.\n */\nfunction parseIrot( r: Reader, box: BoxInfo ): number {\n\tr.pos = box.offset + box.headerSize;\n\treturn ( r.u8() & 0x3 ) * 90;\n}\n\n/**\n * Parse Item Info Box → map of item ID to item type (4-char code).\n *\n * @param r   Binary reader.\n * @param box BoxInfo for the iinf box.\n */\nfunction parseIinf( r: Reader, box: BoxInfo ): Map< number, string > {\n\tr.pos = box.offset + box.headerSize;\n\tconst version = r.u8();\n\tr.pos += 3; // flags\n\n\tconst entryCount = version === 0 ? r.u16() : r.u32();\n\tconst itemTypes = new Map< number, string >();\n\n\t// Parse infe (ItemInfoEntry) sub-boxes.\n\tconst entriesStart = r.pos;\n\tconst boxEnd = box.offset + box.size;\n\n\tconst infeBoxes = findBoxes( r, entriesStart, boxEnd );\n\tfor ( let i = 0; i < Math.min( entryCount, infeBoxes.length ); i++ ) {\n\t\tconst infe = infeBoxes[ i ];\n\t\tif ( infe.type !== 'infe' ) {\n\t\t\tcontinue;\n\t\t}\n\t\tr.pos = infe.offset + infe.headerSize;\n\t\tconst infeVersion = r.u8();\n\t\tr.pos += 3; // flags\n\n\t\tif ( infeVersion >= 2 ) {\n\t\t\tconst itemId = infeVersion === 2 ? r.u16() : r.u32();\n\t\t\tr.u16(); // item_protection_index\n\t\t\tconst itemType = r.str( 4 );\n\t\t\titemTypes.set( itemId, itemType );\n\t\t}\n\t}\n\n\treturn itemTypes;\n}\n\n/**\n * Parse Item Reference Box → map of (fromItemId) to array of referenced item IDs,\n * filtered by reference type.\n *\n * @param r       Binary reader.\n * @param box     BoxInfo for the iref box.\n * @param refType Reference type to filter (e.g. 'dimg').\n */\nfunction parseIref(\n\tr: Reader,\n\tbox: BoxInfo,\n\trefType: string\n): Map< number, number[] > {\n\tr.pos = box.offset + box.headerSize;\n\tconst version = r.u8();\n\tr.pos += 3; // flags\n\n\tconst refs = new Map< number, number[] >();\n\tconst boxEnd = box.offset + box.size;\n\n\twhile ( r.pos < boxEnd ) {\n\t\tconst refBox = readBox( r );\n\t\tif ( ! refBox || refBox.size < 8 ) {\n\t\t\tbreak;\n\t\t}\n\n\t\tr.pos = refBox.offset + refBox.headerSize;\n\t\tconst fromId = version === 0 ? r.u16() : r.u32();\n\t\tconst refCount = r.u16();\n\t\tconst toIds: number[] = [];\n\n\t\tfor ( let i = 0; i < refCount; i++ ) {\n\t\t\ttoIds.push( version === 0 ? r.u16() : r.u32() );\n\t\t}\n\n\t\tif ( refBox.type === refType ) {\n\t\t\trefs.set( fromId, toIds );\n\t\t}\n\n\t\tr.pos = refBox.offset + refBox.size;\n\t}\n\n\treturn refs;\n}\n\n// ---------------------------------------------------------------------------\n// HEVC codec string construction\n// ---------------------------------------------------------------------------\n\n/**\n * Reverse all 32 bits of a number.\n *\n * @param n 32-bit unsigned integer.\n */\nexport function reverseBits32( n: number ): number {\n\tn = ( ( n >>> 1 ) & 0x55555555 ) | ( ( n & 0x55555555 ) << 1 );\n\tn = ( ( n >>> 2 ) & 0x33333333 ) | ( ( n & 0x33333333 ) << 2 );\n\tn = ( ( n >>> 4 ) & 0x0f0f0f0f ) | ( ( n & 0x0f0f0f0f ) << 4 );\n\tn = ( ( n >>> 8 ) & 0x00ff00ff ) | ( ( n & 0x00ff00ff ) << 8 );\n\tn = ( n >>> 16 ) | ( n << 16 );\n\treturn n >>> 0;\n}\n\n/**\n * Build an HEVC codec string from an HEVCDecoderConfigurationRecord.\n *\n * Format: hvc1.{profile}.{compat}.{tier}{level}[.{constraints}]\n * See ISO 14496-15 Annex E and W3C WebCodecs HEVC Codec Registration.\n *\n * @param r            Binary reader.\n * @param recordOffset Byte offset of the HEVCDecoderConfigurationRecord.\n */\nfunction buildCodecString( r: Reader, recordOffset: number ): string {\n\tr.pos = recordOffset;\n\tr.u8(); // configurationVersion\n\n\tconst byte1 = r.u8();\n\tconst profileSpace = ( byte1 >> 6 ) & 0x3;\n\tconst tierFlag = ( byte1 >> 5 ) & 0x1;\n\tconst profileIdc = byte1 & 0x1f;\n\n\tconst compatFlags = r.u32();\n\tconst constraintBytes = r.bytes( 6 );\n\tconst levelIdc = r.u8();\n\n\t// Profile: optional space prefix (A/B/C) + profile_idc.\n\tconst spacePrefix =\n\t\tprofileSpace > 0 ? String.fromCharCode( 64 + profileSpace ) : '';\n\n\t// Compatibility flags: bit-reversed, as hex.\n\tconst compatHex = reverseBits32( compatFlags ).toString( 16 ).toUpperCase();\n\n\t// Tier: 'L' (Main) or 'H' (High).\n\tconst tierChar = tierFlag ? 'H' : 'L';\n\n\t// Constraint indicator flags: each byte as hex, trailing zeros removed.\n\tlet lastNonZero = -1;\n\tfor ( let i = 5; i >= 0; i-- ) {\n\t\tif ( constraintBytes[ i ] !== 0 ) {\n\t\t\tlastNonZero = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tlet constraintStr = '';\n\tif ( lastNonZero >= 0 ) {\n\t\tconst parts: string[] = [];\n\t\tfor ( let i = 0; i <= lastNonZero; i++ ) {\n\t\t\tparts.push( constraintBytes[ i ].toString( 16 ).toUpperCase() );\n\t\t}\n\t\tconstraintStr = '.' + parts.join( '.' );\n\t}\n\n\treturn `hvc1.${ spacePrefix }${ profileIdc }.${ compatHex }.${ tierChar }${ levelIdc }${ constraintStr }`;\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Read item data by concatenating all extents for a given item location.\n *\n * For construction_method 0, offsets are absolute file positions.\n * For construction_method 1, offsets are relative to the idat box data.\n *\n * @param buffer     File ArrayBuffer.\n * @param loc        Item location with extents.\n * @param idatOffset Byte offset of the idat box's data within the file.\n */\nfunction readItemData(\n\tbuffer: ArrayBuffer,\n\tloc: ItemLocation,\n\tidatOffset: number\n): Uint8Array {\n\tconst baseOffset = loc.constructionMethod === 1 ? idatOffset : 0;\n\n\tif ( loc.extents.length === 1 ) {\n\t\tconst ext = loc.extents[ 0 ];\n\t\tconst start = baseOffset + ext.offset;\n\t\treturn new Uint8Array( buffer.slice( start, start + ext.length ) );\n\t}\n\tlet totalLength = 0;\n\tfor ( const ext of loc.extents ) {\n\t\ttotalLength += ext.length;\n\t}\n\tconst data = new Uint8Array( totalLength );\n\tlet pos = 0;\n\tfor ( const ext of loc.extents ) {\n\t\tconst start = baseOffset + ext.offset;\n\t\tdata.set(\n\t\t\tnew Uint8Array( buffer.slice( start, start + ext.length ) ),\n\t\t\tpos\n\t\t);\n\t\tpos += ext.length;\n\t}\n\treturn data;\n}\n\n/**\n * Find hvcC, ispe, and irot property boxes for a given item.\n *\n * @param propIndices 1-based property indices from ipma.\n * @param properties  All property boxes from ipco.\n */\nfunction findHvcProperties(\n\tpropIndices: number[],\n\tproperties: BoxInfo[]\n): { hvcCBox: BoxInfo; ispeBox: BoxInfo; irotBox?: BoxInfo } {\n\tlet hvcCBox: BoxInfo | undefined;\n\tlet ispeBox: BoxInfo | undefined;\n\tlet irotBox: BoxInfo | undefined;\n\n\tfor ( const idx of propIndices ) {\n\t\tif ( idx < 1 || idx > properties.length ) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst prop = properties[ idx - 1 ];\n\t\tif ( prop.type === 'hvcC' && ! hvcCBox ) {\n\t\t\thvcCBox = prop;\n\t\t}\n\t\tif ( prop.type === 'ispe' && ! ispeBox ) {\n\t\t\tispeBox = prop;\n\t\t}\n\t\tif ( prop.type === 'irot' && ! irotBox ) {\n\t\t\tirotBox = prop;\n\t\t}\n\t}\n\n\tif ( ! hvcCBox ) {\n\t\tthrow new Error( 'No HEVC configuration (hvcC) found' );\n\t}\n\tif ( ! ispeBox ) {\n\t\tthrow new Error( 'No image dimensions (ispe) found' );\n\t}\n\n\treturn { hvcCBox, ispeBox, irotBox };\n}\n\n// ---------------------------------------------------------------------------\n// Main entry point\n// ---------------------------------------------------------------------------\n\n/**\n * Parse a HEIC file and extract the data needed for VideoDecoder.\n *\n * Handles both single-image HEIC files and grid/tiled images (the common\n * iPhone format where the full image is split into multiple HEVC tiles).\n *\n * @param buffer Raw HEIC file contents.\n * @return Parsed image data including codec config and HEVC tile data.\n * @throws If the file is not a valid HEIC or lacks required boxes.\n */\nexport function parseHeic( buffer: ArrayBuffer ): HeicImageData {\n\tconst r = new Reader( buffer );\n\tconst fileEnd = buffer.byteLength;\n\n\t// Find top-level 'meta' box.\n\tconst metaBox = findBox( r, 0, fileEnd, 'meta' );\n\tif ( ! metaBox ) {\n\t\tthrow new Error( 'No meta box found in HEIC file' );\n\t}\n\n\t// meta is a FullBox: children start after version (1) + flags (3).\n\tconst metaChildStart = metaBox.offset + metaBox.headerSize + 4;\n\tconst metaEnd = metaBox.offset + metaBox.size;\n\n\t// Locate required child boxes within meta.\n\tconst children = findBoxes( r, metaChildStart, metaEnd );\n\tconst pitmBox = children.find( ( b ) => b.type === 'pitm' );\n\tconst ilocBox = children.find( ( b ) => b.type === 'iloc' );\n\tconst iprpBox = children.find( ( b ) => b.type === 'iprp' );\n\tconst iinfBox = children.find( ( b ) => b.type === 'iinf' );\n\tconst irefBox = children.find( ( b ) => b.type === 'iref' );\n\tconst idatBox = children.find( ( b ) => b.type === 'idat' );\n\n\t// idat data offset (for construction_method 1 items).\n\tconst idatOffset = idatBox ? idatBox.offset + idatBox.headerSize : 0;\n\n\tif ( ! pitmBox || ! ilocBox || ! iprpBox ) {\n\t\tthrow new Error( 'Missing required boxes (pitm, iloc, iprp) in HEIC' );\n\t}\n\n\t// Primary item ID.\n\tconst primaryId = parsePitm( r, pitmBox );\n\n\t// Item locations.\n\tconst locations = parseIloc( r, ilocBox );\n\n\t// Item properties: iprp contains ipco (properties) + ipma (associations).\n\tconst iprpStart = iprpBox.offset + iprpBox.headerSize;\n\tconst iprpEnd = iprpBox.offset + iprpBox.size;\n\tconst iprpChildren = findBoxes( r, iprpStart, iprpEnd );\n\tconst ipcoBox = iprpChildren.find( ( b ) => b.type === 'ipco' );\n\tconst ipmaBox = iprpChildren.find( ( b ) => b.type === 'ipma' );\n\n\tif ( ! ipcoBox || ! ipmaBox ) {\n\t\tthrow new Error( 'Missing ipco or ipma in HEIC properties' );\n\t}\n\n\tconst allAssoc = parseIpma( r, ipmaBox );\n\n\t// Enumerate ipco children (properties are 1-indexed).\n\tconst ipcoStart = ipcoBox.offset + ipcoBox.headerSize;\n\tconst ipcoEnd = ipcoBox.offset + ipcoBox.size;\n\tconst properties = findBoxes( r, ipcoStart, ipcoEnd );\n\n\t// Determine if the primary item is a grid or a direct HEVC image.\n\tlet primaryItemType = 'hvc1';\n\tif ( iinfBox ) {\n\t\tconst itemTypes = parseIinf( r, iinfBox );\n\t\tconst t = itemTypes.get( primaryId );\n\t\tif ( t ) {\n\t\t\tprimaryItemType = t;\n\t\t}\n\t}\n\n\tif ( primaryItemType === 'grid' ) {\n\t\t// --- Grid/tiled image (common iPhone format) ---\n\t\treturn parseGridImage(\n\t\t\tr,\n\t\t\tbuffer,\n\t\t\tprimaryId,\n\t\t\tlocations,\n\t\t\tallAssoc,\n\t\t\tproperties,\n\t\t\tirefBox,\n\t\t\tidatOffset\n\t\t);\n\t}\n\n\t// --- Single HEVC image ---\n\tconst primaryLoc = locations.get( primaryId );\n\tif ( ! primaryLoc || primaryLoc.extents.length === 0 ) {\n\t\tthrow new Error( `No location data for primary item ${ primaryId }` );\n\t}\n\n\tconst primaryPropIndices = allAssoc.get( primaryId );\n\tif ( ! primaryPropIndices || primaryPropIndices.length === 0 ) {\n\t\tthrow new Error( 'No property associations for primary item' );\n\t}\n\n\tconst { hvcCBox, ispeBox, irotBox } = findHvcProperties(\n\t\tprimaryPropIndices,\n\t\tproperties\n\t);\n\n\tconst hvcCDataStart = hvcCBox.offset + hvcCBox.headerSize;\n\tconst hvcCDataSize = hvcCBox.size - hvcCBox.headerSize;\n\tconst description = new Uint8Array(\n\t\tbuffer.slice( hvcCDataStart, hvcCDataStart + hvcCDataSize )\n\t);\n\tconst codecString = buildCodecString( r, hvcCDataStart );\n\tconst { width, height } = parseIspe( r, ispeBox );\n\tconst rotation = irotBox ? parseIrot( r, irotBox ) : 0;\n\n\treturn {\n\t\tcodecString,\n\t\tdescription,\n\t\ttiles: [\n\t\t\t{\n\t\t\t\tdata: readItemData( buffer, primaryLoc, idatOffset ),\n\t\t\t\tx: 0,\n\t\t\t\ty: 0,\n\t\t\t},\n\t\t],\n\t\ttileWidth: width,\n\t\ttileHeight: height,\n\t\toutputWidth: width,\n\t\toutputHeight: height,\n\t\trotation,\n\t\texifOrientation:\n\t\t\trotation === 0 ? getUnappliedExifOrientation( buffer ) : 1,\n\t};\n}\n\n/**\n * Parse a grid/tiled HEIC image.\n *\n * @param r          Binary reader.\n * @param buffer     File ArrayBuffer.\n * @param gridItemId The grid item's ID.\n * @param locations  Parsed iloc data.\n * @param allAssoc   Parsed ipma data.\n * @param properties ipco property boxes.\n * @param irefBox    iref box (required for grid).\n * @param idatOffset Byte offset of idat box data (for construction_method 1).\n */\nfunction parseGridImage(\n\tr: Reader,\n\tbuffer: ArrayBuffer,\n\tgridItemId: number,\n\tlocations: Map< number, ItemLocation >,\n\tallAssoc: Map< number, number[] >,\n\tproperties: BoxInfo[],\n\tirefBox: BoxInfo | undefined,\n\tidatOffset: number\n): HeicImageData {\n\t// Parse grid descriptor from the grid item's data.\n\tconst gridLoc = locations.get( gridItemId );\n\tif ( ! gridLoc || gridLoc.extents.length === 0 ) {\n\t\tthrow new Error( 'No location data for grid item' );\n\t}\n\tconst gridData = readItemData( buffer, gridLoc, idatOffset );\n\n\t// Grid descriptor format:\n\t// version (1 byte), flags (1 byte),\n\t// rows_minus_one (1 byte), columns_minus_one (1 byte),\n\t// output_width (2 or 4 bytes), output_height (2 or 4 bytes)\n\tconst largeFields = gridData.length > 1 && ( gridData[ 1 ] & 1 ) !== 0;\n\tconst minGridSize = largeFields ? 12 : 8;\n\tif ( gridData.length < minGridSize ) {\n\t\tthrow new Error(\n\t\t\t`Grid descriptor too short: ${ gridData.length } bytes`\n\t\t);\n\t}\n\n\tconst rows = gridData[ 2 ] + 1;\n\tconst columns = gridData[ 3 ] + 1;\n\n\tconst gv = new DataView( gridData.buffer, gridData.byteOffset );\n\tlet outputWidth: number;\n\tlet outputHeight: number;\n\tif ( largeFields ) {\n\t\toutputWidth = gv.getUint32( 4 );\n\t\toutputHeight = gv.getUint32( 8 );\n\t} else {\n\t\toutputWidth = gv.getUint16( 4 );\n\t\toutputHeight = gv.getUint16( 6 );\n\t}\n\n\t// Find tile item IDs from iref 'dimg' references.\n\tif ( ! irefBox ) {\n\t\tthrow new Error( 'Grid image requires iref box' );\n\t}\n\tconst dimgRefs = parseIref( r, irefBox, 'dimg' );\n\tconst tileItemIds = dimgRefs.get( gridItemId );\n\tif ( ! tileItemIds || tileItemIds.length === 0 ) {\n\t\tthrow new Error( 'No tile references found for grid item' );\n\t}\n\n\t// The iref may include extra references (alpha planes, thumbnails).\n\t// Use at least rows * columns tiles; ignore any surplus.\n\tconst expectedTiles = rows * columns;\n\tif ( tileItemIds.length < expectedTiles ) {\n\t\tthrow new Error(\n\t\t\t`Grid expects ${ expectedTiles } tiles but found ${ tileItemIds.length }`\n\t\t);\n\t}\n\n\t// Get hvcC and ispe from the first tile item's properties.\n\t// All tiles in a grid share the same HEVC configuration.\n\tconst firstTileProps = allAssoc.get( tileItemIds[ 0 ] );\n\tif ( ! firstTileProps || firstTileProps.length === 0 ) {\n\t\tthrow new Error( 'No property associations for tile item' );\n\t}\n\n\tconst { hvcCBox, ispeBox } = findHvcProperties(\n\t\tfirstTileProps,\n\t\tproperties\n\t);\n\n\t// irot is associated with the grid item, not the tiles.\n\tconst gridProps = allAssoc.get( gridItemId ) || [];\n\tlet irotBox: BoxInfo | undefined;\n\tfor ( const idx of gridProps ) {\n\t\tif ( idx >= 1 && idx <= properties.length ) {\n\t\t\tconst prop = properties[ idx - 1 ];\n\t\t\tif ( prop.type === 'irot' ) {\n\t\t\t\tirotBox = prop;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tconst hvcCDataStart = hvcCBox.offset + hvcCBox.headerSize;\n\tconst hvcCDataSize = hvcCBox.size - hvcCBox.headerSize;\n\tconst description = new Uint8Array(\n\t\tbuffer.slice( hvcCDataStart, hvcCDataStart + hvcCDataSize )\n\t);\n\tconst codecString = buildCodecString( r, hvcCDataStart );\n\tconst { width: tileWidth, height: tileHeight } = parseIspe( r, ispeBox );\n\n\t// Extract tile data in raster scan order (left→right, top→bottom).\n\tconst tiles: HeicTile[] = [];\n\tfor ( let row = 0; row < rows; row++ ) {\n\t\tfor ( let col = 0; col < columns; col++ ) {\n\t\t\tconst tileIdx = row * columns + col;\n\t\t\tconst tileId = tileItemIds[ tileIdx ];\n\t\t\tconst tileLoc = locations.get( tileId );\n\t\t\tif ( ! tileLoc || tileLoc.extents.length === 0 ) {\n\t\t\t\tthrow new Error( `No location data for tile item ${ tileId }` );\n\t\t\t}\n\t\t\ttiles.push( {\n\t\t\t\tdata: readItemData( buffer, tileLoc, idatOffset ),\n\t\t\t\tx: col * tileWidth,\n\t\t\t\ty: row * tileHeight,\n\t\t\t} );\n\t\t}\n\t}\n\n\tconst rotation = irotBox ? parseIrot( r, irotBox ) : 0;\n\n\treturn {\n\t\tcodecString,\n\t\tdescription,\n\t\ttiles,\n\t\ttileWidth,\n\t\ttileHeight,\n\t\toutputWidth,\n\t\toutputHeight,\n\t\trotation,\n\t\texifOrientation:\n\t\t\trotation === 0 ? getUnappliedExifOrientation( buffer ) : 1,\n\t};\n}\n\n// ---------------------------------------------------------------------------\n// EXIF orientation (shared by AVIF and HEIF)\n// ---------------------------------------------------------------------------\n\n/**\n * Read the EXIF Orientation tag from a raw EXIF/TIFF payload.\n *\n * The payload is the body of an ISOBMFF `Exif` item: a 4-byte\n * `exif_tiff_header_offset` followed by a TIFF block. Some encoders omit the\n * offset prefix and start with the TIFF byte-order marker directly, so both\n * layouts are handled.\n *\n * @param payload EXIF item body.\n * @return Orientation value (1-8), or 1 when absent/unparseable.\n */\nfunction readTiffOrientation( payload: Uint8Array ): number {\n\tif ( payload.length < 8 ) {\n\t\treturn 1;\n\t}\n\tconst view = new DataView(\n\t\tpayload.buffer,\n\t\tpayload.byteOffset,\n\t\tpayload.byteLength\n\t);\n\n\t// Locate the TIFF header. 0x4949 ('II') and 0x4D4D ('MM') are the\n\t// little/big-endian byte-order markers; if the payload starts with one\n\t// there is no 4-byte offset prefix.\n\tlet tiffStart = 0;\n\tconst firstWord = view.getUint16( 0 );\n\tif ( firstWord !== 0x4949 && firstWord !== 0x4d4d ) {\n\t\ttiffStart = view.getUint32( 0 ) + 4;\n\t}\n\tif ( tiffStart + 8 > payload.length ) {\n\t\treturn 1;\n\t}\n\n\tconst byteOrder = view.getUint16( tiffStart );\n\tlet little: boolean;\n\tif ( byteOrder === 0x4949 ) {\n\t\tlittle = true;\n\t} else if ( byteOrder === 0x4d4d ) {\n\t\tlittle = false;\n\t} else {\n\t\treturn 1;\n\t}\n\n\t// IFD0 offset is relative to the TIFF header.\n\tconst ifd0 = tiffStart + view.getUint32( tiffStart + 4, little );\n\tif ( ifd0 + 2 > payload.length ) {\n\t\treturn 1;\n\t}\n\n\tconst entryCount = view.getUint16( ifd0, little );\n\tfor ( let i = 0; i < entryCount; i++ ) {\n\t\tconst entry = ifd0 + 2 + i * 12;\n\t\tif ( entry + 12 > payload.length ) {\n\t\t\tbreak;\n\t\t}\n\t\t// Orientation tag (0x0112) is a SHORT whose value sits in the first\n\t\t// two bytes of the 4-byte value/offset field.\n\t\tif ( view.getUint16( entry, little ) === 0x0112 ) {\n\t\t\tconst value = view.getUint16( entry + 8, little );\n\t\t\treturn value >= 1 && value <= 8 ? value : 1;\n\t\t}\n\t}\n\n\treturn 1;\n}\n\n/**\n * Locate the `meta` box and its child boxes for an ISOBMFF (HEIF/AVIF) file.\n *\n * @param r Binary reader.\n * @return The meta box plus its parsed child boxes, or null if absent.\n */\nfunction findMeta( r: Reader ): { box: BoxInfo; children: BoxInfo[] } | null {\n\tconst metaBox = findBox( r, 0, r.view.byteLength, 'meta' );\n\tif ( ! metaBox ) {\n\t\treturn null;\n\t}\n\t// meta is a FullBox: children start after version (1) + flags (3).\n\tconst start = metaBox.offset + metaBox.headerSize + 4;\n\tconst end = metaBox.offset + metaBox.size;\n\treturn { box: metaBox, children: findBoxes( r, start, end ) };\n}\n\n/**\n * Extract the EXIF orientation from an ISOBMFF (HEIF/AVIF) container.\n *\n * AVIF and HEIF store EXIF metadata as an `Exif` item inside the `meta` box.\n * Neither WordPress's server-side `exif_read_data()` nor libheif applies this\n * orientation, so it is read here for client-side rotation.\n *\n * @param buffer Raw file contents.\n * @return EXIF orientation (1-8), or 1 when absent/unparseable.\n */\nexport function parseExifOrientation( buffer: ArrayBuffer ): number {\n\ttry {\n\t\tconst r = new Reader( buffer );\n\t\tconst meta = findMeta( r );\n\t\tif ( ! meta ) {\n\t\t\treturn 1;\n\t\t}\n\n\t\tconst iinfBox = meta.children.find( ( b ) => b.type === 'iinf' );\n\t\tconst ilocBox = meta.children.find( ( b ) => b.type === 'iloc' );\n\t\tif ( ! iinfBox || ! ilocBox ) {\n\t\t\treturn 1;\n\t\t}\n\n\t\tconst itemTypes = parseIinf( r, iinfBox );\n\t\tlet exifItemId: number | undefined;\n\t\tfor ( const [ id, type ] of itemTypes ) {\n\t\t\tif ( type === 'Exif' ) {\n\t\t\t\texifItemId = id;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif ( exifItemId === undefined ) {\n\t\t\treturn 1;\n\t\t}\n\n\t\tconst loc = parseIloc( r, ilocBox ).get( exifItemId );\n\t\tif ( ! loc || loc.extents.length === 0 ) {\n\t\t\treturn 1;\n\t\t}\n\n\t\tconst idatBox = meta.children.find( ( b ) => b.type === 'idat' );\n\t\tconst idatOffset = idatBox ? idatBox.offset + idatBox.headerSize : 0;\n\n\t\treturn readTiffOrientation( readItemData( buffer, loc, idatOffset ) );\n\t} catch {\n\t\treturn 1;\n\t}\n}\n\n/**\n * Whether an ISOBMFF (HEIF/AVIF) file carries a native `irot`/`imir` transform.\n *\n * libheif/libvips apply these on decode, so when one is present the EXIF\n * orientation must NOT be applied again to avoid double-rotation.\n *\n * @param r Binary reader.\n * @return True if an `irot` or `imir` property box is present.\n */\nfunction hasNativeTransform( r: Reader ): boolean {\n\tconst meta = findMeta( r );\n\tif ( ! meta ) {\n\t\treturn false;\n\t}\n\tconst iprp = meta.children.find( ( b ) => b.type === 'iprp' );\n\tif ( ! iprp ) {\n\t\treturn false;\n\t}\n\tconst ipco = findBox(\n\t\tr,\n\t\tiprp.offset + iprp.headerSize,\n\t\tiprp.offset + iprp.size,\n\t\t'ipco'\n\t);\n\tif ( ! ipco ) {\n\t\treturn false;\n\t}\n\tconst start = ipco.offset + ipco.headerSize;\n\tconst end = ipco.offset + ipco.size;\n\treturn Boolean(\n\t\tfindBox( r, start, end, 'irot' ) || findBox( r, start, end, 'imir' )\n\t);\n}\n\n/**\n * Get the EXIF orientation that neither the server nor libheif/libvips will\n * apply automatically for an ISOBMFF (HEIF/AVIF) image.\n *\n * Returns 1 (no correction needed) when the file has a native `irot`/`imir`\n * transform, because libheif already rotates it on decode. Otherwise returns\n * the EXIF Orientation tag so the caller can rotate explicitly.\n *\n * @param buffer Raw file contents.\n * @return EXIF orientation (1-8) requiring explicit rotation, or 1.\n */\nexport function getUnappliedExifOrientation( buffer: ArrayBuffer ): number {\n\ttry {\n\t\tif ( hasNativeTransform( new Reader( buffer ) ) ) {\n\t\t\treturn 1;\n\t\t}\n\t} catch {\n\t\treturn 1;\n\t}\n\treturn parseExifOrientation( buffer );\n}\n\n/* eslint-enable no-bitwise */\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA2DA,IAAM,SAAN,MAAa;AAAA,EACH;AAAA,EACA;AAAA,EACT;AAAA,EAEA,YAAa,QAAqB,SAAS,GAAI;AAC9C,SAAK,SAAS;AACd,SAAK,OAAO,IAAI,SAAU,MAAO;AACjC,SAAK,MAAM;AAAA,EACZ;AAAA,EAEA,KAAa;AACZ,UAAM,IAAI,KAAK,KAAK,SAAU,KAAK,GAAI;AACvC,SAAK,OAAO;AACZ,WAAO;AAAA,EACR;AAAA,EAEA,MAAc;AACb,UAAM,IAAI,KAAK,KAAK,UAAW,KAAK,GAAI;AACxC,SAAK,OAAO;AACZ,WAAO;AAAA,EACR;AAAA,EAEA,MAAc;AACb,UAAM,IAAI,KAAK,KAAK,UAAW,KAAK,GAAI;AACxC,SAAK,OAAO;AACZ,WAAO;AAAA,EACR;AAAA,EAEA,MAAc;AACb,UAAM,KAAK,KAAK,KAAK,UAAW,KAAK,GAAI;AACzC,UAAM,KAAK,KAAK,KAAK,UAAW,KAAK,MAAM,CAAE;AAC7C,SAAK,OAAO;AACZ,WAAO,KAAK,aAAc;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,GAAI,MAAuB;AAC1B,QAAK,SAAS,GAAI;AACjB,aAAO;AAA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