New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.1.0 to 0.2.0

dist/utils/arrayBinaryReader.d.ts

2

dist/libpgs.js

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

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.libpgs=e():t.libpgs=e()}(self,(()=>(()=>{"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)}}}},97: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.PgsRenderer=void 0;const s=i(719),o=i(489),r=i(494);e.PgsRenderer=class{constructor(t){if(this.displaySets=[],this.currentIndex=-1,this.onTimeUpdate=t=>{this.video&&this.renderAtTimestamp(this.video.currentTime)},t.video&&(this.video=t.video),t.canvas)this.canvas=t.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)}this.context=this.canvas.getContext("2d"),this.registerEvents(),t.fileUrl&&this.loadFromUrlAsync(t.fileUrl).then()}createCanvasElement(){const t=document.createElement("canvas");return t.style.position="absolute",t.style.top="0",t.style.left="0",t.style.right="0",t.style.bottom="0",t.style.pointerEvents="none",t.style.objectFit="contain",t.style.width="100%",t.style.height="100%",t}destroyCanvasElement(){this.canvas.remove()}registerEvents(){this.video&&this.video.addEventListener("timeupdate",this.onTimeUpdate)}unregisterEvents(){this.video&&this.video.removeEventListener("timeupdate",this.onTimeUpdate)}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=[];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)}}renderAtTimestamp(t){let e=-1;t=1e3*t*90;for(const i of this.displaySets){if(i.presentationTimestamp>t)break;e++}if(this.currentIndex==e)return;if(this.currentIndex=e,e<0)return;const i=this.displaySets[e];this.renderDisplaySet(i)}renderDisplaySet(t){if(t.presentationComposition){this.canvas.width=t.presentationComposition.width,this.canvas.height=t.presentationComposition.height;for(const e of t.presentationComposition.compositionObjects)this.renderComposition(t,e)}}renderComposition(t,e){if(!t.presentationComposition)return;let i=t.windowDefinitions.flatMap((t=>t.windows)).find((t=>t.id===e.windowId));if(!i)return;const n=this.getPixelDataFromComposition(t,e);n&&this.context.drawImage(n,i.horizontalPosition,i.verticalPosition)}getPixelDataFromComposition(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,o=0;const a=[];for(const i of t.objectDefinitions)i.id==e.id&&(i.isFirstInSequence&&(n=i.width,s=i.height),i.data&&(o+=i.data.length,a.push(i.data)));if(0==o)return;const d=new Uint8Array(o);let h=0;for(const t of a)d.set(t,h),h+=t.length;const c=document.createElement("canvas"),p=c.getContext("2d");c.width=n,c.height=s;const l=p.createImageData(n,s),u=l.data;return(0,r.runLengthEncoding)(d,((t,e,n,s)=>{const o=null==i?void 0:i.entries[s];o&&(u[4*t]=o.r,u[4*t+1]=o.g,u[4*t+2]=o.b,u[4*t+3]=o.a)})),p.putImageData(l,0,0),c}dispose(){this.unregisterEvents(),this.canvasOwner&&this.destroyCanvasElement(),this.displaySets=[]}}},489:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.BigEndianBinaryReader=void 0,e.BigEndianBinaryReader=class{constructor(t){this.current=0,this.buffer=t}get position(){return this.current}get length(){return this.buffer.length}readUInt8(){return this.buffer[this.current++]}readUInt16(){return(this.readUInt8()<<8)+this.readUInt8()}readUInt24(){return(this.readUInt8()<<16)+(this.readUInt8()<<8)+this.readUInt8()}readUInt32(){return(this.readUInt8()<<24)+(this.readUInt8()<<16)+(this.readUInt8()<<8)+this.readUInt8()}readBytes(t){const e=this.buffer.slice(this.current,this.current+t);return this.current+=t,e}}},494:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.runLengthEncoding=void 0,e.runLengthEncoding=function(t,e){let i=0,n=0,s=0,o=0;for(;s<t.length;){const r=t[s++];if(0!=r){e(o++,i++,n,r);continue}const a=t[s++];if(0==a){i=0,n++;continue}const d=!!(128&a);let h=63&a;64&a&&(h=(h<<8)+t[s++]);const c=d?t[s++]:0;for(let t=0;t<h;t++)e(o++,i++,n,c)}}}},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}var n={};return(()=>{var t=n;Object.defineProperty(t,"__esModule",{value:!0}),t.PgsRenderer=void 0;const e=i(97);Object.defineProperty(t,"PgsRenderer",{enumerable:!0,get:function(){return e.PgsRenderer}})})(),n})()));
!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={719:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DisplaySet=void 0;const n=i(498),s=i(866),o=i(818),r=i(207),a=i(32);t.DisplaySet=class{constructor(){this.presentationTimestamp=0,this.decodingTimestamp=0,this.paletteDefinitions=[],this.objectDefinitions=[],this.windowDefinitions=[]}read(e,t){for(this.presentationTimestamp=0,this.decodingTimestamp=0,this.presentationComposition=void 0,this.paletteDefinitions=[],this.objectDefinitions=[],this.windowDefinitions=[];;){if(t){if(20551!=e.readUInt16())throw new Error("Invalid magic number!");this.presentationTimestamp=e.readUInt32(),this.decodingTimestamp=e.readUInt32()}const i=e.readUInt8(),d=e.readUInt16();switch(i){case a.SegmentType.paletteDefinition:const t=new s.PaletteDefinitionSegment;t.read(e,d),this.paletteDefinitions.push(t);break;case a.SegmentType.objectDefinition:const h=new o.ObjectDefinitionSegment;h.read(e,d),this.objectDefinitions.push(h);break;case a.SegmentType.presentationComposition:const c=new n.PresentationCompositionSegment;c.read(e,d),this.presentationComposition=c;break;case a.SegmentType.windowDefinition:const p=new r.WindowDefinitionSegment;p.read(e,d),this.windowDefinitions.push(p);break;case a.SegmentType.end:return;default:throw new Error(`Unsupported segment type ${i}`)}}}}},818:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ObjectDefinitionSegment=void 0;const n=i(32);t.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(e,t){this.id=e.readUInt16(),this.versionNumber=e.readUInt8(),this.lastInSequenceFlag=e.readUInt8(),this.isFirstInSequence?(this.dataLength=e.readUInt24(),this.width=e.readUInt16(),this.height=e.readUInt16(),this.data=e.readBytes(t-11)):this.data=e.readBytes(t-4)}}},866:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PaletteDefinitionSegment=t.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}}t.PaletteEntry=s;class o{constructor(){this.id=0,this.versionNumber=0,this.entries={}}get segmentType(){return n.SegmentType.paletteDefinition}read(e,t){this.id=e.readUInt8(),this.versionNumber=e.readUInt8();const i=(t-2)/5;this.entries={};for(let t=0;t<i;t++){const t=e.readUInt8(),i=new s;i.y=e.readUInt8(),i.cr=e.readUInt8(),i.cb=e.readUInt8(),i.a=e.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[t]=i}}static clamp(e,t,i){return Math.max(t,Math.min(e,i))}}t.PaletteDefinitionSegment=o},498:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PresentationCompositionSegment=t.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)}}t.CompositionObject=s,t.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(e,t){this.width=e.readUInt16(),this.height=e.readUInt16(),this.frameRate=e.readUInt8(),this.compositionNumber=e.readUInt16(),this.compositionState=e.readUInt8(),this.paletteUpdateFlag=e.readUInt8(),this.paletteId=e.readUInt8();const i=e.readUInt8();this.compositionObjects=[];for(let t=0;t<i;t++){const t=new s;t.id=e.readUInt16(),t.windowId=e.readUInt8(),t.croppedFlag=e.readUInt8(),t.horizontalPosition=e.readUInt16(),t.verticalPosition=e.readUInt16(),t.hasCropping&&(t.croppingHorizontalPosition=e.readUInt16(),t.croppingVerticalPosition=e.readUInt16(),t.croppingWidth=e.readUInt16(),t.croppingHeightPosition=e.readUInt16()),this.compositionObjects.push(t)}}}},32:(e,t)=>{var i;Object.defineProperty(t,"__esModule",{value:!0}),t.SegmentType=void 0,function(e){e[e.paletteDefinition=20]="paletteDefinition",e[e.objectDefinition=21]="objectDefinition",e[e.presentationComposition=22]="presentationComposition",e[e.windowDefinition=23]="windowDefinition",e[e.end=128]="end"}(i||(t.SegmentType=i={}))},207:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.WindowDefinitionSegment=t.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}}t.WindowDefinition=s,t.WindowDefinitionSegment=class{constructor(){this.windows=[]}get segmentType(){return n.SegmentType.windowDefinition}read(e,t){const i=e.readUInt8();this.windows=[];for(let t=0;t<i;t++){const t=new s;t.id=e.readUInt8(),t.horizontalPosition=e.readUInt16(),t.verticalPosition=e.readUInt16(),t.width=e.readUInt16(),t.height=e.readUInt16(),this.windows.push(t)}}}},97:function(e,t,i){var n=this&&this.__awaiter||function(e,t,i,n){return new(i||(i=Promise))((function(s,o){function r(e){try{d(n.next(e))}catch(e){o(e)}}function a(e){try{d(n.throw(e))}catch(e){o(e)}}function d(e){var t;e.done?s(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}d((n=n.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.PgsRenderer=void 0;const s=i(719),o=i(489),r=i(494),a=i(381);t.PgsRenderer=class{constructor(e){if(this.displaySets=[],this.currentIndex=-1,this.onTimeUpdate=e=>{this.video&&this.renderAtTimestamp(this.video.currentTime)},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 t=this.canvas.getContext("2d");if(!t)throw new Error("Can not create 2d canvas context!");this.context=t,this.registerEvents(),e.subUrl&&this.loadFromUrlAsync(e.subUrl).then()}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()}registerEvents(){this.video&&this.video.addEventListener("timeupdate",this.onTimeUpdate)}unregisterEvents(){this.video&&this.video.removeEventListener("timeupdate",this.onTimeUpdate)}loadFromUrlAsync(e){return n(this,void 0,void 0,(function*(){const t=yield fetch(e),i=yield t.arrayBuffer();this.loadFromBuffer(i)}))}loadFromBuffer(e){this.displaySets=[];const t=new o.BigEndianBinaryReader(new Uint8Array(e));for(;t.position<t.length;){const e=new s.DisplaySet;e.read(t,!0),this.displaySets.push(e)}}renderAtTimestamp(e){let t=-1;e=1e3*e*90;for(const i of this.displaySets){if(i.presentationTimestamp>e)break;t++}if(this.currentIndex==t)return;if(this.currentIndex=t,t<0)return;const i=this.displaySets[t];this.renderDisplaySet(i)}renderDisplaySet(e){if(e.presentationComposition){this.canvas.width=e.presentationComposition.width,this.canvas.height=e.presentationComposition.height;for(const t of e.presentationComposition.compositionObjects)this.renderComposition(e,t)}}renderComposition(e,t){if(!e.presentationComposition)return;let i=e.windowDefinitions.flatMap((e=>e.windows)).find((e=>e.id===t.windowId));if(!i)return;const n=this.getPixelDataFromComposition(e,t);n&&this.context.drawImage(n,i.horizontalPosition,i.verticalPosition)}getPixelDataFromComposition(e,t){if(!e.presentationComposition)return;let i=e.paletteDefinitions.find((t=>{var i;return t.id===(null===(i=e.presentationComposition)||void 0===i?void 0:i.paletteId)}));if(!i)return;let n=0,s=0;const o=[];for(const i of e.objectDefinitions)i.id==t.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=document.createElement("canvas"),c=h.getContext("2d");h.width=n,h.height=s;const p=c.createImageData(n,s),l=p.data;return r.RunLengthEncoding.decode(d,((e,t,n,s)=>{const o=null==i?void 0:i.entries[s];o&&(l[4*e]=o.r,l[4*e+1]=o.g,l[4*e+2]=o.b,l[4*e+3]=o.a)})),c.putImageData(p,0,0),h}dispose(){this.unregisterEvents(),this.canvasOwner&&this.destroyCanvasElement(),this.displaySets=[]}}},385:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ArrayBinaryReader=void 0,t.ArrayBinaryReader=class{constructor(e){this._position=0,this.array=e}get position(){return this._position}get length(){return this.array.length}readByte(){return this.array[this._position++]}readBytes(e){const t=this.array.slice(this._position,this._position+e);return this._position+=e,t}}},489:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.BigEndianBinaryReader=void 0;const n=i(385);t.BigEndianBinaryReader=class{constructor(e){e instanceof Uint8Array?this.reader=new n.ArrayBinaryReader(e):this.reader=e}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(e){return this.reader.readBytes(e)}}},381:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.CombinedBinaryReader=void 0;const n=i(385);t.CombinedBinaryReader=class{constructor(e){this._position=0,this.subReaderIndex=0,this.subReaders=e.map((e=>e instanceof Uint8Array?new n.ArrayBinaryReader(e):e));let t=0;for(const i of e)t+=i.length;this._length=t}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(e){const t=new Uint8Array(e);for(let i=0;i<e;i++)t[i]=this.readByte();return t}}},494:(e,t,i)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.RunLengthEncoding=void 0;const n=i(385);t.RunLengthEncoding=class{static decode(e,t){e instanceof Uint8Array&&(e=new n.ArrayBinaryReader(e));let i=0,s=0,o=0;for(;e.position<e.length;){const n=e.readByte();if(0!=n){t(o++,i++,s,n);continue}const r=e.readByte();if(0==r){i=0,s++;continue}const a=!!(128&r);let d=63&r;64&r&&(d=(d<<8)+e.readByte());const h=a?e.readByte():0;for(let e=0;e<d;e++)t(o++,i++,s,h)}}}}},t={};function i(n){var s=t[n];if(void 0!==s)return s.exports;var o=t[n]={exports:{}};return e[n].call(o.exports,o,o.exports,i),o.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.PgsRenderer=void 0;const t=i(97);Object.defineProperty(e,"PgsRenderer",{enumerable:!0,get:function(){return t.PgsRenderer}})})(),n})()));
import { PgsRendererOptions } from "./pgsRendererOptions";
/**
* Renders PGS subtitle on-top of a video element.
* Renders PGS subtitle on-top of a video element using a canvas element.
*/

