Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

libpgs

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libpgs - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

2

dist/libpgs.js

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.libpgs=t():e.libpgs=t()}(self,(()=>(()=>{"use strict";var e={97:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PgsRenderer=void 0,t.PgsRenderer=class{constructor(e){var t,s;if(this.$timeOffset=0,this.onTimeUpdate=()=>{this.renderAtVideoTimestamp()},this.updateTimestamps=[],this.previousTimestampIndex=0,this.onWorkerMessage=e=>{"loaded"===e.data.op&&(this.updateTimestamps=e.data.updateTimestamps,this.renderAtVideoTimestamp())},e.video&&(this.video=e.video),e.canvas)this.canvas=e.canvas,this.canvasOwner=!1;else{if(!this.video)throw new Error("No canvas or video element was provided!");this.canvas=this.createCanvasElement(),this.canvasOwner=!0,this.video.parentElement.appendChild(this.canvas)}const i=this.canvas.transferControlToOffscreen(),r=null!==(t=e.workerUrl)&&void 0!==t?t:"libpgs.worker.js";this.worker=new Worker(r),this.worker.onmessage=this.onWorkerMessage,this.worker.postMessage({op:"init",canvas:i},[i]),this.$timeOffset=null!==(s=e.timeOffset)&&void 0!==s?s:0,e.subUrl&&this.loadFromUrl(e.subUrl),this.registerVideoEvents()}get timeOffset(){return this.$timeOffset}set timeOffset(e){this.$timeOffset!==e&&(this.$timeOffset=e,this.renderAtVideoTimestamp())}registerVideoEvents(){this.video&&this.video.addEventListener("timeupdate",this.onTimeUpdate)}unregisterVideoEvents(){this.video&&this.video.removeEventListener("timeupdate",this.onTimeUpdate)}renderAtVideoTimestamp(){this.video&&this.renderAtTimestamp(this.video.currentTime+this.$timeOffset)}createCanvasElement(){const e=document.createElement("canvas");return e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.right="0",e.style.bottom="0",e.style.pointerEvents="none",e.style.objectFit="contain",e.style.width="100%",e.style.height="100%",e}destroyCanvasElement(){this.canvas.remove()}renderAtTimestamp(e){e=1e3*e*90;let t=-1;for(const s of this.updateTimestamps){if(s>e)break;t++}this.previousTimestampIndex!=t&&(this.previousTimestampIndex=t,t<0||this.worker.postMessage({op:"render",index:t}))}loadFromUrl(e){this.worker.postMessage({op:"loadFromUrl",url:e})}loadFromBuffer(e){this.worker.postMessage({op:"loadFromBuffer",buffer:e})}dispose(){this.worker.terminate(),this.unregisterVideoEvents(),this.canvasOwner&&this.destroyCanvasElement()}}}},t={};function s(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i](o,o.exports,s),o.exports}var i={};return(()=>{var e=i;Object.defineProperty(e,"__esModule",{value:!0}),e.PgsRenderer=void 0;const t=s(97);Object.defineProperty(e,"PgsRenderer",{enumerable:!0,get:function(){return t.PgsRenderer}})})(),i})()));
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.libpgs=t():e.libpgs=t()}(self,(()=>(()=>{"use strict";var e={97:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PgsRenderer=void 0,t.PgsRenderer=class{constructor(e){var t,s;if(this.$timeOffset=0,this.onTimeUpdate=()=>{this.renderAtVideoTimestamp()},this.updateTimestamps=[],this.previousTimestampIndex=0,this.onWorkerMessage=e=>{"loaded"===e.data.op&&(this.updateTimestamps=e.data.updateTimestamps,this.renderAtVideoTimestamp())},e.video&&(this.video=e.video),e.canvas)this.canvas=e.canvas,this.canvasOwner=!1;else{if(!this.video)throw new Error("No canvas or video element was provided!");this.canvas=this.createCanvasElement(),this.canvasOwner=!0,this.video.parentElement.appendChild(this.canvas)}const i=this.canvas.transferControlToOffscreen(),r=null!==(t=e.workerUrl)&&void 0!==t?t:"libpgs.worker.js";this.worker=new Worker(r),this.worker.onmessage=this.onWorkerMessage,this.worker.postMessage({op:"init",canvas:i},[i]),this.$timeOffset=null!==(s=e.timeOffset)&&void 0!==s?s:0,e.subUrl&&this.loadFromUrl(e.subUrl),this.registerVideoEvents()}get timeOffset(){return this.$timeOffset}set timeOffset(e){this.$timeOffset!==e&&(this.$timeOffset=e,this.renderAtVideoTimestamp())}registerVideoEvents(){this.video&&this.video.addEventListener("timeupdate",this.onTimeUpdate)}unregisterVideoEvents(){this.video&&this.video.removeEventListener("timeupdate",this.onTimeUpdate)}renderAtVideoTimestamp(){this.video&&this.renderAtTimestamp(this.video.currentTime+this.$timeOffset)}createCanvasElement(){const e=document.createElement("canvas");return e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.right="0",e.style.bottom="0",e.style.pointerEvents="none",e.style.objectFit="contain",e.style.width="100%",e.style.height="100%",e}destroyCanvasElement(){this.canvas.remove()}renderAtTimestamp(e){e=1e3*e*90;let t=-1;for(const s of this.updateTimestamps){if(s>e)break;t++}this.previousTimestampIndex!=t&&(this.previousTimestampIndex=t,this.worker.postMessage({op:"render",index:t}))}loadFromUrl(e){this.worker.postMessage({op:"loadFromUrl",url:e})}loadFromBuffer(e){this.worker.postMessage({op:"loadFromBuffer",buffer:e})}dispose(){this.worker.terminate(),this.unregisterVideoEvents(),this.canvasOwner&&this.destroyCanvasElement()}}}},t={};function s(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i](o,o.exports,s),o.exports}var i={};return(()=>{var e=i;Object.defineProperty(e,"__esModule",{value:!0}),e.PgsRenderer=void 0;const t=s(97);Object.defineProperty(e,"PgsRenderer",{enumerable:!0,get:function(){return t.PgsRenderer}})})(),i})()));

