diff --git a/src/gfx/generated_image.cpp b/src/gfx/generated_image.cpp index 8d0ef742..5dc915b9 100644 --- a/src/gfx/generated_image.cpp +++ b/src/gfx/generated_image.cpp @@ -14,6 +14,7 @@ #include #include #include // load_resource_image +#include // ----------------------------------------------------------------------------- : GeneratedImage @@ -515,3 +516,50 @@ bool ImageValueToImage::operator == (const GeneratedImage& that) const { return that2 && filename == that2->filename && age == that2->age; } + +// ----------------------------------------------------------------------------- : ExternalImage + +Image ExternalImage::generate(const Options& opt) const { + wxFileName fname(filepath, wxPATH_UNIX); + + // does the file pointed to by filepath exist? + if (!fname.FileExists()) { + String filePathString = fname.GetAbsolutePath().ToStdString(); + throw ScriptError(format_string(_("The file '%s' was not found."),filePathString)); + } + + String fileExt = fname.GetExt(); + wxBitmapType bitmapType; + if (fileExt == _("png")) + bitmapType = wxBITMAP_TYPE_PNG; + else if (fileExt == _("jpg")) + bitmapType = wxBITMAP_TYPE_JPEG; + else + bitmapType = wxBITMAP_TYPE_BMP; + + // does the file exist in the package? + String fileNameNoExtension = fname.GetName(); + if (!opt.local_package->existsIn(fileNameNoExtension)) { + auto outStream = opt.local_package->openOut(fileNameNoExtension); + wxFileInputStream inStream = wxFileInputStream(filepath.ToStdString()); + if (!inStream.IsOk()) throw ScriptError("Failed to create file stream."); + outStream->Write(inStream); + if (!outStream->IsOk()) throw ScriptError("Failed to write image to set."); + outStream->Close(); + } + + // save the package with the new image + opt.local_package->saveAs(opt.local_package->relativeFilename(), false, false); + + auto imageInputStream = opt.local_package->openIn(fileNameNoExtension); + Image img(*imageInputStream.get(), bitmapType); + + if (!img.IsOk()) throw ScriptError("The image could not be created."); + + return img; +} + +bool ExternalImage::operator == (const GeneratedImage& that) const { + const ExternalImage* that2 = dynamic_cast(&that); + return that2 && that2->filepath == filepath; +} diff --git a/src/gfx/generated_image.hpp b/src/gfx/generated_image.hpp index d1e78221..42941923 100644 --- a/src/gfx/generated_image.hpp +++ b/src/gfx/generated_image.hpp @@ -398,4 +398,16 @@ private: LocalFileName filename; Age age; ///< Age the image was last updated }; - + +// ----------------------------------------------------------------------------- : ExternalImage + +/// Load an image from the filesystem +class ExternalImage : public GeneratedImage { +public: + ExternalImage(const String& filepath) : filepath(filepath) {}; + Image generate(const Options&) const override; + bool operator == (const GeneratedImage& that) const override; + inline String toString() { return filepath; } +private: + String filepath; +}; diff --git a/src/script/functions/construction.cpp b/src/script/functions/construction.cpp index d876d579..468bdd80 100644 --- a/src/script/functions/construction.cpp +++ b/src/script/functions/construction.cpp @@ -14,9 +14,11 @@ #include #include #include +#include #include #include #include +#include // ----------------------------------------------------------------------------- : new_card @@ -45,6 +47,9 @@ SCRIPT_FUNCTION(new_card) { pvalue->package_name = v->toString(); } else if (ColorValue* cvalue = dynamic_cast(value)) { cvalue->value = v->toColor(); + } else if (ImageValue* ivalue = dynamic_cast(value)) { + wxFileName fname( static_cast(v.get())->toString() ); + ivalue->filename = LocalFileName::fromReadString( fname.GetName(), ""); } else { throw ScriptError(format_string(_("Can not set value '%s', it is not of the right type"),name)); } diff --git a/src/script/functions/image.cpp b/src/script/functions/image.cpp index 23951556..927d529b 100644 --- a/src/script/functions/image.cpp +++ b/src/script/functions/image.cpp @@ -43,6 +43,11 @@ SCRIPT_FUNCTION(to_card_image) { return make_intrusive(export_bitmap(set, input, (zoom / 100), deg_to_rad(angle)).ConvertToImage()); } } + +SCRIPT_FUNCTION(import_image) { + SCRIPT_PARAM(String, path); + return make_intrusive(path); +} // ----------------------------------------------------------------------------- : Image functions @@ -261,4 +266,5 @@ void init_script_image_functions(Context& ctx) { ctx.setVariable(_("drop_shadow"), script_drop_shadow); ctx.setVariable(_("symbol_variation"), script_symbol_variation); ctx.setVariable(_("built_in_image"), script_built_in_image); + ctx.setVariable(_("import_image"), script_import_image); }