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

fix mime type error #5419

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/packages/looker/src/processOverlays.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";

import HeatmapOverlay from "./overlays/heatmap";
import SegmentationOverlay from "./overlays/segmentation";
import { filter } from "./processOverlays";

const EMPTY = {
id: "",
tags: [],
};

describe("test overlay processing", () => {
it("filters heatmap without a map", () => {
expect(filter(new HeatmapOverlay("test", EMPTY), {})).toBe(true);
});

it("filters segmentations without a mask", () => {
expect(filter(new SegmentationOverlay("test", EMPTY), {})).toBe(true);
});
});
benjaminpkane marked this conversation as resolved.
Show resolved Hide resolved
55 changes: 31 additions & 24 deletions app/packages/looker/src/processOverlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* Copyright 2017-2025, Voxel51, Inc.
*/

import { CONTAINS, Overlay } from "./overlays/base";
import type { Overlay } from "./overlays/base";
import { CONTAINS } from "./overlays/base";
import { ClassificationsOverlay } from "./overlays/classifications";
import HeatmapOverlay from "./overlays/heatmap";
import SegmentationOverlay from "./overlays/segmentation";
import { BaseState } from "./state";
import type { BaseState } from "./state";
import { rotate } from "./util";

const processOverlays = <State extends BaseState>(
Expand All @@ -30,33 +31,17 @@ const processOverlays = <State extends BaseState>(
continue;
}

if (!(overlay.field && overlay.field in bins)) continue;

// todo: find a better approach / place for this.
// for instance, this won't work in detection overlay, where
// we might want the bounding boxes but masks might not have been loaded
if (
overlay instanceof SegmentationOverlay &&
overlay.label.mask_path &&
!overlay.label.mask
) {
continue;
}

if (
overlay instanceof HeatmapOverlay &&
overlay.label.map_path &&
!overlay.label.map
) {
continue;
}

if (!overlay.isShown(state)) continue;

if (filter(overlay, bins)) continue;

bins[overlay.field].push(overlay);
}

let ordered = activePaths.reduce((acc, cur) => [...acc, ...bins[cur]], []);
let ordered = activePaths.reduce((acc, cur) => {
acc.push(...bins[cur]);
return acc;
}, []);

if (classifications && !state.config.thumbnail) {
ordered = [classifications, ...ordered];
Expand Down Expand Up @@ -97,4 +82,26 @@ const processOverlays = <State extends BaseState>(
return [[...contained, ...outside], newRotate];
};

export const filter = <State extends BaseState>(
overlay: Overlay<State>,
bins: {
[k: string]: Overlay<State>[];
}
) => {
if (!(overlay.field && overlay.field in bins)) return true;

// todo: find a better approach / place for this.
// for instance, this won't work in detection overlay, where
// we might want the bounding boxes but masks might not have been loaded
if (overlay instanceof HeatmapOverlay && !overlay.label.map) {
return true;
}

if (overlay instanceof SegmentationOverlay && !overlay.label.mask) {
return true;
}

return false;
};

export default processOverlays;
11 changes: 7 additions & 4 deletions app/packages/looker/src/worker/canvas-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const canvasAndCtx = (() => {
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
/**
* Reads the PNG's image header chunk to determine the color type.
* Returns the color type if PNG, otherwise undefined.
* Returns the color type if valid PNG, otherwise undefined.
*/
const getPngcolorType = async (blob: Blob): Promise<number | undefined> => {
const getMaybePngcolorType = async (
blob: Blob
): Promise<number | undefined> => {
// https://www.w3.org/TR/2003/REC-PNG-20031110/#11IHDR

// PNG signature is 8 bytes
Expand Down Expand Up @@ -70,8 +72,9 @@ export const recastBufferToMonoChannel = (
export const decodeWithCanvas = async (blob: Blob, cls: string) => {
let channels: number = 4;

if (blob.type === "image/png") {
const colorType = await getPngcolorType(blob);
if (blob.type !== "image/jpg" && blob.type !== "image/jpeg") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implies we still rely on MIME type, specifically for jpeg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really... more like a heuristic. We don't want to unnecessarily check if it's PNG if the mime type says it's a jpeg.

Note that all of this is used just to determine number of channels.

// note that the following function doesn't rely on MIME type and instead reads the file header
const colorType = await getMaybePngcolorType(blob);
if (colorType !== undefined) {
// according to PNG specs:
// 0: Grayscale => 1 channel
Expand Down
Loading