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.c File Reference

Go to the source code of this file.

Data Structures

struct  test_pattern
 test pattern is intentionally a small, concrete record. Fields such as image, rgba give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed. More...

Macros

#define TEST_PATTERN_GRADIENT_INTERVALS   4096

Functions

asciichat_error_t test_pattern_resize (test_pattern_t *p, int width, int height)
asciichat_error_t test_pattern_create (int width, int height, test_pattern_t **out)
void test_pattern_destroy (test_pattern_t *p)
image_t * test_pattern_image (test_pattern_t *p)
const uint8_t * test_pattern_rgba (test_pattern_t *p)
asciichat_error_t test_pattern_render (test_pattern_t *p, int index, double time_ms, bool cadence)
asciichat_error_t test_pattern_render_at (test_pattern_t *p, int index, double time_ms, bool cadence)

Macro Definition Documentation

◆ TEST_PATTERN_GRADIENT_INTERVALS

#define TEST_PATTERN_GRADIENT_INTERVALS   4096

Definition at line 3 of file video/anim/test_pattern.c.

Referenced by test_pattern_render_at().

Function Documentation

◆ test_pattern_create()

asciichat_error_t test_pattern_create ( int width,
int height,
test_pattern_t ** out )

Independent, reusable RGB renderer. Time is elapsed milliseconds, not frame count.

Definition at line 109 of file video/anim/test_pattern.c.

109 {
110 if (!out)
111 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing test pattern output");
112 *out = NULL;
113 test_pattern_t *p = SAFE_CALLOC(1, sizeof(*p), test_pattern_t *);
115 if (result != ASCIICHAT_OK) {
117 return result;
118 }
119 *out = p;
120 return ASCIICHAT_OK;
121}
#define SET_ERRNO(code,...)
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
#define SAFE_CALLOC(count, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
static bool size_t gotel_query_t * out
Definition queries.h:10
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

References ASCIICHAT_OK, ERROR_INVALID_PARAM, height, out, SAFE_CALLOC, SET_ERRNO, test_pattern_destroy(), test_pattern_resize(), and width.

Referenced by TestPatternBinding::TestPatternBinding().

◆ test_pattern_destroy()

void test_pattern_destroy ( test_pattern_t * p)

Definition at line 122 of file video/anim/test_pattern.c.

122 {
123 if (!p)
124 return;
125 if (p->image)
127 SAFE_FREE(p->rgba);
128 SAFE_FREE(p);
129}
#define SAFE_FREE(ptr)
void image_destroy(image_t *image)
Destroy an image allocated with image_new().

References test_pattern::image, image_destroy(), test_pattern::rgba, and SAFE_FREE.

Referenced by TestPatternBinding::close(), media_source_destroy(), and test_pattern_create().

◆ test_pattern_image()

image_t * test_pattern_image ( test_pattern_t * p)

Definition at line 130 of file video/anim/test_pattern.c.

130 {
131 return p ? p->image : NULL;
132}

References test_pattern::image.

◆ test_pattern_render()

asciichat_error_t test_pattern_render ( test_pattern_t * p,
int index,
double time_ms,
bool cadence )

Definition at line 149 of file video/anim/test_pattern.c.

149 {
150 animation_sample_t sample;
151 animation_config_t config = {.type = ANIMATION_TEST_PATTERN, .fps = 60, .speed = 1};
152 asciichat_error_t err = animation_sample_at(config, time_ms / 1000.0, &sample);
153 if (err != ASCIICHAT_OK)
154 return err;
155 animation_target_t target = {.type = ANIMATION_TARGET_TEST_PATTERN, .test_pattern = {.context = p, .index = index, .cadence = cadence}};
156 return animation_apply(&sample, &target);
157}
static bool size_t index
Definition queries.h:10
For animation config, this is the live record that ties type, fps, speed, hidden_policy together....
asciichat_error_t animation_apply(const animation_sample_t *s, animation_target_t *t)
asciichat_error_t animation_sample_at(animation_config_t config, double seconds, animation_sample_t *out)
@ ANIMATION_TEST_PATTERN
@ ANIMATION_TARGET_TEST_PATTERN

References animation_apply(), animation_sample_at(), ANIMATION_TARGET_TEST_PATTERN, ANIMATION_TEST_PATTERN, ASCIICHAT_OK, and index.

Referenced by TestPatternBinding::render().

◆ test_pattern_render_at()

asciichat_error_t test_pattern_render_at ( test_pattern_t * p,
int index,
double time_ms,
bool cadence )

