mirror of
https://github.com/Nexoniarz/YAPF.git
synced 2026-11-03 04:54:26 +00:00
Language bindings, KDE thumbnails, yapf compare, in-memory encoding
- yapf_encode() / yapf_free_buffer(): encode to memory; yapf_save uses it - yapf.c also compiles as C++ - `yapf compare`: size and decode speed of the original format vs YAPF, with a lossless check - bindings with tested examples: C++ (header-only), C# (P/Invoke), Rust (crate, builds the C code), Go (cgo), Java (FFM API, Java 22+), Zig (@cImport), LuaJIT (FFI), TypeScript types for the JS port; Python now encodes in memory - docs/LANGUAGES.md and examples/ with measured results - KDE Dolphin thumbnail plugin (KF6, CMake) - README: honest status for Photoshop / VS Code / Figma (not run in the apps yet) and macOS (cross-compiled only), platform table Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2ca726e3af
commit
d7f494a0c9
52 changed files with 1926 additions and 106 deletions
62
examples/README.md
Normal file
62
examples/README.md
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Examples: how fast, how small
|
||||
|
||||
## Try it on your own images
|
||||
|
||||
```sh
|
||||
make # builds build/yapf
|
||||
build/yapf compare photo.png ui.png # any PNG / JPG / TGA / BMP
|
||||
```
|
||||
|
||||
`yapf compare` encodes each image to YAPF in memory, checks that it decodes
|
||||
back to exactly the same pixels, and times decoding the original format
|
||||
against YAPF on one core and on all cores:
|
||||
|
||||
```
|
||||
ui_reverb.png (261 x 430, RGBA)
|
||||
raw 438.4 KB
|
||||
PNG 176.6 KB 40.3% of raw decode 2.049 ms
|
||||
YAPF 143.7 KB 32.8% of raw decode 0.170 ms 12.1x faster, 81% of the PNG size
|
||||
YAPF on all cores: decode 0.176 ms (2.5 GB/s), encode 8 ms, lossless: yes
|
||||
```
|
||||
|
||||
## Results on a mixed set of images
|
||||
|
||||
AMD Ryzen 7 5700G, `make` (gcc -O2), PNG decoded with stb_image. The test
|
||||
images (a 4K wallpaper, UI screenshots, an app icon and seamless textures
|
||||
from open-source projects) are not redistributed here; run `yapf compare`
|
||||
on your own files to reproduce.
|
||||
|
||||
| Image | PNG | YAPF | YAPF / PNG size | PNG decode | YAPF decode | Faster |
|
||||
| :---- | --: | ---: | --------------: | ---------: | ----------: | -----: |
|
||||
| UI screenshot 261×430 | 176.6 KB | 143.7 KB | **81 %** | 2.05 ms | 0.17 ms | **12×** |
|
||||
| UI screenshot 305×529 | 201.5 KB | 185.0 KB | **92 %** | 2.55 ms | 0.24 ms | **11×** |
|
||||
| App icon 128×128 | 24.9 KB | 23.1 KB | **93 %** | 0.30 ms | 0.026 ms | **11×** |
|
||||
| Wallpaper 3840×2160 | 1.77 MB | 2.21 MB | 125 % | 73.7 ms | 6.8 ms | **11×** |
|
||||
| Rock texture 512×512 | 182.6 KB | 221.8 KB | 121 % | 1.64 ms | 0.17 ms | **10×** |
|
||||
| Wood texture 512×512 | 185.8 KB | 234.1 KB | 126 % | 1.64 ms | 0.17 ms | **10×** |
|
||||
| Canvas texture 512×512 | 171.5 KB | 245.6 KB | 143 % | 1.36 ms | 0.16 ms | **9×** |
|
||||
| YAPF logo 512×512 | 60.5 KB | 89.3 KB | 148 % | 1.25 ms | 0.25 ms | **5×** |
|
||||
|
||||
In short: **smaller than PNG on UI-style images, 20–50 % larger on photos
|
||||
and textures, and 5–12× faster to decode** — always lossless. The decoder
|
||||
reaches 5–6 GB/s on large images on one core, and more with
|
||||
`yapf_load_mt`.
|
||||
|
||||
## Code examples
|
||||
|
||||
| Language | Example |
|
||||
| :------- | :------ |
|
||||
| C | [`examples/c/example.c`](c/example.c) |
|
||||
| C++ | [`bindings/cpp/example.cpp`](../bindings/cpp/example.cpp) |
|
||||
| Python | [`bindings/python/example.py`](../bindings/python/example.py) |
|
||||
| C# | [`bindings/csharp/Example`](../bindings/csharp/Example) |
|
||||
| Rust | [`bindings/rust/examples/demo.rs`](../bindings/rust/examples/demo.rs) |
|
||||
| Go | [`bindings/go/example`](../bindings/go/example) |
|
||||
| Java | [`bindings/java/Example.java`](../bindings/java/Example.java) |
|
||||
| Zig | [`bindings/zig/example.zig`](../bindings/zig/example.zig) |
|
||||
| Lua | [`bindings/lua/example.lua`](../bindings/lua/example.lua) |
|
||||
| JavaScript | [`bindings/js/example.mjs`](../bindings/js/example.mjs) |
|
||||
|
||||
Each one loads a `.yapf` file, prints its size and decode time, re-encodes
|
||||
it (byte-identical to the file) and writes a new image from its own pixels.
|
||||
See [docs/LANGUAGES.md](../docs/LANGUAGES.md) for setup in each language.
|
||||
61
examples/c/example.c
Normal file
61
examples/c/example.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* example.c — load, inspect, time and write YAPF images from C.
|
||||
*
|
||||
* cc -O2 -std=c99 -I../.. example.c ../../yapf.c -o example -pthread
|
||||
* ./example ../../other/YAPF.YAPF
|
||||
*/
|
||||
#include "yapf.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
static double seconds(void) { return (double)clock() / CLOCKS_PER_SEC; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const char *path = argc > 1 ? argv[1] : "../../other/YAPF.YAPF";
|
||||
|
||||
/* 1. Load a file (yapf_load_memory works the same on a buffer). */
|
||||
yapf_image_t *img = yapf_load(path);
|
||||
if (!img) { fprintf(stderr, "cannot load %s\n", path); return 1; }
|
||||
double t = 1e9; /* decode time: best of 10 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
double t0 = seconds();
|
||||
yapf_free(yapf_load(path));
|
||||
t0 = seconds() - t0;
|
||||
if (t0 < t) t = t0;
|
||||
}
|
||||
printf("%s: %u x %u, %u channels, %u mip level(s), decoded in %.2f ms\n",
|
||||
path, img->width, img->height, img->channels, img->mip_levels, t * 1e3);
|
||||
|
||||
/* 2. Pixels: width * height * channels bytes, rows top to bottom. */
|
||||
const uint8_t *p = img->pixels;
|
||||
printf("top-left pixel: %u %u %u %u\n", p[0], p[1], p[2], img->channels == 4 ? p[3] : 255);
|
||||
|
||||
/* 3. Encode to memory and to a file. */
|
||||
void *data = NULL;
|
||||
size_t size = 0;
|
||||
if (yapf_encode(img, &data, &size) == YAPF_OK) {
|
||||
printf("re-encoded: %zu bytes (%.1f%% of raw)\n", size,
|
||||
100.0 * size / ((double)img->width * img->height * img->channels));
|
||||
yapf_free_buffer(data);
|
||||
}
|
||||
|
||||
/* 4. Create an image from your own pixels. */
|
||||
enum { W = 256, H = 256 };
|
||||
static uint8_t rgba[W * H * 4];
|
||||
for (int y = 0; y < H; y++)
|
||||
for (int x = 0; x < W; x++) {
|
||||
uint8_t *q = rgba + (y * W + x) * 4;
|
||||
q[0] = (uint8_t)x; q[1] = (uint8_t)y; q[2] = 128; q[3] = 255;
|
||||
}
|
||||
yapf_image_t mine;
|
||||
memset(&mine, 0, sizeof(mine));
|
||||
mine.width = W; mine.height = H; mine.channels = 4; mine.mip_levels = 1;
|
||||
mine.pixels = rgba; mine.flags = YAPF_FLAG_SRGB; mine.gpu_format = YAPF_GPU_SRGB8_A8;
|
||||
if (yapf_save("gradient.yapf", &mine) == YAPF_OK) printf("wrote gradient.yapf\n");
|
||||
|
||||
yapf_free(img);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue