summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorDaniel J. Ramirez <djrmuv@gmail.com>2017-08-18 21:21:24 -0500
committerDaniel J. Ramirez <djrmuv@gmail.com>2017-08-20 13:53:02 -0500
commitda8fecf25e11ea05ad6aaab4f9676c78e4faecdc (patch)
treef56d429c0bca3b6bfd214e4cbc1a8c481691b43e /modules
parentab13d0aeed43b7263b1e62bb62551525b70822a8 (diff)
Added support for SVG
Diffstat (limited to 'modules')
-rw-r--r--modules/hdr/image_loader_hdr.cpp2
-rw-r--r--modules/hdr/image_loader_hdr.h2
-rw-r--r--modules/jpg/image_loader_jpegd.cpp2
-rw-r--r--modules/jpg/image_loader_jpegd.h2
-rw-r--r--modules/svg/SCsub34
-rw-r--r--modules/svg/config.py7
-rw-r--r--modules/svg/image_loader_svg.cpp107
-rw-r--r--modules/svg/image_loader_svg.h67
-rw-r--r--modules/svg/register_types.cpp45
-rw-r--r--modules/svg/register_types.h31
-rw-r--r--modules/tga/image_loader_tga.cpp2
-rw-r--r--modules/tga/image_loader_tga.h2
-rw-r--r--modules/tinyexr/image_loader_tinyexr.cpp2
-rw-r--r--modules/tinyexr/image_loader_tinyexr.h2
-rw-r--r--modules/webp/image_loader_webp.cpp2
-rw-r--r--modules/webp/image_loader_webp.h2
16 files changed, 301 insertions, 10 deletions
diff --git a/modules/hdr/image_loader_hdr.cpp b/modules/hdr/image_loader_hdr.cpp
index 19df27b962..a3f0601043 100644
--- a/modules/hdr/image_loader_hdr.cpp
+++ b/modules/hdr/image_loader_hdr.cpp
@@ -34,7 +34,7 @@
#include "thirdparty/tinyexr/tinyexr.h"
-Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {
+Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
String header = f->get_token();
diff --git a/modules/hdr/image_loader_hdr.h b/modules/hdr/image_loader_hdr.h
index 127833ebd0..1e08e954e1 100644
--- a/modules/hdr/image_loader_hdr.h
+++ b/modules/hdr/image_loader_hdr.h
@@ -38,7 +38,7 @@
class ImageLoaderHDR : public ImageFormatLoader {
public:
- virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
+ virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderHDR();
};
diff --git a/modules/jpg/image_loader_jpegd.cpp b/modules/jpg/image_loader_jpegd.cpp
index 8c73b69f1b..4f38e83274 100644
--- a/modules/jpg/image_loader_jpegd.cpp
+++ b/modules/jpg/image_loader_jpegd.cpp
@@ -89,7 +89,7 @@ Error jpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p
return OK;
}
-Error ImageLoaderJPG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {
+Error ImageLoaderJPG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
PoolVector<uint8_t> src_image;
int src_image_len = f->get_len();
diff --git a/modules/jpg/image_loader_jpegd.h b/modules/jpg/image_loader_jpegd.h
index aa073b493d..917c0e1d95 100644
--- a/modules/jpg/image_loader_jpegd.h
+++ b/modules/jpg/image_loader_jpegd.h
@@ -38,7 +38,7 @@
class ImageLoaderJPG : public ImageFormatLoader {
public:
- virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
+ virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderJPG();
};
diff --git a/modules/svg/SCsub b/modules/svg/SCsub
new file mode 100644
index 0000000000..0d3a347b7e
--- /dev/null
+++ b/modules/svg/SCsub
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+Import('env')
+
+# Thirdparty source files
+thirdparty_dir = "#thirdparty/nanosvg/src/"
+thirdparty_sources = [
+ "nanosvg.cc"
+]
+thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
+
+# env.add_source_files(env.modules_sources, thirdparty_sources)
+
+lib = env.Library("svg_builtin", thirdparty_sources)
+# Needs to be appended to arrive after libscene in the linker call,
+# but we don't want it to arrive *after* system libs, so manual hack
+# LIBS contains first SCons Library objects ("SCons.Node.FS.File object")
+# and then plain strings for system library. We insert between the two.
+inserted = False
+for idx, linklib in enumerate(env["LIBS"]):
+ if isinstance(linklib, basestring): # first system lib such as "X11", otherwise SCons lib object
+ env["LIBS"].insert(idx, lib)
+ inserted = True
+ break
+if not inserted:
+ env.Append(LIBS=[lib])
+
+env.Append(CPPPATH=[thirdparty_dir])
+env.Append(CCFLAGS=["-DSVG_ENABLED"])
+
+# Godot's own source files
+env.add_source_files(env.modules_sources, "*.cpp")
+
+Export('env') \ No newline at end of file
diff --git a/modules/svg/config.py b/modules/svg/config.py
new file mode 100644
index 0000000000..fb920482f5
--- /dev/null
+++ b/modules/svg/config.py
@@ -0,0 +1,7 @@
+
+def can_build(platform):
+ return True
+
+
+def configure(env):
+ pass
diff --git a/modules/svg/image_loader_svg.cpp b/modules/svg/image_loader_svg.cpp
new file mode 100644
index 0000000000..cc801775b8
--- /dev/null
+++ b/modules/svg/image_loader_svg.cpp
@@ -0,0 +1,107 @@
+/*************************************************************************/
+/* image_loader_svg.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* http://www.godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+#include "image_loader_svg.h"
+
+#include "os/os.h"
+#include "print_string.h"
+
+#include <ustring.h>
+
+void SVGRasterizer::rasterize(NSVGimage *p_image, float p_tx, float p_ty, float p_scale, unsigned char *p_dst, int p_w, int p_h, int p_stride) {
+ nsvgRasterize(rasterizer, p_image, p_tx, p_ty, p_scale, p_dst, p_w, p_h, p_stride);
+}
+
+SVGRasterizer::SVGRasterizer() {
+ rasterizer = nsvgCreateRasterizer();
+}
+SVGRasterizer::~SVGRasterizer() {
+ nsvgDeleteRasterizer(rasterizer);
+}
+
+SVGRasterizer ImageLoaderSVG::rasterizer;
+
+Error ImageLoaderSVG::_create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale) {
+ NSVGimage *svg_image;
+ PoolVector<uint8_t>::Read src_r = p_data->read();
+ svg_image = nsvgParse((char *)src_r.ptr(), "px", 96);
+ if (svg_image == NULL) {
+ ERR_PRINT("SVG Corrupted");
+ return ERR_FILE_CORRUPT;
+ }
+ int w = (int)(svg_image->width * p_scale);
+ int h = (int)(svg_image->height * p_scale);
+
+ PoolVector<uint8_t> dst_image;
+ dst_image.resize(w * h * 4);
+
+ PoolVector<uint8_t>::Write dw = dst_image.write();
+
+ rasterizer.rasterize(svg_image, 0, 0, p_scale, (unsigned char *)dw.ptr(), w, h, w * 4);
+
+ dw = PoolVector<uint8_t>::Write();
+ p_image->create(w, h, false, Image::FORMAT_RGBA8, dst_image);
+ if (upsample)
+ p_image->shrink_x2();
+
+ nsvgDelete(svg_image);
+
+ return OK;
+}
+
+Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, const char *svg_str, float p_scale) {
+
+ size_t str_len = strlen(svg_str);
+ PoolVector<uint8_t> src_data;
+ src_data.resize(str_len);
+ PoolVector<uint8_t>::Write src_w = src_data.write();
+ memcpy(src_w.ptr(), svg_str, str_len);
+
+ return _create_image(p_image, &src_data, p_scale);
+}
+
+Error ImageLoaderSVG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
+
+ uint32_t size = f->get_len();
+ PoolVector<uint8_t> src_image;
+ src_image.resize(size);
+ PoolVector<uint8_t>::Write src_w = src_image.write();
+ f->get_buffer(src_w.ptr(), size);
+
+ return _create_image(p_image, &src_image, p_scale);
+}
+
+void ImageLoaderSVG::get_recognized_extensions(List<String> *p_extensions) const {
+
+ p_extensions->push_back("svg");
+ p_extensions->push_back("svgz");
+}
+
+ImageLoaderSVG::ImageLoaderSVG() {
+}
diff --git a/modules/svg/image_loader_svg.h b/modules/svg/image_loader_svg.h
new file mode 100644
index 0000000000..2b6a59edc8
--- /dev/null
+++ b/modules/svg/image_loader_svg.h
@@ -0,0 +1,67 @@
+/*************************************************************************/
+/* image_loader_svg.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* http://www.godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+#ifndef IMAGE_LOADER_SVG_H
+#define IMAGE_LOADER_SVG_H
+
+#include "io/image_loader.h"
+#include "ustring.h"
+
+#include <nanosvg.h>
+#include <nanosvgrast.h>
+
+/**
+ @author Daniel Ramirez <djrmuv@gmail.com>
+*/
+
+class SVGRasterizer {
+
+ NSVGrasterizer *rasterizer;
+
+public:
+ void rasterize(NSVGimage *p_image, float p_tx, float p_ty, float p_scale, unsigned char *p_dst, int p_w, int p_h, int p_stride);
+
+ SVGRasterizer();
+ ~SVGRasterizer();
+};
+
+class ImageLoaderSVG : public ImageFormatLoader {
+
+ static SVGRasterizer rasterizer;
+ static Error _create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale);
+
+public:
+ static Error create_image_from_string(Ref<Image> p_image, const char *p_svg_str, float p_scale);
+
+ virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale);
+ virtual void get_recognized_extensions(List<String> *p_extensions) const;
+ ImageLoaderSVG();
+};
+
+#endif \ No newline at end of file
diff --git a/modules/svg/register_types.cpp b/modules/svg/register_types.cpp
new file mode 100644
index 0000000000..e0f967ca06
--- /dev/null
+++ b/modules/svg/register_types.cpp
@@ -0,0 +1,45 @@
+/*************************************************************************/
+/* register_types.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* http://www.godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+#include "register_types.h"
+
+#include "image_loader_svg.h"
+
+static ImageLoaderSVG *image_loader_svg = NULL;
+
+void register_svg_types() {
+
+ image_loader_svg = memnew(ImageLoaderSVG);
+ ImageLoader::add_image_format_loader(image_loader_svg);
+}
+
+void unregister_svg_types() {
+
+ memdelete(image_loader_svg);
+}
diff --git a/modules/svg/register_types.h b/modules/svg/register_types.h
new file mode 100644
index 0000000000..920b724623
--- /dev/null
+++ b/modules/svg/register_types.h
@@ -0,0 +1,31 @@
+/*************************************************************************/
+/* register_types.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* http://www.godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+void register_svg_types();
+void unregister_svg_types();
diff --git a/modules/tga/image_loader_tga.cpp b/modules/tga/image_loader_tga.cpp
index 5b8610b975..2329d0bcc2 100644
--- a/modules/tga/image_loader_tga.cpp
+++ b/modules/tga/image_loader_tga.cpp
@@ -203,7 +203,7 @@ Error ImageLoaderTGA::convert_to_image(Ref<Image> p_image, const uint8_t *p_buff
return OK;
}
-Error ImageLoaderTGA::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {
+Error ImageLoaderTGA::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
PoolVector<uint8_t> src_image;
int src_image_len = f->get_len();
diff --git a/modules/tga/image_loader_tga.h b/modules/tga/image_loader_tga.h
index 11329ec68a..f42a3f7e75 100644
--- a/modules/tga/image_loader_tga.h
+++ b/modules/tga/image_loader_tga.h
@@ -75,7 +75,7 @@ class ImageLoaderTGA : public ImageFormatLoader {
static Error convert_to_image(Ref<Image> p_image, const uint8_t *p_buffer, const tga_header_s &p_header, const uint8_t *p_palette, const bool p_is_monochrome);
public:
- virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
+ virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderTGA();
};
diff --git a/modules/tinyexr/image_loader_tinyexr.cpp b/modules/tinyexr/image_loader_tinyexr.cpp
index 4eb91da10b..18d453a717 100644
--- a/modules/tinyexr/image_loader_tinyexr.cpp
+++ b/modules/tinyexr/image_loader_tinyexr.cpp
@@ -34,7 +34,7 @@
#include "thirdparty/tinyexr/tinyexr.h"
-Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {
+Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
PoolVector<uint8_t> src_image;
int src_image_len = f->get_len();
diff --git a/modules/tinyexr/image_loader_tinyexr.h b/modules/tinyexr/image_loader_tinyexr.h
index a52894b12b..f9636a303e 100644
--- a/modules/tinyexr/image_loader_tinyexr.h
+++ b/modules/tinyexr/image_loader_tinyexr.h
@@ -38,7 +38,7 @@
class ImageLoaderTinyEXR : public ImageFormatLoader {
public:
- virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
+ virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderTinyEXR();
};
diff --git a/modules/webp/image_loader_webp.cpp b/modules/webp/image_loader_webp.cpp
index 87c2e811b3..4ffcdefbe9 100644
--- a/modules/webp/image_loader_webp.cpp
+++ b/modules/webp/image_loader_webp.cpp
@@ -115,7 +115,7 @@ static Ref<Image> _webp_lossy_unpack(const PoolVector<uint8_t> &p_buffer) {
return img;
}
-Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear) {
+Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
uint32_t size = f->get_len();
PoolVector<uint8_t> src_image;
diff --git a/modules/webp/image_loader_webp.h b/modules/webp/image_loader_webp.h
index 1ac2196a71..b8c5933512 100644
--- a/modules/webp/image_loader_webp.h
+++ b/modules/webp/image_loader_webp.h
@@ -38,7 +38,7 @@
class ImageLoaderWEBP : public ImageFormatLoader {
public:
- virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear);
+ virtual Error load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderWEBP();
};