Definition at line 159 of file video/anim/test_pattern.c.

159 {
160 if (!p || !p->image || index < 0 || index > 1 || !isfinite(time_ms) || time_ms < 0)
161 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid test pattern frame parameters");
162 int w = p->image->w, h = p->image->h;
163 double phase = fmod(time_ms, index ? 4000 : 10000) / (index ? 4000 : 10000);
164 rgb_pixel_t dark = {16, 24, 32}, white = {255, 255, 255};
165 rgb_pixel_t first = hue(phase * 360, 0.5), last = hue(fmod(phase + 0.5, 1) * 360, 0.5);
166 double box = fmin(fmax(32, fmin(w, h) / 4.0), fmin(w, h) / 2.0);
167 double bx = fmod(time_ms / 11, w + box) - box, by = fmod(time_ms / 17, h + box) - box;
168 double stripe = w / 12.0, offset = phase * w;
169 int first_stripe = (int)ceil(-offset / stripe);
170 double radius = fmin(fmax(12, fmin(w, h) / 8.0), fmin(w, h) / 4.0);
171 double cx = w + radius - phase * (w + radius * 2), cy = h / 2.0 + sin(phase * 12.566370614359172) * h / 4.0;
173 for (int i = 0; i < 12; i++)
174 colors[i] = hue(i * 30, 0.55);
175 uint32_t frame = (uint32_t)fmod(floor(time_ms / (1000.0 / 60)), 512);
176 uint32_t code = frame ^ (frame >> 1);
177 rgb_pixel_t *pixels = p->image->pixels;
178 if (index == 1) {
179 // The background is constant down each column. Rasterize one row, then copy.
180 for (int x = 0; x < w; x++) {
181 rgb_pixel_t pixel = dark;
182 int k = (int)floor((x - offset) / stripe) + 1;
183 for (int j = k; j <= k + 1; j++)
184 if (j >= first_stripe && j <= first_stripe + 12)
185 blend(&pixel, colors[(j % 12 + 12) % 12], overlap(x, j * stripe + offset - stripe, j * stripe + offset));
186 pixels[x] = pixel;
187 }
188 for (int y = 1; y < h; y++)
189 memcpy(pixels + (size_t)y * w, pixels, (size_t)w * sizeof(*pixels));
190 if (cadence) {
191 // Show all nine Gray-code bits in a compact 3x3 marker. Showing only
192 // bit zero repeats every other 60 Hz frame, hiding valid presentations
193 // from a visual cadence check. Adjacent codes differ in exactly one
194 // tile while the surrounding animated pattern remains visible.
195 int marker = (int)fmax(16, fmin(w, h) / 16.0);
196 int marker_left = (w - marker) / 2;
197 int marker_top = (h - marker) / 2;
198 for (int y = marker_top; y < marker_top + marker && y < h; y++)
199 for (int x = marker_left; x < marker_left + marker && x < w; x++) {
200 unsigned tile_x = (unsigned)(x - marker_left) * 3 / (unsigned)marker;
201 unsigned tile_y = (unsigned)(y - marker_top) * 3 / (unsigned)marker;
202 unsigned bit = tile_y * 3 + tile_x;
203 pixels[(size_t)y * w + x] = (code & (1u << bit)) ? white : (rgb_pixel_t){0, 0, 0};
204 }
205 return ASCIICHAT_OK;
206 }
207 int top = (int)fmax(0, floor(cy - radius - 1)), bottom = (int)fmin(h, ceil(cy + radius + 1));
208 int left = (int)fmax(0, floor(cx - radius - 1)), right = (int)fmin(w, ceil(cx + radius + 1));
209 for (int y = top; y < bottom; y++)
210 for (int x = left; x < right; x++) {
211 double dx0 = x + 0.5 - cx, dy0 = y + 0.5 - cy, d2 = dx0 * dx0 + dy0 * dy0;
212 if (d2 > (radius + 0.75) * (radius + 0.75))
213 continue;
214 double coverage = 1;
215 if (d2 > (radius - 0.75) * (radius - 0.75)) {
216 int inside = 0;
217 for (int sy = 0; sy < 4; sy++)
218 for (int sx = 0; sx < 4; sx++) {
219 double dx = x + (sx + 0.5) / 4 - cx, dy = y + (sy + 0.5) / 4 - cy;
220 if (dx * dx + dy * dy < radius * radius)
221 inside++;
222 }
223 coverage = inside / 16.0;
224 }
225 blend(&pixels[(size_t)y * w + x], white, coverage);
226 }
227 } else {
228 // A fixed-point ramp avoids three floating-point conversions per source
229 // pixel. 4096 intervals keep rounding within one RGB level of Canvas.
231 for (int i = 0; i <= TEST_PATTERN_GRADIENT_INTERVALS; i++) {
232 double t = i / (double)TEST_PATTERN_GRADIENT_INTERVALS;
233 gradient[i] = (rgb_pixel_t){(uint8_t)(first.r * (1 - t) + last.r * t + 0.5), (uint8_t)(first.g * (1 - t) + last.g * t + 0.5), (uint8_t)(first.b * (1 - t) + last.b * t + 0.5)};
234 }
235 double scale = (double)TEST_PATTERN_GRADIENT_INTERVALS * 65536 / ((double)w * w + (double)h * h);
236 uint32_t step = (uint32_t)(w * scale + 0.5);
237 for (int y = 0; y < h; y++) {
238 uint32_t position = (uint32_t)((0.5 * w + (y + 0.5) * h) * scale + 0.5);
239 for (int x = 0; x < w; x++, position += step)
240 pixels[(size_t)y * w + x] = gradient[position >> 16];
241 }
242 int top = (int)fmax(0, floor(by)), bottom = (int)fmin(h, ceil(by + box));
243 int left = (int)fmax(0, floor(bx)), right = (int)fmin(w, ceil(bx + box));
244 for (int y = top; y < bottom; y++)
245 for (int x = left; x < right; x++)
246 blend(&pixels[(size_t)y * w + x], white, 0.8 * overlap(x, bx, bx + box) * overlap(y, by, by + box));
247 }
248 render_test_label(p->image);
249 return ASCIICHAT_OK;
250}
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned int uint32_t
unsigned char uint8_t
int h
Image height in pixels (must be > 0).
rgb_pixel_t * pixels
Pixel data array (width * height RGB pixels, row-major order).
int w
Image width in pixels (must be > 0).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
#define TEST_PATTERN_GRADIENT_INTERVALS

