Skip to content

Commit f82272e

Browse files
committed
new test code
1 parent ae20766 commit f82272e

File tree

1 file changed

+86
-8
lines changed

1 file changed

+86
-8
lines changed

.github/workflows/test-opencvsharp.yml

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,93 @@ jobs:
4646
4747
- name: Create test.c
4848
run: |
49-
mkdir test && cd test && echo '#include <stdio.h>
50-
int core_Mat_sizeof();
51-
int main()
49+
mkdir -p test
50+
cat > test/test.c <<'EOF'
51+
#include <stdio.h>
52+
#include <stdint.h>
53+
#include <stdlib.h>
54+
#include <assert.h>
55+
56+
/* ==== 简易类型 / 宏定义 ==== */
57+
typedef long long uint64; /* 对应 C# 的 UInt64 */
58+
typedef int ExceptionStatus; /* OpenCvSharpExtern 约定: 0 = OK */
59+
typedef struct { double val[4]; } MyCvScalar;
60+
typedef struct cv_Mat cv_Mat; /* 不需要知道内部实现 */
61+
62+
/* OpenCV 常量(直接抄一份足够用即可) */
63+
#define CV_8UC3 16 /* 8-bit unsigned, 3 channels */
64+
#define IMWRITE_JPEG_QUALITY 1
65+
66+
/* ==== C API 声明 ==== */
67+
/* core */
68+
extern uint64 core_Mat_sizeof(void);
69+
extern ExceptionStatus core_Mat_new3(int rows,int cols,int type, MyCvScalar scalar, cv_Mat **returnValue);
70+
extern ExceptionStatus core_Mat_delete(cv_Mat *self);
71+
/* imgcodecs */
72+
extern ExceptionStatus imgcodecs_imwrite(const char *filename, cv_Mat *img, int *params,int paramsLength,int *returnValue);
73+
extern ExceptionStatus imgcodecs_imread (const char *filename,int flags, cv_Mat **returnValue);
74+
75+
/* ==== 帮助函数:检查返回值 ==== */
76+
static void CHECK_OK(ExceptionStatus st, const char *msg)
5277
{
53-
int i = core_Mat_sizeof();
54-
printf("sizeof(Mat) = %d\n", i);
55-
return 0;
56-
}' > test.c
57-
cat test.c
78+
if(st != 0) { fprintf(stderr,"%s failed: %d\n", msg, st); exit(st); }
79+
}
80+
static void CHECK_BOOL(int ok, const char *msg)
81+
{
82+
if(!ok) { fprintf(stderr,"%s returned false\n", msg); exit(1); }
83+
}
84+
85+
int main(void)
86+
{
87+
printf("sizeof(Mat) reported by native = %llu\n",
88+
(unsigned long long)core_Mat_sizeof());
89+
90+
/* 1. 先创建一张 256x256、彩色渐变图像 ----------------------------- */
91+
cv_Mat *img = NULL;
92+
MyCvScalar black = {0};
93+
CHECK_OK(core_Mat_new3(256, 256, CV_8UC3, black, &img), "core_Mat_new3");
94+
95+
/* 通过指针手动绘制一个 BGR 渐变: B = x, G = y, R = 128 */
96+
unsigned char *data = NULL;
97+
extern ExceptionStatus core_Mat_data(cv_Mat *self, unsigned char **returnValue);
98+
CHECK_OK(core_Mat_data(img, &data), "core_Mat_data");
99+
for(int y = 0; y < 256; ++y)
100+
{
101+
for(int x = 0; x < 256; ++x)
102+
{
103+
unsigned char *pix = data + (y*256 + x)*3;
104+
pix[0] = (unsigned char)x; /* Blue 渐变 */
105+
pix[1] = (unsigned char)y; /* Green 渐变 */
106+
pix[2] = 128; /* Red 固定 */
107+
}
108+
}
109+
110+
/* 2. 保存为 PNG -------------------------------------------------- */
111+
int ok = 0;
112+
CHECK_OK(imgcodecs_imwrite("test.png", img, NULL, 0, &ok), "imwrite PNG");
113+
CHECK_BOOL(ok, "imwrite PNG");
114+
115+
/* 3. 读取刚写出的 PNG ------------------------------------------- */
116+
cv_Mat *img2 = NULL;
117+
CHECK_OK(imgcodecs_imread("test.png", /*flags=*/1, &img2), "imread PNG");
118+
/* 不需要检查 NULL, 因为 C API 出错会抛异常,已在上一步捕获 */
119+
120+
/* 4. 以 JPEG 质量 90 再保存为 JPG -------------------------------- */
121+
int jpegParams[2] = { IMWRITE_JPEG_QUALITY, 90 };
122+
CHECK_OK(imgcodecs_imwrite("test.jpg", img2,
123+
jpegParams, 2, &ok), "imwrite JPG");
124+
CHECK_BOOL(ok, "imwrite JPG");
125+
126+
printf("PNG and JPG written successfully.\n");
127+
128+
/* 5. 释放资源 ---------------------------------------------------- */
129+
core_Mat_delete(img);
130+
core_Mat_delete(img2);
131+
132+
return 0;
133+
}
134+
EOF
135+
cat test/test.c
58136
59137
- name: Setup MSVC(Windows only)
60138
uses: ilammy/msvc-dev-cmd@v1

0 commit comments

Comments
 (0)