zigar.image.Any(acc) ᴾᴴᴾ - chung-leong/zigar GitHub Wiki

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 resource handle in an array.

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] });
                }
            }
        }
    }
}
$m = ZigImporter::load(__DIR__ . '/greyscale.zig');
$image = imagecreatefrompng('big-foot.png');
$m->greyscale($image);
imagepng($image, 'output.png');

Union fields: