Skip to content

Commit 4d4bbb8

Browse files
authored
Merge pull request #5419 from voxel51/fix/mime-type-error
fix mime type error
2 parents 4fd2106 + 6cd09e6 commit 4d4bbb8

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import HeatmapOverlay from "./overlays/heatmap";
4+
import SegmentationOverlay from "./overlays/segmentation";
5+
import { filter } from "./processOverlays";
6+
7+
const EMPTY = {
8+
id: "",
9+
tags: [],
10+
};
11+
12+
describe("test overlay processing", () => {
13+
it("filters heatmap without a map", () => {
14+
expect(filter(new HeatmapOverlay("test", EMPTY), {})).toBe(true);
15+
});
16+
17+
it("filters segmentations without a mask", () => {
18+
expect(filter(new SegmentationOverlay("test", EMPTY), {})).toBe(true);
19+
});
20+
});

app/packages/looker/src/processOverlays.ts

+31-24
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* Copyright 2017-2025, Voxel51, Inc.
33
*/
44

5-
import { CONTAINS, Overlay } from "./overlays/base";
5+
import type { Overlay } from "./overlays/base";
6+
import { CONTAINS } from "./overlays/base";
67
import { ClassificationsOverlay } from "./overlays/classifications";
78
import HeatmapOverlay from "./overlays/heatmap";
89
import SegmentationOverlay from "./overlays/segmentation";
9-
import { BaseState } from "./state";
10+
import type { BaseState } from "./state";
1011
import { rotate } from "./util";
1112

1213
const processOverlays = <State extends BaseState>(
@@ -30,33 +31,17 @@ const processOverlays = <State extends BaseState>(
3031
continue;
3132
}
3233

33-
if (!(overlay.field && overlay.field in bins)) continue;
34-
35-
// todo: find a better approach / place for this.
36-
// for instance, this won't work in detection overlay, where
37-
// we might want the bounding boxes but masks might not have been loaded
38-
if (
39-
overlay instanceof SegmentationOverlay &&
40-
overlay.label.mask_path &&
41-
!overlay.label.mask
42-
) {
43-
continue;
44-
}
45-
46-
if (
47-
overlay instanceof HeatmapOverlay &&
48-
overlay.label.map_path &&
49-
!overlay.label.map
50-
) {
51-
continue;
52-
}
53-
5434
if (!overlay.isShown(state)) continue;
5535

36+
if (filter(overlay, bins)) continue;
37+
5638
bins[overlay.field].push(overlay);
5739
}
5840

59-
let ordered = activePaths.reduce((acc, cur) => [...acc, ...bins[cur]], []);
41+
let ordered = activePaths.reduce((acc, cur) => {
42+
acc.push(...bins[cur]);
43+
return acc;
44+
}, []);
6045

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

85+
export const filter = <State extends BaseState>(
86+
overlay: Overlay<State>,
87+
bins: {
88+
[k: string]: Overlay<State>[];
89+
}
90+
) => {
91+
if (!(overlay.field && overlay.field in bins)) return true;
92+
93+
// todo: find a better approach / place for this.
94+
// for instance, this won't work in detection overlay, where
95+
// we might want the bounding boxes but masks might not have been loaded
96+
if (overlay instanceof HeatmapOverlay && !overlay.label.map) {
97+
return true;
98+
}
99+
100+
if (overlay instanceof SegmentationOverlay && !overlay.label.mask) {
101+
return true;
102+
}
103+
104+
return false;
105+
};
106+
100107
export default processOverlays;

app/packages/looker/src/worker/canvas-decoder.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ const canvasAndCtx = (() => {
1515
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
1616
/**
1717
* Reads the PNG's image header chunk to determine the color type.
18-
* Returns the color type if PNG, otherwise undefined.
18+
* Returns the color type if valid PNG, otherwise undefined.
1919
*/
20-
const getPngcolorType = async (blob: Blob): Promise<number | undefined> => {
20+
const getMaybePngcolorType = async (
21+
blob: Blob
22+
): Promise<number | undefined> => {
2123
// https://www.w3.org/TR/2003/REC-PNG-20031110/#11IHDR
2224

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

73-
if (blob.type === "image/png") {
74-
const colorType = await getPngcolorType(blob);
75+
if (blob.type !== "image/jpg" && blob.type !== "image/jpeg") {
76+
// note that the following function doesn't rely on MIME type and instead reads the file header
77+
const colorType = await getMaybePngcolorType(blob);
7578
if (colorType !== undefined) {
7679
// according to PNG specs:
7780
// 0: Grayscale => 1 channel

0 commit comments

Comments
 (0)