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

Enforce non-empty images #1997

Merged
merged 2 commits into from
Jul 27, 2023
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
9 changes: 8 additions & 1 deletion backend/src/nodes/properties/outputs/numpy_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ def get_broadcast_type(self, value: np.ndarray):
def enforce(self, value) -> np.ndarray:
assert isinstance(value, np.ndarray)

_, _, c = get_h_w_c(value)
h, w, c = get_h_w_c(value)

if h == 0 or w == 0:
raise ValueError(
f"The output {self.label} returned an empty image (w={w} h={h})."
f" This is a bug in the implementation of the node."
f" Please report this bug."
)

if self.channels is not None and c != self.channels:
expected = format_image_with_channels([self.channels])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
image_type="""
let anyImages = bool::or(Input0 == Image, Input1 == Image, Input2 == Image, Input3 == Image);

def getWidth(i: any) = match i { Image => i.width, _ => uint };
def getHeight(i: any) = match i { Image => i.height, _ => uint };
def getWidth(i: any) = match i { Image => i.width, _ => Image.width };
def getHeight(i: any) = match i { Image => i.height, _ => Image.height };

let valid = if anyImages { any } else { never };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
image_type="""
let anyImages = bool::or(Input0 == Image, Input1 == Image);

def getWidth(i: any) = match i { Image => i.width, _ => uint };
def getHeight(i: any) = match i { Image => i.height, _ => uint };
def getWidth(i: any) = match i { Image => i.width, _ => Image.width };
def getHeight(i: any) = match i { Image => i.height, _ => Image.height };

let valid = if anyImages { any } else { never };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ def getChannels(img: Image | null) {
def getAdjustedWidth(img: Image | null) {
match img {
null => 0,
_ as i => uint & round(i.width * maxHeight / i.height)
_ as i => int(1..) & round(i.width * maxHeight / i.height)
}
}
def getAdjustedHeight(img: Image | null) {
match img {
null => 0,
_ as i => uint & round(i.height * maxWidth / i.width)
_ as i => int(1..) & round(i.height * maxWidth / i.width)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def transform(x: number, y: number) {
let p2 = transform(w, h);
let p3 = transform(0, h);

let expandWidth = uint & (
let expandWidth = Image.width & (
ceil(max(p0.x, p1.x, p2.x, p3.x))
- floor(min(p0.x, p1.x, p2.x, p3.x))
);
let expandHeight = uint & (
let expandHeight = Image.height & (
ceil(max(p0.y, p1.y, p2.y, p3.y))
- floor(min(p0.y, p1.y, p2.y, p3.y))
);
Expand Down
4 changes: 2 additions & 2 deletions src/common/types/chainner-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct Audio;

struct ImageFile { path: string }
struct Image {
width: uint,
height: uint,
width: int(1..),
height: int(1..),
channels: int(1..),
}
struct Color { channels: int(1..) }
Expand Down
7 changes: 1 addition & 6 deletions src/common/types/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,10 @@ const explainStruct = (s: StructType, options: ExplainOptions): string | undefin
const height = s.fields[1].type;
const channels = s.fields[2].type;

if (isInt(width, 0) && isInt(height, 0)) {
if (isInt(width, 1) && isInt(height, 1)) {
if (isInt(channels, 1)) return detailed('an image', 'of any size and any colorspace');
return detailed(formatChannelNumber(channels, 'image'), 'of any size');
}
if (isInt(width, 1) && isInt(height, 1)) {
if (isInt(channels, 1)) return detailed('an non-empty image', 'of any colorspace');
const formatted = formatChannelNumber(channels, 'image');
if (formatted) return `${formatted} that isn't empty`;
}
}

if (isColor(s)) {
Expand Down