Преглед изворни кода

feat: add OOP polymorphism to image library

lujw2 пре 11 месеци
комит
a7ef917033
9 измењених фајлова са 230 додато и 0 уклоњено
  1. 3 0
      .gitignore
  2. 24 0
      CMakeLists.txt
  3. 33 0
      include/image_lib.h
  4. 12 0
      src/image_interface.h
  5. 25 0
      src/image_lib.c
  6. 52 0
      src/jpg.c
  7. 52 0
      src/png.c
  8. 13 0
      test/CMakeLists.txt
  9. 16 0
      test/test.c

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+build*/
+.cache/
+.clang-format

+ 24 - 0
CMakeLists.txt

@@ -0,0 +1,24 @@
+cmake_minimum_required(VERSION 3.16)
+project(lib_test)
+# src/CMakeLists.txt
+# 添加库
+add_library(c_lib ./src/image_lib.c ./src/jpg.c ./src/png.c)
+# add_library(cpp_lib ./src/image_lib.cpp)
+
+# 指定头文件路径
+target_include_directories(c_lib PUBLIC ./include)
+# target_include_directories(cpp_lib PUBLIC ./include)
+
+
+# 设置C接口编译选项,保证C++代码可以暴露C接口
+# set_target_properties(image_lib PROPERTIES
+#     CXX_STANDARD 11
+#     CXX_STANDARD_REQUIRED YES
+# )
+
+# 启用测试
+enable_testing()
+
+if(BUILD_TESTING)
+    add_subdirectory(test)
+endif()

+ 33 - 0
include/image_lib.h

@@ -0,0 +1,33 @@
+// image_lib.h
+#ifndef IMAGE_LIB_H
+#define IMAGE_LIB_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// 定义放大、缩小、转换格式、销毁等操作的函数指针
+typedef struct Image Image;
+
+// 创建图像对象并初始化函数指针
+Image* jpg_create(const char* file_path);
+Image* png_create(const char* file_path);
+
+// 销毁图像对象
+void image_destroy(Image* img);
+
+// 放大图像
+int image_zoom_in(Image* img, double scale);
+
+// 缩小图像
+int image_zoom_out(Image* img, double scale);
+
+// 转换图像格式
+int image_convert_format(Image* img, const char* new_format);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // IMAGE_LIB_H

+ 12 - 0
src/image_interface.h

@@ -0,0 +1,12 @@
+#ifndef IMAGE_INTERFACE_H
+#define IMAGE_INTERFACE_H
+#include "image_lib.h"
+
+struct Image
+{
+    int (*zoom_in)(struct Image* img, double scale);
+    int (*zoom_out)(struct Image* img, double scale);
+    int (*convert_format)(struct Image* img, const char* new_format);
+    void (*destroy)(struct Image* img);
+};
+#endif  // IMAGE_INTERFACE_H

+ 25 - 0
src/image_lib.c

@@ -0,0 +1,25 @@
+#include "image_interface.h"
+
+// 销毁图像对象
+void image_destroy(Image* img) {
+    if (!img) return;
+    img->destroy(img);
+}
+
+// 放大图像
+int image_zoom_in(Image* img, double scale) {
+    img->zoom_in(img, scale);
+    return 0;
+}
+
+// 缩小图像
+int image_zoom_out(Image* img, double scale) {
+    img->zoom_out(img, scale);
+    return 0;
+}
+
+// 转换图像格式
+int image_convert_format(Image* img, const char* new_format) {
+    img->convert_format(img, new_format);
+    return 0;
+}

+ 52 - 0
src/jpg.c

