ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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 close();
20 }
23 bool valid() const {
24 return pattern_ != nullptr;
25 }
26 int error() const {
27 return valid() ? 0 : ERROR_INIT;
28 }
29 void close() {
30 if (!pattern_)
31 return;
32 test_pattern_destroy(pattern_);
33 pattern_ = nullptr;
34 }
35 bool render(int width, int height, int index, double time, bool cadence, PatternPixels output) {
36 if (!pattern_) {
37 SET_ERRNO(ERROR_INVALID_STATE, "Test pattern is not initialized");
38 return false;
39 }
40 if (width <= 0 || height <= 0 || static_cast<uint64_t>(width) * static_cast<uint64_t>(height) * 4 != output["length"].as<size_t>()) {
41 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid test pattern image: %dx%d", width, height);
42 return false;
43 }
44 if (test_pattern_resize(pattern_, width, height) != ASCIICHAT_OK || test_pattern_render(pattern_, index, time, cadence) != ASCIICHAT_OK)
45 return false;
46 const uint8_t *pixels = test_pattern_rgba(pattern_);
47 if (!pixels)
48 return false;
49 output.call<void>("set", emscripten::val(emscripten::typed_memory_view(output["length"].as<size_t>(), pixels)));
50 return true;
51 }
52
53private:
54 test_pattern_t *pattern_ = nullptr;
55};
56
57EMSCRIPTEN_BINDINGS(ascii_chat_test_pattern) {
58 emscripten::register_type<PatternPixels>("PatternPixels", "Uint8ClampedArray");
59 emscripten::class_<TestPatternBinding>("TestPatternBinding")
60 .constructor<int, int>()
61 .function("valid", &TestPatternBinding::valid)
62 .function("error", &TestPatternBinding::error)
63 .function("close", &TestPatternBinding::close)
64 .function("render", &TestPatternBinding::render);
65}
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:24
unsigned int width
Definition fft.c:24
unsigned long long uint64_t
unsigned char uint8_t
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INIT
Definition error_codes.h:60
@ ERROR_INVALID_PARAM
static bool size_t index
Definition queries.h:10
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