References ASCIICHAT_OK, rgb_pixel_t::b, colors, ERROR_INVALID_PARAM, rgb_pixel_t::g, image::h, test_pattern::image, index, image::pixels, rgb_pixel_t::r, SET_ERRNO, TEST_PATTERN_GRADIENT_INTERVALS, and image::w.

Referenced by animation_apply().

◆ test_pattern_resize()

asciichat_error_t test_pattern_resize ( test_pattern_t * p,
int width,
int height )

Definition at line 65 of file video/anim/test_pattern.c.

65 {
66 if (!p || width < 1 || height < 1)
67 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid test pattern dimensions");
68 if (p->image && p->image->w == width && p->image->h == height)
69 return ASCIICHAT_OK;
70 image_t *next = image_new(width, height);
71 if (!next)
72 return SET_ERRNO(ERROR_MEMORY, "Test pattern image allocation failed");
73 if (p->image)
75 p->image = next;
76 SAFE_FREE(p->rgba);
77 return ASCIICHAT_OK;
78}
@ ERROR_MEMORY
Definition error_codes.h:55
image_t * image_new(size_t width, size_t height)
Create a new image with standard allocation.

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_MEMORY, image::h, height, test_pattern::image, image_destroy(), image_new(), test_pattern::rgba, SAFE_FREE, SET_ERRNO, image::w, and width.

Referenced by TestPatternBinding::render(), and test_pattern_create().

◆ test_pattern_rgba()

const uint8_t * test_pattern_rgba ( test_pattern_t * pattern)

Reusable RGBA view for browser upload; invalidated by resize/destroy.

Definition at line 134 of file video/anim/test_pattern.c.

134 {
135 if (!p || !p->image)
136 return NULL;
137 size_t count = (size_t)p->image->w * p->image->h;
138 if (!p->rgba)
139 p->rgba = SAFE_MALLOC(count * 4, uint8_t *);
140 for (size_t i = 0; i < count; i++) {
141 p->rgba[i * 4] = p->image->pixels[i].r;
142 p->rgba[i * 4 + 1] = p->image->pixels[i].g;
143 p->rgba[i * 4 + 2] = p->image->pixels[i].b;
144 p->rgba[i * 4 + 3] = 255;
145 }
146 return p->rgba;
147}
#define SAFE_MALLOC(size, cast)

References rgb_pixel_t::b, rgb_pixel_t::g, image::h, test_pattern::image, image::pixels, rgb_pixel_t::r, test_pattern::rgba, SAFE_MALLOC, and image::w.

Referenced by TestPatternBinding::render().