mirror of
https://github.com/aseprite/aseprite.git
synced 2025-01-31 00:32:48 +00:00
lua: Add functions to load/save palettes and images directly
This commit is contained in:
parent
d24fc0a93a
commit
0bf5d1de30
@ -14,6 +14,7 @@
|
||||
#include "app/commands/params.h"
|
||||
#include "app/context.h"
|
||||
#include "app/doc.h"
|
||||
#include "app/doc_access.h"
|
||||
#include "app/i18n/strings.h"
|
||||
#include "app/loop_tag.h"
|
||||
#include "app/pref/preferences.h"
|
||||
@ -36,6 +37,8 @@
|
||||
#include "base/fs.h"
|
||||
#include "doc/frame_tag.h"
|
||||
#include "doc/layer.h"
|
||||
#include "doc/primitives.h"
|
||||
#include "render/render.h"
|
||||
#include "ui/alert.h"
|
||||
|
||||
#include <iostream>
|
||||
@ -43,14 +46,13 @@
|
||||
namespace app {
|
||||
namespace script {
|
||||
|
||||
namespace {
|
||||
|
||||
int App_open(lua_State* L)
|
||||
int load_sprite_from_file(lua_State* L, const char* filename,
|
||||
const LoadSpriteFromFileParam param)
|
||||
{
|
||||
std::string absFn = base::get_absolute_path(luaL_checkstring(L, 1));
|
||||
if (!ask_access(L, absFn.c_str(), FileAccessMode::Read, true))
|
||||
return luaL_error(L, "script doesn't have access to open file %s",
|
||||
absFn.c_str());
|
||||
std::string absFn = base::get_absolute_path(filename);
|
||||
if (!ask_access(L, absFn.c_str(), FileAccessMode::Read, true))
|
||||
return luaL_error(L, "script doesn't have access to open file %s",
|
||||
absFn.c_str());
|
||||
|
||||
app::Context* ctx = App::instance()->context();
|
||||
Doc* oldDoc = ctx->activeDocument();
|
||||
@ -59,16 +61,54 @@ int App_open(lua_State* L)
|
||||
Commands::instance()->byId(CommandId::OpenFile());
|
||||
Params params;
|
||||
params.set("filename", absFn.c_str());
|
||||
if (param == LoadSpriteFromFileParam::OneFrameAsImage)
|
||||
params.set("oneframe", "true");
|
||||
ctx->executeCommand(openCommand, params);
|
||||
|
||||
Doc* newDoc = ctx->activeDocument();
|
||||
if (newDoc != oldDoc)
|
||||
push_docobj(L, newDoc->sprite());
|
||||
if (newDoc != oldDoc) {
|
||||
if (param == LoadSpriteFromFileParam::OneFrameAsImage) {
|
||||
doc::Sprite* sprite = newDoc->sprite();
|
||||
|
||||
// Render the first frame of the sprite
|
||||
// TODO add "frame" parameter to render different frames
|
||||
std::unique_ptr<doc::Image> image(doc::Image::create(sprite->spec()));
|
||||
doc::clear_image(image.get(), sprite->transparentColor());
|
||||
render::Render().renderSprite(image.get(), sprite, 0);
|
||||
|
||||
// Restore the old document and active and destroy the recently
|
||||
// loaded sprite.
|
||||
ctx->setActiveDocument(oldDoc);
|
||||
|
||||
try {
|
||||
DocDestroyer destroyer(ctx, newDoc, 500);
|
||||
destroyer.destroyDocument();
|
||||
}
|
||||
catch (const LockedDocException& ex) {
|
||||
// Almost impossible?
|
||||
luaL_error(L, "cannot lock document to close it\n%s", ex.what());
|
||||
}
|
||||
|
||||
push_image(L, image.release());
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
push_docobj(L, newDoc->sprite());
|
||||
}
|
||||
}
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
int App_open(lua_State* L)
|
||||
{
|
||||
return load_sprite_from_file(
|
||||
L, luaL_checkstring(L, 1), LoadSpriteFromFileParam::FullAniAsSprite);
|
||||
}
|
||||
|
||||
int App_exit(lua_State* L)
|
||||
{
|
||||
app::Context* ctx = App::instance()->context();
|
||||
|
@ -26,8 +26,8 @@ int ColorSpace_new(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
else if (lua_istable(L, 1)) {
|
||||
// Load ICC profile when ColorSpace{ filename="..." } is specified
|
||||
if (lua_getfield(L, 1, "filename") != LUA_TNIL) {
|
||||
// Load ICC profile when ColorSpace{ fromFile="..." } is specified
|
||||
if (lua_getfield(L, 1, "fromFile") != LUA_TNIL) {
|
||||
const char* fn = lua_tostring(L, -1);
|
||||
if (fn) {
|
||||
auto buf = base::read_file_content(fn);
|
||||
|
@ -114,6 +114,7 @@ namespace app {
|
||||
void push_cels(lua_State* L, doc::Sprite* sprite);
|
||||
void push_color_space(lua_State* L, const gfx::ColorSpace& cs);
|
||||
void push_doc_range(lua_State* L, Site& site, const DocRange& docRange);
|
||||
void push_image(lua_State* L, doc::Image* image);
|
||||
void push_images(lua_State* L, const doc::ObjectIds& images);
|
||||
void push_layers(lua_State* L, const doc::ObjectIds& layers);
|
||||
void push_sprite_cel(lua_State* L, doc::Cel* cel);
|
||||
@ -127,8 +128,8 @@ namespace app {
|
||||
void push_sprite_slices(lua_State* L, doc::Sprite* sprite);
|
||||
void push_sprite_tags(lua_State* L, doc::Sprite* sprite);
|
||||
void push_sprites(lua_State* L);
|
||||
void push_userdata(lua_State* L, doc::WithUserData* userData);
|
||||
void push_tool(lua_State* L, tools::Tool* tool);
|
||||
void push_userdata(lua_State* L, doc::WithUserData* userData);
|
||||
|
||||
gfx::Point convert_args_into_point(lua_State* L, int index);
|
||||
gfx::Rect convert_args_into_rect(lua_State* L, int index);
|
||||
@ -143,6 +144,12 @@ namespace app {
|
||||
const doc::Mask* get_mask_from_arg(lua_State* L, int index);
|
||||
tools::Tool* get_tool_from_arg(lua_State* L, int index);
|
||||
|
||||
// Used by App.open(), Sprite{ fromFile }, and Image{ fromFile }
|
||||
enum class LoadSpriteFromFileParam { FullAniAsSprite,
|
||||
OneFrameAsImage };
|
||||
int load_sprite_from_file(lua_State* L, const char* filename,
|
||||
const LoadSpriteFromFileParam param);
|
||||
|
||||
} // namespace script
|
||||
} // namespace app
|
||||
|
||||
|
@ -11,11 +11,15 @@
|
||||
|
||||
#include "app/cmd/copy_rect.h"
|
||||
#include "app/cmd/copy_region.h"
|
||||
#include "app/doc.h"
|
||||
#include "app/file/file.h"
|
||||
#include "app/script/docobj.h"
|
||||
#include "app/script/engine.h"
|
||||
#include "app/script/luacpp.h"
|
||||
#include "app/script/security.h"
|
||||
#include "app/tx.h"
|
||||
#include "app/util/autocrop.h"
|
||||
#include "base/fs.h"
|
||||
#include "doc/algorithm/shrink_bounds.h"
|
||||
#include "doc/cel.h"
|
||||
#include "doc/image.h"
|
||||
@ -72,9 +76,38 @@ int Image_new(lua_State* L)
|
||||
if (auto spec2 = may_get_obj<doc::ImageSpec>(L, 1)) {
|
||||
spec = *spec2;
|
||||
}
|
||||
else if (auto otherImg = may_get_obj<ImageObj>(L, 1)) {
|
||||
else if (may_get_obj<ImageObj>(L, 1)) {
|
||||
return Image_clone(L);
|
||||
}
|
||||
else if (lua_istable(L, 1)) {
|
||||
// Image{ fromFile }
|
||||
int type = lua_getfield(L, 1, "fromFile");
|
||||
if (type != LUA_TNIL) {
|
||||
if (const char* fromFile = lua_tostring(L, -1)) {
|
||||
std::string fn = fromFile;
|
||||
lua_pop(L, 1);
|
||||
return load_sprite_from_file(
|
||||
L, fn.c_str(),
|
||||
LoadSpriteFromFileParam::OneFrameAsImage);
|
||||
}
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
// In case that there is no "fromFile" field
|
||||
if (type == LUA_TNIL) {
|
||||
// Image{ width, height, colorMode }
|
||||
lua_getfield(L, 1, "width");
|
||||
lua_getfield(L, 1, "height");
|
||||
spec.setWidth(lua_tointeger(L, -2));
|
||||
spec.setHeight(lua_tointeger(L, -1));
|
||||
lua_pop(L, 2);
|
||||
|
||||
type = lua_getfield(L, 1, "colorMode");
|
||||
if (type != LUA_TNIL)
|
||||
spec.setColorMode((doc::ColorMode)lua_tointeger(L, -1));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
const int w = lua_tointeger(L, 1);
|
||||
const int h = lua_tointeger(L, 2);
|
||||
@ -263,6 +296,37 @@ int Image_isPlain(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Image_saveAs(lua_State* L)
|
||||
{
|
||||
auto obj = get_obj<ImageObj>(L, 1);
|
||||
auto img = obj->image(L);
|
||||
const char* fn = luaL_checkstring(L, 2);
|
||||
bool result = false;
|
||||
if (fn) {
|
||||
std::string absFn = base::get_absolute_path(fn);
|
||||
if (!ask_access(L, absFn.c_str(), FileAccessMode::Write, true))
|
||||
return luaL_error(L, "script doesn't have access to write file %s",
|
||||
absFn.c_str());
|
||||
|
||||
std::unique_ptr<Sprite> sprite(Sprite::createBasicSprite(img->spec(), 256));
|
||||
|
||||
std::vector<Image*> oneImage;
|
||||
sprite->getImages(oneImage);
|
||||
ASSERT(oneImage.size() == 1);
|
||||
if (!oneImage.empty())
|
||||
copy_image(oneImage.front(), img);
|
||||
|
||||
std::unique_ptr<Doc> doc(new Doc(sprite.get()));
|
||||
sprite.release();
|
||||
doc->setFilename(absFn);
|
||||
|
||||
app::Context* ctx = App::instance()->context();
|
||||
result = (save_document(ctx, doc.get()) >= 0);
|
||||
}
|
||||
lua_pushboolean(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Image_get_width(lua_State* L)
|
||||
{
|
||||
const auto obj = get_obj<ImageObj>(L, 1);
|
||||
@ -309,6 +373,7 @@ const luaL_Reg Image_methods[] = {
|
||||
{ "isEqual", Image_isEqual },
|
||||
{ "isEmpty", Image_isEmpty },
|
||||
{ "isPlain", Image_isPlain },
|
||||
{ "saveAs", Image_saveAs },
|
||||
{ "__gc", Image_gc },
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
@ -340,6 +405,11 @@ void push_cel_image(lua_State* L, doc::Cel* cel)
|
||||
push_new<ImageObj>(L, cel);
|
||||
}
|
||||
|
||||
void push_image(lua_State* L, doc::Image* image)
|
||||
{
|
||||
push_new<ImageObj>(L, image);
|
||||
}
|
||||
|
||||
doc::Image* may_get_image_from_arg(lua_State* L, int index)
|
||||
{
|
||||
auto obj = may_get_obj<ImageObj>(L, index);
|
||||
|
@ -1,5 +1,5 @@
|
||||
// Aseprite
|
||||
// Copyright (C) 2018 Igara Studio S.A.
|
||||
// Copyright (C) 2018-2019 Igara Studio S.A.
|
||||
// Copyright (C) 2018 David Capello
|
||||
//
|
||||
// This program is distributed under the terms of
|
||||
@ -11,10 +11,13 @@
|
||||
|
||||
#include "app/cmd/set_palette.h"
|
||||
#include "app/color.h"
|
||||
#include "app/file/palette_file.h"
|
||||
#include "app/script/docobj.h"
|
||||
#include "app/script/engine.h"
|
||||
#include "app/script/luacpp.h"
|
||||
#include "app/script/security.h"
|
||||
#include "app/tx.h"
|
||||
#include "base/fs.h"
|
||||
#include "doc/palette.h"
|
||||
#include "doc/sprite.h"
|
||||
|
||||
@ -64,6 +67,28 @@ int Palette_new(lua_State* L)
|
||||
if (auto pal2 = may_get_obj<PaletteObj>(L, 1)) {
|
||||
push_new<PaletteObj>(L, nullptr, new Palette(*pal2->palette(L)));
|
||||
}
|
||||
else if (lua_istable(L, 1)) {
|
||||
// Palette{ fromFile }
|
||||
int type = lua_getfield(L, 1, "fromFile");
|
||||
if (type != LUA_TNIL) {
|
||||
if (const char* fromFile = lua_tostring(L, -1)) {
|
||||
std::string absFn = base::get_absolute_path(fromFile);
|
||||
lua_pop(L, 1);
|
||||
|
||||
if (!ask_access(L, absFn.c_str(), FileAccessMode::Read, true))
|
||||
return luaL_error(L, "script doesn't have access to open file %s",
|
||||
absFn.c_str());
|
||||
|
||||
Palette* pal = load_palette(absFn.c_str());
|
||||
if (pal)
|
||||
push_new<PaletteObj>(L, nullptr, pal);
|
||||
else
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
else {
|
||||
int ncolors = lua_tointeger(L, 1);
|
||||
push_new<PaletteObj>(L, nullptr,
|
||||
@ -165,6 +190,21 @@ int Palette_get_frame(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Palette_saveAs(lua_State* L)
|
||||
{
|
||||
auto obj = get_obj<PaletteObj>(L, 1);
|
||||
auto pal = obj->palette(L);
|
||||
const char* fn = luaL_checkstring(L, 2);
|
||||
if (fn) {
|
||||
std::string absFn = base::get_absolute_path(fn);
|
||||
if (!ask_access(L, absFn.c_str(), FileAccessMode::Write, true))
|
||||
return luaL_error(L, "script doesn't have access to write file %s",
|
||||
absFn.c_str());
|
||||
save_palette(absFn.c_str(), pal, pal->size());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Palette_get_frameNumber(lua_State* L)
|
||||
{
|
||||
auto obj = get_obj<PaletteObj>(L, 1);
|
||||
@ -179,6 +219,7 @@ const luaL_Reg Palette_methods[] = {
|
||||
{ "resize", Palette_resize },
|
||||
{ "getColor", Palette_getColor },
|
||||
{ "setColor", Palette_setColor },
|
||||
{ "saveAs", Palette_saveAs },
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
|
||||
|
@ -57,7 +57,7 @@ int Sprite_new(lua_State* L)
|
||||
{
|
||||
std::unique_ptr<Doc> doc;
|
||||
|
||||
// Duplicate a sprie
|
||||
// Duplicate a sprite
|
||||
if (auto otherSpr = may_get_docobj<doc::Sprite>(L, 1)) {
|
||||
Doc* otherDoc = static_cast<Doc*>(otherSpr->document());
|
||||
doc.reset(otherDoc->duplicate(DuplicateExactCopy));
|
||||
@ -68,15 +68,51 @@ int Sprite_new(lua_State* L)
|
||||
spec = *spec2;
|
||||
}
|
||||
else {
|
||||
const int w = lua_tointeger(L, 1);
|
||||
const int h = lua_tointeger(L, 2);
|
||||
const int colorMode = (lua_isnone(L, 3) ? IMAGE_RGB: lua_tointeger(L, 3));
|
||||
spec.setWidth(w);
|
||||
spec.setHeight(h);
|
||||
spec.setColorMode((doc::ColorMode)colorMode);
|
||||
if (lua_istable(L, 1)) {
|
||||
// Sprite{ fromFile }
|
||||
int type = lua_getfield(L, 1, "fromFile");
|
||||
if (type != LUA_TNIL) {
|
||||
if (const char* fromFile = lua_tostring(L, -1)) {
|
||||
std::string fn = fromFile;
|
||||
lua_pop(L, 1);
|
||||
return load_sprite_from_file(
|
||||
L, fn.c_str(),
|
||||
LoadSpriteFromFileParam::FullAniAsSprite);
|
||||
}
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
// In case that there is no "fromFile" field
|
||||
if (type == LUA_TNIL) {
|
||||
// Sprite{ width, height, colorMode }
|
||||
lua_getfield(L, 1, "width");
|
||||
lua_getfield(L, 1, "height");
|
||||
spec.setWidth(lua_tointeger(L, -2));
|
||||
spec.setHeight(lua_tointeger(L, -1));
|
||||
lua_pop(L, 2);
|
||||
|
||||
type = lua_getfield(L, 1, "colorMode");
|
||||
if (type != LUA_TNIL)
|
||||
spec.setColorMode((doc::ColorMode)lua_tointeger(L, -1));
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
const int w = lua_tointeger(L, 1);
|
||||
const int h = lua_tointeger(L, 2);
|
||||
const int colorMode = (lua_isnone(L, 3) ? IMAGE_RGB: lua_tointeger(L, 3));
|
||||
spec.setWidth(w);
|
||||
spec.setHeight(h);
|
||||
spec.setColorMode((doc::ColorMode)colorMode);
|
||||
}
|
||||
spec.setColorSpace(get_working_rgb_space_from_preferences());
|
||||
}
|
||||
|
||||
if (spec.width() < 1)
|
||||
return luaL_error(L, "invalid width value = %d in Sprite()", spec.width());
|
||||
if (spec.height() < 1)
|
||||
return luaL_error(L, "invalid height value = %d in Sprite()", spec.height());
|
||||
|
||||
std::unique_ptr<Sprite> sprite(Sprite::createBasicSprite(spec, 256));
|
||||
doc.reset(new Doc(sprite.get()));
|
||||
sprite.release();
|
||||
|
Loading…
x
Reference in New Issue
Block a user