将数据URI合并到APNG中?

时间:2015-08-02 23:21:30

标签: javascript png apng

看看APNG规范,似乎APNG是PNG,用于描述第一个在“额外”块之后的帧“。所以看起来通过合成PNG头很容易在Javascript中创建APNG是可行的并将dataURI附加到它们。但它并不顺利。

function compileAPNG (frames, width, height, fps) {

    var fromCharCode = String.fromCharCode;

    var CRC = fromCharCode(0, 0, 0, 0);

    var pngData = fromCharCode(137, 80, 78, 71, 13, 10, 26, 10);

    pngData += fromCharCode(0, 0, 0, 13) + "IHDR" + convertIntToBytes(width) + convertIntToBytes(height) + fromCharCode(0, 0, 0, 0, 0) + CRC
    pngData += fromCharCode(0, 0, 0, 8) + "acTL" + convertIntToBytes(frames.length) + fromCharCode(0, 0, 0, 0) + CRC;
    pngData += fromCharCode(0, 0, 0, 25) + "fcTL" + fromCharCode(0, 0, 0, 0) + convertIntToBytes(width) + convertIntToBytes(height);
    pngData += fromCharCode(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + convertShortToBytes(fps) + fromCharCode(1, 0) + CRC;
    pngData += convertIntToBytes(frames[0].toDataURL().length) + "IDAT" + frames[0].toDataURL() + CRC;

    for (index = 1; index < frames.length; index++) {
        pngData += fromCharCode(0, 0, 0, 25) + "fcTL" + fromCharCode(0, 0, 0, 0) + convertIntToBytes(width) + convertIntToBytes(height);
        pngData += fromCharCode(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + convertShortToBytes(fps) + fromCharCode(1, 0) + CRC;
        pngData += convertIntToBytes(frames[index].toDataURL().length) + "fdAT" + convertIntToBytes(index) + frames[index].toDataURL() + CRC;
    }
    pngData += fromCharCode(0, 0, 0, 0) + "IEND" + CRC;
    window.open("data:image/png;base64," + btoa(pngData));
}

CRC计算的计算成本很高,所以我试图通过将它们全部设置为0来捏造它们。这就是为什么Firefox不接受dataURI作为图像的原因吗?

1 个答案:

答案 0 :(得分:2)

至少由于以下原因,您的文件被Firefox拒绝:

  1. Firefox会拒绝CRC错误的块。

  2. IHDR位深度不能为零。

  3. fcTL块和fdAT块上的序列号,除了     第一个fcTL块,不能为零(必须是0001,0002等)