Skip to content

Commit

Permalink
Enforce non-empty images (#1997)
Browse files Browse the repository at this point in the history
* Enforce non-empty images

* code golfing
  • Loading branch information
RunDevelopment committed Jul 27, 2023
1 parent 60cf539 commit 56b1c13
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
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

0 comments on commit 56b1c13

Please sign in to comment.