MCPcopy Create free account
hub / github.com/Vanilagy/webcodecs-polyfill / ParseVisibleRect

Function ParseVisibleRect

src/video_frame.ts:676–717  ·  view source on GitHub ↗
(
	defaultRect: DOMRectInit,
	overrideRect: DOMRectInit | undefined,
	codedWidth: number,
	codedHeight: number,
	format: VideoPixelFormat | null,
)

Source from the content-addressed store, hash-verified

674};
675
676const ParseVisibleRect = (
677 defaultRect: DOMRectInit,
678 overrideRect: DOMRectInit | undefined,
679 codedWidth: number,
680 codedHeight: number,
681 format: VideoPixelFormat | null,
682): DOMRectInit => {
683 // 1. Let sourceRect be defaultRect
684 const sourceRect = { ...defaultRect };
685
686 // 2. If overrideRect is not undefined:
687 if (overrideRect !== undefined) {
688 // If either of overrideRect.width or height is 0, return a TypeError.
689 if (overrideRect.width === 0 || overrideRect.height === 0) {
690 throw new TypeError('visibleRect dimensions cannot be zero');
691 }
692 // If the sum of overrideRect.x and overrideRect.width is greater than codedWidth, return a TypeError.
693 if ((overrideRect.x || 0) + (overrideRect.width || 0) > codedWidth) {
694 throw new TypeError('visibleRect exceeds codedWidth');
695 }
696 // If the sum of overrideRect.y and overrideRect.height is greater than codedHeight, return a TypeError.
697 if ((overrideRect.y || 0) + (overrideRect.height || 0) > codedHeight) {
698 throw new TypeError('visibleRect exceeds codedHeight');
699 }
700 // Assign overrideRect to sourceRect.
701 sourceRect.x = overrideRect.x || 0;
702 sourceRect.y = overrideRect.y || 0;
703 sourceRect.width = overrideRect.width || 0;
704 sourceRect.height = overrideRect.height || 0;
705 }
706
707 // 3. Let validAlignment be the result of running the Verify Rect Offset Alignment algorithm.
708 const validAlignment = VerifyRectOffsetAlignment(format, sourceRect);
709
710 // 4. If validAlignment is false, throw a TypeError.
711 if (!validAlignment) {
712 throw new TypeError('visibleRect alignment is invalid for the format');
713 }
714
715 // 5. Return sourceRect.
716 return sourceRect;
717};
718
719const VerifyRectOffsetAlignment = (format: VideoPixelFormat | null, rect: DOMRectInit): boolean => {
720 // 1. If format is null, return true.

Callers 2

constructorMethod · 0.85

Calls 1

Tested by

no test coverage detected