Browse Source

It is now possible to import images as a separate resource, closes #5738 and likely many others

Juan Linietsky 7 years ago
parent
commit
317dee95de

+ 80 - 0
core/io/image_loader.cpp

@@ -107,3 +107,83 @@ void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) {
 	ERR_FAIL_COND(loader_count >= MAX_LOADERS);
 	loader[loader_count++] = p_loader;
 }
+
+/////////////////
+
+RES ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error) {
+
+	FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
+	if (!f) {
+		if (r_error) {
+			*r_error = ERR_CANT_OPEN;
+		}
+		memdelete(f);
+		return RES();
+	}
+
+	uint8_t header[4] = { 0, 0, 0, 0 };
+	f->get_buffer(header, 4);
+
+	bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M';
+	if (unrecognized) {
+		memdelete(f);
+		if (r_error) {
+			*r_error = ERR_FILE_UNRECOGNIZED;
+		}
+		ERR_FAIL_V(RES());
+	}
+
+	String extension = f->get_pascal_string();
+
+	int idx = -1;
+
+	for (int i = 0; i < ImageLoader::loader_count; i++) {
+		if (ImageLoader::loader[i]->recognize(extension)) {
+			idx = i;
+			break;
+		}
+	}
+
+	if (idx == -1) {
+		memdelete(f);
+		if (r_error) {
+			*r_error = ERR_FILE_UNRECOGNIZED;
+		}
+		ERR_FAIL_V(RES());
+	}
+
+	Ref<Image> image;
+	image.instance();
+
+	Error err = ImageLoader::loader[idx]->load_image(image, f, false, 1.0);
+
+	memdelete(f);
+
+	if (err != OK) {
+		if (r_error) {
+			*r_error = err;
+		}
+		return RES();
+	}
+
+	if (r_error) {
+		*r_error = OK;
+	}
+
+	return image;
+}
+
+void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {
+
+	p_extensions->push_back("image");
+}
+
+bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {
+
+	return p_type == "Image";
+}
+
+String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
+
+	return "Image";
+}

+ 12 - 1
core/io/image_loader.h

@@ -32,9 +32,11 @@
 #define IMAGE_LOADER_H
 
 #include "image.h"
+#include "io/resource_loader.h"
 #include "list.h"
 #include "os/file_access.h"
 #include "ustring.h"
+
 /**
 	@author Juan Linietsky <[email protected]>
 */
