Browse Source

Fix more corner cases in olivec_copy()

rexim 3 years ago
parent
commit
16588b8895
9 changed files with 31 additions and 1 deletions
  1. 0 1
      build.sh
  2. 8 0
      olive.c
  3. 23 0
      test.c
  4. BIN
      test/copy_empty_rect_expected.png
  5. BIN
      test/copy_null_src_expected.png
  6. BIN
      wasm/3d.wasm
  7. BIN
      wasm/squish.wasm
  8. BIN
      wasm/triangle.wasm
  9. BIN
      wasm/triangle3d.wasm

+ 0 - 1
build.sh

@@ -22,7 +22,6 @@ mkdir -p ./build/assets/
 
 # Build tests
 clang $COMMON_CFLAGS -fsanitize=memory -o ./build/test -Ithirdparty test.c -lm
-exit 0
 
 # Build VC demos
 build_vc_demo triangle &

+ 8 - 0
olive.c

@@ -704,8 +704,16 @@ OLIVECDEF void olivec_text(Olivec_Canvas oc, const char *text, int tx, int ty, O
 }
 
 // TODO: bilinear interpolation for olivec_copy
+// TODO: dst must come before src, 'cause otherwise it's inconsistent with the rest of the functions
 OLIVECDEF void olivec_copy(Olivec_Canvas src, Olivec_Canvas dst, int x, int y, int w, int h)
 {
+    if (src.width == 0) return;
+    if (src.height == 0) return;
+
+    // No need to render empty rectangle
+    if (w == 0) return;
+    if (h == 0) return;
+
     int ox1 = x;
     int oy1 = y;
 

+ 23 - 0
test.c

@@ -523,6 +523,27 @@ Olivec_Canvas test_empty_rect(void)
     return dst;
 }
 
+Olivec_Canvas test_copy_empty_rect(void)
+{
+    size_t w = 8;
+    size_t h = 8;
+    Olivec_Canvas dst = canvas_alloc(w, h);
+    Olivec_Canvas src = olivec_canvas(png, png_width, png_height, png_width);
+    olivec_fill(dst, BACKGROUND_COLOR);
+    olivec_copy(src, dst, 0, 0, 0, 0);
+    return dst;
+}
+
+Olivec_Canvas test_copy_null_src(void)
+{
+    size_t w = 8;
+    size_t h = 8;
+    Olivec_Canvas dst = canvas_alloc(w, h);
+    olivec_fill(dst, BACKGROUND_COLOR);
+    olivec_copy(OLIVEC_CANVAS_NULL, dst, 0, 0, w, h);
+    return dst;
+}
+
 Test_Case test_cases[] = {
     DEFINE_TEST_CASE(fill_rect),
     DEFINE_TEST_CASE(fill_circle),
@@ -540,7 +561,9 @@ Test_Case test_cases[] = {
     DEFINE_TEST_CASE(copy_out_of_bounds_cut),
     DEFINE_TEST_CASE(copy_flip),
     DEFINE_TEST_CASE(copy_flip_cut),
+    DEFINE_TEST_CASE(copy_empty_rect),
     DEFINE_TEST_CASE(empty_rect),
+    DEFINE_TEST_CASE(copy_null_src),
 };
 #define TEST_CASES_COUNT (sizeof(test_cases)/sizeof(test_cases[0]))
 

BIN
test/copy_empty_rect_expected.png


BIN
test/copy_null_src_expected.png


BIN
wasm/3d.wasm


BIN
wasm/squish.wasm


BIN
wasm/triangle.wasm


BIN
wasm/triangle3d.wasm