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
1 changed files with 23 additions and 11 deletions

View File

@ -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);
}