Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison to hologram.nvim #9

Closed
donovanglover opened this issue Jul 15, 2023 · 8 comments
Closed

Comparison to hologram.nvim #9

donovanglover opened this issue Jul 15, 2023 · 8 comments

Comments

@donovanglover
Copy link

donovanglover commented Jul 15, 2023

Would be great if there was documentation on how this project compares to hologram.nvim. For example, does it fix any of the display issues present in the original?

@3rd
Copy link
Owner

3rd commented Jul 15, 2023

The aim is not to have any display issues.
Making it compatible with zen-mode is out of scope, as it's not an issue but a missing feature at best, but my guess is that it should work even there.
Try it out.

@3rd 3rd closed this as completed Jul 16, 2023
@donovanglover
Copy link
Author

I would love to try image.nvim! Unfortunately, I haven't been able to get magick working yet. The instructions in the README have been a great help, however changing try_to_load to /nix/store/vhc3pka4gjs6hghbsk7alc2nsry9k81i-imagemagick-7.1.1-12/lib/libMagickWand-7.Q16HDRI.so doesn't change the error message for me.

I was trying to add magick to nixpkgs earlier. I think patching it like this could get it to work and I might be using the wrong .so. See: NixOS/nixpkgs#243687

@3rd
Copy link
Owner

3rd commented Jul 16, 2023

Yes, it's a bit of a pain, would be great to get it in nixpkgs. I'm looking to replace it as the rock can't be used to do masking.

To get it to work I patched ~/.luarocks/share/lua/5.1/magick/wand/lib.lua like this:

Screenshot_2023-07-16-21-03-32-902_com.server.auditor.ssh.client.jpg

@donovanglover
Copy link
Author

Not sure if I missed anything but I couldn't get that to work either. Can you share the file later? Planning to add the patch to the PR if I can get it to work.

@3rd
Copy link
Owner

3rd commented Jul 17, 2023

I did a luarocks --local install magick and then went to find the wand .so from the nix store:

cd (dirname (which magick))/../lib
# found libMagickWand-7.Q16HDRI.so

And then I just hardcoded the path to that .so in ~/.luarocks/share/lua/5.1/magick/wand/lib.lua

~/.luarocks/share/lua/5.1/magick/wand/lib.lua
local ffi = require("ffi")
local lib
ffi.cdef([[  typedef void MagickWand;
  typedef void PixelWand;

  typedef int MagickBooleanType;
  typedef int ExceptionType;
  typedef int ssize_t;
  typedef int CompositeOperator;
  typedef int GravityType;
  typedef int OrientationType;
  typedef int InterlaceType;

  void MagickWandGenesis();
  MagickWand* NewMagickWand();
  MagickWand* CloneMagickWand(const MagickWand *wand);
  MagickWand* DestroyMagickWand(MagickWand*);
  MagickBooleanType MagickReadImage(MagickWand*, const char*);
  MagickBooleanType MagickReadImageBlob(MagickWand*, const void*, const size_t);

  char* MagickGetException(const MagickWand*, ExceptionType*);

  int MagickGetImageWidth(MagickWand*);
  int MagickGetImageHeight(MagickWand*);

  MagickBooleanType MagickAddImage(MagickWand*, const MagickWand*);

  MagickBooleanType MagickAdaptiveResizeImage(MagickWand*, const size_t, const size_t);

  MagickBooleanType MagickWriteImage(MagickWand*, const char*);

  unsigned char* MagickGetImageBlob(MagickWand*, size_t*);

  void* MagickRelinquishMemory(void*);

  MagickBooleanType MagickCropImage(MagickWand*,
    const size_t, const size_t, const ssize_t, const ssize_t);

  MagickBooleanType MagickBlurImage(MagickWand*, const double, const double);
  MagickBooleanType MagickModulateImage(MagickWand*, const double, const double, const double);
  MagickBooleanType MagickBrightnessContrastImage(MagickWand*, const double, const double);

  MagickBooleanType MagickSetImageFormat(MagickWand* wand, const char* format);
  char* MagickGetImageFormat(MagickWand* wand);

  size_t MagickGetImageCompressionQuality(MagickWand * wand);
  MagickBooleanType MagickSetImageCompressionQuality(MagickWand *wand,
  const size_t quality);

  MagickBooleanType MagickSharpenImage(MagickWand *wand,
    const double radius,const double sigma);

  MagickBooleanType MagickScaleImage(MagickWand *wand,
    const size_t columns,const size_t rows);

  MagickBooleanType MagickRotateImage(MagickWand *wand,
  const PixelWand *background,const double degrees);

  MagickBooleanType MagickSetOption(MagickWand *,const char *,const char *);
  char* MagickGetOption(MagickWand *,const char *);

  MagickBooleanType MagickCompositeImage(MagickWand *wand,
    const MagickWand *source_wand,const CompositeOperator compose,
    const ssize_t x,const ssize_t y);

  GravityType MagickGetImageGravity(MagickWand *wand);
  MagickBooleanType MagickSetImageGravity(MagickWand *wand,
    const GravityType gravity);

  MagickBooleanType MagickStripImage(MagickWand *wand);

  MagickBooleanType MagickGetImagePixelColor(MagickWand *wand,
    const ssize_t x,const ssize_t y,PixelWand *color);

  MagickWand* MagickCoalesceImages(MagickWand*);

  PixelWand *NewPixelWand(void);
  PixelWand *DestroyPixelWand(PixelWand *);

  double PixelGetAlpha(const PixelWand *);
  double PixelGetRed(const PixelWand *);
  double PixelGetGreen(const PixelWand *);
  double PixelGetBlue(const PixelWand *);

  void PixelSetAlpha(PixelWand *wand, const double alpha);
  void PixelSetRed(PixelWand *wand, const double red);
  void PixelSetGreen(PixelWand *wand, const double green);
  void PixelSetBlue(PixelWand *wand, const double blue);

  MagickBooleanType MagickTransposeImage(MagickWand *wand);

  MagickBooleanType MagickTransverseImage(MagickWand *wand);

  MagickBooleanType MagickFlipImage(MagickWand *wand);

  MagickBooleanType MagickFlopImage(MagickWand *wand);

  char* MagickGetImageProperty(MagickWand *wand, const char *property);
  MagickBooleanType MagickSetImageProperty(MagickWand *wand,
    const char *property,const char *value);

  OrientationType MagickGetImageOrientation(MagickWand *wand);
  MagickBooleanType MagickSetImageOrientation(MagickWand *wand,
    const OrientationType orientation);

  InterlaceType MagickGetImageInterlaceScheme(MagickWand *wand);
  MagickBooleanType MagickSetImageInterlaceScheme(MagickWand *wand,
    const InterlaceType interlace_scheme);

  MagickBooleanType MagickAutoOrientImage(MagickWand *wand);

  MagickBooleanType MagickResetImagePage(MagickWand *wand, const char *page);

  MagickBooleanType MagickSetImageDepth(MagickWand *,const unsigned long);
  unsigned long MagickGetImageDepth(MagickWand *);

]])
local get_flags
get_flags = function()
  local proc = io.popen("pkg-config --cflags --libs MagickWand", "r")
  local flags = proc:read("*a")
  get_flags = function()
    return flags
  end
  proc:close()
  return flags
