Skip to content

Commit c5d2033

Browse files
committed
Merge pull request godotengine#109123 from beicause/fix-img-nearest-cubic-resize-bias
Fix `Image` nearest and cubic resizing bias
2 parents 4200cea + a32b596 commit c5d2033

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

core/io/image.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -801,24 +801,21 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_
801801

802802
for (uint32_t y = 0; y < p_dst_height; y++) {
803803
// Y coordinates
804-
oy = (double)y * yfac - 0.5f;
804+
oy = (double)(y + 0.5) * yfac - 0.5;
805805
oy1 = (int)oy;
806806
dy = oy - (double)oy1;
807807

808808
for (uint32_t x = 0; x < p_dst_width; x++) {
809809
// X coordinates
810-
ox = (double)x * xfac - 0.5f;
810+
ox = (double)(x + 0.5) * xfac - 0.5;
811811
ox1 = (int)ox;
812812
dx = ox - (double)ox1;
813813

814814
// initial pixel value
815815

816816
T *__restrict dst = ((T *)p_dst) + (y * p_dst_width + x) * CC;
817817

818-
double color[CC];
819-
for (int i = 0; i < CC; i++) {
820-
color[i] = 0;
821-
}
818+
double color[CC] = {};
822819

823820
for (int n = -1; n < 3; n++) {
824821
// get Y coefficient
@@ -961,11 +958,11 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict
961958
template <int CC, typename T>
962959
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) {
963960
for (uint32_t i = 0; i < p_dst_height; i++) {
964-
uint32_t src_yofs = i * p_src_height / p_dst_height;
961+
uint32_t src_yofs = (i + 0.5) * p_src_height / p_dst_height;
965962
uint32_t y_ofs = src_yofs * p_src_width * CC;
966963

967964
for (uint32_t j = 0; j < p_dst_width; j++) {
968-
uint32_t src_xofs = j * p_src_width / p_dst_width;
965+
uint32_t src_xofs = (j + 0.5) * p_src_width / p_dst_width;
969966
src_xofs *= CC;
970967

971968
for (uint32_t l = 0; l < CC; l++) {

0 commit comments

Comments
 (0)