Comparing version 0.3.1 to 0.3.2
@@ -1,1 +0,1 @@ | ||
(()=>{"use strict";var t={719:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.DisplaySet=void 0;const n=i(498),s=i(866),o=i(818),r=i(207),a=i(32);e.DisplaySet=class{constructor(){this.presentationTimestamp=0,this.decodingTimestamp=0,this.paletteDefinitions=[],this.objectDefinitions=[],this.windowDefinitions=[]}read(t,e){for(this.presentationTimestamp=0,this.decodingTimestamp=0,this.presentationComposition=void 0,this.paletteDefinitions=[],this.objectDefinitions=[],this.windowDefinitions=[];;){if(e){if(20551!=t.readUInt16())throw new Error("Invalid magic number!");this.presentationTimestamp=t.readUInt32(),this.decodingTimestamp=t.readUInt32()}const i=t.readUInt8(),d=t.readUInt16();switch(i){case a.SegmentType.paletteDefinition:const e=new s.PaletteDefinitionSegment;e.read(t,d),this.paletteDefinitions.push(e);break;case a.SegmentType.objectDefinition:const h=new o.ObjectDefinitionSegment;h.read(t,d),this.objectDefinitions.push(h);break;case a.SegmentType.presentationComposition:const c=new n.PresentationCompositionSegment;c.read(t,d),this.presentationComposition=c;break;case a.SegmentType.windowDefinition:const p=new r.WindowDefinitionSegment;p.read(t,d),this.windowDefinitions.push(p);break;case a.SegmentType.end:return;default:throw new Error(`Unsupported segment type ${i}`)}}}}},818:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ObjectDefinitionSegment=void 0;const n=i(32);e.ObjectDefinitionSegment=class{constructor(){this.id=0,this.versionNumber=0,this.lastInSequenceFlag=0,this.width=0,this.height=0,this.dataLength=0}get isFirstInSequence(){return!!(128&this.lastInSequenceFlag)}get isLastInSequence(){return!!(64&this.lastInSequenceFlag)}get segmentType(){return n.SegmentType.objectDefinition}read(t,e){this.id=t.readUInt16(),this.versionNumber=t.readUInt8(),this.lastInSequenceFlag=t.readUInt8(),this.isFirstInSequence?(this.dataLength=t.readUInt24(),this.width=t.readUInt16(),this.height=t.readUInt16(),this.data=t.readBytes(e-11)):this.data=t.readBytes(e-4)}}},866:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.PaletteDefinitionSegment=e.PaletteEntry=void 0;const n=i(32);class s{constructor(){this.y=0,this.cr=0,this.cb=0,this.r=0,this.g=0,this.b=0,this.a=0}}e.PaletteEntry=s;class o{constructor(){this.id=0,this.versionNumber=0,this.entries={}}get segmentType(){return n.SegmentType.paletteDefinition}read(t,e){this.id=t.readUInt8(),this.versionNumber=t.readUInt8();const i=(e-2)/5;this.entries={};for(let e=0;e<i;e++){const e=t.readUInt8(),i=new s;i.y=t.readUInt8(),i.cr=t.readUInt8(),i.cb=t.readUInt8(),i.a=t.readUInt8();const n=i.y,r=i.cb-128,a=i.cr-128;i.r=o.clamp(Math.round(n+1.402*a),0,255),i.g=o.clamp(Math.round(n-.34414*r-.71414*a),0,255),i.b=o.clamp(Math.round(n+1.772*r),0,255),this.entries[e]=i}}static clamp(t,e,i){return Math.max(e,Math.min(t,i))}}e.PaletteDefinitionSegment=o},498:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.PresentationCompositionSegment=e.CompositionObject=void 0;const n=i(32);class s{constructor(){this.id=0,this.windowId=0,this.croppedFlag=0,this.horizontalPosition=0,this.verticalPosition=0,this.croppingHorizontalPosition=0,this.croppingVerticalPosition=0,this.croppingWidth=0,this.croppingHeightPosition=0}get hasCropping(){return!!(128&this.croppedFlag)}}e.CompositionObject=s,e.PresentationCompositionSegment=class{constructor(){this.width=0,this.height=0,this.frameRate=0,this.compositionNumber=0,this.compositionState=0,this.paletteUpdateFlag=0,this.paletteId=0,this.compositionObjects=[]}get segmentType(){return n.SegmentType.presentationComposition}read(t,e){this.width=t.readUInt16(),this.height=t.readUInt16(),this.frameRate=t.readUInt8(),this.compositionNumber=t.readUInt16(),this.compositionState=t.readUInt8(),this.paletteUpdateFlag=t.readUInt8(),this.paletteId=t.readUInt8();const i=t.readUInt8();this.compositionObjects=[];for(let e=0;e<i;e++){const e=new s;e.id=t.readUInt16(),e.windowId=t.readUInt8(),e.croppedFlag=t.readUInt8(),e.horizontalPosition=t.readUInt16(),e.verticalPosition=t.readUInt16(),e.hasCropping&&(e.croppingHorizontalPosition=t.readUInt16(),e.croppingVerticalPosition=t.readUInt16(),e.croppingWidth=t.readUInt16(),e.croppingHeightPosition=t.readUInt16()),this.compositionObjects.push(e)}}}},32:(t,e)=>{var i;Object.defineProperty(e,"__esModule",{value:!0}),e.SegmentType=void 0,function(t){t[t.paletteDefinition=20]="paletteDefinition",t[t.objectDefinition=21]="objectDefinition",t[t.presentationComposition=22]="presentationComposition",t[t.windowDefinition=23]="windowDefinition",t[t.end=128]="end"}(i||(e.SegmentType=i={}))},207:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.WindowDefinitionSegment=e.WindowDefinition=void 0;const n=i(32);class s{constructor(){this.id=0,this.horizontalPosition=0,this.verticalPosition=0,this.width=0,this.height=0}}e.WindowDefinition=s,e.WindowDefinitionSegment=class{constructor(){this.windows=[]}get segmentType(){return n.SegmentType.windowDefinition}read(t,e){const i=t.readUInt8();this.windows=[];for(let e=0;e<i;e++){const e=new s;e.id=t.readUInt8(),e.horizontalPosition=t.readUInt16(),e.verticalPosition=t.readUInt16(),e.width=t.readUInt16(),e.height=t.readUInt16(),this.windows.push(e)}}}},16:function(t,e,i){var n=this&&this.__awaiter||function(t,e,i,n){return new(i||(i=Promise))((function(s,o){function r(t){try{d(n.next(t))}catch(t){o(t)}}function a(t){try{d(n.throw(t))}catch(t){o(t)}}function d(t){var e;t.done?s(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(r,a)}d((n=n.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.PgsRendererInternal=void 0;const s=i(719),o=i(489),r=i(494),a=i(381);e.PgsRendererInternal=class{constructor(){this.displaySets=[],this.updateTimestamps=[]}loadFromUrlAsync(t){return n(this,void 0,void 0,(function*(){const e=yield fetch(t),i=yield e.arrayBuffer();this.loadFromBuffer(i)}))}loadFromBuffer(t){this.displaySets=[],this.updateTimestamps=[];const e=new o.BigEndianBinaryReader(new Uint8Array(t));for(;e.position<e.length;){const t=new s.DisplaySet;t.read(e,!0),this.displaySets.push(t),this.updateTimestamps.push(t.presentationTimestamp)}}setCanvas(t){this.canvas=t,this.context=t.getContext("2d")}renderAtIndex(t){t<0||t>=this.displaySets.length?this.clearCanvas():this.renderDisplaySet(this.displaySets[t])}clearCanvas(){this.canvas&&this.context&&this.context.clearRect(0,0,this.canvas.width,this.canvas.height)}renderDisplaySet(t){if(this.canvas&&t.presentationComposition){this.canvas.width==t.presentationComposition.width&&this.canvas.height==t.presentationComposition.height||(this.canvas.width=t.presentationComposition.width,this.canvas.height=t.presentationComposition.height),this.clearCanvas();for(const e of t.presentationComposition.compositionObjects)this.renderDisplaySetComposition(t,e)}}renderDisplaySetComposition(t,e){if(!this.context)return;if(!t.presentationComposition)return;let i=t.windowDefinitions.flatMap((t=>t.windows)).find((t=>t.id===e.windowId));if(!i)return;const n=this.getPixelDataFromDisplaySetComposition(t,e);n&&this.context.drawImage(n,i.horizontalPosition,i.verticalPosition)}getPixelDataFromDisplaySetComposition(t,e){if(!t.presentationComposition)return;let i=t.paletteDefinitions.find((e=>{var i;return e.id===(null===(i=t.presentationComposition)||void 0===i?void 0:i.paletteId)}));if(!i)return;let n=0,s=0;const o=[];for(const i of t.objectDefinitions)i.id==e.id&&(i.isFirstInSequence&&(n=i.width,s=i.height),i.data&&o.push(i.data));if(0==o.length)return;const d=new a.CombinedBinaryReader(o),h=new OffscreenCanvas(n,s),c=h.getContext("2d"),p=c.createImageData(n,s),l=p.data;return r.RunLengthEncoding.decode(d,((t,e,n,s)=>{const o=null==i?void 0:i.entries[s];o&&(l[4*t]=o.r,l[4*t+1]=o.g,l[4*t+2]=o.b,l[4*t+3]=o.a)})),c.putImageData(p,0,0),h}}},385:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ArrayBinaryReader=void 0,e.ArrayBinaryReader=class{constructor(t){this.$position=0,this.array=t}get position(){return this.$position}get length(){return this.array.length}readByte(){return this.array[this.$position++]}readBytes(t){const e=this.array.slice(this.$position,this.$position+t);return this.$position+=t,e}}},489:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.BigEndianBinaryReader=void 0;const n=i(385);e.BigEndianBinaryReader=class{constructor(t){t instanceof Uint8Array?this.reader=new n.ArrayBinaryReader(t):this.reader=t}get position(){return this.reader.position}get length(){return this.reader.length}readUInt8(){return this.reader.readByte()}readUInt16(){return(this.reader.readByte()<<8)+this.reader.readByte()}readUInt24(){return(this.reader.readByte()<<16)+(this.reader.readByte()<<8)+this.reader.readByte()}readUInt32(){return(this.reader.readByte()<<24)+(this.reader.readByte()<<16)+(this.reader.readByte()<<8)+this.reader.readByte()}readBytes(t){return this.reader.readBytes(t)}}},381:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.CombinedBinaryReader=void 0;const n=i(385);e.CombinedBinaryReader=class{constructor(t){this.$position=0,this.subReaderIndex=0,this.subReaders=t.map((t=>t instanceof Uint8Array?new n.ArrayBinaryReader(t):t));let e=0;for(const i of t)e+=i.length;this.$length=e}get position(){return this.$position}get length(){return this.$length}readByte(){for(;this.subReaders[this.subReaderIndex].position>=this.subReaders[this.subReaderIndex].length;)this.subReaderIndex++;return this.$position++,this.subReaders[this.subReaderIndex].readByte()}readBytes(t){const e=new Uint8Array(t);for(let i=0;i<t;i++)e[i]=this.readByte();return e}}},494:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.RunLengthEncoding=void 0;const n=i(385);e.RunLengthEncoding=class{static decode(t,e){t instanceof Uint8Array&&(t=new n.ArrayBinaryReader(t));let i=0,s=0,o=0;for(;t.position<t.length;){const n=t.readByte();if(0!=n){e(o++,i++,s,n);continue}const r=t.readByte();if(0==r){i=0,s++;continue}const a=!!(128&r);let d=63&r;64&r&&(d=(d<<8)+t.readByte());const h=a?t.readByte():0;for(let t=0;t<d;t++)e(o++,i++,s,h)}}}}},e={};function i(n){var s=e[n];if(void 0!==s)return s.exports;var o=e[n]={exports:{}};return t[n].call(o.exports,o,o.exports,i),o.exports}(()=>{const t=new(i(16).PgsRendererInternal),e=()=>{postMessage({op:"loaded",updateTimestamps:t.updateTimestamps})};onmessage=i=>{switch(i.data.op){case"init":const n=i.data.canvas;t.setCanvas(n);break;case"loadFromUrl":const s=i.data.url;t.loadFromUrlAsync(s).then((()=>{e()}));break;case"loadFromBuffer":const o=i.data.buffer;t.loadFromBuffer(o),e();break;case"render":const r=i.data.index;t.renderAtIndex(r)}}})()})(); | ||
(()=>{"use strict";var t={719:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.DisplaySet=void 0;const n=i(498),s=i(866),o=i(818),r=i(207),a=i(32);e.DisplaySet=class{constructor(){this.presentationTimestamp=0,this.decodingTimestamp=0,this.paletteDefinitions=[],this.objectDefinitions=[],this.windowDefinitions=[]}read(t,e){for(this.presentationTimestamp=0,this.decodingTimestamp=0,this.presentationComposition=void 0,this.paletteDefinitions=[],this.objectDefinitions=[],this.windowDefinitions=[];;){if(e){if(20551!=t.readUInt16())throw new Error("Invalid magic number!");this.presentationTimestamp=t.readUInt32(),this.decodingTimestamp=t.readUInt32()}const i=t.readUInt8(),d=t.readUInt16();switch(i){case a.SegmentType.paletteDefinition:const e=new s.PaletteDefinitionSegment;e.read(t,d),this.paletteDefinitions.push(e);break;case a.SegmentType.objectDefinition:const h=new o.ObjectDefinitionSegment;h.read(t,d),this.objectDefinitions.push(h);break;case a.SegmentType.presentationComposition:const c=new n.PresentationCompositionSegment;c.read(t,d),this.presentationComposition=c;break;case a.SegmentType.windowDefinition:const p=new r.WindowDefinitionSegment;p.read(t,d),this.windowDefinitions.push(p);break;case a.SegmentType.end:return;default:throw new Error(`Unsupported segment type ${i}`)}}}}},818:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ObjectDefinitionSegment=void 0;const n=i(32);e.ObjectDefinitionSegment=class{constructor(){this.id=0,this.versionNumber=0,this.lastInSequenceFlag=0,this.width=0,this.height=0,this.dataLength=0}get isFirstInSequence(){return!!(128&this.lastInSequenceFlag)}get isLastInSequence(){return!!(64&this.lastInSequenceFlag)}get segmentType(){return n.SegmentType.objectDefinition}read(t,e){this.id=t.readUInt16(),this.versionNumber=t.readUInt8(),this.lastInSequenceFlag=t.readUInt8(),this.isFirstInSequence?(this.dataLength=t.readUInt24(),this.width=t.readUInt16(),this.height=t.readUInt16(),this.data=t.readBytes(e-11)):this.data=t.readBytes(e-4)}}},866:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.PaletteDefinitionSegment=e.PaletteEntry=void 0;const n=i(32);class s{constructor(){this.y=0,this.cr=0,this.cb=0,this.r=0,this.g=0,this.b=0,this.a=0}}e.PaletteEntry=s;class o{constructor(){this.id=0,this.versionNumber=0,this.entries={}}get segmentType(){return n.SegmentType.paletteDefinition}read(t,e){this.id=t.readUInt8(),this.versionNumber=t.readUInt8();const i=(e-2)/5;this.entries={};for(let e=0;e<i;e++){const e=t.readUInt8(),i=new s;i.y=t.readUInt8(),i.cr=t.readUInt8(),i.cb=t.readUInt8(),i.a=t.readUInt8();const n=i.y,r=i.cb-128,a=i.cr-128;i.r=o.clamp(Math.round(n+1.402*a),0,255),i.g=o.clamp(Math.round(n-.34414*r-.71414*a),0,255),i.b=o.clamp(Math.round(n+1.772*r),0,255),this.entries[e]=i}}static clamp(t,e,i){return Math.max(e,Math.min(t,i))}}e.PaletteDefinitionSegment=o},498:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.PresentationCompositionSegment=e.CompositionObject=void 0;const n=i(32);class s{constructor(){this.id=0,this.windowId=0,this.croppedFlag=0,this.horizontalPosition=0,this.verticalPosition=0,this.croppingHorizontalPosition=0,this.croppingVerticalPosition=0,this.croppingWidth=0,this.croppingHeightPosition=0}get hasCropping(){return!!(128&this.croppedFlag)}}e.CompositionObject=s,e.PresentationCompositionSegment=class{constructor(){this.width=0,this.height=0,this.frameRate=0,this.compositionNumber=0,this.compositionState=0,this.paletteUpdateFlag=0,this.paletteId=0,this.compositionObjects=[]}get segmentType(){return n.SegmentType.presentationComposition}read(t,e){this.width=t.readUInt16(),this.height=t.readUInt16(),this.frameRate=t.readUInt8(),this.compositionNumber=t.readUInt16(),this.compositionState=t.readUInt8(),this.paletteUpdateFlag=t.readUInt8(),this.paletteId=t.readUInt8();const i=t.readUInt8();this.compositionObjects=[];for(let e=0;e<i;e++){const e=new s;e.id=t.readUInt16(),e.windowId=t.readUInt8(),e.croppedFlag=t.readUInt8(),e.horizontalPosition=t.readUInt16(),e.verticalPosition=t.readUInt16(),e.hasCropping&&(e.croppingHorizontalPosition=t.readUInt16(),e.croppingVerticalPosition=t.readUInt16(),e.croppingWidth=t.readUInt16(),e.croppingHeightPosition=t.readUInt16()),this.compositionObjects.push(e)}}}},32:(t,e)=>{var i;Object.defineProperty(e,"__esModule",{value:!0}),e.SegmentType=void 0,function(t){t[t.paletteDefinition=20]="paletteDefinition",t[t.objectDefinition=21]="objectDefinition",t[t.presentationComposition=22]="presentationComposition",t[t.windowDefinition=23]="windowDefinition",t[t.end=128]="end"}(i||(e.SegmentType=i={}))},207:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.WindowDefinitionSegment=e.WindowDefinition=void 0;const n=i(32);class s{constructor(){this.id=0,this.horizontalPosition=0,this.verticalPosition=0,this.width=0,this.height=0}}e.WindowDefinition=s,e.WindowDefinitionSegment=class{constructor(){this.windows=[]}get segmentType(){return n.SegmentType.windowDefinition}read(t,e){const i=t.readUInt8();this.windows=[];for(let e=0;e<i;e++){const e=new s;e.id=t.readUInt8(),e.horizontalPosition=t.readUInt16(),e.verticalPosition=t.readUInt16(),e.width=t.readUInt16(),e.height=t.readUInt16(),this.windows.push(e)}}}},16:function(t,e,i){var n=this&&this.__awaiter||function(t,e,i,n){return new(i||(i=Promise))((function(s,o){function r(t){try{d(n.next(t))}catch(t){o(t)}}function a(t){try{d(n.throw(t))}catch(t){o(t)}}function d(t){var e;t.done?s(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(r,a)}d((n=n.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.PgsRendererInternal=void 0;const s=i(719),o=i(489),r=i(494),a=i(381);e.PgsRendererInternal=class{constructor(){this.displaySets=[],this.updateTimestamps=[]}loadFromUrlAsync(t){return n(this,void 0,void 0,(function*(){const e=yield fetch(t),i=yield e.arrayBuffer();this.loadFromBuffer(i)}))}loadFromBuffer(t){this.displaySets=[],this.updateTimestamps=[];const e=new o.BigEndianBinaryReader(new Uint8Array(t));for(;e.position<e.length;){const t=new s.DisplaySet;t.read(e,!0),this.displaySets.push(t),this.updateTimestamps.push(t.presentationTimestamp)}}setCanvas(t){this.canvas=t,this.context=t.getContext("2d")}renderAtIndex(t){var e,i;if(!this.canvas||!this.context)return;if(this.context.clearRect(0,0,this.canvas.width,this.canvas.height),t<0||t>=this.displaySets.length)return;const n=this.displaySets[t];if(!n.presentationComposition)return;this.canvas.width==n.presentationComposition.width&&this.canvas.height==n.presentationComposition.height||(this.canvas.width=n.presentationComposition.width,this.canvas.height=n.presentationComposition.height);const s=[],o=[],r=[];let a=t;for(;a>=0&&(s.unshift(...this.displaySets[a].objectDefinitions),o.unshift(...this.displaySets[a].paletteDefinitions),r.unshift(...this.displaySets[a].windowDefinitions.flatMap((t=>t.windows))),0===(null===(e=this.displaySets[a].presentationComposition)||void 0===e?void 0:e.compositionState));)a--;let d=o.find((t=>{var e;return t.id===(null===(e=n.presentationComposition)||void 0===e?void 0:e.paletteId)}));if(d)for(const t of n.presentationComposition.compositionObjects){let e=r.find((e=>e.id===t.windowId));if(!e)continue;const n=this.getPixelDataFromComposition(t,d,s);n&&(null===(i=this.context)||void 0===i||i.drawImage(n,e.horizontalPosition,e.verticalPosition))}}getPixelDataFromComposition(t,e,i){let n=0,s=0;const o=[];for(const e of i)e.id==t.id&&(e.isFirstInSequence&&(n=e.width,s=e.height),e.data&&o.push(e.data));if(0==o.length)return;const d=new a.CombinedBinaryReader(o),h=new OffscreenCanvas(n,s),c=h.getContext("2d"),p=c.createImageData(n,s),l=p.data;return r.RunLengthEncoding.decode(d,((t,i,n,s)=>{const o=null==e?void 0:e.entries[s];o&&(l[4*t]=o.r,l[4*t+1]=o.g,l[4*t+2]=o.b,l[4*t+3]=o.a)})),c.putImageData(p,0,0),h}}},385:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ArrayBinaryReader=void 0,e.ArrayBinaryReader=class{constructor(t){this.$position=0,this.array=t}get position(){return this.$position}get length(){return this.array.length}readByte(){return this.array[this.$position++]}readBytes(t){const e=this.array.slice(this.$position,this.$position+t);return this.$position+=t,e}}},489:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.BigEndianBinaryReader=void 0;const n=i(385);e.BigEndianBinaryReader=class{constructor(t){t instanceof Uint8Array?this.reader=new n.ArrayBinaryReader(t):this.reader=t}get position(){return this.reader.position}get length(){return this.reader.length}readUInt8(){return this.reader.readByte()}readUInt16(){return(this.reader.readByte()<<8)+this.reader.readByte()}readUInt24(){return(this.reader.readByte()<<16)+(this.reader.readByte()<<8)+this.reader.readByte()}readUInt32(){return(this.reader.readByte()<<24)+(this.reader.readByte()<<16)+(this.reader.readByte()<<8)+this.reader.readByte()}readBytes(t){return this.reader.readBytes(t)}}},381:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.CombinedBinaryReader=void 0;const n=i(385);e.CombinedBinaryReader=class{constructor(t){this.$position=0,this.subReaderIndex=0,this.subReaders=t.map((t=>t instanceof Uint8Array?new n.ArrayBinaryReader(t):t));let e=0;for(const i of t)e+=i.length;this.$length=e}get position(){return this.$position}get length(){return this.$length}readByte(){for(;this.subReaders[this.subReaderIndex].position>=this.subReaders[this.subReaderIndex].length;)this.subReaderIndex++;return this.$position++,this.subReaders[this.subReaderIndex].readByte()}readBytes(t){const e=new Uint8Array(t);for(let i=0;i<t;i++)e[i]=this.readByte();return e}}},494:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.RunLengthEncoding=void 0;const n=i(385);e.RunLengthEncoding=class{static decode(t,e){t instanceof Uint8Array&&(t=new n.ArrayBinaryReader(t));let i=0,s=0,o=0;for(;t.position<t.length;){const n=t.readByte();if(0!=n){e(o++,i++,s,n);continue}const r=t.readByte();if(0==r){i=0,s++;continue}const a=!!(128&r);let d=63&r;64&r&&(d=(d<<8)+t.readByte());const h=a?t.readByte():0;for(let t=0;t<d;t++)e(o++,i++,s,h)}}}}},e={};function i(n){var s=e[n];if(void 0!==s)return s.exports;var o=e[n]={exports:{}};return t[n].call(o.exports,o,o.exports,i),o.exports}(()=>{const t=new(i(16).PgsRendererInternal),e=()=>{postMessage({op:"loaded",updateTimestamps:t.updateTimestamps})};onmessage=i=>{switch(i.data.op){case"init":const n=i.data.canvas;t.setCanvas(n);break;case"loadFromUrl":const s=i.data.url;t.loadFromUrlAsync(s).then((()=>{e()}));break;case"loadFromBuffer":const o=i.data.buffer;t.loadFromBuffer(o),e();break;case"render":const r=i.data.index;t.renderAtIndex(r)}}})()})(); |
@@ -37,6 +37,3 @@ import { DisplaySet } from "./pgs/displaySet"; | ||
renderAtIndex(index: number): void; | ||
private clearCanvas; | ||
private renderDisplaySet; | ||
private renderDisplaySetComposition; | ||
private getPixelDataFromDisplaySetComposition; | ||
private getPixelDataFromComposition; | ||
} |
{ | ||
"name": "libpgs", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"author": "David Schulte", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -6,2 +6,5 @@ import {DisplaySet} from "./pgs/displaySet"; | ||
import {CombinedBinaryReader} from "./utils/combinedBinaryReader"; | ||
import {PaletteDefinitionSegment} from "./pgs/paletteDefinitionSegment"; | ||
import {ObjectDefinitionSegment} from "./pgs/objectDefinitionSegment"; | ||
import {WindowDefinition} from "./pgs/windowDefinitionSegment"; | ||
@@ -73,19 +76,12 @@ /** | ||
public renderAtIndex(index: number): void { | ||
if (!this.canvas || !this.context) return; | ||
// Clear the canvas on invalid indices. It is possible to seek to a position before the first subtitle while | ||
// a later subtile is on screen. This subtitle must be clear, even there is no valid new subtitle data. | ||
// Ignoring the render would keep the previous subtitle on screen. | ||
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
if (index < 0 || index >= this.displaySets.length) { | ||
this.clearCanvas(); | ||
return; | ||
} | ||
this.renderDisplaySet(this.displaySets[index]); | ||
} | ||
private clearCanvas(): void { | ||
if (!this.canvas || !this.context) return; | ||
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
} | ||
private renderDisplaySet(displaySet: DisplaySet): void { | ||
if (!this.canvas) return; | ||
const displaySet = this.displaySets[index]; | ||
if (!displaySet.presentationComposition) return; | ||
@@ -100,28 +96,48 @@ | ||
this.clearCanvas(); | ||
for (const composition of displaySet.presentationComposition.compositionObjects) { | ||
this.renderDisplaySetComposition(displaySet, composition); | ||
// We need to collect all valid objects and palettes up to this point. PGS can update and reuse elements from | ||
// previous display sets. The `compositionState` defines if the previous elements should be cleared. | ||
// If it is `0` previous elements should be kept. Because the user can seek through the file, we can not store | ||
// previous elements, and we should collect these elements for every new render. | ||
const ctxObjects: ObjectDefinitionSegment[] = []; | ||
const ctxPalettes: PaletteDefinitionSegment[] = []; | ||
const ctxWindows: WindowDefinition[] = []; | ||
let curIndex = index; | ||
while (curIndex >= 0) { | ||
// Because we are moving backwards, we would end up with the inverted array order. | ||
// We'll use `unshift` to add these elements to the front of the array. | ||
ctxObjects.unshift(...this.displaySets[curIndex].objectDefinitions); | ||
ctxPalettes.unshift(...this.displaySets[curIndex].paletteDefinitions); | ||
ctxWindows.unshift(...this.displaySets[curIndex].windowDefinitions | ||
.flatMap(w => w.windows)); | ||
// Any other state that `0` frees all previous segments, so we can stop here. | ||
if (this.displaySets[curIndex].presentationComposition?.compositionState !== 0) { | ||
break; | ||
} | ||
curIndex--; | ||
} | ||
} | ||
private renderDisplaySetComposition(displaySet: DisplaySet, composition: CompositionObject): void { | ||
if (!this.context) return; | ||
if (!displaySet.presentationComposition) return; | ||
let window = displaySet.windowDefinitions | ||
.flatMap(w => w.windows) | ||
.find(w => w.id === composition.windowId); | ||
if (!window) return; | ||
// Find the used palette for this composition. | ||
let palette = ctxPalettes | ||
.find(w => w.id === displaySet.presentationComposition?.paletteId); | ||
if (!palette) return; | ||
const pixelData = this.getPixelDataFromDisplaySetComposition(displaySet, composition); | ||
if (pixelData) { | ||
this.context.drawImage(pixelData, window.horizontalPosition, window.verticalPosition); | ||
for (const compositionObject of displaySet.presentationComposition.compositionObjects) { | ||
// Find the window to draw on. | ||
let window = ctxWindows.find(w => w.id === compositionObject.windowId); | ||
if (!window) continue; | ||
// Builds the subtitle. | ||
const pixelData = this.getPixelDataFromComposition(compositionObject, palette, ctxObjects); | ||
if (pixelData) { | ||
this.context?.drawImage(pixelData, window.horizontalPosition, window.verticalPosition); | ||
} | ||
} | ||
} | ||
private getPixelDataFromDisplaySetComposition(displaySet: DisplaySet, composition: CompositionObject): | ||
private getPixelDataFromComposition(composition: CompositionObject, palette: PaletteDefinitionSegment, | ||
ctxObjects: ObjectDefinitionSegment[]): | ||
OffscreenCanvas | undefined { | ||
if (!displaySet.presentationComposition) return undefined; | ||
let palette = displaySet.paletteDefinitions | ||
.find(p => p.id === displaySet.presentationComposition?.paletteId); | ||
if (!palette) return undefined; | ||
@@ -133,3 +149,3 @@ // Multiple object definition can define a single subtitle image. | ||
const dataChunks: Uint8Array[] = []; | ||
for (const ods of displaySet.objectDefinitions) { | ||
for (const ods of ctxObjects) { | ||
if (ods.id != composition.id) continue; | ||
@@ -136,0 +152,0 @@ if (ods.isFirstInSequence) { |
68736
1355