end
local get_filters
get_filters = function()
  local suffixes = {
    "magick/resample.h",
    "MagickCore/resample.h",
  }
  local prefixes = {
    "/usr/include/ImageMagick",
    "/usr/local/include/ImageMagick",
    -- unpack((function()
    --   local _accum_0 = {}
    --   local _len_0 = 1
    --   for p in get_flags():gmatch("-I([^%s]+)") do
    --     _accum_0[_len_0] = p
    --     _len_0 = _len_0 + 1
    --   end
    --   return _accum_0
    -- end)()),
  }
  for _index_0 = 1, #prefixes do
    local p = prefixes[_index_0]
    for _index_1 = 1, #suffixes do
      local suffix = suffixes[_index_1]
      local full = tostring(p) .. "/" .. tostring(suffix)
      do
        local f = io.open(full)
        if f then
          local content
          do
            local _with_0 = f:read("*a")
            f:close()
            content = _with_0
          end
          local filter_types = content:match("(typedef enum.-FilterTypes?;)")
          if filter_types then
            ffi.cdef(filter_types)
            if filter_types:match("FilterTypes;") then return "FilterTypes" end
            return "FilterType"
          end
        end
      end
    end
  end
  return false
end
local get_filter
get_filter = function(name)
  return lib[name .. "Filter"]
end
local can_resize
do
  local enum_name = get_filters()
  if enum_name then
    ffi.cdef([[    MagickBooleanType MagickResizeImage(MagickWand*,
      const size_t, const size_t,
      const ]] .. enum_name .. [[, const double);
  ]])
    can_resize = true
  end
end
local try_to_load
try_to_load = function(...)
  local out
  local _list_0 = {
    ...,
  }
  for _index_0 = 1, #_list_0 do
    local _continue_0 = false
    repeat
      local name = _list_0[_index_0]
      if "function" == type(name) then
        name = name()
        if not name then
          _continue_0 = true
          break
        end
      end
      return ffi.load(name)
    -- if pcall(function()
    -- out = ffi.load(name)
    -- end) then
    --   return out
    -- end
    -- _continue_0 = true
    until true
    if not _continue_0 then break end
  end
  return error("Failed to load ImageMagick (" .. tostring(...) .. ")")
end
lib = try_to_load(
  "/nix/store/48zg59xi37kxsw4m0bx28jgiszxpxpr1-imagemagick-7.1.1-11/lib/libMagickWand-7.Q16HDRI.so",
  function()
    -- return "/nix/store/48zg59xi37kxsw4m0bx28jgiszxpxpr1-imagemagick-7.1.1-11/lib/libMagickWand-7.Q16HDRI.so"
    -- local lname = get_flags():match("-l(MagickWand[^%s]*)")
    -- local suffix
    -- if ffi.os == "OSX" then
    --   suffix = ".dylib"
    -- elseif ffi.os == "Windows" then
    --   suffix = ".dll"
    -- else
    --   suffix = ".so"
    -- end
    -- return lname and "lib" .. lname .. suffix
  end
)
return {
  lib = lib,
  can_resize = can_resize,
  get_filter = get_filter,
}

@donovanglover
Copy link
Author

Thanks, I got it to work! Took a second for me to figure out that you need to add treesitter since there were no errors with the vimwiki syntax, but having jpg and other image formats in the editor is great.

I made a PR NixOS/nixpkgs#244038 to add image.nvim to nixpkgs and updated the magick PR with the patch.

@3rd
Copy link
Owner

3rd commented Jul 17, 2023

Thanks, I got it to work! Took a second for me to figure out that you need to add treesitter since there were no errors with the vimwiki syntax, but having jpg and other image formats in the editor is great.

I made a PR NixOS/nixpkgs#244038 to add image.nvim to nixpkgs and updated the magick PR with the patch.

Amazing, glad it works, and thanks for the PR!

@Galicarnax
Copy link

This plugin doesn't seem to work nicely with zen-mode either - images become misplaced in zen-mode, and are stuck in the position where there were before switching to zen-mode; switching back from zen-mode, they behave correctly again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants