Skip to content

Commit

Permalink
Update C api
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerstafford committed Jun 2, 2024
1 parent 451ae18 commit c6bb170
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 17 deletions.
43 changes: 26 additions & 17 deletions library/public/image_c_api.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef F3D_C_API_H
#define F3D_C_API_H

#include "export.h" // Ensure F3D_EXPORT is defined

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -19,23 +21,30 @@ typedef struct {

typedef struct f3d_image f3d_image_t;

f3d_image_t* f3d_image_new(void);
void f3d_image_delete(f3d_image_t* img);
void f3d_image_set_resolution(f3d_image_t* img, unsigned int width, unsigned int height);
unsigned int f3d_image_get_width(f3d_image_t* img);
unsigned int f3d_image_get_height(f3d_image_t* img);
unsigned int f3d_image_get_channel_count(f3d_image_t* img);
unsigned int f3d_image_get_channel_type(f3d_image_t* img);
unsigned int f3d_image_get_channel_type_size(f3d_image_t* img);
void f3d_image_set_content(f3d_image_t* img, void* buffer);
void* f3d_image_get_content(f3d_image_t* img);
int f3d_image_compare(f3d_image_t* img, f3d_image_t* reference, double threshold, f3d_image_t* diff, double* error);
void f3d_image_save(f3d_image_t* img, const char* path, int format);
unsigned char* f3d_image_save_buffer(f3d_image_t* img, int format, unsigned int* size);
const char* f3d_image_to_terminal_text(f3d_image_t* img);
void f3d_image_set_metadata(f3d_image_t* img, const char* key, const char* value);
const char* f3d_image_get_metadata(f3d_image_t* img, const char* key);
char** f3d_image_all_metadata(f3d_image_t* img, unsigned int* count);
F3D_EXPORT f3d_image_t* f3d_image_new(void);
F3D_EXPORT void f3d_image_delete(f3d_image_t* img);
F3D_EXPORT void f3d_image_set_resolution(f3d_image_t* img, unsigned int width, unsigned int height);
F3D_EXPORT unsigned int f3d_image_get_width(f3d_image_t* img);
F3D_EXPORT unsigned int f3d_image_get_height(f3d_image_t* img);
F3D_EXPORT unsigned int f3d_image_get_channel_count(f3d_image_t* img);
F3D_EXPORT unsigned int f3d_image_get_channel_type(f3d_image_t* img);
F3D_EXPORT unsigned int f3d_image_get_channel_type_size(f3d_image_t* img);
F3D_EXPORT void f3d_image_set_content(f3d_image_t* img, void* buffer);
F3D_EXPORT void* f3d_image_get_content(f3d_image_t* img);
F3D_EXPORT int f3d_image_compare(f3d_image_t* img, f3d_image_t* reference, double threshold, f3d_image_t* diff, double* error);
F3D_EXPORT void f3d_image_save(f3d_image_t* img, const char* path, int format);
F3D_EXPORT unsigned char* f3d_image_save_buffer(f3d_image_t* img, int format, unsigned int* size);
F3D_EXPORT const char* f3d_image_to_terminal_text(f3d_image_t* img);
F3D_EXPORT void f3d_image_set_metadata(f3d_image_t* img, const char* key, const char* value);
F3D_EXPORT const char* f3d_image_get_metadata(f3d_image_t* img, const char* key);
F3D_EXPORT char** f3d_image_all_metadata(f3d_image_t* img, unsigned int* count);
F3D_EXPORT void f3d_image_free_metadata_keys(char** keys, unsigned int count);
F3D_EXPORT f3d_image_t* f3d_image_create_from_file(const char* path);
F3D_EXPORT f3d_image_t* f3d_image_create_with_params(unsigned int width, unsigned int height, unsigned int channelCount, unsigned int type);
F3D_EXPORT unsigned int f3d_image_get_supported_formats_count();
F3D_EXPORT const char** f3d_image_get_supported_formats();
F3D_EXPORT double* f3d_image_get_normalized_pixel(f3d_image_t* img, int x, int y, unsigned int* count);
F3D_EXPORT void f3d_image_free_normalized_pixel(double* pixel);

#ifdef __cplusplus
}
Expand Down
59 changes: 59 additions & 0 deletions library/src/image_c_api.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "image_c_api.h"
#include "image.h"
#include <cstring>

