Last active
August 7, 2021 18:33
-
-
Save edubart/a59e33adda085e4b474632fc82c79fe0 to your computer and use it in GitHub Desktop.
Example using OpenCV with Nelua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Generated by Nelua 0.2.0-dev */ | |
/* Compile command: g++ -x c++ "/home/bart/projects/nelua/nelua-lang/nelua_cache/play/draft.c" -o "/home/bart/projects/nelua/nelua-lang/nelua_cache/play/draft" -fwrapv -g -Wall -Wextra -I/usr/include/opencv4 -DWITH_OPENEXR=OFF -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lm */ | |
/* Compile hash: 2oc8quwtnUC8s47mULrq1Bsh5u2U */ | |
/* ------------------------------ DIRECTIVES -------------------------------- */ | |
/* Disable some warnings that the generated code can trigger. */ | |
#if defined(__clang__) | |
#pragma clang diagnostic ignored "-Wtype-limits" | |
#pragma clang diagnostic ignored "-Wwrite-strings" | |
#pragma clang diagnostic ignored "-Wunused" | |
#pragma clang diagnostic ignored "-Wunused-parameter" | |
#pragma clang diagnostic ignored "-Wmissing-field-initializers" | |
#pragma clang diagnostic ignored "-Wparentheses-equality" | |
#ifndef __cplusplus | |
#pragma clang diagnostic ignored "-Wmissing-braces" | |
#pragma clang diagnostic ignored "-Wincompatible-pointer-types" | |
#pragma clang diagnostic error "-Wimplicit-function-declaration" | |
#pragma clang diagnostic error "-Wimplicit-int" | |
#else | |
#pragma clang diagnostic ignored "-Wnarrowing" | |
#endif | |
#elif defined(__GNUC__) | |
#pragma GCC diagnostic ignored "-Wtype-limits" | |
#pragma GCC diagnostic ignored "-Wwrite-strings" | |
#pragma GCC diagnostic ignored "-Wunused-parameter" | |
#pragma GCC diagnostic ignored "-Wunused-value" | |
#pragma GCC diagnostic ignored "-Wunused-variable" | |
#pragma GCC diagnostic ignored "-Wunused-function" | |
#pragma GCC diagnostic ignored "-Wunused-but-set-variable" | |
#pragma GCC diagnostic ignored "-Wmissing-field-initializers" | |
#ifndef __cplusplus | |
#pragma GCC diagnostic ignored "-Wmissing-braces" | |
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types" | |
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers" | |
#pragma GCC diagnostic error "-Wimplicit-function-declaration" | |
#pragma GCC diagnostic error "-Wimplicit-int" | |
#else | |
#pragma GCC diagnostic ignored "-Wnarrowing" | |
#endif | |
#endif | |
#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS) | |
#define _CRT_SECURE_NO_WARNINGS | |
#endif | |
/* Macro used to perform compile-time checks. */ | |
#if __STDC_VERSION__ >= 201112L | |
#define nelua_static_assert _Static_assert | |
#elif __cplusplus >= 201103L | |
#define nelua_static_assert static_assert | |
#else | |
#define nelua_static_assert(x, y) | |
#endif | |
/* Macro used to get alignment of a type. */ | |
#if __STDC_VERSION__ >= 201112L | |
#define nelua_alignof _Alignof | |
#elif __cplusplus >= 201103L | |
#define nelua_alignof alignof | |
#elif defined(__GNUC__) | |
#define nelua_alignof __alignof__ | |
#elif defined(_MSC_VER) | |
#define nelua_alignof __alignof | |
#else | |
#define nelua_alignof(x) | |
#endif | |
/* Checks if Nelua and C agrees on pointer size. */ | |
nelua_static_assert(sizeof(void*) == 8 && nelua_alignof(void*) == 8, "Nelua and C disagree on pointer size or alignment"); | |
/* Enable 64 bit offsets for stdio APIs. */ | |
#if !defined(_FILE_OFFSET_BITS) && __SIZEOF_LONG__ >= 8 | |
#define _FILE_OFFSET_BITS 64 | |
#endif | |
/* Enable POSIX APIs in included headers. */ | |
#if !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && !defined(_GNU_SOURCE) && !defined(_DEFAULT_SOURCE) | |
#if defined(__gnu_linux__) | |
#define _GNU_SOURCE | |
#else | |
#define _XOPEN_SOURCE 600 | |
#endif | |
#endif | |
#include <opencv2/core.hpp> | |
#include <opencv2/imgcodecs.hpp> | |
#include <opencv2/highgui.hpp> | |
using namespace cv; | |
using namespace std; | |
#include <string.h> | |
/* Macro used to force inlining a function. */ | |
#ifdef __GNUC__ | |
#define nelua_inline __attribute__((always_inline)) inline | |
#elif defined(_MSC_VER) | |
#define nelua_noinline __forceinline | |
#elif __STDC_VERSION__ >= 199901L | |
#define nelua_inline inline | |
#else | |
#define nelua_inline | |
#endif | |
#include <stdint.h> | |
/* Macro used for branch prediction. */ | |
#if defined(__GNUC__) || defined(__clang__) | |
#define nelua_unlikely(x) __builtin_expect(x, 0) | |
#else | |
#define nelua_unlikely(x) (x) | |
#endif | |
#include <stdio.h> | |
#include <stdlib.h> | |
/* Macro used to import/export extern C functions. */ | |
#ifdef __cplusplus | |
#define nelua_extern extern "C" | |
#else | |
#define nelua_extern extern | |
#endif | |
/* Macro used to generate traceback on aborts when sanitizing. */ | |
#if defined(__clang__) && defined(__has_feature) | |
#if __has_feature(undefined_behavior_sanitizer) | |
#define nelua_ubsan_unreachable __builtin_unreachable | |
#endif | |
#elif defined(__GNUC__) && !defined(_WIN32) | |
nelua_extern void __ubsan_handle_builtin_unreachable(void*) __attribute__((weak)); | |
#define nelua_ubsan_unreachable() {if(&__ubsan_handle_builtin_unreachable) __builtin_unreachable();} | |
#endif | |
#ifndef nelua_ubsan_unreachable | |
#define nelua_ubsan_unreachable() | |
#endif | |
/* Macro used to specify a function that never returns. */ | |
#if __STDC_VERSION__ >= 201112L | |
#define nelua_noreturn _Noreturn | |
#elif defined(__GNUC__) | |
#define nelua_noreturn __attribute__((noreturn)) | |
#elif defined(_MSC_VER) | |
#define nelua_noreturn __declspec(noreturn) | |
#else | |
#define nelua_noreturn | |
#endif | |
#include <stdbool.h> | |
#include <stddef.h> | |
#include <errno.h> | |
/* ------------------------------ DECLARATIONS ------------------------------ */ | |
static nelua_inline void nelua_memory_copy(void* dest, void* src, uintptr_t n); | |
static nelua_noreturn void nelua_abort(void); | |
typedef struct nlstring nlstring; | |
typedef uint8_t* nluint8_arr0_ptr; | |
struct nlstring { | |
nluint8_arr0_ptr data; | |
uintptr_t size; | |
}; nelua_static_assert(sizeof(nlstring) == 16 && nelua_alignof(nlstring) == 8, "Nelua and C disagree on type size or align"); | |
static void nelua_assert_line_1(bool cond, nlstring msg); | |
typedef struct nelua_GeneralAllocator nelua_GeneralAllocator; | |
struct nelua_GeneralAllocator {char x;}; nelua_static_assert(sizeof(nelua_GeneralAllocator) == 1 && nelua_alignof(nelua_GeneralAllocator) == 1, "Nelua and C disagree on type size or align"); | |
static nelua_GeneralAllocator nelua_general_allocator; | |
typedef nelua_GeneralAllocator* nelua_GeneralAllocator_ptr; | |
static nelua_inline void* nelua_GeneralAllocator_alloc(nelua_GeneralAllocator_ptr self, uintptr_t size); | |
static nelua_inline void nelua_GeneralAllocator_dealloc(nelua_GeneralAllocator_ptr self, void* p); | |
static void* nelua_GeneralAllocator_xalloc(nelua_GeneralAllocator_ptr self, uintptr_t size); | |
static nelua_noreturn void nelua_panic_string(nlstring s); | |
typedef nlstring* nlstring_ptr; | |
static void nelua_nlstring_destroy(nlstring_ptr self); | |
static void nelua_nlstring___close(nlstring_ptr self); | |
static nlstring nelua_nlstring_copy(nlstring s); | |
static nelua_inline intptr_t nelua_nlstring___len(nlstring a); | |
static nlstring nelua_tostring_1(string x); | |
static string play_draft_string___convert(nlstring s); | |
static nlstring play_draft_string___tostring(string self); | |
static nelua_inline nlstring nelua_cstring2string(const char* s); | |
static int nelua_main(int nelua_argc, char** nelua_argv); | |
/* ------------------------------ DEFINITIONS ------------------------------- */ | |
void nelua_abort(void) { | |
fflush(stderr); | |
nelua_ubsan_unreachable(); | |
abort(); | |
} | |
void nelua_assert_line_1(bool cond, nlstring msg) { | |
if(nelua_unlikely(!cond)) { | |
fwrite("/home/bart/projects/nelua/nelua-lang/lib/memory.nelua\033[1m:27:14: \033[31m\033[1mruntime error: \033[0m\033[1m", 1, 97, stderr); | |
fwrite(msg.data, msg.size, 1, stderr); | |
fwrite("\033[0m\n check(dest and src, 'invalid pointer')\n \033[1m\033[32m^\033[0m\033[35m~~~~~~\033[0m\n", 1, 89, stderr); | |
nelua_abort(); | |
} | |
} | |
void nelua_memory_copy(void* dest, void* src, uintptr_t n) { | |
if(nelua_unlikely((n == 0))) { | |
return; | |
} | |
nelua_assert_line_1(((dest != NULL) && (src != NULL)), ((nlstring){(uint8_t*)"invalid pointer", 15})); | |
memcpy(dest, src, (size_t)n); | |
} | |
void* nelua_GeneralAllocator_alloc(nelua_GeneralAllocator_ptr self, uintptr_t size) { | |
if(nelua_unlikely((size == 0))) { | |
return (void*)NULL; | |
} | |
return malloc((size_t)size); | |
} | |
void nelua_GeneralAllocator_dealloc(nelua_GeneralAllocator_ptr self, void* p) { | |
if(nelua_unlikely((p == (void*)NULL))) { | |
return; | |
} | |
free(p); | |
} | |
void nelua_panic_string(nlstring s) { | |
if(s.size > 0) { | |
fwrite(s.data, 1, s.size, stderr); | |
fputc('\n', stderr); | |
} | |
nelua_abort(); | |
} | |
void* nelua_GeneralAllocator_xalloc(nelua_GeneralAllocator_ptr self, uintptr_t size) { | |
void* p = nelua_GeneralAllocator_alloc(self, size); | |
if(nelua_unlikely(((p == (void*)NULL) && (size > 0)))) { | |
nelua_panic_string(((nlstring){(uint8_t*)"out of memory", 13})); | |
} | |
return p; | |
} | |
void nelua_nlstring_destroy(nlstring_ptr self) { | |
if(nelua_unlikely((self->size == 0))) { | |
return; | |
} | |
nelua_GeneralAllocator_dealloc((&nelua_general_allocator), (void*)self->data); | |
self->data = ((nluint8_arr0_ptr)NULL); | |
self->size = 0U; | |
} | |
void nelua_nlstring___close(nlstring_ptr self) { | |
nelua_nlstring_destroy(self); | |
} | |
nlstring nelua_nlstring_copy(nlstring s) { | |
nlstring clone = {0}; | |
if(nelua_unlikely((s.size == 0))) { | |
return clone; | |
} | |
clone.data = ((nluint8_arr0_ptr)nelua_GeneralAllocator_xalloc((&nelua_general_allocator), (s.size + 1))); | |
nelua_memory_copy((void*)clone.data, (void*)s.data, s.size); | |
clone.data[s.size] = 0U; | |
clone.size = s.size; | |
return clone; | |
} | |
intptr_t nelua_nlstring___len(nlstring a) { | |
return (intptr_t)a.size; | |
} | |
nlstring nelua_tostring_1(string x) { | |
return play_draft_string___tostring(x); | |
} | |
string play_draft_string___convert(nlstring s) { | |
string res = {0}; | |
res = (char*)s.data; | |
return res; | |
} | |
nlstring nelua_cstring2string(const char* s) { | |
if(s == NULL) { | |
return (nlstring){0}; | |
} | |
uintptr_t size = strlen(s); | |
if(size == 0) { | |
return (nlstring){0}; | |
} | |
return (nlstring){(uint8_t*)s, size}; | |
} | |
nlstring play_draft_string___tostring(string self) { | |
char* cs = NULL; | |
cs = (char*)self.c_str(); | |
return nelua_nlstring_copy(nelua_cstring2string(cs)); | |
} | |
int nelua_main(int nelua_argc, char** nelua_argv) { | |
{ | |
nlstring image_path = nelua_tostring_1(samples::findFile(play_draft_string___convert(((nlstring){(uint8_t*)"lena.jpg", 8})))); | |
if((nelua_nlstring___len(image_path) == 0)) { | |
nelua_panic_string(((nlstring){(uint8_t*)"Could not find lena image!", 26})); | |
} | |
Mat img = imread(play_draft_string___convert(image_path), IMREAD_COLOR); | |
imshow(play_draft_string___convert(((nlstring){(uint8_t*)"Display windows", 15})), img); | |
waitKey(0); | |
{ /* defer */ | |
nelua_nlstring___close((&image_path)); | |
} | |
} | |
return 0; | |
} | |
int main(int argc, char** argv) { | |
return nelua_main(argc, argv); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##[[ | |
-- Disables the garbage collector, we will use manual memory management. | |
pragmas.nogc = true | |
-- Include OpenCV headers. | |
cflags '-I/usr/include/opencv4 -DWITH_OPENEXR=OFF' | |
cinclude '<opencv2/core.hpp>' | |
cinclude '<opencv2/imgcodecs.hpp>' | |
cinclude '<opencv2/highgui.hpp>' | |
-- Link used OpenCV libraries. | |
linklib 'opencv_core' | |
linklib 'opencv_imgcodecs' | |
linklib 'opencv_highgui' | |
-- Use C++ namespaces to make easier to import C++ types. | |
cemitdecl [==[ | |
using namespace cv; | |
using namespace std; | |
]==] | |
-- Uncomment to use RAW C++ code instead of Nelua code. | |
--RAWCPP = true | |
]] | |
## if RAWCPP then | |
##[[ cemit [==[ | |
string image_path = samples::findFile("lena.jpg"); | |
Mat img = imread(image_path, IMREAD_COLOR); | |
if(img.empty()) { | |
return 1; | |
} | |
imshow("Display window", img); | |
waitKey(0); | |
]==] ]] | |
## else | |
-- Import 'string' runtime library (to use 'string.copy' and 'tostring'). | |
require 'string' | |
-- Import C++ 'string' as 'cppstring'. | |
local cppstring <cimport'string',nodecl,cincomplete> = @record{} | |
-- Converts a 'string' to a 'cppstring'. | |
function cppstring.__convert(s: string): cppstring | |
local res: cppstring | |
## cemit[[ res = (char*)s.data;]] | |
return res | |
end | |
-- Converts a 'cppstring' to a 'string'. | |
function cppstring.__tostring(self: cppstring): string | |
local cs: cstring | |
## cemit[[ cs = (char*)self.c_str();]] | |
return string.copy(cs) | |
end | |
-- Import some OpenCV functions. | |
local IMREAD_COLOR: cint <cimport,nodecl> | |
local Mat <cimport,nodecl,cincomplete> = @record{} | |
local function imread(path: cppstring, flags: cint): Mat <cimport,nodecl> end | |
local function imshow(path: cppstring, img: Mat): void <cimport,nodecl> end | |
local function samples_findFile(path: cppstring): cppstring <cimport'samples::findFile',nodecl> end | |
local function waitKey(delay: cint): cint <cimport,nodecl> end | |
do -- Getting Started with Images example. | |
-- Load Lena image. | |
local image_path: string <close> = tostring(samples_findFile('lena.jpg')) | |
if #image_path == 0 then | |
error 'Could not find lena image!' | |
end | |
local img = imread(image_path, IMREAD_COLOR) | |
-- Display Lena image. | |
imshow("Display windows", img) | |
-- Wait for a key stroke. | |
waitKey(0) | |
end | |
## end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment