Save data into PNG image file

Not sure if this is the right place to post, or maybe Discord?.. Please tell me if this is inappropriate!

I want to save data into a PNG file with zigimg.

My data is a slice []u8 of consecutive triplets {r,g,b} of u8. I want to transform this into a PNG image file.

I don’t see how to do this. My code below fails on the last line of image as built with fromRawPixels is probably wrong but I don’t see what else could I use.

var image = try zigimg.Image.fromRawPixels(allocator, w, h, pixels, .rgb24);
defer image.deinit();

print("check pixels: {}", .{pixels.len}); // w= 100 x h = 200 x 3 = 60_000 ok

try image.writeToFilePath("junk.png", .{ .png = .{} });

Does it fail to compile or do you get a runtime error? What error do you get?

It fails to compile, corresponding to line 96 in zigimg/imageunmanaged,

error: no field named 'struct' in enum '@typeInfo(builtin.Type).Union.tag_type.?'

I understand that my entry is not the struct the function writeToFilePath wants. So image must be badly built.

What version of the compiler are you using?

version 0.13, and I fetched zigimg from the github, latest

I rather think my code is wrong, not the compiler nor the library. Its more that I don’t understand how to use this library.

It’s not that the compiler is wrong, its that there is a version mismatch. It looks like zigimg latest targets the latest version of the compiler. There was a change into the names of the types in the std.builtin.Type enum between version 0.13 and 0.14.0.dev, where the names (like struct were changed from uppercase to lower case). You’ll need to get a version of the compiler that matches what zigimg supports.

1 Like

Ah ok! So I installed version: 0.14.0-dev.183+b2e89d and my code seems to work.
Thanks!