summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/image.cpp176
-rw-r--r--core/image.h4
-rw-r--r--core/math/vector2.cpp2
-rw-r--r--core/math/vector3.h2
4 files changed, 141 insertions, 43 deletions
diff --git a/core/image.cpp b/core/image.cpp
index e4573c5ebb..7778169995 100644
--- a/core/image.cpp
+++ b/core/image.cpp
@@ -37,6 +37,7 @@
#include "print_string.h"
#include "io/resource_loader.h"
+#include "math_funcs.h"
#include "thirdparty/misc/hq2x.h"
#include <stdio.h>
@@ -525,7 +526,7 @@ static double _bicubic_interp_kernel(double x) {
return bc;
}
-template <int CC>
+template <int CC, class T>
static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
// get source image size
@@ -556,7 +557,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_
// initial pixel value
- uint8_t *__restrict dst = p_dst + (y * p_dst_width + x) * CC;
+ T *__restrict dst = ((T *)p_dst) + (y * p_dst_width + x) * CC;
double color[CC];
for (int i = 0; i < CC; i++) {
@@ -584,23 +585,32 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_
ox2 = xmax;
// get pixel of original image
- const uint8_t *__restrict p = p_src + (oy2 * p_src_width + ox2) * CC;
+ const T *__restrict p = ((T *)p_src) + (oy2 * p_src_width + ox2) * CC;
for (int i = 0; i < CC; i++) {
-
- color[i] += p[i] * k2;
+ if (sizeof(T) == 2) { //half float
+ color[i] = Math::half_to_float(p[i]);
+ } else {
+ color[i] += p[i] * k2;
+ }
}
}
}
for (int i = 0; i < CC; i++) {
- dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255);
+ if (sizeof(T) == 1) { //byte
+ dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255);
+ } else if (sizeof(T) == 2) { //half float
+ dst[i] = Math::make_half_float(color[i]);
+ } else {
+ dst[i] = color[i];
+ }
}
}
}
}
-template <int CC>
+template <int CC, class T>
static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
enum {
@@ -640,22 +650,58 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict
for (uint32_t l = 0; l < CC; l++) {
- uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS;
- uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS;
- uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS;
- uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS;
-
- uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS);
- uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS);
- uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS);
- interp >>= FRAC_BITS;
- p_dst[i * p_dst_width * CC + j * CC + l] = interp;
+ if (sizeof(T) == 1) { //uint8
+ uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS;
+ uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS;
+ uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS;
+ uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS;
+
+ uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS);
+ uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS);
+ uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS);
+ interp >>= FRAC_BITS;
+ p_dst[i * p_dst_width * CC + j * CC + l] = interp;
+ } else if (sizeof(T) == 2) { //half float
+
+ float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS);
+ float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS);
+ const T *src = ((const T *)p_src);
+ T *dst = ((T *)p_dst);
+
+ float p00 = Math::half_to_float(src[y_ofs_up + src_xofs_left + l]);
+ float p10 = Math::half_to_float(src[y_ofs_up + src_xofs_right + l]);
+ float p01 = Math::half_to_float(src[y_ofs_down + src_xofs_left + l]);
+ float p11 = Math::half_to_float(src[y_ofs_down + src_xofs_right + l]);
+
+ float interp_up = p00 + (p10 - p00) * xofs_frac;
+ float interp_down = p01 + (p11 - p01) * xofs_frac;
+ float interp = interp_up + ((interp_down - interp_up) * yofs_frac);
+
+ dst[i * p_dst_width * CC + j * CC + l] = Math::make_half_float(interp);
+ } else if (sizeof(T) == 4) { //float
+
+ float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS);
+ float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS);
+ const T *src = ((const T *)p_src);
+ T *dst = ((T *)p_dst);
+
+ float p00 = src[y_ofs_up + src_xofs_left + l];
+ float p10 = src[y_ofs_up + src_xofs_right + l];
+ float p01 = src[y_ofs_down + src_xofs_left + l];
+ float p11 = src[y_ofs_down + src_xofs_right + l];
+
+ float interp_up = p00 + (p10 - p00) * xofs_frac;
+ float interp_down = p01 + (p11 - p01) * xofs_frac;
+ float interp = interp_up + ((interp_down - interp_up) * yofs_frac);
+
+ dst[i * p_dst_width * CC + j * CC + l] = interp;
+ }
}
}
}
}
-template <int CC>
+template <int CC, class T>
static void _scale_nearest(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) {
for (uint32_t i = 0; i < p_dst_height; i++) {
@@ -670,8 +716,11 @@ static void _scale_nearest(const uint8_t *__restrict p_src, uint8_t *__restrict
for (uint32_t l = 0; l < CC; l++) {
- uint32_t p = p_src[y_ofs + src_xofs + l];
- p_dst[i * p_dst_width * CC + j * CC + l] = p;
+ const T *src = ((const T *)p_src);
+ T *dst = ((T *)p_dst);
+
+ T p = src[y_ofs + src_xofs + l];
+ dst[i * p_dst_width * CC + j * CC + l] = p;
}
}
}
@@ -766,12 +815,30 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
case INTERPOLATE_NEAREST: {
- switch (get_format_pixel_size(format)) {
- case 1: _scale_nearest<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
- case 2: _scale_nearest<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
- case 3: _scale_nearest<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
- case 4: _scale_nearest<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
+ switch (get_format_pixel_size(format)) {
+ case 1: _scale_nearest<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 2: _scale_nearest<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 3: _scale_nearest<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 4: _scale_nearest<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ }
+ } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
+ switch (get_format_pixel_size(format)) {
+ case 4: _scale_nearest<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 8: _scale_nearest<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 12: _scale_nearest<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 16: _scale_nearest<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ }
+
+ } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
+ switch (get_format_pixel_size(format)) {
+ case 2: _scale_nearest<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 4: _scale_nearest<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 6: _scale_nearest<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 8: _scale_nearest<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ }
}
+
} break;
case INTERPOLATE_BILINEAR:
case INTERPOLATE_TRILINEAR: {
@@ -812,11 +879,27 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
}
}
- switch (get_format_pixel_size(format)) {
- case 1: _scale_bilinear<1>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
- case 2: _scale_bilinear<2>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
- case 3: _scale_bilinear<3>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
- case 4: _scale_bilinear<4>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
+ switch (get_format_pixel_size(format)) {
+ case 1: _scale_bilinear<1, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 2: _scale_bilinear<2, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 3: _scale_bilinear<3, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 4: _scale_bilinear<4, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ }
+ } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
+ switch (get_format_pixel_size(format)) {
+ case 4: _scale_bilinear<1, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 8: _scale_bilinear<2, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 12: _scale_bilinear<3, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 16: _scale_bilinear<4, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ }
+ } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
+ switch (get_format_pixel_size(format)) {
+ case 2: _scale_bilinear<1, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 4: _scale_bilinear<2, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 6: _scale_bilinear<3, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ case 8: _scale_bilinear<4, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); break;
+ }
}
}
@@ -829,13 +912,28 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) {
} break;
case INTERPOLATE_CUBIC: {
- switch (get_format_pixel_size(format)) {
- case 1: _scale_cubic<1>(r_ptr, w_ptr, width, height, p_width, p_height); break;
- case 2: _scale_cubic<2>(r_ptr, w_ptr, width, height, p_width, p_height); break;
- case 3: _scale_cubic<3>(r_ptr, w_ptr, width, height, p_width, p_height); break;
- case 4: _scale_cubic<4>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) {
+ switch (get_format_pixel_size(format)) {
+ case 1: _scale_cubic<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 2: _scale_cubic<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 3: _scale_cubic<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 4: _scale_cubic<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ }
+ } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) {
+ switch (get_format_pixel_size(format)) {
+ case 4: _scale_cubic<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 8: _scale_cubic<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 12: _scale_cubic<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 16: _scale_cubic<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ }
+ } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) {
+ switch (get_format_pixel_size(format)) {
+ case 2: _scale_cubic<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 4: _scale_cubic<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 6: _scale_cubic<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ case 8: _scale_cubic<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); break;
+ }
}
-
} break;
}
@@ -1685,7 +1783,7 @@ Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_loss
case COMPRESS_S3TC: {
ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE);
- _image_compress_bc_func(this, p_source);
+ _image_compress_bc_func(this, p_lossy_quality, p_source);
} break;
case COMPRESS_PVRTC2: {
@@ -2048,7 +2146,7 @@ ImageMemLoadFunc Image::_png_mem_loader_func = NULL;
ImageMemLoadFunc Image::_jpg_mem_loader_func = NULL;
ImageMemLoadFunc Image::_webp_mem_loader_func = NULL;
-void (*Image::_image_compress_bc_func)(Image *, Image::CompressSource) = NULL;
+void (*Image::_image_compress_bc_func)(Image *, float, Image::CompressSource) = NULL;
void (*Image::_image_compress_bptc_func)(Image *, float, Image::CompressSource) = NULL;
void (*Image::_image_compress_pvrtc2_func)(Image *) = NULL;
void (*Image::_image_compress_pvrtc4_func)(Image *) = NULL;
@@ -2569,7 +2667,7 @@ void Image::_bind_methods() {
BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL);
}
-void Image::set_compress_bc_func(void (*p_compress_func)(Image *, CompressSource)) {
+void Image::set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource)) {
_image_compress_bc_func = p_compress_func;
}
diff --git a/core/image.h b/core/image.h
index 854096b773..6af55ca8d9 100644
--- a/core/image.h
+++ b/core/image.h
@@ -126,7 +126,7 @@ public:
static ImageMemLoadFunc _jpg_mem_loader_func;
static ImageMemLoadFunc _webp_mem_loader_func;
- static void (*_image_compress_bc_func)(Image *, CompressSource p_source);
+ static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
static void (*_image_compress_pvrtc2_func)(Image *);
static void (*_image_compress_pvrtc4_func)(Image *);
@@ -316,7 +316,7 @@ public:
Rect2 get_used_rect() const;
Ref<Image> get_rect(const Rect2 &p_area) const;
- static void set_compress_bc_func(void (*p_compress_func)(Image *, CompressSource));
+ static void set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource));
static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource));
static String get_format_name(Format p_format);
diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp
index 75d9b8b311..84c9f0fca6 100644
--- a/core/math/vector2.cpp
+++ b/core/math/vector2.cpp
@@ -122,7 +122,7 @@ Vector2 Vector2::rotated(real_t p_by) const {
}
Vector2 Vector2::project(const Vector2 &p_b) const {
- return p_b * (dot(p_b) / p_b.dot(p_b));
+ return p_b * (dot(p_b) / p_b.length_squared());
}
Vector2 Vector2::snapped(const Vector2 &p_by) const {
diff --git a/core/math/vector3.h b/core/math/vector3.h
index a719e3965d..5f0e8919ff 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -241,7 +241,7 @@ real_t Vector3::distance_squared_to(const Vector3 &p_b) const {
}
Vector3 Vector3::project(const Vector3 &p_b) const {
- return p_b * (dot(p_b) / p_b.dot(p_b));
+ return p_b * (dot(p_b) / p_b.length_squared());
}
real_t Vector3::angle_to(const Vector3 &p_b) const {