decouple magick init/save into helper functions

This commit is contained in:
Luna 2019-07-22 20:37:57 -03:00
parent 73794e1867
commit 8784f3baf9

View file

@ -32,29 +32,41 @@ pub const MagickContext = struct {
} }
}; };
pub fn runRotate(image: *Image) !void { fn magickLoad(image: *Image) !MagickContext {
mc.MagickWandGenesis();
var mctx = try MagickContext.init(); var mctx = try MagickContext.init();
defer mctx.deinit();
errdefer mctx.deinit(); errdefer mctx.deinit();
var status: mc.MagickBooleanType = undefined;
var curpath = try std.cstr.addNullByte(image.allocator, image.curpath); var curpath = try std.cstr.addNullByte(image.allocator, image.curpath);
defer image.allocator.free(curpath); defer image.allocator.free(curpath);
status = mc.MagickReadImage(mctx.wand, curpath.ptr); if (mc.MagickReadImage(mctx.wand, curpath.ptr) == .MagickFalse)
if (status == .MagickFalse) try mctx.doErr(); return error.MagickReadFail;
// TODO run rotate here return mctx;
}
fn magickSave(image: *Image, mctx: *MagickContext) !void {
var tmpnam = try images.temporaryName(image.allocator); var tmpnam = try images.temporaryName(image.allocator);
defer image.allocator.free(tmpnam); defer image.allocator.free(tmpnam);
var c_tmpnam = try std.cstr.addNullByte(image.allocator, tmpnam); var c_tmpnam = try std.cstr.addNullByte(image.allocator, tmpnam);
defer image.allocator.free(c_tmpnam); defer image.allocator.free(c_tmpnam);
status = mc.MagickWriteImages(mctx.wand, c_tmpnam.ptr, .MagickTrue); std.debug.warn(
if (status == .MagickFalse) try mctx.doErr(); "\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);
} }