Skip to content

Add example or test case for writeToFn #10

@loftafi

Description

@loftafi

Thank you for putting this package together to make it easier for zig users to use this library.

zstbi.Image.writeToFn(img, write_fn: *const fn (?*anyopaque, ?*anyopaque, c_int) void, context: ?*anyopaque, format);

It is not clear to a beginner to this library, how to use the writeToFn function. How are the parameters of the function supposed to be used? I looked in the zig file and there is no test for using it that might give a clue.

It would be helpful if a more advanced user of this library could add an example to the README.md or a test case in the zig code.

Thanks again for putting this project together.


Just guessing, I presume we need to do something like this:

const format: zstbi.ImageWriteFormat = .{ .jpg = .{ .quality = 75 } };
var buffer: Buffer = .{ .allocator = allocator };
try zstbi.Image.writeToFn(img, write_fn, &buffer, format);
const Buffer = struct {
    allocator: Allocator,
    data: std.ArrayListUnmanaged(u8) = .empty,
};
fn write_fn(context: ?*anyopaque, data: ?anyopaque, size: c_int) void {
    var buffer: *Buffer = @ptrCast(context);
    // Append data to buffer, maybe something like this???
    buffer.data.append(buffer.allocator, data[0..size]);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions