Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working on latest Google Chrome #29

Closed
Bajdzis opened this issue Mar 5, 2022 · 2 comments · Fixed by #30
Closed

Not working on latest Google Chrome #29

Bajdzis opened this issue Mar 5, 2022 · 2 comments · Fixed by #30

Comments

@Bajdzis
Copy link

Bajdzis commented Mar 5, 2022

antimatter15/whammy#70

Can you change content file ts-whammy\utils\parseRIFF.ts by this :

import { IRiff } from '../interfaces';

type ChunkSizeAndBinaryData = string;

function readUint32LittleEndian(buffer: string, offset: number): number {
    const val = parseInt(
        buffer
            .substr(offset, 4)
            .split('')
            .map(function (i) {
                const unpadded = i.charCodeAt(0).toString(2);
                return new Array(8 - unpadded.length + 1).join('0') + unpadded;
            })
            .reverse()
            .join(''),
        2,
    );
    return val;
}

/**
 * 对于 VP8X,需要提取出其中的 VP8 或 VP8L bit stream chunk。
 * 关于 VP8X 格式,参见 Extended file format: https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
 * @param buffer VP8X Chunk数据,不含 "VP8X" tag
 */
function extractBitStreamFromVp8x(buffer: string): ChunkSizeAndBinaryData {
    /*
   32bit VP8X Chunk size
   8bit Flags: Rsv I L E X A R
   24bit Reserved
   24bit Canvas Width Minus One
   24bit Canvas Height Minus One
  */
    let offset = 4 + 1 + 3 + 3 + 3;
    while (offset < buffer.length) {
        const chunkTag = buffer.substr(offset, 4);
        offset += 4;
        const chunkSize = readUint32LittleEndian(buffer, offset);
        offset += 4;
        switch (chunkTag) {
            case 'VP8 ':
            case 'VP8L':
                // eslint-disable-next-line no-case-declarations
                const size = buffer.substr(offset - 4, 4);
                // eslint-disable-next-line no-case-declarations
                const body = buffer.substr(offset, chunkSize);
                return size + body;
            default:
                offset += chunkSize;
                break;
        }
    }
    throw new Error('VP8X format error: missing VP8/VP8L chunk.');
}

export default function parseRIFF(string: string): IRiff {
    let offset = 0;
    const chunks: {
        [index: string]: any;
    } = {};

    while (offset < string.length) {
        const id = string.substr(offset, 4);
        chunks[id] = chunks[id] || [];
        if (id === 'RIFF' || id === 'LIST') {
            const len = readUint32LittleEndian(string, offset + 4);
            const data = string.substr(offset + 4 + 4, len);
            offset += 4 + 4 + len;
            chunks[id].push(parseRIFF(data));
        } else if (id === 'WEBP') {
            const vpVersion = string.substr(offset + 4, 4);
            switch (vpVersion) {
                case 'VP8X':
                    chunks[id].push(extractBitStreamFromVp8x(string.substr(offset + 8)));
                    break;
                case 'VP8 ':
                case 'VP8L':
                    // Use (offset + 8) to skip past "VP8 " / "VP8L" field after "WEBP"
                    chunks[id].push(string.substr(offset + 8));
                    break;
                default:
                    // eslint-disable-next-line no-console
                    console.error(`not supported webp version: "${vpVersion}"`);
                    break;
            }
            offset = string.length;
        } else {
            // Unknown chunk type; push entire payload
            chunks[id].push(string.substr(offset + 4));
            offset = string.length;
        }
    }
    return (chunks as any) as IRiff;
}
@Akimyou
Copy link
Owner

Akimyou commented Mar 5, 2022

Ok, i will update and test later.

@Akimyou
Copy link
Owner

Akimyou commented Mar 5, 2022

if you have time, you can make an PR please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants