Skip to content

Commit

Permalink
Make scalar plain C (#350)
Browse files Browse the repository at this point in the history
This removes the need for a C++ compiler to be installed.
It is much more likely for a C compiler to be installed on someone's Linux machine in comparison.
  • Loading branch information
Speykious authored Apr 11, 2024
1 parent 29af982 commit 633e6cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn main() {
} else if !env.contains("windows") && !env.contains("wasm32") {
// build scalar on non-windows and non-mac
cc::Build::new()
.file("src/native/posix/scalar.cpp")
.file("src/native/posix/scalar.c")
.opt_level(3) // always build with opts for scaler so it's fast in debug also
.compile("libscalar.a")
}
Expand Down
25 changes: 12 additions & 13 deletions src/native/posix/scalar.cpp → src/native/posix/scalar.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdint.h>
#include <stdio.h>

extern "C" void image_resize_linear(
void image_resize_linear(
uint32_t* dst,
const uint32_t dst_width,
const uint32_t dst_height,
Expand All @@ -10,8 +9,8 @@ extern "C" void image_resize_linear(
const uint32_t src_height,
const uint32_t src_stride
) {
const float x_ratio = float(src_width) / float(dst_width);
const float y_ratio = float(src_height) / float(dst_height);
const float x_ratio = (float)(src_width) / (float)(dst_width);
const float y_ratio = (float)(src_height) / (float)(dst_height);
const int step_x = x_ratio * 1024.0f;
const int step_y = y_ratio * 1024.0f;
int fixed_y = 0;
Expand Down Expand Up @@ -39,8 +38,8 @@ static void image_resize_linear_stride(
const uint32_t src_stride,
const uint32_t stride
) {
const float x_ratio = float(src_width) / float(dst_width);
const float y_ratio = float(src_height) / float(dst_height);
const float x_ratio = (float)(src_width) / (float)(dst_width);
const float y_ratio = (float)(src_height) / (float)(dst_height);
const int step_x = x_ratio * 1024.0f;
const int step_y = y_ratio * 1024.0f;
const int stride_step = stride - dst_width;
Expand All @@ -60,7 +59,7 @@ static void image_resize_linear_stride(
}
}

extern "C" void image_resize_linear_aspect_fill(
void image_resize_linear_aspect_fill(
uint32_t* dst,
const uint32_t dst_width,
const uint32_t dst_height,
Expand All @@ -75,19 +74,19 @@ extern "C" void image_resize_linear_aspect_fill(
dst[i] = bg_clear;
}

const float buffer_aspect = float(src_width) / float(src_height);
const float win_aspect = float(dst_width) / float(dst_height);
const float buffer_aspect = (float)(src_width) / (float)(src_height);
const float win_aspect = (float)(dst_width) / (float)(dst_height);

if (buffer_aspect > win_aspect) {
const uint32_t new_height = uint32_t(dst_width / buffer_aspect);
const uint32_t new_height = (uint32_t)(dst_width / buffer_aspect);
const int offset = (new_height - dst_height) / -2;
image_resize_linear(
dst + (offset * dst_width),
dst_width, new_height,
src, src_width, src_height, src_stride
);
} else {
const uint32_t new_width = uint32_t(dst_height * buffer_aspect);
const uint32_t new_width = (uint32_t)(dst_height * buffer_aspect);
const int offset = (new_width - dst_width) / -2;
image_resize_linear_stride(
dst + offset,
Expand All @@ -98,7 +97,7 @@ extern "C" void image_resize_linear_aspect_fill(
}
}

extern "C" void image_center(
void image_center(
uint32_t* dst,
const uint32_t dst_width,
const uint32_t dst_height,
Expand Down Expand Up @@ -173,7 +172,7 @@ extern "C" void image_center(
}
}

extern "C" void image_upper_left(
void image_upper_left(
uint32_t* dst,
const uint32_t dst_width,
const uint32_t dst_height,
Expand Down

0 comments on commit 633e6cd

Please sign in to comment.