123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- /*
- * pixiv_down - CLI-based downloading tool for https://www.pixiv.net.
- * Copyright (C) 2023, 2024 Mio
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, version 3 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- module pd.imaging.magick;
- import core.stdc.config : c_ulong;
- alias MagickWand = void;
- mixin template GenBinding(string FunctionName, string FunctionSignature)
- {
- mixin("alias _" ~ FunctionName ~ " = nothrow extern(C) @system @nogc " ~ FunctionSignature ~ ";");
- mixin("__gshared _" ~ FunctionName ~ " " ~ FunctionName ~ ";");
- }
- template GenMagickSym(string FunctionName)
- {
- const char[] GenMagickSym = FunctionName ~ " = cast(typeof("~FunctionName~"))dlsym(libMagick, \""~FunctionName~"\");
- if ((errmsg = dlerror()) !is null) {
- fprintf(stderr, \"Failed to bind symbol: %s\\n\", errmsg);
- dlerror();
- }";
- }
- template GenWandSym(string FunctionName)
- {
- const char[] GenWandSym = FunctionName ~ " = cast(typeof("~FunctionName~"))dlsym(libWand, \""~FunctionName~"\");
- if ((errmsg = dlerror()) !is null) {
- fprintf(stderr, \"Failed to bind symbol: %s\\n\", errmsg);
- dlerror();
- }";
- }
- // GraphicsMagick
- mixin GenBinding!("InitializeMagick", "void function(const char*)");
- mixin GenBinding!("DestroyMagick", "void function()");
- mixin GenBinding!("GetMagickVersion", "char* function(c_ulong*)");
- // GraphicsMagickWand
- mixin GenBinding!("CloneMagickWand", "MagickWand* function(MagickWand*)");
- mixin GenBinding!("DestroyMagickWand", "void function(MagickWand*)");
- mixin GenBinding!("MagickAddImage", "uint function(MagickWand*, MagickWand*)");
- mixin GenBinding!("MagickGetImageFilename", "char* function(MagickWand*)");
- mixin GenBinding!("MagickGetNumberImages", "c_ulong function(MagickWand*)");
- mixin GenBinding!("MagickNextImage", "uint function(MagickWand*)");
- mixin GenBinding!("MagickReadImage", "uint function(MagickWand*, const char*)");
- mixin GenBinding!("MagickResetIterator", "void function(MagickWand*)");
- mixin GenBinding!("MagickSetImageDelay", "uint function(MagickWand*, c_ulong)");
- mixin GenBinding!("MagickSetImageDispose", "uint function(MagickWand*, int)");
- mixin GenBinding!("MagickSetImageFormat", "uint function(MagickWand*, const char*)");
- mixin GenBinding!("MagickSetImageIterations", "uint function(MagickWand*, int)");
- mixin GenBinding!("MagickSetImageIndex", "uint function(MagickWand*, c_ulong)");
- mixin GenBinding!("MagickWriteImages", "uint function(MagickWand*, const char*, uint)");
- mixin GenBinding!("NewMagickWand", "MagickWand* function()");
- // Based off of GM -- TODO: check IM
- enum PreviousDispose = 3;
- void loadMagick(string programName)
- {
- import core.sys.posix.dlfcn;
- import core.stdc.stdio;
- import std.experimental.logger;
- import std.string : toStringz, fromStringz;
- void* libMagick = dlopen("libGraphicsMagick.so", RTLD_LAZY);
- if (null is libMagick) {
- // TODO: handle error
- critical(fromStringz(dlerror()));
- }
- void* libWand = dlopen("libGraphicsMagickWand.so", RTLD_LAZY);
- if (null is libWand) {
- // TODO: handle error
- critical(fromStringz(dlerror()));
- }
- dlerror();
- char* errmsg;
- mixin(GenMagickSym!("InitializeMagick"));
- mixin(GenMagickSym!("DestroyMagick"));
- mixin(GenMagickSym!("GetMagickVersion"));
- mixin(GenWandSym!("CloneMagickWand"));
- mixin(GenWandSym!("DestroyMagickWand"));
- mixin(GenWandSym!("MagickAddImage"));
- mixin(GenWandSym!("MagickGetImageFilename"));
- mixin(GenWandSym!("MagickGetNumberImages"));
- mixin(GenWandSym!("MagickNextImage"));
- mixin(GenWandSym!("MagickReadImage"));
- mixin(GenWandSym!("MagickResetIterator"));
- mixin(GenWandSym!("MagickSetImageDelay"));
- mixin(GenWandSym!("MagickSetImageDispose"));
- mixin(GenWandSym!("MagickSetImageFormat"));
- mixin(GenWandSym!("MagickSetImageIndex"));
- mixin(GenWandSym!("MagickSetImageIterations"));
- mixin(GenWandSym!("MagickWriteImages"));
- mixin(GenWandSym!("NewMagickWand"));
- InitializeMagick(programName.toStringz);
- }
- void destroyMagick()
- {
- DestroyMagick();
- }
- void SetImageDispose(MagickWand* wand, int type)
- {
- // TODO: Check for IM
- MagickSetImageDispose(wand, type);
- }
|