diff options
Diffstat (limited to 'core/image.cpp')
-rw-r--r-- | core/image.cpp | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/core/image.cpp b/core/image.cpp index 422c0e407b..2f2d7efd7c 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2018 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 */ @@ -47,6 +47,7 @@ const char *Image::format_names[Image::FORMAT_MAX] = { "RGBA8", "RGBA4444", "RGBA5551", + "RGB10A2", "RFloat", //float "RGFloat", "RGBFloat", @@ -112,6 +113,7 @@ int Image::get_format_pixel_size(Format p_format) { case FORMAT_RGBA8: return 4; case FORMAT_RGBA4444: return 2; case FORMAT_RGBA5551: return 2; + case FORMAT_RGB10A2: return 4; case FORMAT_RF: return 4; //float case FORMAT_RGF: return 8; @@ -1978,6 +1980,15 @@ Color Image::get_pixel(int p_x, int p_y) const { float a = ((u >> 15) & 0x1) / 1.0; return Color(r, g, b, a); } break; + case FORMAT_RGB10A2: { + + uint32_t u = ((uint32_t *)ptr)[ofs]; + float r = (u & 0x3FF) / 1023.0; + float g = ((u >> 10) & 0x3FF) / 1023.0; + float b = ((u >> 20) & 0x3FF) / 1023.0; + float a = ((u >> 30) & 0x3) / 3.0; + return Color(r, g, b, a); + } break; case FORMAT_RF: { float r = ((float *)ptr)[ofs]; @@ -2123,6 +2134,18 @@ void Image::set_pixel(int p_x, int p_y, const Color &p_color) { ((uint16_t *)ptr)[ofs] = rgba; } break; + case FORMAT_RGB10A2: { + + uint32_t rgba = 0; + + rgba = uint32_t(CLAMP(p_color.r * 1023.0, 0, 1023)); + rgba |= uint32_t(CLAMP(p_color.g * 1023.0, 0, 1023)) << 10; + rgba |= uint32_t(CLAMP(p_color.b * 1023.0, 0, 1023)) << 20; + rgba |= uint32_t(CLAMP(p_color.a * 3.0, 0, 3)) << 30; + + ((uint32_t *)ptr)[ofs] = rgba; + + } break; case FORMAT_RF: { ((float *)ptr)[ofs] = p_color.r; @@ -2287,6 +2310,9 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel); ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel); + ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer); + ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer); + ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data"); BIND_ENUM_CONSTANT(FORMAT_L8); //luminance @@ -2297,6 +2323,7 @@ void Image::_bind_methods() { BIND_ENUM_CONSTANT(FORMAT_RGBA8); BIND_ENUM_CONSTANT(FORMAT_RGBA4444); BIND_ENUM_CONSTANT(FORMAT_RGBA5551); + BIND_ENUM_CONSTANT(FORMAT_RGB10A2); BIND_ENUM_CONSTANT(FORMAT_RF); //float BIND_ENUM_CONSTANT(FORMAT_RGF); BIND_ENUM_CONSTANT(FORMAT_RGBF); @@ -2505,6 +2532,40 @@ String Image::get_format_name(Format p_format) { return format_names[p_format]; } +Error Image::load_png_from_buffer(const PoolVector<uint8_t> &p_array) { + + int buffer_size = p_array.size(); + + ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!_png_mem_loader_func, ERR_INVALID_PARAMETER); + + PoolVector<uint8_t>::Read r = p_array.read(); + + Ref<Image> image = _png_mem_loader_func(r.ptr(), buffer_size); + ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR); + + copy_internals_from(image); + + return OK; +} + +Error Image::load_jpg_from_buffer(const PoolVector<uint8_t> &p_array) { + + int buffer_size = p_array.size(); + + ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!_jpg_mem_loader_func, ERR_INVALID_PARAMETER); + + PoolVector<uint8_t>::Read r = p_array.read(); + + Ref<Image> image = _jpg_mem_loader_func(r.ptr(), buffer_size); + ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR); + + copy_internals_from(image); + + return OK; +} + Image::Image(const uint8_t *p_mem_png_jpg, int p_len) { width = 0; |