@@ -55,6 +57,7 @@ class ImageLoader;
 
 class ImageFormatLoader {
 	friend class ImageLoader;
+	friend class ResourceFormatLoaderImage;
 
 protected:
 	virtual Error load_image(Ref<Image> p_image, FileAccess *p_fileaccess, bool p_force_linear, float p_scale) = 0;
@@ -70,7 +73,7 @@ class ImageLoader {
 	enum {
 		MAX_LOADERS = 8
 	};
-
+	friend class ResourceFormatLoaderImage;
 	static ImageFormatLoader *loader[MAX_LOADERS];
 	static int loader_count;
 
@@ -83,4 +86,12 @@ public:
 	static void add_image_format_loader(ImageFormatLoader *p_loader);
 };
 
+class ResourceFormatLoaderImage : public ResourceFormatLoader {
+public:
+	virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
+	virtual void get_recognized_extensions(List<String> *p_extensions) const;
+	virtual bool handles_type(const String &p_type) const;
+	virtual String get_resource_type(const String &p_path) const;
+};
+
 #endif

+ 9 - 0
core/register_core_types.cpp

@@ -41,6 +41,7 @@
 #include "input_map.h"
 #include "io/config_file.h"
 #include "io/http_client.h"
+#include "io/image_loader.h"
 #include "io/marshalls.h"
 #include "io/multiplayer_api.h"
 #include "io/networked_multiplayer_peer.h"
@@ -60,11 +61,14 @@
 #include "path_remap.h"
 #include "project_settings.h"
 #include "translation.h"
+
 #include "undo_redo.h"
 static ResourceFormatSaverBinary *resource_saver_binary = NULL;
 static ResourceFormatLoaderBinary *resource_loader_binary = NULL;
 static ResourceFormatImporter *resource_format_importer = NULL;
 
+static ResourceFormatLoaderImage *resource_format_image = NULL;
+
 static _ResourceLoader *_resource_loader = NULL;
 static _ResourceSaver *_resource_saver = NULL;
 static _OS *_os = NULL;
@@ -111,6 +115,9 @@ void register_core_types() {
 	resource_format_importer = memnew(ResourceFormatImporter);
 	ResourceLoader::add_resource_format_loader(resource_format_importer);
 
+	resource_format_image = memnew(ResourceFormatLoaderImage);
+	ResourceLoader::add_resource_format_loader(resource_format_image);
+
 	ClassDB::register_class<Object>();
 
 	ClassDB::register_virtual_class<Script>();
@@ -237,6 +244,8 @@ void unregister_core_types() {
 
 	memdelete(_geometry);
 
+	if (resource_format_image)
+		memdelete(resource_format_image);
 	if (resource_saver_binary)
 		memdelete(resource_saver_binary);
 	if (resource_loader_binary)

+ 5 - 0
editor/editor_node.cpp

@@ -62,6 +62,7 @@
 #include "editor/import/editor_scene_importer_gltf.h"
 #include "editor/import/resource_importer_bitmask.h"
 #include "editor/import/resource_importer_csv_translation.h"
+#include "editor/import/resource_importer_image.h"
 #include "editor/import/resource_importer_obj.h"
 #include "editor/import/resource_importer_scene.h"
 #include "editor/import/resource_importer_texture.h"
@@ -4664,6 +4665,10 @@ EditorNode::EditorNode() {
 		import_texture.instance();
 		ResourceFormatImporter::get_singleton()->add_importer(import_texture);
 
+		Ref<ResourceImporterImage> import_image;
+		import_image.instance();
+		ResourceFormatImporter::get_singleton()->add_importer(import_image);
+
 		Ref<ResourceImporterCSVTranslation> import_csv_translation;
 		import_csv_translation.instance();
 		ResourceFormatImporter::get_singleton()->add_importer(import_csv_translation);

+ 79 - 0
editor/import/resource_importer_image.cpp

@@ -0,0 +1,79 @@
+#include "resource_importer_image.h"
+
+#include "io/image_loader.h"
+#include "io/resource_saver.h"
+#include "os/file_access.h"
+#include "scene/resources/texture.h"
+
+String ResourceImporterImage::get_importer_name() const {
+
+	return "image";
+}
+
+String ResourceImporterImage::get_visible_name() const {
+
+	return "Image";
+}
+void ResourceImporterImage::get_recognized_extensions(List<String> *p_extensions) const {
+
+	ImageLoader::get_recognized_extensions(p_extensions);
+}
+
+String ResourceImporterImage::get_save_extension() const {
+	return "image";
+}
+
+String ResourceImporterImage::get_resource_type() const {
+
+	return "Image";
+}
+
+bool ResourceImporterImage::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
+
+	return true;
+}
+
+int ResourceImporterImage::get_preset_count() const {
+	return 0;
+}
+String ResourceImporterImage::get_preset_name(int p_idx) const {
+
+	return String();
+}
+
+void ResourceImporterImage::get_import_options(List<ImportOption> *r_options, int p_preset) const {
+}
+
+Error ResourceImporterImage::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files) {
+
+	FileAccess *f = FileAccess::open(p_source_file, FileAccess::READ);
+	if (!f) {
+		ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);
+	}
+
+	size_t len = f->get_len();
+
+	Vector<uint8_t> data;
+	data.resize(len);
+
+	f->get_buffer(data.ptrw(), len);
+
+	memdelete(f);
+
+	f = FileAccess::open(p_save_path + ".image", FileAccess::WRITE);
+
+	//save the header GDIM
+	const uint8_t header[4] = { 'G', 'D', 'I', 'M' };
+	f->store_buffer(header, 4);
+	//SAVE the extension (so it can be recognized by the loader later
+	f->store_pascal_string(p_source_file.get_extension().to_lower());
+	//SAVE the actual image
+	f->store_buffer(data.ptr(), len);
+
+	memdelete(f);
+
+	return OK;
+}
+
+ResourceImporterImage::ResourceImporterImage() {
+}

+ 27 - 0
editor/import/resource_importer_image.h

@@ -0,0 +1,27 @@
+#ifndef RESOURCE_IMPORTER_IMAGE_H
+#define RESOURCE_IMPORTER_IMAGE_H
+
+#include "image.h"
+#include "io/resource_import.h"
+
+class ResourceImporterImage : public ResourceImporter {
+	GDCLASS(ResourceImporterImage, ResourceImporter)
+public:
+	virtual String get_importer_name() const;
+	virtual String get_visible_name() const;
+	virtual void get_recognized_extensions(List<String> *p_extensions) const;
+	virtual String get_save_extension() const;
+	virtual String get_resource_type() const;
+
+	virtual int get_preset_count() const;
+	virtual String get_preset_name(int p_idx) const;
+
+	virtual void get_import_options(List<ImportOption> *r_options, int p_preset = 0) const;
+	virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const;
+
+	virtual Error import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = NULL);
+
+	ResourceImporterImage();
+};
+
+#endif // RESOURCE_IMPORTER_IMAGE_H

