[lua] Add Image:flip()

This commit is contained in:
Gaspar Capello 2023-05-11 15:09:31 -03:00 committed by David Capello
parent 66ac837305
commit f3ed22e1a6
4 changed files with 87 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include "app/doc_range.h"
#include "app/pref/preferences.h"
#include "app/script/blend_mode.h"
#include "app/script/flip_type.h"
#include "app/script/luacpp.h"
#include "app/script/require.h"
#include "app/script/security.h"
@ -449,6 +450,13 @@ Engine::Engine()
setfield_integer(L, "INTERSECT", (int)gen::SelectionMode::INTERSECT);
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "FlipType");
setfield_integer(L, "HORIZONTAL", app::script::FlipType::HORIZONTAL);
setfield_integer(L, "VERTICAL", app::script::FlipType::VERTICAL);
lua_pop(L, 1);
// Register classes/prototypes
register_brush_class(L);
register_cel_class(L);

View File

@ -0,0 +1,22 @@
// Aseprite
// Copyright (c) 2023 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_SCRIPT_FLIP_TYPE_H_INCLUDED
#define APP_SCRIPT_FLIP_TYPE_H_INCLUDED
#pragma once
namespace app {
namespace script {
enum class FlipType : int {
HORIZONTAL,
VERTICAL
};
} // namespace script
} // namespace app
#endif

View File

@ -11,6 +11,7 @@
#include "app/cmd/copy_rect.h"
#include "app/cmd/copy_region.h"
#include "app/cmd/flip_image.h"
#include "app/commands/new_params.h" // Used for enum <-> Lua conversions
#include "app/context.h"
#include "app/doc.h"
@ -18,6 +19,7 @@
#include "app/script/blend_mode.h"
#include "app/script/docobj.h"
#include "app/script/engine.h"
#include "app/script/flip_type.h"
#include "app/script/luacpp.h"
#include "app/script/security.h"
#include "app/site.h"
@ -578,6 +580,20 @@ int Image_shrinkBounds(lua_State* L)
return 1;
}
int Image_flip(lua_State* L)
{
auto obj = get_obj<ImageObj>(L, 1);
doc::Image* img = obj->image(L);
doc::algorithm::FlipType flipType = doc::algorithm::FlipType::FlipHorizontal;
if (lua_isinteger(L, 2) && lua_tointeger(L, 2) > 0)
flipType = doc::algorithm::FlipType::FlipVertical;
Tx tx;
tx(new cmd::FlipImage(img, img->bounds(), flipType));
tx.commit();
return 0;
}
int Image_get_id(lua_State* L)
{
const auto obj = get_obj<ImageObj>(L, 1);
@ -679,6 +695,7 @@ const luaL_Reg Image_methods[] = {
{ "saveAs", Image_saveAs },
{ "resize", Image_resize },
{ "shrinkBounds", Image_shrinkBounds },
{ "flip", Image_flip },
{ "__gc", Image_gc },
{ "__eq", Image_eq },
{ nullptr, nullptr }

View File

@ -403,3 +403,43 @@ do
r_255, r_g127, r_g064,
r_255, r_255, g_255 })
end
-- Tests for Image:flip()
do
local img = Image(3, 3)
local r = Color(255, 0, 0).rgbaPixel
local g = Color(0, 255, 0).rgbaPixel
img:clear(0)
img:drawPixel(0, 0, g)
img:drawPixel(1, 1, r)
img:drawPixel(2, 2, r)
expect_img(img, { g, 0, 0,
0, r, 0,
0, 0, r })
img:flip()
expect_img(img, { 0, 0, g,
0, r, 0,
r, 0, 0 })
app.undo()
expect_img(img, { g, 0, 0,
0, r, 0,
0, 0, r })
img:flip(FlipType.HORIZONTAL)
expect_img(img, { 0, 0, g,
0, r, 0,
r, 0, 0 })
app.undo()
img:flip(FlipType.VERTICAL)
expect_img(img, { 0, 0, r,
0, r, 0,
g, 0, 0 })
img:flip(FlipType.VERTICAL)
expect_img(img, { g, 0, 0,
0, r, 0,
0, 0, r })
app.undo()
app.undo()
expect_img(img, { g, 0, 0,
0, r, 0,
0, 0, r })
end