-
Notifications
You must be signed in to change notification settings - Fork 6
zigar.image.Any(acc)
Chung Leong edited this page Jul 28, 2026
·
2 revisions
JavaScript | PHP
Parametric union type designed to accept either a JavaScript ImageData or a PHP GD image resource.
Zigar will automatically select the correct field for the given input. You do not need to wrap
the ImageData in an object.
The object in question doesn't need to be an actual instance of ImageData. It only needs to
have its key properties: data, width, and height.
There's no support for HDR image currently.
Usage:
const std = @import("std");
const zigar = @import("zigar");
pub fn greyscale(image: zigar.image.Any(.rw)) void {
inline for (zigar.image.formats) |tag| {
if (image == tag) {
const in = image.getField(tag);
const out = in;
for (0..out.getHeight()) |y| {
for (0..out.getWidth()) |x| {
const pixel = in.getPixel(@Vector(4, f32), x, y);
const value = (pixel[0] + pixel[1] + pixel[2]) / 3;
out.setPixel(@Vector(4, f32), x, y, .{ value, value, value, pixel[3] });
}
}
}
}
}const { data, info } = await Sharp('bigfoot.png').ensureAlpha().raw().toBuffer({ resolveWithObject: true });
greyscale({ data, width: info.width, height: info.height });
await Sharp(data, { raw: info }).png().toFile('output.png');Union fields:
-
gd: GdImage(acc) -
web: WebImage(acc, )