Type getImgFromFilename

This commit is contained in:
Jordan Eldredge 2019-04-29 07:09:00 -07:00
parent 8b49b5fa6a
commit f56e707a16
2 changed files with 23 additions and 26 deletions

View file

@ -51,31 +51,8 @@ const CURSORS = [
*/
];
async function genImgFromFilename(zip, fileName) {
// Winamp only supports .bmp images, but WACUP set a precidence of supporting
// .png as well to reduce size. Since we care about size as well, we follow
// suit. Our default skin uses .png to save 14kb.
const file = await SkinParserUtils.getFileFromZip(
zip,
fileName,
"(png|bmp)",
"blob"
);
if (!file) {
return null;
}
const mimeType = `image/${SkinParserUtils.getFileExtension(file.name) ||
"*"}`;
// The spec for createImageBitmap() says the browser should try to sniff the
// mime type, but it looks like Firefox does not. So we specify it here
// explicitly.
const typedBlob = new Blob([file.contents], { type: mimeType });
return SkinParserUtils.getImgFromBlob(typedBlob);
}
async function genSpriteUrisFromFilename(zip, fileName) {
const img = await genImgFromFilename(zip, fileName);
const img = await SkinParserUtils.getImgFromFilename(zip, fileName);
if (img == null) {
return {};
}
@ -162,7 +139,7 @@ async function genRegion(zip) {
}
async function genGenTextSprites(zip) {
const img = await genImgFromFilename(zip, "GEN");
const img = await SkinParserUtils.getImgFromFilename(zip, "GEN");
if (img == null) {
return null;
}
@ -208,7 +185,7 @@ async function genGenTextSprites(zip) {
}
async function genGenExColors(zip) {
const img = await genImgFromFilename(zip, "GENEX");
const img = await SkinParserUtils.getImgFromFilename(zip, "GENEX");
if (img == null) {
return null;
}

View file

@ -75,3 +75,23 @@ export function getSpriteUrisFromImg(
});
return images;
}
export async function getImgFromFilename(
zip: JSZip,
fileName: string
): Promise<HTMLImageElement | ImageBitmap | null> {
// Winamp only supports .bmp images, but WACUP set a precidence of supporting
// .png as well to reduce size. Since we care about size as well, we follow
// suit. Our default skin uses .png to save 14kb.
const file = await getFileFromZip(zip, fileName, "(png|bmp)", "blob");
if (!file) {
return null;
}
const mimeType = `image/${getFileExtension(file.name) || "*"}`;
// The spec for createImageBitmap() says the browser should try to sniff the
// mime type, but it looks like Firefox does not. So we specify it here
// explicitly.
const typedBlob = new Blob([file.contents], { type: mimeType });
return getImgFromBlob(typedBlob);
}