From 73794e1867fce2b64cd2aa0ab0353ef9876e3e4c Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 22 Jul 2019 20:32:50 -0300 Subject: [PATCH 1/2] add magickwand load/unload --- examples/rotate.scri | 3 +++ src/image.zig | 4 +++- src/magick.zig | 53 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 examples/rotate.scri diff --git a/examples/rotate.scri b/examples/rotate.scri new file mode 100644 index 0000000..0b934c6 --- /dev/null +++ b/examples/rotate.scri @@ -0,0 +1,3 @@ +load :0; +rotate 30; +quicksave; diff --git a/src/image.zig b/src/image.zig index 1523133..f33efb4 100644 --- a/src/image.zig +++ b/src/image.zig @@ -67,7 +67,7 @@ fn sf_tell(file: *c.SNDFILE) i64 { return -frames; } -fn temporaryName(allocator: *std.mem.Allocator) ![]u8 { +pub fn temporaryName(allocator: *std.mem.Allocator) ![]u8 { const template_start = "/temp/temp_"; const template = "/tmp/temp_XXXXXXXX"; var nam = try allocator.alloc(u8, template.len); @@ -146,6 +146,8 @@ pub const Image = struct { } pub fn close(self: *Image) void { + //self.allocator.free(self.path); + //self.allocator.free(self.curpath); var st: i32 = c.sf_close(self.sndfile); if (st != 0) { diff --git a/src/magick.zig b/src/magick.zig index a41c5e9..8608f0f 100644 --- a/src/magick.zig +++ b/src/magick.zig @@ -4,6 +4,57 @@ const images = @import("image.zig"); const Image = images.Image; +const mc = @cImport({ + @cInclude("MagickWand/MagickWand.h"); +}); + +pub const MagickContext = struct { + wand: *mc.MagickWand, + + pub fn init() !MagickContext { + mc.MagickWandGenesis(); + + var wand = mc.NewMagickWand(); + if (wand == null) return error.WandCreateFail; + + return MagickContext{ + .wand = wand.?, + }; + } + + pub fn deinit(self: *MagickContext) void { + _ = mc.DestroyMagickWand(self.wand); + mc.MagickWandTerminus(); + } + + pub fn doErr(self: *MagickContext) !void { + return error.WandError; + } +}; + pub fn runRotate(image: *Image) !void { - return error.NotImplementedYet; + mc.MagickWandGenesis(); + + var mctx = try MagickContext.init(); + defer mctx.deinit(); + errdefer mctx.deinit(); + + var status: mc.MagickBooleanType = undefined; + + var curpath = try std.cstr.addNullByte(image.allocator, image.curpath); + defer image.allocator.free(curpath); + + status = mc.MagickReadImage(mctx.wand, curpath.ptr); + if (status == .MagickFalse) try mctx.doErr(); + + // TODO run rotate here + + var tmpnam = try images.temporaryName(image.allocator); + defer image.allocator.free(tmpnam); + + var c_tmpnam = try std.cstr.addNullByte(image.allocator, tmpnam); + defer image.allocator.free(c_tmpnam); + + status = mc.MagickWriteImages(mctx.wand, c_tmpnam.ptr, .MagickTrue); + if (status == .MagickFalse) try mctx.doErr(); } From 8784f3baf994793067ae6151cd42c983a3f86f6a Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 22 Jul 2019 20:37:57 -0300 Subject: [PATCH 2/2] decouple magick init/save into helper functions --- src/magick.zig | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/magick.zig b/src/magick.zig index 8608f0f..6468fee 100644 --- a/src/magick.zig +++ b/src/magick.zig @@ -32,29 +32,41 @@ pub const MagickContext = struct { } }; -pub fn runRotate(image: *Image) !void { - mc.MagickWandGenesis(); - +fn magickLoad(image: *Image) !MagickContext { var mctx = try MagickContext.init(); - defer mctx.deinit(); errdefer mctx.deinit(); - var status: mc.MagickBooleanType = undefined; - var curpath = try std.cstr.addNullByte(image.allocator, image.curpath); defer image.allocator.free(curpath); - status = mc.MagickReadImage(mctx.wand, curpath.ptr); - if (status == .MagickFalse) try mctx.doErr(); + if (mc.MagickReadImage(mctx.wand, curpath.ptr) == .MagickFalse) + return error.MagickReadFail; - // TODO run rotate here + return mctx; +} +fn magickSave(image: *Image, mctx: *MagickContext) !void { var tmpnam = try images.temporaryName(image.allocator); defer image.allocator.free(tmpnam); var c_tmpnam = try std.cstr.addNullByte(image.allocator, tmpnam); defer image.allocator.free(c_tmpnam); - status = mc.MagickWriteImages(mctx.wand, c_tmpnam.ptr, .MagickTrue); - if (status == .MagickFalse) try mctx.doErr(); + std.debug.warn( + "\tmagick: saving from '{}' to '{}'\n", + image.curpath, + c_tmpnam, + ); + + if (mc.MagickWriteImages(mctx.wand, c_tmpnam.ptr, .MagickTrue) == .MagickFalse) + return error.MagickWriteFail; +} + +pub fn runRotate(image: *Image) !void { + var mctx = try magickLoad(image); + defer mctx.deinit(); + + // TODO run rotate here + + try magickSave(image, &mctx); }