add magickwand load/unload

This commit is contained in:
Luna 2019-07-22 20:32:50 -03:00
parent b056a0edb3
commit 73794e1867
3 changed files with 58 additions and 2 deletions

3
examples/rotate.scri Normal file
View file

@ -0,0 +1,3 @@
load :0;
rotate 30;
quicksave;

View file

@ -67,7 +67,7 @@ fn sf_tell(file: *c.SNDFILE) i64 {
return -frames; return -frames;
} }
fn temporaryName(allocator: *std.mem.Allocator) ![]u8 { pub fn temporaryName(allocator: *std.mem.Allocator) ![]u8 {
const template_start = "/temp/temp_"; const template_start = "/temp/temp_";
const template = "/tmp/temp_XXXXXXXX"; const template = "/tmp/temp_XXXXXXXX";
var nam = try allocator.alloc(u8, template.len); var nam = try allocator.alloc(u8, template.len);
@ -146,6 +146,8 @@ pub const Image = struct {
} }
pub fn close(self: *Image) void { pub fn close(self: *Image) void {
//self.allocator.free(self.path);
//self.allocator.free(self.curpath);
var st: i32 = c.sf_close(self.sndfile); var st: i32 = c.sf_close(self.sndfile);
if (st != 0) { if (st != 0) {

View file

@ -4,6 +4,57 @@ const images = @import("image.zig");
const Image = images.Image; 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 { 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();
} }