Skip to content

test complex test.c #31

test complex test.c

test complex test.c #31

name: Test-OpenCvSharp
on:
push:
branches:
- main
- test/glibc
paths:
.github/workflows/test-opencvsharp.yml
workflow_dispatch:
jobs:
test:
name: Test OpenCvSharp
runs-on: ${{ matrix.runs-on }}
defaults:
run:
shell: bash
strategy:
matrix:
include:
- { os: linux, arch: x64, runs-on: ubuntu-22.04 }
- { os: linux, arch: x64, runs-on: ubuntu-24.04 }
steps:
- name: Download OpenCvSharp Artifacts
env:
GH_TOKEN: ${{ github.token }}
run: |
echo "Fetching lastest run of opencvsharp.yml on branch main"
RUN_ID=$(gh run list -R ${{ github.repository }} --workflow=opencvsharp.yml --branch=main --status=success --limit=1 --json databaseId | jq -r '.[0].databaseId')
echo "Latest opencvsharp run ID: $RUN_ID"
echo "Downloading artifact 'opencvsharp-${{ matrix.os }}-${{ matrix.arch }}' from run ${RUN_ID}"
gh run download -R ${{ github.repository }} $RUN_ID --name opencvsharp-${{ matrix.os }}-${{ matrix.arch }} --dir opencvsharp
echo "::group::OpenCvSharp Artifacts"
ls -lR opencvsharp
echo "::endgroup::"
- name: Create test.c
run: |
mkdir test && cd test
cat > test.c << 'EOF'
#include <stdio.h>
#include <stdint.h>
/* ========== OpenCvSharpExtern exported C prototypes ========== */
typedef struct Mat Mat;
typedef struct { double val[4]; } Scalar;
typedef struct { int x; int y; } Point;
/* core */
int core_Mat_sizeof(void);
Mat* core_Mat_new2(int rows, int cols, int type);
void core_Mat_delete(Mat* mat);
uint8_t* core_Mat_data(Mat* mat);
int core_Mat_rows(Mat* mat);
int core_Mat_cols(Mat* mat);
int core_Mat_type(Mat* mat);
/* imgcodecs */
int imgcodecs_imwrite(const char* fileName, Mat* mat,
const int* params, int paramsLength);
/* ========== Convenience macro (same数值和 OpenCV 保持一致) ========== */
#define CV_8U 0
#define CV_CN_SHIFT 3
#define CV_MAKETYPE(depth, cn) ( (depth) + ( (cn - 1) << CV_CN_SHIFT ) )
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
int main(void)
{
printf("sizeof(cv::Mat) reported by core_Mat_sizeof = %d bytes\n",
core_Mat_sizeof());
/* 1. 创建 100×100×3 uchar 图像 */
Mat* img = core_Mat_new2(100, 100, CV_8UC3);
if (!img) {
fprintf(stderr, "core_Mat_new2 failed\n");
return 1;
}
/* 2. 填充像素:让 B=行, G=列, R=(行+列)/2 */
uint8_t* data = core_Mat_data(img);
int rows = core_Mat_rows(img);
int cols = core_Mat_cols(img);
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
int idx = (r * cols + c) * 3;
data[idx + 0] = (uint8_t)r; /* B */
data[idx + 1] = (uint8_t)c; /* G */
data[idx + 2] = (uint8_t)((r + c) >> 1); /* R */
}
}
/* 3. 写入 PNG 文件 */
const char* outName = "test-output.png";
int ok = imgcodecs_imwrite(outName, img, NULL, 0);
printf("imgcodecs_imwrite(\"%s\") returned %d\n", outName, ok);
/* 4. 清理 */
core_Mat_delete(img);
return ok ? 0 : 1;
}
EOF
cat test.c
- name: Setup MSVC(Windows only)
uses: ilammy/msvc-dev-cmd@v1
if: startsWith(matrix.os, 'win')
with:
arch: ${{ matrix.arch }}
- name: Build Test
run: |
cd test
if [[ "${{ matrix.os }}" == win* ]]; then
export MSYS2_ARG_CONV_EXCL="*"
cl /nologo /EHsc test.c ../opencvsharp/lib/OpenCvSharpExtern.lib /link /OUT:test.exe
else
# For x64 architecture
gcc test.c -L../opencvsharp/lib -lOpenCvSharpExtern -o test.exe
fi
ls -lR
- name: Run Test
run: |
if [[ "${{ matrix.os }}" == osx* ]]; then
export DYLD_LIBRARY_PATH=${{ github.workspace }}/opencvsharp/lib:$DYLD_LIBRARY_PATH
elif [[ "${{ matrix.os }}" == win* ]]; then
export PATH="$(cygpath -u "${{ github.workspace }}/opencvsharp/lib")":$PATH
else
export LD_LIBRARY_PATH=${{ github.workspace }}/opencvsharp/lib:$LD_LIBRARY_PATH
fi
cd test && ./test.exe
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: test-${{ matrix.os }}-${{ matrix.arch }}
path: test