## Measures a captured image, so a blank or broken render fails automatically. ## A capture still has to be looked at by a person or agent; this only catches the ## obvious failures. class_name ImageStats extends RefCounted ## Below this luminance variance (on a 0..1 scale) an image counts as blank. const BLANK_VARIANCE: float = 0.0005 ## A real scene has far more distinct colours than a cleared or broken frame. const BLANK_MAX_COLOURS: int = 16 ## Sampling stride, in pixels. Enough for a 1280x720 frame in milliseconds. const STRIDE: int = 4 ## Returns mean and variance of luminance, and the number of distinct sampled colours. static func measure(image: Image) -> Dictionary: var source: Image = image if source.is_compressed(): source = image.duplicate() as Image source.decompress() var sum: float = 0.0 var sum_squares: float = 0.0 var samples: int = 0 var colours: Dictionary = {} for y: int in range(0, source.get_height(), STRIDE): for x: int in range(0, source.get_width(), STRIDE): var colour: Color = source.get_pixel(x, y) var luminance: float = colour.get_luminance() sum += luminance sum_squares += luminance * luminance samples += 1 colours[colour.to_rgba32()] = true if samples == 0: return {"samples": 0, "mean": 0.0, "variance": 0.0, "colours": 0} var mean: float = sum / samples return { "samples": samples, "mean": mean, "variance": maxf(sum_squares / samples - mean * mean, 0.0), "colours": colours.size(), } static func is_blank(stats: Dictionary) -> bool: return stats["variance"] < BLANK_VARIANCE or stats["colours"] <= BLANK_MAX_COLOURS