@@ -5,0 +5,0 @@ export declare class PgsRenderer {

@@ -15,3 +15,3 @@ export interface PgsRendererOptions {

*/
fileUrl?: string;
subUrl?: string;
}

@@ -0,5 +1,8 @@

import { BinaryReader } from "./binaryReader";
export declare class BigEndianBinaryReader {
private readonly buffer;
private current;
constructor(buffer: Uint8Array);
/**
* The base binary reader.
*/
private readonly reader;
constructor(buffer: BinaryReader | Uint8Array);
get position(): number;

@@ -6,0 +9,0 @@ get length(): number;

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

export declare function runLengthEncoding(data: Uint8Array, setter: (idx: number, x: number, y: number, value: number) => void): void;
import { BinaryReader } from "./binaryReader";
export declare abstract class RunLengthEncoding {
/**
* Decodes the run length encoded image.
* @param reader The run length encoded binary data reader or buffer.
* @param setter This callback is invoked for every pixel and provides the index, coordinate and value.
*/
static decode(reader: BinaryReader | Uint8Array, setter: (idx: number, x: number, y: number, value: number) => void): void;
}
{
"name": "libpgs",
"version": "0.1.0",
"version": "0.2.0",
"author": "David Schulte",

@@ -25,2 +25,3 @@ "license": "MIT",

"build": "webpack -c webpack.configuration.js",
"test": "jest",
"clear": "rm -rf ./dist"

@@ -30,2 +31,5 @@ },

"@tsconfig/recommended": "^1.0.6",
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"ts-jest": "^29.1.4",
"ts-loader": "^9.5.1",

@@ -32,0 +36,0 @@ "ts-node": "^10.9.2",

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

# libpgs-js
This library renders the graphical subtitles format PGS _(.sub / .sup)_ in the browser.
## Usage
Install the package via npm:
```
npm i --save libpgs
```
```html
<div style="position: relative">
<video id="video-element" src="./video.mp4"></video>
</div>
```
```javascript
const videoElement = document.getElementById('video-element');
const pgsRenderer = new libpgs.PgsRenderer({
video: videoElement,
subUrl: './subtitle.sup'
});
```
## Licence
[MIT License](LICENSE)
import {DisplaySet} from "./pgs/displaySet";
import {BigEndianBinaryReader} from "./utils/bigEndianBinaryReader";
import {runLengthEncoding} from "./utils/runLengthEncoding";
import {RunLengthEncoding} from "./utils/runLengthEncoding";
import {CompositionObject} from "./pgs/presentationCompositionSegment";
import {PgsRendererOptions} from "./pgsRendererOptions";
import {CombinedBinaryReader} from "./utils/combinedBinaryReader";
/**
* Renders PGS subtitle on-top of a video element.
* Renders PGS subtitle on-top of a video element using a canvas element.
*/

@@ -34,3 +35,7 @@ export class PgsRenderer {

this.context = this.canvas.getContext('2d')!;
const context = this.canvas.getContext('2d');
if (!context) {
throw new Error('Can not create 2d canvas context!');
}
this.context = context;

@@ -40,4 +45,4 @@ this.registerEvents();

// Load the initial subtitle file
if (options.fileUrl) {
this.loadFromUrlAsync(options.fileUrl).then();
if (options.subUrl) {
this.loadFromUrlAsync(options.subUrl).then();
}

@@ -161,3 +166,2 @@ }

let height: number = 0;
let dataLength: number = 0;
const dataChunks: Uint8Array[] = [];

@@ -172,18 +176,10 @@ for (const ods of displaySet.objectDefinitions) {

if (ods.data) {
dataLength += ods.data.length;
dataChunks.push(ods.data);
}
}
if (dataLength == 0) {
if (dataChunks.length == 0) {
return undefined;
}
// Merge the data into one binary blob.
// TODO: Make this more memory efficient...
const runLengthData = new Uint8Array(dataLength);
let offset: number = 0;
for (const dataChunk of dataChunks) {
runLengthData.set(dataChunk, offset);
offset += dataChunk.length;
}
const data = new CombinedBinaryReader(dataChunks);

@@ -199,3 +195,3 @@ // Building the subtitle image.

// The pixel data is run-length encoded. The value is the palette entry index.
runLengthEncoding(runLengthData, (idx, x, y, value) => {
RunLengthEncoding.decode(data, (idx, x, y, value) => {
const col = palette?.entries[value];

@@ -202,0 +198,0 @@ if (!col) return;

@@ -17,3 +17,3 @@ export interface PgsRendererOptions {

*/
fileUrl?: string;
subUrl?: string;
}

@@ -0,25 +1,34 @@

import {BinaryReader} from "./binaryReader";
import {ArrayBinaryReader} from "./arrayBinaryReader";
export class BigEndianBinaryReader {
private readonly buffer: Uint8Array;
/**
* The base binary reader.
*/
private readonly reader: BinaryReader;
private current: number = 0;
public constructor(buffer: Uint8Array) {
this.buffer = buffer;
public constructor(buffer: BinaryReader | Uint8Array) {
if (buffer instanceof Uint8Array) {
this.reader = new ArrayBinaryReader(buffer);
}
else {
this.reader = buffer;
}
}
public get position(): number {
return this.current;
return this.reader.position;
}
public get length(): number {
return this.buffer.length;
return this.reader.length;
}
public readUInt8(): number {
return this.buffer[this.current++];
return this.reader.readByte();
}
public readUInt16(): number {
const b1 = this.readUInt8();
const b2 = this.readUInt8();
const b1 = this.reader.readByte();
const b2 = this.reader.readByte();
return (b1 << 8) + b2;

@@ -29,5 +38,5 @@ }

public readUInt24(): number {
const b1 = this.readUInt8();
const b2 = this.readUInt8();
const b3 = this.readUInt8();
const b1 = this.reader.readByte();
const b2 = this.reader.readByte();
const b3 = this.reader.readByte();
return (b1 << 16) + (b2 << 8) + b3;

@@ -37,6 +46,6 @@ }

public readUInt32(): number {
const b1 = this.readUInt8();
const b2 = this.readUInt8();
const b3 = this.readUInt8();
const b4 = this.readUInt8();
const b1 = this.reader.readByte();
const b2 = this.reader.readByte();
const b3 = this.reader.readByte();
const b4 = this.reader.readByte();
return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;

@@ -46,6 +55,4 @@ }

public readBytes(count: number): Uint8Array {
const data = this.buffer.slice(this.current, this.current + count);
this.current += count;
return data;
return this.reader.readBytes(count);
}
}

@@ -1,33 +0,46 @@

export function runLengthEncoding(data: Uint8Array, setter: (idx: number, x: number, y: number, value: number) => void): void {
let x = 0;
let y = 0;
let p = 0;
let idx = 0;
while (p < data.length) {
const byte1 = data[p++];
// Raw byte
if (byte1 != 0x00) {
setter(idx++, x++, y, byte1);
continue;
}
import {BinaryReader} from "./binaryReader";
import {ArrayBinaryReader} from "./arrayBinaryReader";
const byte2 = data[p++];
// End of line
if (byte2 == 0x00) {
x = 0;
y++;
continue;
export abstract class RunLengthEncoding {
/**
* Decodes the run length encoded image.
* @param reader The run length encoded binary data reader or buffer.
* @param setter This callback is invoked for every pixel and provides the index, coordinate and value.
*/
static decode(reader: BinaryReader | Uint8Array, setter: (idx: number, x: number, y: number, value: number) => void): void {
if (reader instanceof Uint8Array) {
reader = new ArrayBinaryReader(reader);
}
const bit8 = (byte2 & 0b10000000) != 0;
const bit7 = (byte2 & 0b01000000) != 0;
let num = byte2 & 0b00111111;
if (bit7) {
num = (num << 8) + data[p++];
let x = 0;
let y = 0;
let idx = 0;
while (reader.position < reader.length) {
const byte1 = reader.readByte();
// Raw byte
if (byte1 != 0x00) {
setter(idx++, x++, y, byte1);
continue;
}
const byte2 = reader.readByte();
// End of line
if (byte2 == 0x00) {
x = 0;
y++;
continue;
}
const bit8 = (byte2 & 0b10000000) != 0;
const bit7 = (byte2 & 0b01000000) != 0;
let num = byte2 & 0b00111111;
if (bit7) {
num = (num << 8) + reader.readByte();
}
const value = bit8 ? reader.readByte() : 0x00;
for (let i = 0; i < num; i++) {
setter(idx++, x++, y, value);
}
}
const value = bit8 ? data[p++] : 0x00;
for (let i = 0; i < num; i++) {
setter(idx++, x++, y, value);
}
}
}
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