AtlatestRepositorysigil-ui
1#include <sigil/sigil.h>
2#include "appkit-runtime.h"
3#include <math.h>
4#include <stdio.h>
5
6extern void sigil__init_coretext_test_fixture(SigilVM *);
7static bool expected_components(SWId space, double red, double green, double blue, double values[4])
8{
9 SWId color = ((SWId (*)(SWId, SWSel, double, double, double, double))sw_appkit.message)(
10 sw_class("NSColor"), sw_sel("colorWithSRGBRed:green:blue:alpha:"), red, green, blue, 1.0);
11 SWId converted = ((SWId (*)(SWId, SWSel, SWId))sw_appkit.message)(
12 color, sw_sel("colorUsingColorSpace:"), space);
13 if (!converted || sw_uint(converted, "numberOfComponents") != 4) return false;
14 ((void (*)(SWId, SWSel, double *))sw_appkit.message)(converted, sw_sel("getComponents:"), values);
15 for (unsigned index = 0; index < 4; ++index)
16 if (!isfinite(values[index])) return false;
17 return true;
19static Value capture(SigilVM *vm, int argc, Value *args)
21 (void)argc; (void)args;
22 if (!sw_appkit_main_thread()) return SIGIL_FALSE;
23 void *pool = sw_appkit.pool_push();
24 SWId application = sw_id(sw_class("NSApplication"), "sharedApplication");
25 SWId windows = sw_id(application, "windows");
26 SWId view = NULL;
27 unsigned canvases = 0;
28 for (unsigned long index = 0; index < sw_uint(windows, "count"); ++index) {
29 SWId window = ((SWId (*)(SWId, SWSel, unsigned long))sw_appkit.message)(
30 windows, sw_sel("objectAtIndex:"), index);
31 SWId subviews = sw_id(sw_id(window, "contentView"), "subviews");
32 for (unsigned long child = 0; child < sw_uint(subviews, "count"); ++child) {
33 SWId candidate = ((SWId (*)(SWId, SWSel, unsigned long))sw_appkit.message)(
34 subviews, sw_sel("objectAtIndex:"), child);
35 if (((SWObjCBool (*)(SWId, SWSel, SWClass))sw_appkit.message)(
36 candidate, sw_sel("isKindOfClass:"), sw_class("SigilWindowCanvas"))) {
37 view = candidate;
38 ++canvases;
39 }
40 }
41 }
42 if (canvases != 1) {
43 printf("CAPTURE invalid: expected one host canvas, found %u\n", canvases);
44 sw_appkit.pool_pop(pool);
45 return SIGIL_FALSE;
46 }
47 SWRect bounds = sw_bounds(view, "bounds");
48 SWId bitmap = ((SWId (*)(SWId, SWSel, SWRect))sw_appkit.message)(
49 view, sw_sel("bitmapImageRepForCachingDisplayInRect:"), bounds);
50 SWId profile = sw_id(bitmap, "colorSpace");
51 unsigned long bits = sw_uint(bitmap, "bitsPerSample");
52 unsigned long samples = sw_uint(bitmap, "samplesPerPixel");
53 unsigned long width = sw_uint(bitmap, "pixelsWide"), height = sw_uint(bitmap, "pixelsHigh");
54 unsigned long format = sw_uint(bitmap, "bitmapFormat");
55 bool alpha = sw_bool(bitmap, "hasAlpha");
56 const char *space = (const char *)sw_id(sw_id(profile, "localizedName"), "UTF8String");
57 const char *named_space = (const char *)sw_id(sw_id(bitmap, "colorSpaceName"), "UTF8String");
58 printf("CAPTURE bounds=%.3fx%.3f pixels=%lux%lu bits=%lu samples=%lu format=0x%lx profile=%s colorSpaceName=%s\n",
59 bounds.size.width, bounds.size.height, width, height, bits, samples,
60 format, space ? space : "unknown", named_space ? named_space : "unknown");
61 double expected_bg[4] = {0}, expected_white[4] = {0};
62 bool valid = bitmap && profile && bits == 8 && !(format & 4) &&
63 samples == (alpha ? 4UL : 3UL) && sw_uint(profile, "numberOfColorComponents") == 3 &&
64 width && height && width <= 2048 && height <= 2048 &&
65 expected_components(profile, 18.0 / 255, 52.0 / 255, 86.0 / 255, expected_bg) &&
66 expected_components(profile, 1, 1, 1, expected_white);
67 printf("CAPTURE source-sRGB-bg=18,52,86 expected-device-bg=%.3f,%.3f,%.3f expected-device-white=%.3f,%.3f,%.3f\n",
68 expected_bg[0] * 255, expected_bg[1] * 255, expected_bg[2] * 255,
69 expected_white[0] * 255, expected_white[1] * 255, expected_white[2] * 255);
70 unsigned long white = 0, background = 0;
71 unsigned long rgb_start = alpha && (format & 1) ? 1 : 0;
72 unsigned long alpha_index = format & 1 ? 0 : 3;
73 int brightest[4] = {0};
74 if (valid) {
75 ((void (*)(SWId, SWSel, SWRect, SWId))sw_appkit.message)(
76 view, sw_sel("cacheDisplayInRect:toBitmapImageRep:"), bounds, bitmap);
77 for (unsigned long y = 0; valid && y < height; ++y) {
78 for (unsigned long x = 0; valid && x < width; ++x) {
79 unsigned long raw[4] = {0};
80 ((void (*)(SWId, SWSel, unsigned long *, long, long))sw_appkit.message)(
81 bitmap, sw_sel("getPixel:atX:y:"), raw, (long)x, (long)y);
82 unsigned long opacity = alpha ? raw[alpha_index] : 255;
83 bool is_white = opacity > 240, is_background = opacity > 240;
84 for (unsigned index = 0; index < 3; ++index) {
85 double sample = (double)raw[rgb_start + index];
86 if (fabs(sample - expected_bg[index] * 255) > 2) is_background = false;
87 if (fabs(sample - expected_white[index] * 255) > 14) is_white = false;
88 }
89 int light = (int)raw[rgb_start];
90 if ((int)raw[rgb_start + 1] < light) light = (int)raw[rgb_start + 1];
91 if ((int)raw[rgb_start + 2] < light) light = (int)raw[rgb_start + 2];
92 if (light > brightest[0]) {
93 brightest[0] = light;
94 for (unsigned index = 0; index < 3; ++index) brightest[index + 1] = (int)raw[rgb_start + index];
95 }
96 if (is_white) ++white;
97 if (is_background) ++background;
98 if ((!x && !y) || (x == width / 2 && y == height / 2))
99 printf("CAPTURE sample=%lu,%lu raw=%lu,%lu,%lu,%lu profile-RGB=%lu,%lu,%lu alpha=%lu\n",
100 x, y, raw[0], raw[1], raw[2], raw[3],
101 raw[rgb_start], raw[rgb_start + 1], raw[rgb_start + 2], opacity);
102 }
103 }
104 }
105 printf("CAPTURE bg-tolerance=2 near-white-tolerance=14 counts-white=%lu background=%lu total=%lu required-bg=%lu required-white=200 brightest=%d,%d,%d valid=%d\n",
106 white, background, width * height, (width * height * 3 + 3) / 4,
107 brightest[1], brightest[2], brightest[3], valid);
108 sw_appkit.pool_pop(pool);
109 if (!valid) return SIGIL_FALSE;
110 Value values[] = {sigil_fixnum((int64_t)white), sigil_fixnum((int64_t)background),
111 sigil_fixnum((int64_t)width), sigil_fixnum((int64_t)height)};
112 return sigil_array_create(vm, 4, values);
114void sigil__init_drawing_tests(SigilVM *vm)
116 sigil__init_coretext_test_fixture(vm);
117 if (!sigil_begin_module(vm, "(drawing native)")) return;
118 sigil_module_register_native(vm, "capture-white-pixels", capture, SIGIL_ARITY_EXACT(0), "Compare raw cached pixels with profile-matched sRGB expectations");
119 sigil_module_export(vm, "capture-white-pixels");
120 sigil_end_module(vm);