Selaa lähdekoodia

Image and tests

Kajetan Johannes Hammerle 11 kuukautta sitten
vanhempi
commit
d039043a42
8 muutettua tiedostoa jossa 134 lisäystä ja 98 poistoa
  1. 5 4
      CMakeLists.txt
  2. 25 0
      include/core/Image.h
  3. 0 21
      include/core/ImageReader.h
  4. 48 0
      src/Image.c
  5. 0 40
      src/ImageReader.c
  6. 1 1
      tasks
  7. 0 32
      test/modules/ImageReaderTests.c
  8. 55 0
      test/modules/ImageTests.c

+ 5 - 4
CMakeLists.txt

@@ -4,14 +4,14 @@ project(gaming_core)
 set(CMAKE_C_STANDARD 23)
 
 set(SRC
-    "src/ImageReader.c"
+    "src/Image.c"
     "src/Network.c"
     "src/Window.c"
 )
 
 set(SRC_TESTS
     "test/Main.c"
-    "test/modules/ImageReaderTests.c"
+    "test/modules/ImageTests.c"
     "test/modules/NetworkTests.c"
 )
 
@@ -57,11 +57,12 @@ target_compile_definitions(gaming_core
     PRIVATE ${DEFINITIONS}
 )
 target_link_libraries(gaming_core 
-    PRIVATE core ${LINK_OPTIONS}
+    PRIVATE m core ${LINK_OPTIONS}
 )
 target_include_directories(gaming_core SYSTEM 
     PUBLIC ${CMAKE_INSTALL_PREFIX}/include
     PUBLIC enet/include
+    PUBLIC stb
 )
 target_link_directories(gaming_core SYSTEM 
     PUBLIC ${CMAKE_INSTALL_PREFIX}/lib
@@ -70,7 +71,7 @@ target_sources(gaming_core PUBLIC
     FILE_SET HEADERS
     BASE_DIRS include
     FILES 
-        ./include/core/ImageReader.h
+        ./include/core/Image.h
         ./include/core/Network.h
         ./include/core/Window.h
 )

+ 25 - 0
include/core/Image.h

@@ -0,0 +1,25 @@
+#ifndef CORE_IMAGE_H
+#define CORE_IMAGE_H
+
+#include <core/Types.h>
+
+typedef struct {
+    u8* data;
+    int width;
+    int height;
+    int channels;
+} CoreImage8;
+
+typedef struct {
+    u16* data;
+    int width;
+    int height;
+    int channels;
+} CoreImage16;
+
+bool coreInitImage8(CoreImage8* image, const char* path);
+void coreDestroyImage8(CoreImage8* image);
+bool coreInitImage16(CoreImage16* image, const char* path);
+void coreDestroyImage16(CoreImage16* image);
+
+#endif

+ 0 - 21
include/core/ImageReader.h

@@ -1,21 +0,0 @@
-#ifndef CORE_IMAGE_READER_H
-#define CORE_IMAGE_READER_H
-
-void readImage(void);
-/*namespace ImageReader {
-    class Image final {
-        static void clean(ColorChannel*& c);
-
-    public:
-        Image();
-        Cleaner<ColorChannel*, clean> data;
-        unsigned int width;
-        unsigned int height;
-        unsigned int channels;
-        unsigned int bitdepth;
-    };
-
-    Error load(Image& image, const char* path);
-}*/
-
-#endif

+ 48 - 0
src/Image.c

@@ -0,0 +1,48 @@
+#include "core/Image.h"
+
+#include <core/Utility.h>
+#define STB_IMAGE_IMPLEMENTATION
+#define STBI_ONLY_PNG
+#define STBI_MALLOC(n) coreAllocate(n)
+#define STBI_REALLOC(p, n) coreReallocate(p, n)
+#define STBI_FREE(p) coreFree(p)
+#include <core/Logger.h>
+#include <errno.h>
+#include <stb_image.h>
+#include <string.h>
+
+static void printError(const char* path) {
+    CORE_LOG_ERROR("Cannot read image '%s': %s", path, strerror(errno));
+}
+
+bool coreInitImage8(CoreImage8* image, const char* path) {
+    *image = (CoreImage8){0};
+    image->data =
+        stbi_load(path, &image->width, &image->height, &image->channels, 0);
+    if(image->data == nullptr) {
+        printError(path);
+        return true;
+    }
+    return false;
+}
+
+void coreDestroyImage8(CoreImage8* image) {
+    stbi_image_free(image->data);
+    *image = (CoreImage8){0};
+}
+
+bool coreInitImage16(CoreImage16* image, const char* path) {
+    *image = (CoreImage16){0};
+    image->data =
+        stbi_load_16(path, &image->width, &image->height, &image->channels, 0);
+    if(image->data == nullptr) {
+        printError(path);
+        return true;
+    }
+    return false;
+}
+
+void coreDestroyImage16(CoreImage16* image) {
+    stbi_image_free(image->data);
+    *image = (CoreImage16){0};
+}

+ 0 - 40
src/ImageReader.c

@@ -1,40 +0,0 @@
-#include "core/ImageReader.h"
-
-/*void ImageReader::Image::clean(ColorChannel*& c) {
-    free(c);
-}
-
-static void cleanRawData(ColorChannel*& c) {
-    free(c);
-}
-
-static void cleanState(LodePNGState& state) {
-    lodepng_state_cleanup(&state);
-}
-
-ImageReader::Image::Image() : data(nullptr) {
-}
-
-Error ImageReader::load(Image& image, const char* path) {
-    Cleaner<ColorChannel*, cleanRawData> rawData(nullptr);
-    size_t rawSize;
-    unsigned int error = lodepng_load_file(&rawData, &rawSize, path);
-    if(error) {
-        Error e{"cannot load file '"};
-        e.message.append(path).append("': ").append(lodepng_error_text(error));
-        return e;
-    }
-    Cleaner<LodePNGState, cleanState> state;
-    lodepng_state_init(&state);
-    state->decoder.color_convert = false;
-    error = lodepng_decode(&image.data, &image.width, &image.height, &state,
-                           rawData, rawSize);
-    if(error) {
-        Error e{"cannot decode file '"};
-        e.message.append(path).append("'").append(lodepng_error_text(error));
-        return e;
-    }
-    image.channels = lodepng_get_channels(&state->info_png.color);
-    image.bitdepth = state->info_png.color.bitdepth;
-    return {};
-}*/

+ 1 - 1
tasks

@@ -141,7 +141,7 @@ if $install; then
 fi
 
 function runTests() {
-    LLVM_PROFILE_FILE="default.profraw" $valgrind ./test wusi $valgrind || true
+    LLVM_PROFILE_FILE="default.profraw" $valgrind ./test ../test/resources $valgrind || true
 }
 
 if $test_debug; then

+ 0 - 32
test/modules/ImageReaderTests.c

@@ -1,32 +0,0 @@
-#include "../Tests.h"
-
-/*static void testReadPNG(Test& test, const char* path, const char* name,
-                        unsigned int channels, unsigned int bitdepth) {
-    ImageReader::Image image;
-    Error error =
-        ImageReader::load(image, String(path).append(name).append(".png"));
-    if(error.has()) {
-        test.checkEqual(false, true,
-                        String("read ").append(name).append(" error"));
-        return;
-    }
-    test.checkEqual(32u, image.width, String(name).append(" width"));
-    test.checkEqual(64u, image.height, String(name).append(" height"));
-    test.checkEqual(channels, image.channels, String(name).append(" channels"));
-    test.checkEqual(bitdepth, image.bitdepth, String(name).append(" bitdepth"));
-    test.checkEqual(true, image.data != nullptr, String(name).append(" data"));
-}*/
-
-void coreTestImageReader(const char* path) {
-    (void)path;
-    // Test test("ImageReader");
-    // testReadPNG(test, path, "rgb8", 3, 8);
-    // testReadPNG(test, path, "rgb16", 3, 16);
-    // testReadPNG(test, path, "rgba8", 4, 8);
-    // testReadPNG(test, path, "rgba16", 4, 16);
-    // testReadPNG(test, path, "gray8", 1, 8);
-    // testReadPNG(test, path, "gray16", 1, 16);
-    // testReadPNG(test, path, "graya8", 2, 8);
-    // testReadPNG(test, path, "graya16", 2, 16);
-    // test.finalize();
-}

+ 55 - 0
test/modules/ImageTests.c

@@ -0,0 +1,55 @@
+#include <stdio.h>
+
+#include "../Tests.h"
+#include "core/Image.h"
+
+static void testReadPNG8(const char* path, const char* name, int width,
+                         int height, int channels) {
+    char fullPath[512];
+    snprintf(fullPath, sizeof(fullPath), "%s/%s.png", path, name);
+    CoreImage8 image;
+    CORE_TEST_BOOL(width == 0, coreInitImage8(&image, fullPath));
+    if(width != 0) {
+        CORE_TEST_NOT_NULL(image.data);
+    }
+    CORE_TEST_INT(width, image.width);
+    CORE_TEST_INT(height, image.height);
+    CORE_TEST_INT(channels, image.channels);
+    coreDestroyImage8(&image);
+}
+
+static void testReadPNG16(const char* path, const char* name, int width,
+                          int height, int channels) {
+    char fullPath[512];
+    snprintf(fullPath, sizeof(fullPath), "%s/%s.png", path, name);
+    CoreImage16 image;
+    CORE_TEST_BOOL(width == 0, coreInitImage16(&image, fullPath));
+    if(width != 0) {
+        CORE_TEST_NOT_NULL(image.data);
+    }
+    CORE_TEST_INT(width, image.width);
+    CORE_TEST_INT(height, image.height);
+    CORE_TEST_INT(channels, image.channels);
+    coreDestroyImage16(&image);
+}
+
+void coreTestImageReader(const char* path) {
+    testReadPNG8(path, "rgb8", 32, 64, 3);
+    testReadPNG8(path, "rgb16", 32, 64, 3);
+    testReadPNG8(path, "rgba8", 32, 64, 4);
+    testReadPNG8(path, "rgba16", 32, 64, 4);
+    testReadPNG8(path, "gray8", 32, 64, 1);
+    testReadPNG8(path, "gray16", 32, 64, 1);
+    testReadPNG8(path, "graya8", 32, 64, 2);
+    testReadPNG8(path, "graya16", 32, 64, 2);
+    testReadPNG8(path, "nope", 0, 0, 0);
+    testReadPNG16(path, "rgb8", 32, 64, 3);
+    testReadPNG16(path, "rgb16", 32, 64, 3);
+    testReadPNG16(path, "rgba8", 32, 64, 4);
+    testReadPNG16(path, "rgba16", 32, 64, 4);
+    testReadPNG16(path, "gray8", 32, 64, 1);
+    testReadPNG16(path, "gray16", 32, 64, 1);
+    testReadPNG16(path, "graya8", 32, 64, 2);
+    testReadPNG16(path, "graya16", 32, 64, 2);
+    testReadPNG16(path, "nope", 0, 0, 0);
+}