ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
test_pattern.cpp
Go to the documentation of this file.
1#include <emscripten/bind.h>
2extern "C" {
5}
6
7#include <cstddef>
8#include <cstdint>
9
11
13public:
15 if (test_pattern_create(width, height, &pattern_) != ASCIICHAT_OK)
16 pattern_ = nullptr;
17 }
19 if (pattern_)
20 test_pattern_destroy(pattern_);
21 }
24 bool valid() const {
25 return pattern_ != nullptr;
26 }
27 bool render(int width, int height, int index, double time, bool cadence, PatternPixels output) {
28 if (!pattern_) {
29 SET_ERRNO(ERROR_INVALID_STATE, "Test pattern is not initialized");
30 return false;
31 }
32 if (width <= 0 || height <= 0 ||
33 static_cast<uint64_t>(width) * static_cast<uint64_t>(height) * 4 != output["length"].as<size_t>()) {
34 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid test pattern image: %dx%d", width, height);
35 return false;
36 }
37 if (test_pattern_resize(pattern_, width, height) != ASCIICHAT_OK ||
38 test_pattern_render(pattern_, index, time, cadence) != ASCIICHAT_OK)
39 return false;
40 const uint8_t *pixels = test_pattern_rgba(pattern_);
41 if (!pixels)
42 return false;
43 output.call<void>("set", emscripten::val(emscripten::typed_memory_view(output["length"].as<size_t>(), pixels)));
44 return true;
45 }
46
47private:
48 test_pattern_t *pattern_ = nullptr;
49};
50
51EMSCRIPTEN_BINDINGS(ascii_chat_test_pattern) {
52 emscripten::register_type<PatternPixels>("PatternPixels", "Uint8ClampedArray");
53 emscripten::class_<TestPatternBinding>("TestPatternBinding")
54 .constructor<int, int>()
55 .function("valid", &TestPatternBinding::valid)
56 .function("render", &TestPatternBinding::render);
57}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
bool render(int width, int height, int index, double time, bool cadence, PatternPixels output)
TestPatternBinding(const TestPatternBinding &)=delete
TestPatternBinding & operator=(const TestPatternBinding &)=delete
TestPatternBinding(int width, int height)
unsigned int height
Definition fft.c:23
unsigned int width
Definition fft.c:23
unsigned long long uint64_t
Definition common.h:63
unsigned char uint8_t
Definition common.h:60
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_PARAM
static bool size_t index
Definition queries.h:8
EMSCRIPTEN_DECLARE_VAL_TYPE(PatternPixels)
EMSCRIPTEN_BINDINGS(ascii_chat_test_pattern)
asciichat_error_t test_pattern_render(test_pattern_t *p, int index, double time_ms, bool cadence)
asciichat_error_t test_pattern_create(int width, int height, test_pattern_t **out)
const uint8_t * test_pattern_rgba(test_pattern_t *p)
void test_pattern_destroy(test_pattern_t *p)
asciichat_error_t test_pattern_resize(test_pattern_t *p, int width, int height)
struct test_pattern test_pattern_t