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

Add several functions #32

Merged
merged 1 commit into from
Jul 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions magick/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,33 @@ local gravity_type = { }
for i, t in ipairs(gravity_str) do
gravity_type[t] = i
end
local orientation_str = {
"TopLeftOrientation",
"TopRightOrientation",
"BottomRightOrientation",
"BottomLeftOrientation",
"LeftTopOrientation",
"RightTopOrientation",
"RightBottomOrientation",
"LeftBottomOrientation"
}
local orientation_type = { }
for i, t in ipairs(orientation_str) do
orientation_type[t] = i
end
local interlace_str = {
"NoInterlace",
"LineInterlace",
"PlaneInterlace",
"PartitionInterlace",
"GIFInterlace",
"JPEGInterlace",
"PNGInterlace"
}
local interlace_type = { }
for i, t in ipairs(interlace_str) do
interlace_type[t] = i
end
lib.MagickWandGenesis()
local filter
filter = function(name)
Expand Down Expand Up @@ -309,6 +336,57 @@ do
assert(lib.MagickGetImagePixelColor(self.wand, x, y, self.pixel_wand), "failed to get pixel")
return lib.PixelGetRed(self.pixel_wand), lib.PixelGetGreen(self.pixel_wand), lib.PixelGetBlue(self.pixel_wand), lib.PixelGetAlpha(self.pixel_wand)
end,
transpose = function(self)
return handle_result(self, lib.MagickTransposeImage(self.wand))
end,
transverse = function(self)
return handle_result(self, lib.MagickTransverseImage(self.wand))
end,
flip = function(self)
return handle_result(self, lib.MagickFlipImage(self.wand))
end,
flop = function(self)
return handle_result(self, lib.MagickFlopImage(self.wand))
end,
get_property = function(self, property)
local res = lib.MagickGetImageProperty(self.wand, property)
if nil ~= res then
do
local _with_0 = ffi.string(res)
lib.MagickRelinquishMemory(res)
return _with_0
end
else
local code, msg = get_exception(self.wand)
return nil, msg, code
end
end,
set_property = function(self, property, value)
return handle_result(self, lib.MagickSetImageProperty(self.wand, property, value))
end,
get_orientation = function(self)
return orientation_str[lib.MagickGetImageOrientation(self.wand)]
end,
set_orientation = function(self, orientation)
local type = orientation_type[orientation]
if not (type) then
error("invalid orientation type")
end
return lib.MagickSetImageOrientation(self.wand, type)
end,
get_interlace_scheme = function(self)
return interlace_str[lib.MagickGetImageInterlaceScheme(self.wand)]
end,
set_interlace_scheme = function(self, interlace_scheme)
local type = interlace_type[interlace_scheme]
if not (type) then
error("invalid interlace type")
end
return lib.MagickSetImageInterlaceScheme(self.wand, type)
end,
auto_orient = function(self)
return handle_result(self, lib.MagickAutoOrientImage(self.wand))
end,
__tostring = function(self)
return "Image<" .. tostring(self.path) .. ", " .. tostring(self.wand) .. ">"
end
Expand Down
76 changes: 75 additions & 1 deletion magick/init.moon
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,37 @@ gravity_type = {}
for i, t in ipairs gravity_str
gravity_type[t] = i

orientation_str = {
"TopLeftOrientation",
"TopRightOrientation",
"BottomRightOrientation",
"BottomLeftOrientation",
"LeftTopOrientation",
"RightTopOrientation",
"RightBottomOrientation",
"LeftBottomOrientation"
}

orientation_type = {}

for i, t in ipairs orientation_str
orientation_type[t] = i

interlace_str = {
"NoInterlace",
"LineInterlace",
"PlaneInterlace",
"PartitionInterlace",
"GIFInterlace",
"JPEGInterlace",
"PNGInterlace"
}

interlace_type = {}

for i, t in ipairs interlace_str
interlace_type[t] = i

lib.MagickWandGenesis!

filter = (name) -> lib[name .. "Filter"]
Expand Down Expand Up @@ -273,6 +304,50 @@ class Image

lib.PixelGetRed(@pixel_wand), lib.PixelGetGreen(@pixel_wand), lib.PixelGetBlue(@pixel_wand), lib.PixelGetAlpha(@pixel_wand)

transpose: =>
handle_result @, lib.MagickTransposeImage @wand

transverse: =>
handle_result @, lib.MagickTransverseImage @wand

flip: =>
handle_result @, lib.MagickFlipImage @wand