+ 73 - 22
editor/plugins/editor_preview_plugins.cpp

@@ -78,7 +78,7 @@ bool EditorTexturePreviewPlugin::handles(const String &p_type) const {
 	return ClassDB::is_parent_class(p_type, "Texture");
 }
 
-Ref<Texture> EditorTexturePreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorTexturePreviewPlugin::generate(const RES &p_from) const {
 
 	Ref<Image> img;
 	Ref<AtlasTexture> atex = p_from;
@@ -138,12 +138,66 @@ EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() {
 
 ////////////////////////////////////////////////////////////////////////////
 
+bool EditorImagePreviewPlugin::handles(const String &p_type) const {
+
+	return p_type == "Image";
+}
+
+Ref<Texture> EditorImagePreviewPlugin::generate(const RES &p_from) const {
+
+	Ref<Image> img = p_from;
+
+	if (img.is_null() || img->empty())
+		return Ref<Image>();
+
+	img = img->duplicate();
+	img->clear_mipmaps();
+
+	int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
+	thumbnail_size *= EDSCALE;
+	if (img->is_compressed()) {
+		if (img->decompress() != OK)
+			return Ref<Image>();
+	} else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
+		img->convert(Image::FORMAT_RGBA8);
+	}
+
+	int width, height;
+	if (img->get_width() > thumbnail_size && img->get_width() >= img->get_height()) {
+
+		width = thumbnail_size;
+		height = img->get_height() * thumbnail_size / img->get_width();
+	} else if (img->get_height() > thumbnail_size && img->get_height() >= img->get_width()) {
+
+		height = thumbnail_size;
+		width = img->get_width() * thumbnail_size / img->get_height();
+	} else {
+
+		width = img->get_width();
+		height = img->get_height();
+	}
+
+	img->resize(width, height);
+	post_process_preview(img);
+
+	Ref<ImageTexture> ptex;
+	ptex.instance();
+
+	ptex->create_from_image(img, 0);
+	return ptex;
+}
+
+EditorImagePreviewPlugin::EditorImagePreviewPlugin() {
+}
+
+////////////////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////
 bool EditorBitmapPreviewPlugin::handles(const String &p_type) const {
 
 	return ClassDB::is_parent_class(p_type, "BitMap");
 }
 
-Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES &p_from) const {
 
 	Ref<BitMap> bm = p_from;
 
@@ -215,12 +269,12 @@ bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const {
 
 	return ClassDB::is_parent_class(p_type, "PackedScene");
 }
-Ref<Texture> EditorPackedScenePreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorPackedScenePreviewPlugin::generate(const RES &p_from) const {
 
 	return generate_from_path(p_from->get_path());
 }
 
-Ref<Texture> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path) {
+Ref<Texture> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path) const {
 
 	String temp_path = EditorSettings::get_singleton()->get_cache_dir();
 	String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text();
@@ -269,7 +323,7 @@ bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {
 	return ClassDB::is_parent_class(p_type, "Material"); //any material
 }
 
-Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES &p_from) const {
 
 	Ref<Material> material = p_from;
 	ERR_FAIL_COND_V(material.is_null(), Ref<Texture>());
@@ -281,7 +335,7 @@ Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES &p_from) {
 		VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
 
 		preview_done = false;
-		VS::get_singleton()->request_frame_drawn_callback(this, "_preview_done", Variant());
+		VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorMaterialPreviewPlugin *>(this), "_preview_done", Variant());
 
 		while (!preview_done) {
 			OS::get_singleton()->delay_usec(10);
@@ -436,7 +490,7 @@ bool EditorScriptPreviewPlugin::handles(const String &p_type) const {
 	return ClassDB::is_parent_class(p_type, "Script");
 }
 
-Ref<Texture> EditorScriptPreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorScriptPreviewPlugin::generate(const RES &p_from) const {
 
 	Ref<Script> scr = p_from;
 	if (scr.is_null())
@@ -559,7 +613,7 @@ bool EditorAudioStreamPreviewPlugin::handles(const String &p_type) const {
 	return ClassDB::is_parent_class(p_type, "AudioStream");
 }
 
-Ref<Texture> EditorAudioStreamPreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorAudioStreamPreviewPlugin::generate(const RES &p_from) const {
 
 	Ref<AudioStream> stream = p_from;
 	ERR_FAIL_COND_V(stream.is_null(), Ref<Texture>());
@@ -657,7 +711,7 @@ bool EditorMeshPreviewPlugin::handles(const String &p_type) const {
 	return ClassDB::is_parent_class(p_type, "Mesh"); //any Mesh
 }
 
-Ref<Texture> EditorMeshPreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorMeshPreviewPlugin::generate(const RES &p_from) const {
 
 	Ref<Mesh> mesh = p_from;
 	ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture>());
@@ -684,7 +738,7 @@ Ref<Texture> EditorMeshPreviewPlugin::generate(const RES &p_from) {
 	VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
 
 	preview_done = false;
-	VS::get_singleton()->request_frame_drawn_callback(this, "_preview_done", Variant());
+	VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorMeshPreviewPlugin *>(this), "_preview_done", Variant());
 
 	while (!preview_done) {
 		OS::get_singleton()->delay_usec(10);
@@ -771,16 +825,7 @@ bool EditorFontPreviewPlugin::handles(const String &p_type) const {
 	return ClassDB::is_parent_class(p_type, "DynamicFontData");
 }
 
-Ref<Texture> EditorFontPreviewPlugin::generate_from_path(const String &p_path) {
-	if (canvas.is_valid()) {
-		VS::get_singleton()->viewport_remove_canvas(viewport, canvas);
-	}
-
-	canvas = VS::get_singleton()->canvas_create();
-	canvas_item = VS::get_singleton()->canvas_item_create();
-
-	VS::get_singleton()->viewport_attach_canvas(viewport, canvas);
-	VS::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
+Ref<Texture> EditorFontPreviewPlugin::generate_from_path(const String &p_path) const {
 
 	Ref<DynamicFontData> SampledFont;
 	SampledFont.instance();
@@ -809,7 +854,7 @@ Ref<Texture> EditorFontPreviewPlugin::generate_from_path(const String &p_path) {
 	VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
 
 	preview_done = false;
-	VS::get_singleton()->request_frame_drawn_callback(this, "_preview_done", Variant());
+	VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorFontPreviewPlugin *>(this), "_preview_done", Variant());
 
 	while (!preview_done) {
 		OS::get_singleton()->delay_usec(10);
@@ -829,7 +874,7 @@ Ref<Texture> EditorFontPreviewPlugin::generate_from_path(const String &p_path) {
 	return ptex;
 }
 
-Ref<Texture> EditorFontPreviewPlugin::generate(const RES &p_from) {
+Ref<Texture> EditorFontPreviewPlugin::generate(const RES &p_from) const {
 
 	return generate_from_path(p_from->get_path());
 }
@@ -842,6 +887,12 @@ EditorFontPreviewPlugin::EditorFontPreviewPlugin() {
 	VS::get_singleton()->viewport_set_size(viewport, 128, 128);
 	VS::get_singleton()->viewport_set_active(viewport, true);
 	viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
+
+	canvas = VS::get_singleton()->canvas_create();
+	canvas_item = VS::get_singleton()->canvas_item_create();
+
+	VS::get_singleton()->viewport_attach_canvas(viewport, canvas);
+	VS::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
 }
 
 EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {

+ 22 - 13
editor/plugins/editor_preview_plugins.h

@@ -39,16 +39,25 @@ class EditorTexturePreviewPlugin : public EditorResourcePreviewGenerator {
 	GDCLASS(EditorTexturePreviewPlugin, EditorResourcePreviewGenerator)
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
+	virtual Ref<Texture> generate(const RES &p_from) const;
 
 	EditorTexturePreviewPlugin();
 };
 
+class EditorImagePreviewPlugin : public EditorResourcePreviewGenerator {
+	GDCLASS(EditorImagePreviewPlugin, EditorResourcePreviewGenerator)
+public:
+	virtual bool handles(const String &p_type) const;
+	virtual Ref<Texture> generate(const RES &p_from) const;
+
+	EditorImagePreviewPlugin();
+};
+
 class EditorBitmapPreviewPlugin : public EditorResourcePreviewGenerator {
 	GDCLASS(EditorBitmapPreviewPlugin, EditorResourcePreviewGenerator)
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
+	virtual Ref<Texture> generate(const RES &p_from) const;
 
 	EditorBitmapPreviewPlugin();
 };
@@ -57,8 +66,8 @@ class EditorPackedScenePreviewPlugin : public EditorResourcePreviewGenerator {
 
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
-	virtual Ref<Texture> generate_from_path(const String &p_path);
+	virtual Ref<Texture> generate(const RES &p_from) const;
+	virtual Ref<Texture> generate_from_path(const String &p_path) const;
 
 	EditorPackedScenePreviewPlugin();
 };
@@ -77,7 +86,7 @@ class EditorMaterialPreviewPlugin : public EditorResourcePreviewGenerator {
 	RID light2;
 	RID light_instance2;
 	RID camera;
-	volatile bool preview_done;
+	mutable volatile bool preview_done;
 
 	void _preview_done(const Variant &p_udata);
 
@@ -86,7 +95,7 @@ protected:
 
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
+	virtual Ref<Texture> generate(const RES &p_from) const;
 
 	EditorMaterialPreviewPlugin();
 	~EditorMaterialPreviewPlugin();
@@ -95,7 +104,7 @@ public:
 class EditorScriptPreviewPlugin : public EditorResourcePreviewGenerator {
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
+	virtual Ref<Texture> generate(const RES &p_from) const;
 
 	EditorScriptPreviewPlugin();
 };
@@ -103,7 +112,7 @@ public:
 class EditorAudioStreamPreviewPlugin : public EditorResourcePreviewGenerator {
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
+	virtual Ref<Texture> generate(const RES &p_from) const;
 
 	EditorAudioStreamPreviewPlugin();
 };
@@ -121,7 +130,7 @@ class EditorMeshPreviewPlugin : public EditorResourcePreviewGenerator {
 	RID light2;
 	RID light_instance2;
 	RID camera;
-	volatile bool preview_done;
+	mutable volatile bool preview_done;
 
 	void _preview_done(const Variant &p_udata);
 
@@ -130,7 +139,7 @@ protected:
 
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
+	virtual Ref<Texture> generate(const RES &p_from) const;
 
 	EditorMeshPreviewPlugin();
 	~EditorMeshPreviewPlugin();
@@ -144,7 +153,7 @@ class EditorFontPreviewPlugin : public EditorResourcePreviewGenerator {
 	RID viewport_texture;
 	RID canvas;
 	RID canvas_item;
-	volatile bool preview_done;
+	mutable volatile bool preview_done;
 
 	void _preview_done(const Variant &p_udata);
 
@@ -153,8 +162,8 @@ protected:
 
 public:
 	virtual bool handles(const String &p_type) const;
-	virtual Ref<Texture> generate(const RES &p_from);
-	virtual Ref<Texture> generate_from_path(const String &p_path);
+	virtual Ref<Texture> generate(const RES &p_from) const;
+	virtual Ref<Texture> generate_from_path(const String &p_path) const;
 
 	EditorFontPreviewPlugin();
 	~EditorFontPreviewPlugin();