@@ -0,0 +1,52 @@
+#include "image_lib.h"
+#include "image_interface.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct Jpg
+{
+    Image base;
+    char* type;
+} Jpg;
+
+// 具体操作函数
+
+static int jpg_zoom_in(Image* image, double scale) {
+    Jpg* self = (Jpg*)image;
+    printf("Zooming in by %f on jpg: %s\n", scale, self->type);
+    return 0;  // 返回成功
+}
+
+static int jpg_zoom_out(Image* image, double scale) {
+    Jpg* self = (Jpg*)image;
+    printf("Zooming out by %f on jpg: %s\n", scale, self->type);
+    return 0;
+}
+
+static int jpg_convert_format(Image* image, const char* new_format) {
+    Jpg* self = (Jpg*)image;
+    printf("Converting jpg: %s to format: %s\n", self->type, new_format);
+    return 0;
+}
+
+static void jpg_destroy(Image* image) {
+    Jpg* self = (Jpg*)image;
+    if (self) {
+        printf("Destroying : %s\n", self->type);
+        free(self);  // 释放内部结构体的内存
+    }
+}
+
+Image* jpg_create(const char* file_path) {
+    Jpg* jpg = (Jpg*)malloc(sizeof(Jpg));
+    if (jpg == NULL) {
+        printf("Failed to allocate memory for jpg\n");
+        return NULL;
+    }
+    jpg->base.destroy = jpg_destroy;
+    jpg->base.zoom_in = jpg_zoom_in;
+    jpg->base.zoom_out = jpg_zoom_out;
+    jpg->base.convert_format = jpg_convert_format;
+    jpg->type = "jpg";
+    return (Image*)jpg;
+}

+ 52 - 0
src/png.c

@@ -0,0 +1,52 @@
+#include "image_lib.h"
+#include "image_interface.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef struct Png
+{
+    Image base;
+    char* type;
+} Png;
+
+// 具体操作函数
+
+static int png_zoom_in(Image* image, double scale) {
+    Png* self = (Png*)image;
+    printf("Zooming in by %f on png: %s\n", scale, self->type);
+    return 0;  // 返回成功
+}
+
+static int png_zoom_out(Image* image, double scale) {
+    Png* self = (Png*)image;
+    printf("Zooming out by %f on png: %s\n", scale, self->type);
+    return 0;
+}
+
+static int png_convert_format(Image* image, const char* new_format) {
+    Png* self = (Png*)image;
+    printf("Converting png: %s to format: %s\n", self->type, new_format);
+    return 0;
+}
+
+static void png_destroy(Image* image) {
+    Png* self = (Png*)image;
+    if (self) {
+        printf("Destroying : %s\n", self->type);
+        free(self);  // 释放内部结构体的内存
+    }
+}
+
+Image* png_create(const char* file_path) {
+    Png* png = (Png*)malloc(sizeof(Png));
+    if (png == NULL) {
+        printf("Failed to allocate memory for png\n");
+        return NULL;
+    }
+    png->base.destroy = png_destroy;
+    png->base.zoom_in = png_zoom_in;
+    png->base.zoom_out = png_zoom_out;
+    png->base.convert_format = png_convert_format;
+    png->type = "png";
+    return (Image*)png;
+}

+ 13 - 0
test/CMakeLists.txt

@@ -0,0 +1,13 @@
+# tests/CMakeLists.txt
+# 添加可执行文件进行测试
+add_executable(c_lib_test test.c)
+# add_executable(cpp_lib_test test.c)
+
+# 链接测试文件与库
+target_link_libraries(c_lib_test
+    c_lib)
+# target_link_libraries(cpp_lib_test
+#     cpp_lib)
+
+# 添加测试
+add_test(NAME ImageLibTest COMMAND test_image_lib)

+ 16 - 0
test/test.c

@@ -0,0 +1,16 @@
+#include "image_lib.h"
+
+// 主函数
+int main() {
+    Image* jpg = jpg_create("test_image.jpg");
+    Image* png = png_create("test_image.png");
+
+    image_zoom_in(jpg, 2.0);
+    image_zoom_out(jpg, 0.5);
+    image_destroy(jpg);
+    image_zoom_in(png, 2.0);
+    image_zoom_out(png, 0.5);
+    image_destroy(png);
+    return 0;
+}
+