leafo / magick

Lua bindings to ImageMagick for LuaJIT using FFI
401 stars 79 forks source link

Add `magick.new` and `magick.new_image` #26

Open josefnpat opened 8 years ago

josefnpat commented 8 years ago

So I was attempting to create and edit png images on the fly with magick, but I found that neither the magick.new or the magick.new_image function existed.

Would you like me to take a crack at adding them in a PR? I am not familiar with moon or the busted system.

For anyone looking for a really poor performance workaround using os.execute and imagemagick's convert, you can use this:

__tmp = '/tmp/pico2png.temp.png'
function new_image(w,h)
  os.execute('convert -size '..w.."x"..h..' xc:white '..__tmp)
  local png,err = magick.load_image(__tmp)
  return png,err
end
__points = {}
__count = 0
function set_pixel(x,y,r,g,b)
  table.insert(__points,"fill rgb("..r..","..g..","..b..") point "..x..","..y.." ")
  if __count > 2^12 then
    __flush_pixels()
    __count = 0
  end
  __count = __count + 1
end
function __flush_pixels()
  os.execute("convert -draw '"..table.concat(__points).."' "..__tmp.." "..__tmp)
  __points = {}
end
-- run this before reading information about your image object.
function __post()
  __flush_pixels()
  return magick.load_image(__tmp)
end
leafo commented 8 years ago

magick currently doesn't have any drawing functions, so making a new image isn't that useful. I'm not opposed to adding those kinds of functions though.

This library is an FFI binding to ImageMagick. We can easily introduce any function implemented by ImageMagick's C interface. Here are a bunch: http://www.imagemagick.org/api/magick-image.php

You just need to copy the signature of the function into the statement on the top of init.moon and then you can access it: https://github.com/leafo/magick/blob/master/magick/init.moon#L6

I'm not familiar with the internal drawing API, but maybe if you dig around you can find something