flop: =>
handle_result @, lib.MagickFlopImage @wand

get_property: (property) =>
res = lib.MagickGetImageProperty @wand, property
if nil != res
with ffi.string res
lib.MagickRelinquishMemory res
else
code, msg = get_exception @wand
nil, msg, code

set_property: (property, value) =>
handle_result @,
lib.MagickSetImageProperty @wand, property, value

get_orientation: =>
orientation_str[lib.MagickGetImageOrientation @wand]

set_orientation: (orientation) =>
type = orientation_type[orientation]
error "invalid orientation type" unless type
lib.MagickSetImageOrientation @wand, type

get_interlace_scheme: =>
interlace_str[lib.MagickGetImageInterlaceScheme @wand]

set_interlace_scheme: (interlace_scheme) =>
type = interlace_type[interlace_scheme]
error "invalid interlace type" unless type
lib.MagickSetImageInterlaceScheme @wand, type

auto_orient: =>
handle_result @, lib.MagickAutoOrientImage @wand

__tostring: =>
"Image<#{@path}, #{@wand}>"

Expand Down Expand Up @@ -350,4 +425,3 @@ thumb = (img, size_str, output) ->
ret

{ :load_image, :load_image_from_blob, :thumb, :Image, :parse_size_str, :VERSION }

24 changes: 24 additions & 0 deletions magick/wand/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ ffi.cdef([[ typedef void MagickWand;
typedef int ssize_t;
typedef int CompositeOperator;
typedef int GravityType;
typedef int OrientationType;
typedef int InterlaceType;

void MagickWandGenesis();
MagickWand* NewMagickWand();
Expand Down Expand Up @@ -80,6 +82,28 @@ ffi.cdef([[ typedef void MagickWand;
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);
]])
local get_flags
get_flags = function()
Expand Down
24 changes: 24 additions & 0 deletions magick/wand/lib.moon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ffi.cdef [[
typedef int ssize_t;
typedef int CompositeOperator;
typedef int GravityType;
typedef int OrientationType;
typedef int InterlaceType;

void MagickWandGenesis();
MagickWand* NewMagickWand();
Expand Down Expand Up @@ -82,6 +84,28 @@ ffi.cdef [[
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);
]]


Expand Down
Binary file added spec/auto_orient_test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions spec/magick_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ describe "magick", ->
assert img\scale 80
assert img\write out_path "scale.png"

it "transpose", ->
assert img\transpose!
assert img\write out_path "transpose.png"

it "transverse", ->
assert img\transverse!
assert img\write out_path "transverse.png"

it "flip", ->
assert img\flip!
assert img\write out_path "flip.png"

it "flop", ->
assert img\flop!
assert img\write out_path "flop.png"

it "composite", ->
img2 = img\clone!
assert img2\resize 32
Expand Down Expand Up @@ -108,6 +124,29 @@ describe "magick", ->
assert img\set_option "webp", "lossless", "0"
assert.same "0", img\get_option "webp", "lossless"

it "should set property", ->
assert img\set_property "exif:Orientation", "1"
assert.same "1", img\get_property "exif:Orientation"

it "should get non-existent property", ->
assert.same nil, img\get_property "NonExistentProperty"

it "should set orientation", ->
assert img\set_orientation "TopLeftOrientation"
assert.same "TopLeftOrientation", img\get_orientation!

it "should not set orientation", ->
assert.has_error ->
assert img\set_orientation "NonExistentOrientation"

it "should set interlace scheme", ->
assert img\set_interlace_scheme "PlaneInterlace"
assert.same "PlaneInterlace", img\get_interlace_scheme!

it "should not set interlace scheme", ->
assert.has_error ->
assert img\set_interlace_scheme "NonExistentInterlaceScheme"

describe "color_image", ->
import load_image from magick
local img
Expand Down Expand Up @@ -143,6 +182,20 @@ describe "magick", ->
img\strip!
img\write "spec/output_images/exif_test.jpg"

it "should read exif data", ->
import load_image from magick
img = load_image "spec/exif_test.jpg"
assert.same "NIKON D5100", img\get_property "exif:Model"

describe "automatic orientation", ->
it "should automatically orient", ->
import load_image from magick
img = load_image "spec/auto_orient_test.jpg"
assert.same "BottomRightOrientation", img\get_orientation!
assert img\auto_orient!
assert.same "TopLeftOrientation", img\get_orientation!
assert img\write "spec/output_images/auto_orient.jpg"

describe "thumb", ->
import thumb from magick
sizes = {
Expand Down