Skip to content

Commit

Permalink
optimized comparisons for #17
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Sep 13, 2024
1 parent fb294b7 commit 122113c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/functions/images/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const GRADIENT_HEIGHT = 50;
export interface ColorStorage {
hex: string;
opacity: number[];
// https://devblogs.microsoft.com/typescript/announcing-typescript-4-0/#labeled-tuple-elements
rgb: [r: number, g: number, b: number];
count: number;
}
Expand Down
43 changes: 15 additions & 28 deletions src/functions/submission/utility/generateComparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,24 @@ export default async function generateComparison(
require("@resources/packs.json")[pack].submission.reference ?? "default";
const baseURL = `${process.env.API_URL}textures/${texture.id}/url/`;

const newImage = await loadImage(attachment.url);

/**
* IMAGE LOADING
* - tries to go from left to right: Reference | New | Current
* [Reference, New, Current?]
*/

const images: Image[] = [];

try {
images.push(await loadImage(`${baseURL}${reference}/latest`));
} catch {
// reference texture doesn't exist so we use the default repo
try {
images.push(await loadImage(`${baseURL}default/latest`));
} catch {
// default texture doesn't exist either
}
}

// pushing after the reference texture for better ordering
images.push(newImage);

try {
images.push(await loadImage(`${baseURL}${pack}/latest`));
} catch {
// texture being submitted is a new texture, so there's nothing to compare against
}
const images: Image[] = (
await Promise.all([
loadImage(`${baseURL}${reference}/latest`)
// fall back to default if reference doesn't exist
.catch(() => loadImage(`${baseURL}default/latest`))
// default doesn't exist either
.catch(() => null),
loadImage(attachment.url),
// may not be present and that's fine
loadImage(`${baseURL}${pack}/latest`).catch(() => null),
])
).filter((v) => v !== null);

// return early if the reference texture couldn't be fetched
if (images.length == 1) {
if (images.length === 1) {
return {
comparisonImage: await magnifyToAttachment(images[0], "magnified.png"),
hasReference: false,
Expand All @@ -71,7 +58,7 @@ export default async function generateComparison(
if (!texture.paths.some((p) => p.mcmeta === true))
return {
comparisonImage: await magnifyToAttachment(stitched, "compared.png"),
hasReference: images.length == 3,
hasReference: images.length === 3,
};

const { mcmeta } = texture;
Expand Down

0 comments on commit 122113c

Please sign in to comment.