Add support for Animated (A)PNG (#8158)
This commit is contained in:
parent
7798ecfa33
commit
a3e5231873
5 changed files with 106 additions and 48 deletions
|
@ -14,20 +14,28 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { arrayHasDiff } from "./arrays";
|
||||||
|
|
||||||
export function mayBeAnimated(mimeType: string): boolean {
|
export function mayBeAnimated(mimeType: string): boolean {
|
||||||
return ["image/gif", "image/webp"].includes(mimeType);
|
return ["image/gif", "image/webp", "image/png", "image/apng"].includes(mimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayBufferRead(arr: ArrayBuffer, start: number, len: number): Uint8Array {
|
function arrayBufferRead(arr: ArrayBuffer, start: number, len: number): Uint8Array {
|
||||||
return new Uint8Array(arr.slice(start, start + len));
|
return new Uint8Array(arr.slice(start, start + len));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function arrayBufferReadInt(arr: ArrayBuffer, start: number): number {
|
||||||
|
const dv = new DataView(arr, start, 4);
|
||||||
|
return dv.getUint32(0);
|
||||||
|
}
|
||||||
|
|
||||||
function arrayBufferReadStr(arr: ArrayBuffer, start: number, len: number): string {
|
function arrayBufferReadStr(arr: ArrayBuffer, start: number, len: number): string {
|
||||||
return String.fromCharCode.apply(null, arrayBufferRead(arr, start, len));
|
return String.fromCharCode.apply(null, arrayBufferRead(arr, start, len));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function blobIsAnimated(mimeType: string, blob: Blob): Promise<boolean> {
|
export async function blobIsAnimated(mimeType: string, blob: Blob): Promise<boolean> {
|
||||||
if (mimeType === "image/webp") {
|
switch (mimeType) {
|
||||||
|
case "image/webp": {
|
||||||
// Only extended file format WEBP images support animation, so grab the expected data range and verify header.
|
// Only extended file format WEBP images support animation, so grab the expected data range and verify header.
|
||||||
// Based on https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
|
// Based on https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
|
||||||
const arr = await blob.slice(0, 17).arrayBuffer();
|
const arr = await blob.slice(0, 17).arrayBuffer();
|
||||||
|
@ -41,7 +49,10 @@ export async function blobIsAnimated(mimeType: string, blob: Blob): Promise<bool
|
||||||
const animationFlagMask = 1 << 1;
|
const animationFlagMask = 1 << 1;
|
||||||
return (flags & animationFlagMask) != 0;
|
return (flags & animationFlagMask) != 0;
|
||||||
}
|
}
|
||||||
} else if (mimeType === "image/gif") {
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case "image/gif": {
|
||||||
// Based on https://gist.github.com/zakirt/faa4a58cec5a7505b10e3686a226f285
|
// Based on https://gist.github.com/zakirt/faa4a58cec5a7505b10e3686a226f285
|
||||||
// More info at http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
// More info at http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
||||||
const dv = new DataView(await blob.arrayBuffer(), 10);
|
const dv = new DataView(await blob.arrayBuffer(), 10);
|
||||||
|
@ -72,5 +83,37 @@ export async function blobIsAnimated(mimeType: string, blob: Blob): Promise<bool
|
||||||
return !!delayTime;
|
return !!delayTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "image/png":
|
||||||
|
case "image/apng": {
|
||||||
|
// Based on https://stackoverflow.com/a/68618296
|
||||||
|
const arr = await blob.arrayBuffer();
|
||||||
|
if (arrayHasDiff([
|
||||||
|
0x89,
|
||||||
|
0x50, 0x4E, 0x47,
|
||||||
|
0x0D, 0x0A,
|
||||||
|
0x1A,
|
||||||
|
0x0A,
|
||||||
|
], Array.from(arrayBufferRead(arr, 0, 8)))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 8; i < blob.size;) {
|
||||||
|
const length = arrayBufferReadInt(arr, i);
|
||||||
|
i += 4;
|
||||||
|
const type = arrayBufferReadStr(arr, i, 4);
|
||||||
|
i += 4;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case "acTL":
|
||||||
|
return true;
|
||||||
|
case "IDAT":
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i += length + 4;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,7 @@ const ALLOWED_BLOB_MIMETYPES = [
|
||||||
'image/jpeg',
|
'image/jpeg',
|
||||||
'image/gif',
|
'image/gif',
|
||||||
'image/png',
|
'image/png',
|
||||||
|
'image/apng',
|
||||||
'image/webp',
|
'image/webp',
|
||||||
|
|
||||||
'video/mp4',
|
'video/mp4',
|
||||||
|
|
|
@ -29,7 +29,10 @@ describe("Image", () => {
|
||||||
expect(mayBeAnimated("image/webp")).toBeTruthy();
|
expect(mayBeAnimated("image/webp")).toBeTruthy();
|
||||||
});
|
});
|
||||||
it("image/png", async () => {
|
it("image/png", async () => {
|
||||||
expect(mayBeAnimated("image/png")).toBeFalsy();
|
expect(mayBeAnimated("image/png")).toBeTruthy();
|
||||||
|
});
|
||||||
|
it("image/apng", async () => {
|
||||||
|
expect(mayBeAnimated("image/apng")).toBeTruthy();
|
||||||
});
|
});
|
||||||
it("image/jpeg", async () => {
|
it("image/jpeg", async () => {
|
||||||
expect(mayBeAnimated("image/jpeg")).toBeFalsy();
|
expect(mayBeAnimated("image/jpeg")).toBeFalsy();
|
||||||
|
@ -37,25 +40,36 @@ describe("Image", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("blobIsAnimated", () => {
|
describe("blobIsAnimated", () => {
|
||||||
const animatedGif = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.gif"))]);
|
|
||||||
const animatedWebp = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.webp"))]);
|
|
||||||
const staticGif = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.gif"))]);
|
|
||||||
const staticWebp = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.webp"))]);
|
|
||||||
|
|
||||||
it("Animated GIF", async () => {
|
it("Animated GIF", async () => {
|
||||||
expect(await blobIsAnimated("image/gif", animatedGif)).toBeTruthy();
|
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.gif"))]);
|
||||||
|
expect(await blobIsAnimated("image/gif", img)).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Static GIF", async () => {
|
it("Static GIF", async () => {
|
||||||
expect(await blobIsAnimated("image/gif", staticGif)).toBeFalsy();
|
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.gif"))]);
|
||||||
|
expect(await blobIsAnimated("image/gif", img)).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Animated WEBP", async () => {
|
it("Animated WEBP", async () => {
|
||||||
expect(await blobIsAnimated("image/webp", animatedWebp)).toBeTruthy();
|
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.webp"))]);
|
||||||
|
expect(await blobIsAnimated("image/webp", img)).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Static WEBP", async () => {
|
it("Static WEBP", async () => {
|
||||||
expect(await blobIsAnimated("image/webp", staticWebp)).toBeFalsy();
|
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.webp"))]);
|
||||||
|
expect(await blobIsAnimated("image/webp", img)).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Animated PNG", async () => {
|
||||||
|
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.apng"))]);
|
||||||
|
expect(await blobIsAnimated("image/png", img)).toBeTruthy();
|
||||||
|
expect(await blobIsAnimated("image/apng", img)).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Static PNG", async () => {
|
||||||
|
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.png"))]);
|
||||||
|
expect(await blobIsAnimated("image/png", img)).toBeFalsy();
|
||||||
|
expect(await blobIsAnimated("image/apng", img)).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
BIN
test/images/animated-logo.apng
Normal file
BIN
test/images/animated-logo.apng
Normal file
Binary file not shown.
After Width: | Height: | Size: 46 KiB |
BIN
test/images/static-logo.png
Normal file
BIN
test/images/static-logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
Loading…
Add table
Add a link
Reference in a new issue