struct f3d_image {
f3d::image img;
Expand Down Expand Up @@ -105,3 +106,61 @@ char** f3d_image_all_metadata(f3d_image_t* img, unsigned int* count) {
}
return keys;
}

void f3d_image_free_metadata_keys(char** keys, unsigned int count) {
for (unsigned int i = 0; i < count; ++i) {
delete[] keys[i];
}
delete[] keys;
}

// Additional functions to match all functionalities from image.cxx
f3d_image_t* f3d_image_create_from_file(const char* path) {
return new f3d_image_t{ f3d::image(path) };
}

f3d_image_t* f3d_image_create_with_params(unsigned int width, unsigned int height, unsigned int channelCount, unsigned int type) {
f3d::image::ChannelType channel_type;
switch (type) {
case 0:
channel_type = f3d::image::ChannelType::BYTE;
break;
case 1:
channel_type = f3d::image::ChannelType::SHORT;
break;
case 2:
channel_type = f3d::image::ChannelType::FLOAT;
break;
default:
channel_type = f3d::image::ChannelType::BYTE; // Default to BYTE
break;
}
return new f3d_image_t{ f3d::image(width, height, channelCount, channel_type) };
}

unsigned int f3d_image_get_supported_formats_count() {
std::vector<std::string> formats = f3d::image::getSupportedFormats();
return formats.size();
}

const char** f3d_image_get_supported_formats() {
static std::vector<std::string> formats = f3d::image::getSupportedFormats();
static std::vector<const char*> c_formats;
c_formats.clear();
for (const auto& format : formats) {
c_formats.push_back(format.c_str());
}
return c_formats.data();
}

double* f3d_image_get_normalized_pixel(f3d_image_t* img, int x, int y, unsigned int* count) {
std::vector<double> pixel = img->img.getNormalizedPixel({x, y});
*count = pixel.size();
double* c_pixel = new double[pixel.size()];
std::copy(pixel.begin(), pixel.end(), c_pixel);
return c_pixel;
}

void f3d_image_free_normalized_pixel(double* pixel) {
delete[] pixel;
}
29 changes: 29 additions & 0 deletions library/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,32 @@ endif()

# make sure the libf3d API is compatible with C++11
set_target_properties(libf3dSDKTests PROPERTIES CXX_STANDARD 11)

# C API integration

# Include the C API files
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

add_library(f3d_c_api SHARED f3d_c_api.c)

target_link_libraries(f3d_c_api PRIVATE f3d::libf3d)

# Ensure public headers are installed
install(FILES include/image_c_api.h DESTINATION include)

# Add C test
add_executable(test_image_c_api f3d/library/testing/test_image_c_api.c)
target_link_libraries(test_image_c_api PRIVATE f3d_c_api)
add_test(NAME test_image_c_api COMMAND test_image_c_api)

list(APPEND libf3dSDKTestsNoRender_list
test_image_c_api)

foreach (test ${libf3dSDKTests_list})
get_filename_component (TName ${test} NAME_WE)
add_test (NAME libf3d::${TName} COMMAND libf3dSDKTests ${TName} "${F3D_SOURCE_DIR}/testing/" "${CMAKE_BINARY_DIR}/Testing/Temporary/" "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
set_tests_properties(libf3d::${TName} PROPERTIES TIMEOUT 30)
if (NOT F3D_TESTING_ENABLE_RENDERING_TESTS AND NOT ${TName} IN_LIST libf3dSDKTestsNoRender_list)
set_tests_properties(libf3d::${TName} PROPERTIES DISABLED ON)
endif ()
endforeach ()

0 comments on commit c6bb170

Please sign in to comment.