@@ -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.renderDisplaySet(this.displaySets[t])}renderDisplaySet(t){if(this.canvas&&t.presentationComposition){this.canvas.width=t.presentationComposition.width,this.canvas.height=t.presentationComposition.height;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){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)}}})()})();

@@ -37,2 +37,3 @@ import { DisplaySet } from "./pgs/displaySet";

renderAtIndex(index: number): void;
private clearCanvas;
private renderDisplaySet;

@@ -39,0 +40,0 @@ private renderDisplaySetComposition;

{
"name": "libpgs",
"version": "0.3.0",
"version": "0.3.1",
"author": "David Schulte",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -150,4 +150,3 @@ import {PgsRendererOptions} from "./pgsRendererOptions";

// Tell the worker to render
if (index < 0) return;
// Tell the worker to render.
this.worker.postMessage({

@@ -154,0 +153,0 @@ op: 'render',

@@ -72,6 +72,17 @@ import {DisplaySet} from "./pgs/displaySet";

public renderAtIndex(index: number): void {
if (index < 0 || index >= this.displaySets.length) 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.
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 {

@@ -81,6 +92,10 @@ if (!this.canvas) return;

// Setting the width and height will also clear the canvas.
this.canvas.width = displaySet.presentationComposition.width;
this.canvas.height = displaySet.presentationComposition.height;
// Resize the canvas if needed.
if (this.canvas.width != displaySet.presentationComposition.width ||
this.canvas.height != displaySet.presentationComposition.height) {
this.canvas.width = displaySet.presentationComposition.width;
this.canvas.height = displaySet.presentationComposition.height;
}
this.clearCanvas();
for (const composition of displaySet.presentationComposition.compositionObjects) {

@@ -87,0 +102,0 @@ this.renderDisplaySetComposition(displaySet, composition);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc