kittyops 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ name: CI & Wheels
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ tags: ["v*"]
7
+ pull_request:
8
+ workflow_dispatch:
9
+
10
+ permissions:
11
+ contents: read
12
+
13
+ jobs:
14
+ c-tests:
15
+ name: C & Python tests (${{ matrix.os }})
16
+ runs-on: ${{ matrix.os }}
17
+ strategy:
18
+ fail-fast: false
19
+ matrix:
20
+ os: [ubuntu-24.04, macos-15]
21
+ steps:
22
+ - uses: actions/checkout@v4
23
+ - name: Install compression deps (Linux)
24
+ if: runner.os == 'Linux'
25
+ run: |
26
+ sudo apt-get update
27
+ sudo apt-get install -y cmake ninja-build zlib1g-dev libdeflate-dev
28
+ - name: Install compression deps (macOS)
29
+ if: runner.os == 'macOS'
30
+ run: brew install cmake libdeflate
31
+ - name: Build and test C + Python
32
+ run: |
33
+ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
34
+ cmake --build build -j
35
+ ctest --test-dir build --output-on-failure
36
+ if [ "${{ runner.os }}" = "macOS" ]; then
37
+ LIB="$PWD/build/libkittyops.dylib"
38
+ else
39
+ LIB="$PWD/build/libkittyops.so"
40
+ fi
41
+ KITTYOPS_LIB="$LIB" PYTHONPATH="$PWD/python" python3 -m unittest discover -s tests -p 'test_*.py' -v
42
+
43
+ wheels:
44
+ name: Build wheels (${{ matrix.os }} / ${{ matrix.arch }})
45
+ runs-on: ${{ matrix.os }}
46
+ strategy:
47
+ fail-fast: false
48
+ matrix:
49
+ include:
50
+ - os: macos-15
51
+ arch: arm64
52
+ macos_target: "11.0"
53
+ - os: macos-15-intel
54
+ arch: x86_64
55
+ macos_target: "10.15"
56
+ - os: ubuntu-24.04
57
+ arch: x86_64
58
+ macos_target: ""
59
+ - os: ubuntu-24.04-arm
60
+ arch: aarch64
61
+ macos_target: ""
62
+ steps:
63
+ - uses: actions/checkout@v4
64
+ - name: Install macOS static libdeflate
65
+ if: runner.os == 'macOS'
66
+ run: brew install libdeflate
67
+ - name: Build wheel via cibuildwheel
68
+ uses: pypa/cibuildwheel@v2.23.3
69
+ env:
70
+ # ctypes-only wheel with wheel.py-api = "py3": one wheel per OS+arch works on Python 3.11+
71
+ CIBW_BUILD: "cp312-*"
72
+ CIBW_ARCHS_MACOS: ${{ matrix.arch }}
73
+ CIBW_ARCHS_LINUX: ${{ matrix.arch }}
74
+ MACOSX_DEPLOYMENT_TARGET: ${{ matrix.macos_target }}
75
+ CIBW_MANYLINUX_X86_64_IMAGE: manylinux_2_28
76
+ CIBW_MANYLINUX_AARCH64_IMAGE: manylinux_2_28
77
+ CIBW_BEFORE_ALL_LINUX: |
78
+ dnf install -y cmake zlib-devel libdeflate-devel || yum install -y cmake zlib-devel || true
79
+ CIBW_TEST_COMMAND: |
80
+ python -c "import kittyops; print('backend:', kittyops.compression_backend())"
81
+ python -m unittest discover -s {project}/tests -p 'test_*.py' -v
82
+ - uses: actions/upload-artifact@v4
83
+ with:
84
+ name: wheels-${{ matrix.os }}-${{ matrix.arch }}
85
+ path: wheelhouse/*.whl
86
+
87
+ sdist:
88
+ name: Build sdist
89
+ runs-on: ubuntu-24.04
90
+ steps:
91
+ - uses: actions/checkout@v4
92
+ - uses: actions/setup-python@v5
93
+ with:
94
+ python-version: "3.12"
95
+ - run: pip install build
96
+ - run: python -m build --sdist --outdir dist/
97
+ - uses: actions/upload-artifact@v4
98
+ with:
99
+ name: sdist
100
+ path: dist/*.tar.gz
101
+
102
+ release:
103
+ name: Publish release & PyPI
104
+ if: startsWith(github.ref, 'refs/tags/v')
105
+ needs: [c-tests, wheels, sdist]
106
+ runs-on: ubuntu-24.04
107
+ permissions:
108
+ contents: write
109
+ id-token: write
110
+ steps:
111
+ - uses: actions/download-artifact@v4
112
+ with:
113
+ path: dist
114
+ merge-multiple: true
115
+ - name: Create GitHub Release
116
+ uses: softprops/action-gh-release@v2
117
+ with:
118
+ files: dist/*
119
+ generate_release_notes: true
120
+ - name: Publish to PyPI (OIDC trusted publishing)
121
+ uses: pypa/gh-action-pypi-publish@release/v1
122
+ continue-on-error: true
@@ -0,0 +1,11 @@
1
+ build/
2
+ dist/
3
+ wheelhouse/
4
+ .venv/
5
+ *.o
6
+ *.a
7
+ *.dylib
8
+ *.so
9
+ __pycache__/
10
+ *.egg-info/
11
+ .DS_Store
@@ -0,0 +1,161 @@
1
+ cmake_minimum_required(VERSION 3.16)
2
+ project(libkittyops VERSION 0.1.0 LANGUAGES C)
3
+
4
+ set(CMAKE_C_STANDARD 99)
5
+ set(CMAKE_C_STANDARD_REQUIRED ON)
6
+ set(CMAKE_C_EXTENSIONS ON)
7
+
8
+ option(KITTYOPS_BUILD_SHARED "Build shared libkittyops" ON)
9
+ option(KITTYOPS_BUILD_STATIC "Build static libkittyops" ON)
10
+ option(KITTYOPS_BUILD_TESTS "Build C unit tests" ON)
11
+ option(KITTYOPS_BUILD_BENCH "Build 1080p benchmark binary" ON)
12
+ option(KITTYOPS_USE_LIBDEFLATE "Use libdeflate if found" ON)
13
+ option(KITTYOPS_USE_ZLIB_NG "Prefer zlib-ng if found" ON)
14
+ option(KITTYOPS_USE_ISAL "Use Intel ISA-L if found" ON)
15
+ option(KITTYOPS_PREFER_STATIC_DEPS "Prefer static archives for optional compression deps (relocatable builds)" OFF)
16
+ option(KITTYOPS_PYTHON_WHEEL "Build single unversioned shared lib installed into kittyops/ Python package" OFF)
17
+
18
+ include(GNUInstallDirs)
19
+ find_package(Threads REQUIRED)
20
+
21
+ # Compression backend detection:
22
+ # 1. Streaming zlib backend (for parallel multi-slice deflate):
23
+ # prefer zlib-ng if available, else system zlib.
24
+ set(KOPS_ZLIB_LIBS "")
25
+ set(KOPS_DEFS "")
26
+ set(KOPS_INC_DIRS "")
27
+
28
+ if(KITTYOPS_USE_ZLIB_NG)
29
+ find_path(ZLIB_NG_INCLUDE_DIR zlib-ng.h PATHS /opt/homebrew/include /usr/local/include /usr/include)
30
+ find_library(ZLIB_NG_LIBRARY NAMES z-ng zlib-ng PATHS /opt/homebrew/lib /usr/local/lib /usr/lib)
31
+ if(ZLIB_NG_INCLUDE_DIR AND ZLIB_NG_LIBRARY)
32
+ message(STATUS "libkittyops: using zlib-ng (${ZLIB_NG_LIBRARY})")
33
+ list(APPEND KOPS_DEFS KOPS_HAVE_ZLIB_NG=1)
34
+ list(APPEND KOPS_INC_DIRS "${ZLIB_NG_INCLUDE_DIR}")
35
+ list(APPEND KOPS_ZLIB_LIBS "${ZLIB_NG_LIBRARY}")
36
+ endif()
37
+ endif()
38
+
39
+ if(NOT KOPS_ZLIB_LIBS)
40
+ find_package(ZLIB REQUIRED)
41
+ message(STATUS "libkittyops: using system zlib (${ZLIB_LIBRARIES})")
42
+ list(APPEND KOPS_ZLIB_LIBS ZLIB::ZLIB)
43
+ endif()
44
+
45
+ # 2. Fast single-block libdeflate (for small/medium rects < 224 KB and multi-rect parallel workers)
46
+ if(KITTYOPS_USE_LIBDEFLATE)
47
+ find_path(LIBDEFLATE_INCLUDE_DIR libdeflate.h PATHS /opt/homebrew/include /usr/local/include /usr/include)
48
+ if(KITTYOPS_PREFER_STATIC_DEPS)
49
+ find_library(LIBDEFLATE_LIBRARY NAMES libdeflate.a deflate libdeflate PATHS /opt/homebrew/lib /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/aarch64-linux-gnu)
50
+ else()
51
+ find_library(LIBDEFLATE_LIBRARY NAMES deflate libdeflate PATHS /opt/homebrew/lib /usr/local/lib /usr/lib)
52
+ endif()
53
+ if(LIBDEFLATE_INCLUDE_DIR AND LIBDEFLATE_LIBRARY)
54
+ message(STATUS "libkittyops: using libdeflate (${LIBDEFLATE_LIBRARY})")
55
+ list(APPEND KOPS_DEFS KOPS_HAVE_LIBDEFLATE=1)
56
+ list(APPEND KOPS_INC_DIRS "${LIBDEFLATE_INCLUDE_DIR}")
57
+ list(APPEND KOPS_ZLIB_LIBS "${LIBDEFLATE_LIBRARY}")
58
+ endif()
59
+ endif()
60
+
61
+ # 3. Optional Intel ISA-L (ultra-fast level 0-3 deflate with streaming flush)
62
+ if(KITTYOPS_USE_ISAL)
63
+ find_path(ISAL_INCLUDE_DIR isa-l/igzip_lib.h PATHS /opt/homebrew/include /usr/local/include /usr/include)
64
+ find_library(ISAL_LIBRARY NAMES isal PATHS /opt/homebrew/lib /usr/local/lib /usr/lib)
65
+ if(ISAL_INCLUDE_DIR AND ISAL_LIBRARY)
66
+ message(STATUS "libkittyops: using Intel ISA-L (${ISAL_LIBRARY})")
67
+ list(APPEND KOPS_DEFS KOPS_HAVE_ISAL=1)
68
+ list(APPEND KOPS_INC_DIRS "${ISAL_INCLUDE_DIR}")
69
+ list(APPEND KOPS_ZLIB_LIBS "${ISAL_LIBRARY}")
70
+ endif()
71
+ endif()
72
+
73
+ set(KOPS_SOURCES
74
+ src/parallel.c
75
+ src/diff.c
76
+ src/scroll.c
77
+ src/compress.c
78
+ src/apc.c
79
+ src/encoder.c
80
+ )
81
+
82
+ function(kops_configure_target tgt)
83
+ target_include_directories(${tgt}
84
+ PUBLIC
85
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
86
+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
87
+ PRIVATE
88
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
89
+ ${KOPS_INC_DIRS}
90
+ )
91
+ target_compile_definitions(${tgt} PRIVATE ${KOPS_DEFS})
92
+ target_link_libraries(${tgt} PRIVATE ${KOPS_ZLIB_LIBS} Threads::Threads)
93
+ if(NOT MSVC)
94
+ target_compile_options(${tgt} PRIVATE -Wall -Wextra -Wpedantic)
95
+ endif()
96
+ if(UNIX AND NOT APPLE)
97
+ target_link_libraries(${tgt} PRIVATE rt)
98
+ endif()
99
+ endfunction()
100
+
101
+ if(KITTYOPS_BUILD_STATIC)
102
+ add_library(kittyops_static STATIC ${KOPS_SOURCES})
103
+ set_target_properties(kittyops_static PROPERTIES OUTPUT_NAME kittyops POSITION_INDEPENDENT_CODE ON)
104
+ kops_configure_target(kittyops_static)
105
+ add_library(kittyops::static ALIAS kittyops_static)
106
+ endif()
107
+
108
+ if(KITTYOPS_BUILD_SHARED)
109
+ add_library(kittyops SHARED ${KOPS_SOURCES})
110
+ if(KITTYOPS_PYTHON_WHEEL)
111
+ set_target_properties(kittyops PROPERTIES
112
+ C_VISIBILITY_PRESET default
113
+ INSTALL_NAME_DIR "@loader_path"
114
+ )
115
+ else()
116
+ set_target_properties(kittyops PROPERTIES
117
+ VERSION ${PROJECT_VERSION}
118
+ SOVERSION 0
119
+ C_VISIBILITY_PRESET default
120
+ )
121
+ endif()
122
+ kops_configure_target(kittyops)
123
+ add_library(kittyops::shared ALIAS kittyops)
124
+ endif()
125
+
126
+ if(KITTYOPS_BUILD_TESTS)
127
+ enable_testing()
128
+ add_executable(test_kittyops tests/test_kittyops.c)
129
+ if(TARGET kittyops_static)
130
+ target_link_libraries(test_kittyops PRIVATE kittyops_static ${KOPS_ZLIB_LIBS} Threads::Threads)
131
+ else()
132
+ target_link_libraries(test_kittyops PRIVATE kittyops ${KOPS_ZLIB_LIBS} Threads::Threads)
133
+ endif()
134
+ target_include_directories(test_kittyops PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${KOPS_INC_DIRS})
135
+ target_compile_definitions(test_kittyops PRIVATE ${KOPS_DEFS})
136
+ add_test(NAME kittyops_c_tests COMMAND test_kittyops)
137
+ endif()
138
+
139
+ if(KITTYOPS_BUILD_BENCH)
140
+ add_executable(bench_kittyops bench/bench_kittyops.c)
141
+ if(TARGET kittyops_static)
142
+ target_link_libraries(bench_kittyops PRIVATE kittyops_static ${KOPS_ZLIB_LIBS} Threads::Threads)
143
+ else()
144
+ target_link_libraries(bench_kittyops PRIVATE kittyops ${KOPS_ZLIB_LIBS} Threads::Threads)
145
+ endif()
146
+ target_include_directories(bench_kittyops PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
147
+ endif()
148
+
149
+ if(KITTYOPS_PYTHON_WHEEL)
150
+ if(TARGET kittyops)
151
+ install(TARGETS kittyops LIBRARY DESTINATION kittyops RUNTIME DESTINATION kittyops)
152
+ endif()
153
+ else()
154
+ install(FILES include/kittyops.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
155
+ if(TARGET kittyops_static)
156
+ install(TARGETS kittyops_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
157
+ endif()
158
+ if(TARGET kittyops)
159
+ install(TARGETS kittyops LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
160
+ endif()
161
+ endif()
kittyops-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Visionik <visionik@pobox.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,28 @@
1
+ BUILD_DIR ?= build
2
+ BUILD_TYPE ?= Release
3
+ PYTHON ?= python3
4
+
5
+ .PHONY: help build test bench python-test check clean
6
+
7
+ help: ## Show available targets
8
+ @awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z0-9_\/-]+:.*## / {printf " %-16s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
9
+
10
+ $(BUILD_DIR)/CMakeCache.txt: CMakeLists.txt
11
+ cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE)
12
+
13
+ build: $(BUILD_DIR)/CMakeCache.txt ## Build static + shared libkittyops, tests, and bench
14
+ cmake --build $(BUILD_DIR) -j
15
+
16
+ test: build ## Run C unit tests
17
+ ctest --test-dir $(BUILD_DIR) --output-on-failure
18
+
19
+ python-test: build ## Run Python binding tests
20
+ KITTYOPS_LIB=$(CURDIR)/$(BUILD_DIR)/libkittyops.dylib PYTHONPATH=$(CURDIR)/python $(PYTHON) -m unittest discover -s tests -p 'test_*.py' -v
21
+
22
+ bench: build ## Run 1080p diff / scroll / compression benchmarks
23
+ ./$(BUILD_DIR)/bench_kittyops
24
+
25
+ check: test python-test ## Pre-commit check (C + Python tests)
26
+
27
+ clean: ## Remove build artifacts
28
+ rm -rf $(BUILD_DIR)
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.2
2
+ Name: kittyops
3
+ Version: 0.1.0
4
+ Summary: High-performance C99 Kitty graphics accelerator (SIMD tile-diff, a=c scroll compose, parallel zlib/libdeflate) for pixcell and Python TUIs.
5
+ Keywords: kitty,terminal,graphics,pixcell,textual,simd,zlib
6
+ Author-Email: Visionik <visionik@pobox.com>
7
+ License: MIT
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: Environment :: Console
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: C
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Project-URL: Homepage, https://github.com/visionik/libkittyops
19
+ Project-URL: Repository, https://github.com/visionik/libkittyops
20
+ Requires-Python: >=3.11
21
+ Description-Content-Type: text/markdown
22
+
23
+ # libkittyops
24
+
25
+ High-performance, dependency-free **C99** pixel & protocol kernel for the [Kitty Graphics Protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/), shared by [`sdl-kitty`](https://github.com/visionik/sdl-kitty) and [`pixcell`](https://github.com/visionik/pixcell).
26
+
27
+ ## Features
28
+ - **SIMD multi-rect tile diff** (`ARM64 NEON` 32px + runtime-dispatched `x86_64 AVX2`/`SSSE3` clean-tile fast paths with exact 1px X-trimming + 6-worker 1080p bands + fused `XRGB8888`/`ARGB8888 → RGB24`/`RGBA32`).
29
+ - **Coalesce with dead-area bloat guard** (merges nearby rects without inflating pillarboxed games + corner HUDs).
30
+ - **Scroll detection + `a=c` ping-pong frame compose** (`~20 µs` row/column-hash scroll matcher → copies unchanged background inside terminal RAM in 45 bytes, sends `a=f` only for the new edge strip + moving sprites, and flips with `a=a`; auto-gated on terminal `storage_bytes >= 2 * w * h * 4`).
31
+ - **Hybrid parallel compression:** `libdeflate` for small/medium rects (`< 224 KB`) + zero-copy strided multi-slice parallel `zlib` / `zlib-ng` / `ISA-L` (`>= 224 KB`, up to 24 slices) + parallel multi-rect worker with `>= 256 KB` large-rect guard.
32
+ - **Kitty APC framing:** `a=t`, `a=T`, `a=f`, `a=c`, `a=a`, `a=p` (`U=1` virtual placements), `a=d`, `max_escape_bytes` chunking (omitting `m=0` on single-chunk for Rex compatibility), and POSIX `t=s` shared memory.
33
+ - **Thin Python binding** (`python/kittyops`) for optional `pixcell[fast]` acceleration.
34
+
35
+ ## Build, Test & Benchmark
36
+
37
+ ```bash
38
+ make check # Build C99 library + run C and Python unit tests
39
+ make bench # Run 1920x1080 diff, scroll-compose, and compression benchmarks
40
+ ```
41
+
42
+ See [docs/architecture.md](docs/architecture.md) for API details and `sdl-kitty` / `pixcell` integration notes.
@@ -0,0 +1,20 @@
1
+ # libkittyops
2
+
3
+ High-performance, dependency-free **C99** pixel & protocol kernel for the [Kitty Graphics Protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/), shared by [`sdl-kitty`](https://github.com/visionik/sdl-kitty) and [`pixcell`](https://github.com/visionik/pixcell).
4
+
5
+ ## Features
6
+ - **SIMD multi-rect tile diff** (`ARM64 NEON` 32px + runtime-dispatched `x86_64 AVX2`/`SSSE3` clean-tile fast paths with exact 1px X-trimming + 6-worker 1080p bands + fused `XRGB8888`/`ARGB8888 → RGB24`/`RGBA32`).
7
+ - **Coalesce with dead-area bloat guard** (merges nearby rects without inflating pillarboxed games + corner HUDs).
8
+ - **Scroll detection + `a=c` ping-pong frame compose** (`~20 µs` row/column-hash scroll matcher → copies unchanged background inside terminal RAM in 45 bytes, sends `a=f` only for the new edge strip + moving sprites, and flips with `a=a`; auto-gated on terminal `storage_bytes >= 2 * w * h * 4`).
9
+ - **Hybrid parallel compression:** `libdeflate` for small/medium rects (`< 224 KB`) + zero-copy strided multi-slice parallel `zlib` / `zlib-ng` / `ISA-L` (`>= 224 KB`, up to 24 slices) + parallel multi-rect worker with `>= 256 KB` large-rect guard.
10
+ - **Kitty APC framing:** `a=t`, `a=T`, `a=f`, `a=c`, `a=a`, `a=p` (`U=1` virtual placements), `a=d`, `max_escape_bytes` chunking (omitting `m=0` on single-chunk for Rex compatibility), and POSIX `t=s` shared memory.
11
+ - **Thin Python binding** (`python/kittyops`) for optional `pixcell[fast]` acceleration.
12
+
13
+ ## Build, Test & Benchmark
14
+
15
+ ```bash
16
+ make check # Build C99 library + run C and Python unit tests
17
+ make bench # Run 1920x1080 diff, scroll-compose, and compression benchmarks
18
+ ```
19
+
20
+ See [docs/architecture.md](docs/architecture.md) for API details and `sdl-kitty` / `pixcell` integration notes.
@@ -0,0 +1,119 @@
1
+ #include "kittyops.h"
2
+
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ static void fill_scene(uint8_t *fb, int32_t w, int32_t h, int32_t sx, int32_t sy, bool pillarbox)
8
+ {
9
+ const int32_t x0 = pillarbox ? (w - 810) / 2 : 0;
10
+ const int32_t x1 = pillarbox ? x0 + 810 : w;
11
+ int32_t y, x;
12
+ memset(fb, 0, (size_t)w * (size_t)h * 3u);
13
+ for (y = 0; y < h; ++y) {
14
+ const uint32_t py = (uint32_t)(y - sy);
15
+ const uint32_t row_band = py / 24u;
16
+ for (x = x0; x < x1; ++x) {
17
+ const uint32_t px = (uint32_t)(x - sx);
18
+ uint8_t *p = fb + ((size_t)y * (size_t)w + (size_t)x) * 3u;
19
+ /* Pixel-art / web-page-like structured tiles (compressible like real games/browsers) */
20
+ const uint32_t tile = ((px / 16u) * 31u + row_band * 17u) & 255u;
21
+ const uint32_t edge = ((px % 16u) == 0 || (py % 24u) == 0) ? 40u : 0u;
22
+ p[0] = (uint8_t)((tile + edge) & 255u);
23
+ p[1] = (uint8_t)(((tile * 3u) ^ (row_band * 19u)) & 255u);
24
+ p[2] = (uint8_t)((64u + ((px / 32u) * 23u) + edge) & 255u);
25
+ }
26
+ }
27
+ }
28
+
29
+ static void draw_sprite(uint8_t *fb, int32_t w, int32_t h,
30
+ int32_t bx, int32_t by, int32_t bw, int32_t bh,
31
+ uint8_t r, uint8_t g, uint8_t b)
32
+ {
33
+ int32_t y, x;
34
+ for (y = by; y < by + bh && y < h; ++y) {
35
+ for (x = bx; x < bx + bw && x < w; ++x) {
36
+ uint8_t *p = fb + ((size_t)y * (size_t)w + (size_t)x) * 3u;
37
+ p[0] = r; p[1] = g; p[2] = b;
38
+ }
39
+ }
40
+ }
41
+
42
+ static void run_case(const char *label, const uint8_t *prev, const uint8_t *curr,
43
+ int32_t w, int32_t h, const kops_caps *caps, int iters)
44
+ {
45
+ kops_encoder *enc = kops_encoder_create(3);
46
+ const char *apc = NULL;
47
+ size_t apc_len = 0;
48
+ kops_stats st;
49
+ double tot_ms = 0.0, diff_ms = 0.0, scroll_ms = 0.0, comp_ms = 0.0, enc_ms = 0.0;
50
+ size_t raw_kb = 0, wire_kb = 0;
51
+ int32_t rects = 0, sdx = 0, sdy = 0;
52
+ kops_present_mode mode = KOPS_MODE_SKIP;
53
+ int i;
54
+
55
+ /* Warmup */
56
+ kops_encode_frame(enc, 1, prev, curr, w, h, 3, caps, &apc, &apc_len, &st);
57
+
58
+ for (i = 0; i < iters; ++i) {
59
+ kops_encode_frame(enc, 1, prev, curr, w, h, 3, caps, &apc, &apc_len, &st);
60
+ diff_ms += (double)st.diff_ns / 1e6;
61
+ scroll_ms += (double)st.scroll_ns / 1e6;
62
+ comp_ms += (double)st.compress_ns / 1e6;
63
+ enc_ms += (double)st.encode_ns / 1e6;
64
+ tot_ms += (double)(st.diff_ns + st.scroll_ns + st.compress_ns + st.encode_ns) / 1e6;
65
+ raw_kb = st.raw_bytes / 1024u;
66
+ wire_kb = st.wire_bytes / 1024u;
67
+ rects = st.rect_count;
68
+ sdx = st.scroll_dx;
69
+ sdy = st.scroll_dy;
70
+ mode = st.mode;
71
+ }
72
+ printf("%-38s | mode=%d | total=%5.2f ms (diff=%.2f scr=%.2f comp=%.2f b64=%.2f) | raw=%4zu KB | wire=%4zu KB | rects=%2d | d=(%d,%d)\n",
73
+ label, (int)mode, tot_ms / iters, diff_ms / iters, scroll_ms / iters,
74
+ comp_ms / iters, enc_ms / iters, raw_kb, wire_kb, rects, (int)sdx, (int)sdy);
75
+ kops_encoder_destroy(enc);
76
+ }
77
+
78
+ int main(void)
79
+ {
80
+ const int32_t w = 1920, h = 1080;
81
+ const size_t sz = (size_t)w * (size_t)h * 3u;
82
+ uint8_t *a = (uint8_t *)malloc(sz);
83
+ uint8_t *b = (uint8_t *)malloc(sz);
84
+ kops_caps caps_no_scroll, caps_scroll;
85
+
86
+ kops_caps_default(&caps_no_scroll);
87
+ caps_no_scroll.allow_scroll_compose = false;
88
+ caps_scroll = caps_no_scroll;
89
+ caps_scroll.allow_scroll_compose = true;
90
+
91
+ printf("libkittyops 1920x1080 benchmark (backend: %s)\n", kops_compression_backend());
92
+ printf("---------------------------------------------------------------------------------------------------------------------\n");
93
+
94
+ /* 1. Static frame */
95
+ fill_scene(a, w, h, 0, 0, true);
96
+ memcpy(b, a, sz);
97
+ run_case("1080p static identical", a, b, w, h, &caps_no_scroll, 50);
98
+
99
+ /* 2. Vertical 3:4 arcade shmup (1942/Raiden-like: dy=+4 px + player ship + 3 bullets) */
100
+ fill_scene(a, w, h, 0, 0, true);
101
+ fill_scene(b, w, h, 0, 4, true);
102
+ draw_sprite(a, w, h, 930, 860, 64, 64, 240, 220, 40);
103
+ draw_sprite(b, w, h, 934, 860, 64, 64, 240, 220, 40);
104
+ draw_sprite(b, w, h, 956, 700, 12, 28, 255, 80, 40);
105
+ run_case("3:4 shmup scroll dy=+4 (standard a=f)", a, b, w, h, &caps_no_scroll, 30);
106
+ run_case("3:4 shmup scroll dy=+4 (a=c compose)", a, b, w, h, &caps_scroll, 30);
107
+
108
+ /* 3. Full 1080p browser page scroll (dy=-36 px + sticky 64px top navbar) */
109
+ fill_scene(a, w, h, 0, 0, false);
110
+ fill_scene(b, w, h, 0, -36, false);
111
+ draw_sprite(a, w, h, 0, 0, w, 64, 28, 32, 42);
112
+ draw_sprite(b, w, h, 0, 0, w, 64, 28, 32, 42);
113
+ run_case("1080p browser dy=-36 (standard a=f)", a, b, w, h, &caps_no_scroll, 30);
114
+ run_case("1080p browser dy=-36 (a=c compose)", a, b, w, h, &caps_scroll, 30);
115
+
116
+ free(a);
117
+ free(b);
118
+ return 0;
119
+ }
@@ -0,0 +1,23 @@
1
+ # libkittyops Architecture
2
+
3
+ `libkittyops` isolates the CPU-hot, framework-agnostic pixel diffing, scroll compensation, parallel zlib compression, and Kitty APC framing kernel so both C/SDL (`sdl-kitty`) and Python/Textual (`pixcell`) share a single tuned implementation without pulling SDL or Python into each other.
4
+
5
+ ## Pipeline (`kops_encode_frame`)
6
+
7
+ 1. **SIMD Tile Diff (`src/diff.c`):** Parallel band scan (4–6 workers) produces up to 16 tightly trimmed dirty rects (`KOPS_DIFF_NONE` skips the frame in `~0.08 ms`).
8
+ 2. **Scroll Detection & `a=c` Ping-Pong (`src/scroll.c`, `src/encoder.c`):**
9
+ - Triggered when `caps.compose && caps.allow_scroll_compose`, dirty coverage `>= 25%`, and terminal `storage_bytes >= 2 * w * h * 4` (protects Rex's 10 MB storage limit on 1080p full-screen while still fitting `<= 720p` and vertical `810×1080` arcade playfields).
10
+ - Computes 8px-strided row/column fingerprints across the interior ROI, votes on `(dx, dy)`, verifies pixel match `>= 60%`, and emits:
11
+ - `a=c,i=<id>,r=<src>,c=<dst>,w=...,h=...,x=<dst_x>,y=<dst_y>,X=<src_x>,Y=<src_y>,C=1,q=2`
12
+ - minimal `a=f,r=<dst>,...` edits for the newly revealed edge strip + moving sprites/sticky chrome
13
+ - `a=a,i=<id>,c=<dst>,s=1,q=2` to display `<dst>` (compatible with `U=1` Unicode placeholders).
14
+ 3. **Coalesce with Bloat Guard (`kops_coalesce_rects`):** Merges rects within `coalesce_gap` unless the union adds more than `coalesce_max_bloat` (default `64K`) clean pixels.
15
+ 4. **Hybrid Compression (`src/compress.c`):**
16
+ - `< 224 KB`: single-thread `libdeflate` (avoids thread wakeup jitter on small HUD/sprite rects).
17
+ - `>= 224 KB`: zero-copy strided parallel-pack + parallel-zlib (up to 24 slices via GCD on macOS / pthreads on POSIX), auto-detecting `zlib-ng` / `ISA-L` / system `zlib`.
18
+ - Multi-rect (`3..16` rects): parallel `libdeflate` workers, guarded so any rect `>= 256 KB` routes to multi-core strided parallel-zlib instead of serializing on 1 thread.
19
+ 5. **APC Framing (`src/apc.c`):** Chunked by `caps.max_escape_bytes` (omitting `m=0` on single-chunk frames for Rex), plus optional `t=s` POSIX shared memory for full frames on Kitty/Ghostty.
20
+
21
+ ## Consumers
22
+ - **`sdl-kitty`:** Links `libkittyops` directly into `libSDL3` (replacing duplicated rect-coalesce, `FastHash64`, `PackRect`, and `ConvertAndTileDiff` kernels while keeping SDL surface/window/HUD/async-TTY glue in SDL).
23
+ - **`pixcell`:** Stays pure Python by default; imports `kittyops.Encoder` when installed to accelerate `LiveImage` and `BrowserView` with zero-copy buffer handoff.