From cb4e47dab6d286cc4ce650b83e51d95df155b150 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Wed, 1 May 2024 13:30:31 +0200 Subject: [PATCH] add tset get metadata from image --- src/kds_s2000w_image_converter.c | 33 ++++++++-- src/kds_s2000w_image_converter.h | 7 ++- tests/CMakeLists.txt | 21 +++++-- tests/kds_s2000w_image_converter_tests.c | 64 ++++++++++++++++++++ tests/kds_s2000w_image_converter_tests.h | 11 ++++ tests/kds_s2000w_image_converter_tests_run.c | 13 ++++ 6 files changed, 138 insertions(+), 11 deletions(-) create mode 100644 tests/kds_s2000w_image_converter_tests.c create mode 100644 tests/kds_s2000w_image_converter_tests.h create mode 100644 tests/kds_s2000w_image_converter_tests_run.c diff --git a/src/kds_s2000w_image_converter.c b/src/kds_s2000w_image_converter.c index ea4c375..0e01a1f 100644 --- a/src/kds_s2000w_image_converter.c +++ b/src/kds_s2000w_image_converter.c @@ -7,12 +7,6 @@ #include "kds_s2000w_image_converter.h" #include "kds_s2000w_debug.h" -void kds_s2000w_convert_terminate() -{ - debug_printf(ALL, "kds_s2000w_convert_terminate"); - MagickCoreTerminus(); -} - void kds_s2000w_convert_to_pnm_with_depth(blobdata* in, blobdata* out, int depth, @@ -72,4 +66,31 @@ void kds_s2000w_convert_tiff_to_pnm(blobdata* in, blobdata* out) const char* extension = "tiff"; debug_printf(ALL, "kds_s2000w_convert_tiff_to_pnm"); kds_s2000w_convert_to_pnm_with_depth(in, out, -1, extension); +} + +void kds_s2000w_metadata_from_image(blobdata* image, image_metadata* mdata) +{ + debug_printf(ALL, "kds_s2000w_metadata_from_image"); + ExceptionInfo* exception = NULL; + Image* input_image = NULL; + ImageInfo* image_info = NULL; + + exception = AcquireExceptionInfo(); + image_info = CloneImageInfo(NULL); + sprintf(image_info->filename, "image.pnm"); + + input_image = BlobToImage(image_info, image->data, image->size , exception); + + if (exception->severity != UndefinedException) { + fprintf(stderr, "%s\n", GetExceptionMessage(exception->error_number)); + CatchException(exception); + } + + mdata->width = input_image->columns; + mdata->height = input_image->rows; + mdata->depth = input_image->depth; + + DestroyImageInfo(image_info); + DestroyImage(input_image); + DestroyExceptionInfo(exception); } \ No newline at end of file diff --git a/src/kds_s2000w_image_converter.h b/src/kds_s2000w_image_converter.h index 3ab8b3d..01b47b6 100644 --- a/src/kds_s2000w_image_converter.h +++ b/src/kds_s2000w_image_converter.h @@ -6,8 +6,13 @@ typedef struct { void* data; } blobdata; -void kds_s2000w_convert_terminate(); +typedef struct { + int width; + int height; + int depth; +} image_metadata; void kds_s2000w_convert_jpg_to_pnm_with_depth(blobdata* in, blobdata* out, int depth); void kds_s2000w_convert_tiff_to_pnm(blobdata* in, blobdata* out); +void kds_s2000w_metadata_from_image(blobdata* image, image_metadata* mdata); #endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 945e2a7..27576f6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,24 +40,37 @@ TARGET_LINK_LIBRARIES("kds_s2000w_read_config_tests_run" ${CMOCKA_LIBRARY} sane-kds_s2000w_net) +ADD_EXECUTABLE("kds_s2000w_image_converter_tests_run" + "kds_s2000w_image_converter_tests_run.c" + "kds_s2000w_image_converter_tests.c" + "kds_s2000w_client_mock.c") +ADD_DEPENDENCIES("kds_s2000w_image_converter_tests_run" sane-kds_s2000w_net) +TARGET_LINK_LIBRARIES("kds_s2000w_image_converter_tests_run" + ${CMOCKA_LIBRARY} + sane-kds_s2000w_net) + INCLUDE(CTest) ENABLE_TESTING() ADD_TEST(NAME "kds_s2000w_net_get_opt_tests_run" - COMMAND "./kds_s2000w_net_get_opt_tests_run") + COMMAND "kds_s2000w_net_get_opt_tests_run") ADD_TEST(NAME "kds_s2000w_net_read_tests_run" - COMMAND "./kds_s2000w_net_read_tests_run") + COMMAND "kds_s2000w_net_read_tests_run") ADD_TEST(NAME "kds_s2000w_net_tests_run" COMMAND "kds_s2000w_net_tests_run") ADD_TEST(NAME "kds_s2000w_read_config_tests_run" COMMAND "kds_s2000w_read_config_tests_run") +ADD_TEST(NAME "kds_s2000w_converter_tests_run" + COMMAND "kds_s2000w_image_converter_tests_run") ADD_CUSTOM_TARGET("runningtests" ALL "ctest" "--verbose" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - DEPENDS "kds_s2000w_read_config_tests_run" + DEPENDS "kds_s2000w_net_get_opt_tests_run" + "kds_s2000w_net_read_tests_run" "kds_s2000w_net_tests_run" - "kds_s2000w_net_read_tests_run") + "kds_s2000w_read_config_tests_run" + "kds_s2000w_image_converter_tests_run") ADD_CUSTOM_TARGET("generate_gcov_coverage" ALL gcov ${CMAKE_BINARY_DIR}/src/CMakeFiles/sane-kds_s2000w_net.dir/*.c.o diff --git a/tests/kds_s2000w_image_converter_tests.c b/tests/kds_s2000w_image_converter_tests.c new file mode 100644 index 0000000..f34de5a --- /dev/null +++ b/tests/kds_s2000w_image_converter_tests.c @@ -0,0 +1,64 @@ +#include +#include "../src/kds_s2000w_config.h" +#include "../src/kds_s2000w_image_converter.h" +#include "kds_s2000w_client_mock.h" +#include "kds_s2000w_image_converter_tests.h" + +void __wrap_load_config(program_config* config, const char* config_stream) +{ + config->scanner_url = malloc(sizeof(char) * 28); + config->username = malloc(sizeof(char) * 4); + + config->scanner_url = "https://scanner.example.com\0"; + config->username = "Max\0"; +} + +void __wrap_kds_s2000w_convert_tiff_to_pnm(blobdata* in, blobdata* out) +{ + return; +} + +void __wrap_wait_a_second() +{ + return; +} + + +void get_metadata_from_image_test() +{ + blobdata* image = malloc(sizeof(blobdata)); + image->data = malloc(sizeof(char) * 17); + image->size = 17; + char* image_data = (char*) image->data; + image_data[0] = 0x50; + image_data[1] = 0x36; + image_data[2] = 0x0a; + image_data[3] = 0x32; + image_data[4] = 0x20; + image_data[5] = 0x31; + image_data[6] = 0x0a; + image_data[7] = 0x32; + image_data[8] = 0x35; + image_data[9] = 0x35; + image_data[10] = 0x0a; + for (int i = 11; i < 17; i++) { + image_data[i] = 0xff; + } + image_metadata* mdata = malloc(sizeof(image_metadata)); + mdata->width = 0; + mdata->height = 0; + mdata->depth = 0; + + kds_s2000w_metadata_from_image(image, mdata); + + assert_int_equal(mdata->width, 2); + assert_int_equal(mdata->height, 1); + assert_int_equal(mdata->depth, 8); + + free(image->data); + image->data = NULL; + free(image); + image = NULL; + free(mdata); + mdata = NULL; +} \ No newline at end of file diff --git a/tests/kds_s2000w_image_converter_tests.h b/tests/kds_s2000w_image_converter_tests.h new file mode 100644 index 0000000..6522e3d --- /dev/null +++ b/tests/kds_s2000w_image_converter_tests.h @@ -0,0 +1,11 @@ +#ifndef KDS_S2000W_IMAGE_CONVERTER_TESTS_H +#define KDS_S2000W_IMAGE_CONVERTER_TESTS_H +#include +#include +#include +#include +#include + +void get_metadata_from_image_test(); + +#endif \ No newline at end of file diff --git a/tests/kds_s2000w_image_converter_tests_run.c b/tests/kds_s2000w_image_converter_tests_run.c new file mode 100644 index 0000000..048b343 --- /dev/null +++ b/tests/kds_s2000w_image_converter_tests_run.c @@ -0,0 +1,13 @@ +#include +#include +#include "kds_s2000w_client_mock.h" +#include "kds_s2000w_image_converter_tests.h" + +int main() +{ + const struct CMUnitTest converter_tests[] = { + cmocka_unit_test(get_metadata_from_image_test) + }; + + return cmocka_run_group_tests(converter_tests, NULL, NULL); +} \ No newline at end of file -- 2.39.5