zigar.image.Any(acc) - chung-leong/zigar GitHub Wiki
JavaScript | PHP
Parametric union type designed to accept either a JavaScript ImageData object 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. Uint8Array can be used in lieu of
Uint8ClampedArray.
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: