Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

docs: a README in the house style, with a demo video and screenshots

Josh0231ed8b5f86
diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md
index 5815876..27105c0 100644
--- a/ATTRIBUTION.md
+++ b/ATTRIBUTION.md
@@ -16,6 +16,7 @@ them.
| `apps/procraft-android/android/app/src/main/assets/environments/*.hdr` | 3D view light panoramas (11), halved to 512 x 256 and scaled by `tools/env/make_env.py` | Sergej Majboroda, Greg Zaal, Andreas Mischok, Rico Cilliers (per file in the folder's `README.md`) | <https://polyhaven.com> | CC0 1.0 |
| `assets/app-icon/procraft-{1024,512}.png`, `apps/procraft-android/android/app/src/main/res/drawable-*dpi/ic_launcher_{foreground,monochrome}.png`, `apps/procraft-android/android/app/src/main/res/drawable-nodpi/procraft_mark.png`, `apps/procraft-android/android/app/src/main/res/mipmap-anydpi/ic_launcher.xml` | App icon: a paintbrush in a warm-to-cool blend on black | ProCraft contributors | Original work, drawn in code by `tools/app-icon/make_icon.py`; see [`assets/app-icon/README.md`](assets/app-icon/README.md) | MIT OR Apache-2.0 |
| `apps/procraft-android/android/app/src/main/res/drawable/widget_back.xml`, `apps/procraft-android/android/app/src/main/res/drawable-v31/widget_back.xml` | Recent-artwork widget background | ProCraft contributors | Original work | MIT OR Apache-2.0 |
+| `docs/images/{canvas,layers,brushes,studio,colour,actions,adjust,transform}.jpg`, `docs/images/demo.gif`, `docs/images/demo.mp4` | The README's screenshots and demo: ProCraft on a tablet, painting *Dusk* | ProCraft contributors | Original work: the painting made in the app by `tools/showcase/paint_dusk.py`, captured from the screen | MIT OR Apache-2.0 |
| Brushes, brush shapes and grains | Made in code at run time (`crates/engine`) | ProCraft contributors | Original work | MIT OR Apache-2.0 |
### Lucide icons in `LucideIcons.kt`
diff --git a/README.md b/README.md
index ba5910a..5cca302 100644
--- a/README.md
+++ b/README.md
@@ -1,15 +1,142 @@
-<img src="assets/app-icon/procraft-512.png" alt="ProCraft" width="96" align="right">
+<p align="center">
+ <img src="assets/app-icon/procraft-1024.png" alt="ProCraft's app icon: a paintbrush in a warm-to-cool blend on a black tile" width="128">
+</p>
-# ProCraft
+<h1 align="center">ProCraft for Android</h1>
-A painting app for Android tablets, with a Rust engine and GPU renderer under a Kotlin / Jetpack
-Compose interface.
+<p align="center">
+ <b>An Android native open-source painting app for tablets, with its own Rust engine and GPU renderer under a Kotlin/Jetpack Compose UI.</b><br>
+ Pressure-sensitive brushes, layers with blend modes, adjustments, selection and transform, animation and 3D painting, written from scratch.
+</p>
-**Status:** beta. Layers with blend modes, masks and groups; a brush engine with its studio and
-library; colour, adjustments, selection and transform; text, animation, 3D painting; and opening
-and sharing artworks, PSD, PDF, images and brushes.
+<p align="center">
+ <a href="https://github.com/artcraft-android/procraft-android/releases"><img src="https://raw.githubusercontent.com/Kunzisoft/Github-badge/main/get-it-on-github.png" alt="Get it on GitHub" height="60"></a>
+ <a href="https://apps.obtainium.imranr.dev/redirect?r=obtainium://add/https://github.com/artcraft-android/procraft-android"><img src="https://raw.githubusercontent.com/ImranR98/Obtainium/main/assets/graphics/badge_obtainium.png" alt="Get it on Obtainium" height="60"></a>
+</p>
-## Layout
+<p align="center">
+ <img alt="Rust engine" src="https://img.shields.io/badge/engine-Rust-lightblue?style=flat&logo=rust&logoColor=white">
+ <img alt="GPU renderer: wgpu" src="https://img.shields.io/badge/renderer-wgpu-lightblue?style=flat">
+ <img alt="Kotlin and Jetpack Compose" src="https://img.shields.io/badge/UI-Kotlin%20%C2%B7%20Jetpack%20Compose-blue?style=flat">
+ <img alt="Minimum: Android 10 (API 29)" src="https://img.shields.io/badge/min-Android%2010%20%28API%2029%29-blue?style=flat">
+ <img alt="Target: Android 16 (API 36)" src="https://img.shields.io/badge/target-Android%2016%20%28API%2036%29-blue?style=flat">
+ <img alt="License: MIT OR Apache-2.0" src="https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue?style=flat">
+ <img alt="Status: beta" src="https://img.shields.io/badge/status-beta-lightblue?style=flat">
+</p>
+
+<p align="center">
+ <a href="#painting">Painting</a> ·
+ <a href="#layers-and-colour">Layers &amp; colour</a> ·
+ <a href="#adjustments-and-transform">Adjustments &amp; transform</a> ·
+ <a href="#actions">Actions</a> ·
+ <a href="#planned-features">Planned features</a> ·
+ <a href="#building-instructions">Building</a>
+</p>
+
+<br>
+
+<p align="center">
+ <img src="docs/images/demo.gif" alt="Dusk painted in ProCraft from a blank canvas: the sky filled indigo and airbrushed in bands of purple, magenta, orange and gold; a glow and a sun drawn as a circle that snaps round and is filled by dragging the colour in; two ranges of hills outlined and filled the same way; pines and birds inked; stars airbrushed and made to glow with Bloom; the Layers panel opened on its seven layers" width="100%">
+ <br>
+ <sub><b>Dusk</b>, painted from a blank canvas on a Lenovo Legion Tab: the sky filled and airbrushed, a sun snapped round and filled by dragging the colour in, hills filled the same way, pines inked, stars with Bloom. 4× playback with the idle pauses cut; <a href="docs/images/demo.mp4">full video</a> (2×). Every stroke is a pen stroke with pressure, played by the <a href="tools/showcase">demo script</a>.</sub>
+</p>
+
+<br>
+
+<p align="center">
+ <img src="docs/images/canvas.jpg" alt="The finished Dusk on the canvas: a sky from deep indigo through purple, magenta and orange to gold, a pale sun with a soft glow, birds, two ranges of purple hills with dark pines, and stars; the top bar with Gallery and the tools, and the sidebar with the size and opacity sliders, undo and redo" width="100%">
+ <br>
+ <sub>The canvas: the tools along the top, size and opacity in the sidebar, the whole screen for the painting.</sub>
+</p>
+
+<br>
+
+## Painting
+
+<table>
+<tr>
+<td width="50%"><img src="docs/images/brushes.jpg" alt="The Brush library over Dusk: the sets Sketching, Inking, Drawing, Painting (chosen), Watercolour, Calligraphy, Airbrushing, Spray Paint, Charcoals, Textures and Abstract, each with its icon, and the Painting set's brushes with a preview stroke each"><br><sub>The brush library: eleven sets of our own brushes, each previewed with a stroke.</sub></td>
+<td width="50%"><img src="docs/images/studio.jpg" alt="The Brush Studio on Round Brush: the tabs from Stroke path to About this brush, the Shape tab with its shape source and input style, and the Drawing Pad with a pressure scribble"><br><sub>The Brush Studio: stroke path, stabilization, taper, shape, grain, rendering, wet mix, colour dynamics and more, tried out on the drawing pad.</sub></td>
+</tr>
+</table>
+
+<br>
+
+## Layers and colour
+
+<table>
+<tr>
+<td width="50%"><img src="docs/images/layers.jpg" alt="The Layers panel over Dusk: seven layers, each with its thumbnail (stars, pines, the near and far hills, the sun, its glow and the sky), their blend mode letters and visibility boxes, and the background colour"><br><sub>Layers with blend modes, opacity, alpha lock, clipping masks, masks and groups. Layers past what the GPU holds are kept compressed, so artworks with hundreds of layers open and paint smoothly.</sub></td>
+<td width="50%"><img src="docs/images/colour.jpg" alt="The Colors panel on its Disc tab: a hue ring around a saturation and brightness disc, the current and previous colours, and the Classic, Harmony, Value and Palettes tabs"><br><sub>Colour: disc, classic, harmony, value (with hex) and palettes.</sub></td>
+</tr>
+</table>
+
+<br>
+
+## Adjustments and transform
+
+<table>
+<tr>
+<td width="50%"><img src="docs/images/adjust.jpg" alt="Hue, Saturation, Brightness on Dusk's sky: the hue moved to 40 percent turns the sky blue at the top and pink at the horizon, with the Hue, Saturation and Brightness sliders below"><br><sub>Hue, saturation, brightness on the sky layer, live. Also colour balance, curves, gradient map, blurs, noise, sharpen, bloom, glitch, halftone, chromatic aberration, liquify and clone, on the layer or painted on.</sub></td>
+<td width="50%"><img src="docs/images/transform.jpg" alt="Transform in Warp on the sun layer: a mesh with corner handles over the sun, and the toolbar with Freeform, Uniform, Distort and Warp, then Advanced Mesh, Flip Horizontal, Flip Vertical, Rotate 45, Fit to Canvas, Bilinear and Reset"><br><sub>Transform: freeform, uniform, distort and warp, with snapping. Selection: automatic, freehand, rectangle and ellipse, with feather.</sub></td>
+</tr>
+</table>
+
+<br>
+
+## Actions
+
+<p align="center">
+ <img src="docs/images/actions.jpg" alt="The Actions panel on its Canvas tab over Dusk: Crop and Resize, the Animation Assist, Page Assist, Drawing Guide and Reference switches, Edit Drawing Guide, Flip horizontal, Flip vertical and Canvas information, under the Add, Canvas, Share, Video, Prefs and Help tabs" width="100%">
+ <br>
+ <sub>Actions: add files, photos and text; the canvas, its guides and assists; share as PNG, JPEG, PSD, PDF, TIFF, GIF or MP4; a time-lapse of every artwork; preferences and gesture controls.</sub>
+</p>
+
+<br>
+
+## Planned features
+
+ProCraft for Android is in beta.
+
+| Area | Status |
+|---|---|
+| Canvas: the top bar, the sidebar (size, opacity, modify, undo, redo), full screen, gestures (pinch, rotate, quick fit, two- and three-finger taps, scrub to clear) | ✅ |
+| Brush engine: shape and grain sources, stroke path, stabilization, taper, rendering and glazes, wet mix, colour dynamics, pen pressure and tilt; the library and the Brush Studio | ✅ |
+| Layers: blend modes, opacity, alpha lock, clipping masks, masks, groups, merge, the reference layer; large artworks kept compressed off the GPU | ✅ |
+| Colour: disc, classic, harmony, value and palettes; history; eyedropper; filling by dragging the colour onto the canvas | ✅ |
+| Adjustments, each with its toolbar, on the layer or painted on | ✅ |
+| Selection (automatic, freehand, shapes, feather) and transform (freeform, uniform, distort, warp, snapping) | ✅ |
+| Shapes that snap clean, drawing guides and drawing assist (2D, isometric, perspective, symmetry), text | ✅ |
+| Animation and page assists, the reference window | ✅ |
+| Gallery: stacks, new-canvas presets, import and share (PNG, JPEG, PSD, PDF, TIFF, GIF, MP4), time-lapse | 🚧 nearly done |
+| 3D painting: OBJ and USDZ models, texture sets, painting on the mesh, lights and environments | 🚧 |
+| Phone layout | ⬜ |
+
+<sub>✅ works today · 🚧 in progress · ⬜ not started · 🚫 not planned</sub>
+
+<br>
+
+## Building Instructions
+*One Gradle command builds the whole app: it compiles the Rust engine with [cargo-ndk](https://github.com/bbqsrc/cargo-ndk) and generates the Kotlin bindings.*
+
+* Clone this repository
+* Make sure you have at least 6 GB of free disk space
+* enter `apps/procraft-android/android` and run `./gradlew assembleRelease -Pprocraft.abis=arm64-v8a`, or open that folder in Android Studio and press Run.
+
+Build time dependencies:
+
+<details>
+ <summary>required tools</summary>
+
+ ```
+ rustup, with the aarch64-linux-android and x86_64-linux-android targets
+ cargo install cargo-ndk --version "^4" --locked
+ Android SDK (API 37) and NDK 29.0.14206865
+ JDK 17 or newer
+ ```
+</details>
+
+`cargo test --workspace` runs the engine's and the renderer's tests on the desktop.
| Path | What |
|---|---|
@@ -18,18 +145,11 @@ and sharing artworks, PSD, PDF, images and brushes.
| `apps/procraft-android` | the native library for Android (UniFFI bindings) |
| `apps/procraft-android/android` | the Android app (Gradle) |
-## Building
+*Builds are only tested on Windows 11.*
-```
-cargo test --workspace
-cd apps/procraft-android/android
-./gradlew :app:assembleRelease -Pprocraft.abis=arm64-v8a
-```
+<br>
-You need Rust with the Android targets, `cargo-ndk`, the Android SDK with NDK 29.0.14206865, and
-JDK 17.
-
-## Licence
+## License
[MIT](LICENSE-MIT) OR [Apache-2.0](LICENSE-APACHE), at your option. The third-party material the app
bundles (the Inter font, Lucide icons, light panoramas) and the libraries it is built with are listed
@@ -37,4 +157,11 @@ in [NOTICE](NOTICE); every asset, with its author, source and licence, in
[ATTRIBUTION.md](ATTRIBUTION.md).
Icons drawn using vector outlines ([`tools/icons/draw_icons.py`](tools/icons/draw_icons.py), to the
-[style guide](docs/icons/STYLE_GUIDE.md)).
+[style guide](docs/icons/STYLE_GUIDE.md)). The demo painting, *Dusk*, is our own, made in the app.
+
+<br>
+
+## Misc info for myself
+
+Try to use [semantic commit](https://kapeli.com/cheat_sheets/Semantic_Commits.docset/Contents/Resources/Documents/index)
+messages. Use `feat:` / `fix:` / `chore:` / `docs:` / etc.
diff --git a/docs/images/actions.jpg b/docs/images/actions.jpg
new file mode 100644
index 0000000..f94d6f9
Binary files /dev/null and b/docs/images/actions.jpg differ
diff --git a/docs/images/adjust.jpg b/docs/images/adjust.jpg
new file mode 100644
index 0000000..8c486ae
Binary files /dev/null and b/docs/images/adjust.jpg differ
diff --git a/docs/images/brushes.jpg b/docs/images/brushes.jpg
new file mode 100644
index 0000000..901a185
Binary files /dev/null and b/docs/images/brushes.jpg differ
diff --git a/docs/images/canvas.jpg b/docs/images/canvas.jpg
new file mode 100644
index 0000000..201a07b
Binary files /dev/null and b/docs/images/canvas.jpg differ
diff --git a/docs/images/colour.jpg b/docs/images/colour.jpg
new file mode 100644
index 0000000..016a4e4
Binary files /dev/null and b/docs/images/colour.jpg differ
diff --git a/docs/images/demo.gif b/docs/images/demo.gif
new file mode 100644
index 0000000..597e4b5
Binary files /dev/null and b/docs/images/demo.gif differ
diff --git a/docs/images/demo.mp4 b/docs/images/demo.mp4
new file mode 100644
index 0000000..db7379a
Binary files /dev/null and b/docs/images/demo.mp4 differ
diff --git a/docs/images/layers.jpg b/docs/images/layers.jpg
new file mode 100644
index 0000000..402b533
Binary files /dev/null and b/docs/images/layers.jpg differ
diff --git a/docs/images/studio.jpg b/docs/images/studio.jpg
new file mode 100644
index 0000000..b8d7e86
Binary files /dev/null and b/docs/images/studio.jpg differ
diff --git a/docs/images/transform.jpg b/docs/images/transform.jpg
new file mode 100644
index 0000000..75e2c6b
Binary files /dev/null and b/docs/images/transform.jpg differ
diff --git a/tools/showcase/README.md b/tools/showcase/README.md
new file mode 100644
index 0000000..9bf80e2
--- /dev/null
+++ b/tools/showcase/README.md
@@ -0,0 +1,29 @@
+# The showcase
+
+The README's demo (`docs/images/demo.gif`, `demo.mp4`) and screenshots were made with these scripts
+on a Lenovo Legion Tab (2560 x 1600, held sideways), with ProCraft open on an empty gallery.
+
+- `penhid.py`: a virtual pen digitizer (tip, in range, X, Y and 12-bit pressure) for Android's `hid`
+ shell command, and scripts of pen strokes with real timing.
+- `drive.py`: adb taps for the interface (brushes, colours by hex, sizes, layers, the colour dragged
+ onto the canvas to fill) and pen strokes in screen pixels.
+- `paint_dusk.py`: paints *Dusk* from a blank canvas, stage by stage
+ (`new sky sun hills trees stars bloom tour gallery`).
+- `to_srgb.py`: the tablet's captures hold Display P3 values; this makes sRGB JPEGs for the web.
+
+```sh
+export ANDROID_SERIAL=<the tablet>
+adb shell screenrecord --size 1920x1200 --bit-rate 16M --time-limit 0 /sdcard/showcase.mp4 &
+python paint_dusk.py
+adb shell pkill -INT screenrecord && adb pull /sdcard/showcase.mp4
+
+# drop the idle frames, turn P3 into sRGB, then 2x for the video and 4x for the GIF
+ffmpeg -i showcase.mp4 -vf "mpdecimate=hi=768:lo=320:frac=0.33,setpts=N/(30*TB),colorspace=all=bt709:iprimaries=smpte432:itrc=srgb:ispace=bt709:irange=tv,format=yuv420p" -r 30 -an dedup.mp4
+ffmpeg -i dedup.mp4 -vf "setpts=PTS/2,scale=1600:1000:flags=lanczos,tpad=stop_mode=clone:stop_duration=3" -r 30 -an -crf 23 -movflags +faststart demo.mp4
+ffmpeg -i dedup.mp4 -vf "setpts=PTS/4,fps=12,scale=800:-1:flags=lanczos,tpad=stop_mode=clone:stop_duration=2.5,split[a][b];[a]palettegen=max_colors=200:stats_mode=diff[p];[b][p]paletteuse=dither=bayer:bayer_scale=4:diff_mode=rectangle" demo.gif
+```
+
+Notes from making it: start pen strokes clear of the sidebar (left) and of any system handle on the
+screen's edge; drag the colour out of the colour button with a plain swipe (holding first takes the
+previous colour) starting a little below the top edge (nearer, it pulls down the system bars); and
+give a fill a closed outline inside the canvas, its points no more than a stroke's width apart.
diff --git a/tools/showcase/drive.py b/tools/showcase/drive.py
new file mode 100644
index 0000000..df3b46a
--- /dev/null
+++ b/tools/showcase/drive.py
@@ -0,0 +1,187 @@
+"""Drives ProCraft on a tablet (landscape, 2560 x 1600) for the showcase: adb taps for the
+interface, and pen strokes with pressure through a virtual HID pen (penhid.py, Android's `hid`).
+The positions are measured on a Lenovo Legion Tab; set ANDROID_SERIAL to pick the device.
+"""
+import math
+import os
+import subprocess
+import sys
+import time
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+sys.path.insert(0, HERE)
+from penhid import Script # noqa: E402
+
+ADB = os.environ.get("ADB", "adb")
+W, H = 2560, 1600
+
+# the digitizer's axes on the landscape screen: its Y runs left to right, its X bottom to top
+def screen_to_pen(x, y):
+ return 1.0 - y / H, x / W
+
+
+def adb(*args, check=True):
+ return subprocess.run([ADB, *args], check=check, capture_output=True, text=True).stdout
+
+
+def sh(cmd):
+ return adb("shell", cmd)
+
+
+def tap(x, y, wait=0.35):
+ sh(f"input tap {int(x)} {int(y)}")
+ time.sleep(wait)
+
+
+def taps(points, wait=0.35):
+ sh("; ".join(f"input tap {int(x)} {int(y)}" for x, y in points))
+ time.sleep(wait)
+
+
+def swipe(x1, y1, x2, y2, ms=400, wait=0.4):
+ sh(f"input swipe {int(x1)} {int(y1)} {int(x2)} {int(y2)} {int(ms)}")
+ time.sleep(wait)
+
+
+def drag(x1, y1, x2, y2, ms=900, wait=0.6):
+ """A finger drag that holds still at the start first (for drag-and-drop)."""
+ sh(f"input draganddrop {int(x1)} {int(y1)} {int(x2)} {int(y2)} {int(ms)}")
+ time.sleep(wait)
+
+
+def colour_drop(x, y, wait=1.2):
+ """ColorDrop: the colour dragged out of the colour button and let go at (x, y). A plain swipe:
+ holding first would take the previous colour, and starting nearer the top edge pulls down the
+ system bars."""
+ swipe(2432, 74, x, y, 900, wait=wait)
+
+
+class Pen:
+ """Collects strokes in screen pixels, then plays them in one `hid` run."""
+
+ def __init__(self):
+ self.s = Script(map_xy=screen_to_pen)
+
+ def stroke(self, pts, step_ms=8):
+ self.s.stroke(pts, step_ms)
+ return self
+
+ def pause(self, ms):
+ self.s.delay(ms)
+ return self
+
+ def play(self, name="pen"):
+ local = os.path.join(HERE, f"{name}.json")
+ self.s.write(local)
+ adb("push", local, f"/data/local/tmp/{name}.json")
+ sh(f"hid /data/local/tmp/{name}.json")
+ time.sleep(0.3)
+
+
+# the interface, in screen pixels
+PAINT, SMUDGE, ERASE, LAYERS, COLOUR = (1876, 44), (2016, 44), (2156, 44), (2296, 44), (2432, 44)
+ACTIONS, ADJUST, SELECT, TRANSFORM = (268, 44), (384, 44), (500, 44), (616, 44)
+SETS = {"Sketching": 290, "Inking": 414, "Drawing": 538, "Painting": 662, "Watercolour": 786,
+ "Calligraphy": 910, "Airbrushing": 1034, "Spray Paint": 1158, "Charcoals": 1282, "Textures": 1406,
+ "Abstract": 1530}
+HEX_FIELD = (2420, 912)
+HEX_KEYS = {"D": (1355, 642), "E": (1517, 642), "F": (1679, 642), "A": (1355, 750), "B": (1517, 750),
+ "C": (1679, 750), "7": (1355, 858), "8": (1517, 858), "9": (1679, 858), "4": (1355, 966),
+ "5": (1517, 966), "6": (1679, 966), "1": (1355, 1074), "2": (1517, 1074), "3": (1679, 1074),
+ "0": (1436, 1182), "del": (1679, 1182)}
+PANEL_BLANK = (2160, 1000)
+SIZE_TRACK = (50, 340, 660) # x, top (largest), bottom (smallest)
+OPACITY_TRACK = (50, 830, 1140)
+
+
+def ui():
+ """The screen's interface tree, as uiautomator dumps it."""
+ for _ in range(3):
+ out = sh("uiautomator dump /sdcard/u.xml >/dev/null 2>&1; cat /sdcard/u.xml")
+ if "<hierarchy" in out:
+ return out
+ time.sleep(0.4)
+ return ""
+
+
+def bounds_of(text, tree=None):
+ """The centre of the first node whose text is `text`, or None."""
+ import re
+ m = re.search(r'text="' + re.escape(text) + r'"[^>]*?bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"', tree or ui())
+ if not m:
+ return None
+ x0, y0, x1, y1 = map(int, m.groups())
+ return (x0 + x1) // 2, (y0 + y1) // 2
+
+
+def panel_open():
+ return 'text="Colors"' in ui()
+
+
+def colour(hexcode, wait=0.25):
+ """Sets the colour by hex in the colour panel's Value tab, checking each step, and closes it."""
+ want = "#" + hexcode.upper().lstrip("#")
+ for _ in range(3):
+ if not panel_open():
+ tap(*COLOUR, wait=0.8)
+ tap(*HEX_FIELD, wait=0.6)
+ keys = [HEX_KEYS["del"]] * 6 + [HEX_KEYS[c] for c in want[1:]]
+ taps(keys, wait=0.3)
+ tap(*PANEL_BLANK, wait=0.5)
+ if f'text="{want}"' in ui():
+ break
+ if panel_open():
+ tap(*COLOUR, wait=wait)
+
+
+def library_open():
+ return 'text="Brush library"' in ui()
+
+
+def brush(set_name, index, wait=0.3):
+ """Picks a brush from the library: its set, then its row (0 first)."""
+ if not library_open():
+ tap(*PAINT, wait=0.8)
+ if not library_open():
+ tap(*PAINT, wait=0.8)
+ tap(1620, SETS[set_name], wait=0.9)
+ tap(2000, 300 + 176 * index, wait=0.8)
+ tree = ui()
+ if 'text="Brush Studio"' in tree:
+ # the brush was already chosen, and a second tap opens its studio: leave it unchanged
+ at = bounds_of("Cancel", tree)
+ tap(*(at or (2333, 84)), wait=0.8)
+ tree = ui()
+ if 'text="Brush library"' in tree:
+ tap(*PAINT, wait=wait)
+
+
+def size(frac, wait=0.3):
+ """The brush size slider: all the way down, then up to `frac` of its track."""
+ x, top, bottom = SIZE_TRACK
+ swipe(x, top + 10, x, bottom + 300, 300, wait=0.2)
+ swipe(x, bottom, x, bottom - frac * (bottom - top), 500, wait=wait)
+
+
+def opacity(frac, wait=0.3):
+ """The brush opacity slider: all the way down, then up to `frac` of its track."""
+ x, top, bottom = OPACITY_TRACK
+ swipe(x, top + 10, x, bottom + 300, 300, wait=0.2)
+ swipe(x, bottom, x, bottom - frac * (bottom - top) - (40 if frac >= 1 else 0), 500, wait=wait)
+
+
+def new_layer(wait=0.5):
+ tap(*LAYERS, wait=0.6)
+ tap(2460, 176, wait=0.5)
+ tap(*LAYERS, wait=wait)
+
+
+def wave(x0, x1, y, amp, waves, pressure, n=90, phase=0.0, p_amp=0.12):
+ pts = []
+ for i in range(n + 1):
+ t = i / n
+ x = x0 + (x1 - x0) * t
+ yy = y + amp * math.sin(2 * math.pi * waves * t + phase)
+ p = pressure + p_amp * math.sin(math.pi * t)
+ pts.append((x, yy, min(1.0, p)))
+ return pts
diff --git a/tools/showcase/paint_dusk.py b/tools/showcase/paint_dusk.py
new file mode 100644
index 0000000..8588b9a
--- /dev/null
+++ b/tools/showcase/paint_dusk.py
@@ -0,0 +1,164 @@
+"""Paints "Dusk" in ProCraft on the tablet, start to finish, the way a person would: a new canvas,
+a sky filled and airbrushed, a sun drawn with QuickShape and filled with ColorDrop, two ranges of
+hills filled with ColorDrop, pines, stars with Bloom, a look at the layers, back to the gallery.
+
+usage: python -I paint_dusk.py [stage ...] (stages: new sky sun hills trees stars bloom tour gallery)
+"""
+import math
+import os
+import random
+import sys
+import time
+
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+import drive as d # noqa: E402
+
+random.seed(7)
+
+
+def hill(x, base, a1, f1, p1, a2, f2, p2):
+ return base + a1 * math.sin(f1 * x / 2560 * 2 * math.pi + p1) + a2 * math.sin(f2 * x / 2560 * 2 * math.pi + p2)
+
+
+FAR = dict(base=1150, a1=70, f1=1.3, p1=0.6, a2=28, f2=3.7, p2=1.9)
+NEAR = dict(base=1330, a1=55, f1=0.9, p1=2.4, a2=22, f2=4.3, p2=0.4)
+SUN = (1760, 1010, 118)
+
+
+def new_canvas():
+ d.tap(2476, 65, wait=1.0)
+ at = d.bounds_of("Screen Size")
+ d.tap(*at, wait=2.5)
+
+
+def sky():
+ d.brush("Airbrushing", 0)
+ d.size(1.0)
+ d.opacity(1.0)
+ d.colour("1E1B4B", wait=1.0)
+ d.colour_drop(1280, 700)
+ for hexcode, ys in (("4A2A7E", (560, 660)), ("B33F7C", (860, 950)), ("F07F4F", (1090, 1160)),
+ ("FFC46B", (1270, 1340))):
+ d.colour(hexcode, wait=0.5)
+ pen = d.Pen()
+ for k, y in enumerate(ys):
+ # start clear of the sidebar (left) and the system's edge handle (right, 820-1040)
+ x0, x1 = (140, 2552) if 820 <= y <= 1040 else (2552, 8)
+ pen.stroke(d.wave(x0, x1, y, 26, 1.2 + 0.3 * k, 0.78, n=110, phase=k * 1.7), step_ms=7)
+ pen.play("sky")
+
+
+def line(a, b, step=26, p=0.85):
+ n = max(2, int(math.hypot(b[0] - a[0], b[1] - a[1]) / step))
+ return [(a[0] + (b[0] - a[0]) * i / n, a[1] + (b[1] - a[1]) * i / n, p) for i in range(1, n + 1)]
+
+
+def sun():
+ cx, cy, r = SUN
+ # a glow on its own layer, soft, round the sun's place
+ d.new_layer()
+ d.size(0.6)
+ d.colour("FFD9A0", wait=0.5)
+ pen = d.Pen()
+ for k, rr in enumerate((r + 50, r + 95, r + 150)):
+ pen.stroke([(cx + rr * math.cos(t), cy + rr * math.sin(t), 0.42 - 0.1 * k)
+ for t in [i / 90 * 2 * math.pi for i in range(91)]], step_ms=8)
+ pen.play("glow")
+ d.new_layer()
+ d.colour("FFF1D2", wait=0.5)
+ # the disc: a circle held at its end so QuickShape makes it round, then filled by ColorDrop
+ d.brush("Inking", 0)
+ d.size(0.25)
+ pts = [(cx + r * math.cos(t) * (1 + 0.03 * math.sin(3 * t)), cy + r * math.sin(t) * (1 + 0.03 * math.cos(2 * t)), 0.7)
+ for t in [i / 90 * 2 * math.pi * 1.02 - math.pi / 2 for i in range(91)]]
+ pts += [pts[-1]] * 110
+ d.Pen().stroke(pts, step_ms=8).play("circle")
+ time.sleep(0.8)
+ d.colour_drop(cx, cy)
+
+
+def hills():
+ for hexcode, shape, drop_y in (("7B3F8E", FAR, 1530), ("3B2056", NEAR, 1575)):
+ d.new_layer()
+ d.colour(hexcode, wait=1.0)
+ # a closed outline just inside the canvas: the hill's crest right to left, then down,
+ # along the bottom and back up, so ColorDrop has a sealed shape to fill
+ crest = [(x, hill(x, **shape), 0.85) for x in range(2552, 7, -22)] + [(8, hill(8, **shape), 0.85)]
+ pts = crest + line((8, crest[-1][1]), (8, 1590)) + line((8, 1590), (2552, 1590)) + line((2552, 1590), crest[0][:2])
+ d.Pen().stroke(pts, step_ms=7).play("hill")
+ time.sleep(0.4)
+ d.colour_drop(1280, drop_y)
+
+
+def pine(pen, x, base, h):
+ # a trunk, then branches zigzagging out wider as they come down
+ pen.stroke([(x, base + 6, 0.9), (x, base - h, 0.5)], step_ms=30)
+ pts = []
+ rows = max(8, int(h / 9))
+ for i in range(rows + 1):
+ t = i / rows
+ y = base - h + 8 + t * (h - 18)
+ w = 4 + t * h * 0.30
+ side = -1 if i % 2 == 0 else 1
+ pts.append((x + side * w, y, 0.45 + 0.4 * t))
+ pen.stroke(pts, step_ms=22)
+
+
+def trees():
+ d.new_layer()
+ d.colour("150B22", wait=0.5)
+ d.size(0.55)
+ pen = d.Pen()
+ for x, h in ((210, 210), (300, 160), (380, 250), (470, 180), (1090, 150), (1160, 205), (1240, 130),
+ (2050, 230), (2140, 170), (2230, 260), (2330, 190), (2420, 150)):
+ pine(pen, x, hill(x, **NEAR) + 30, h)
+ pen.play("trees")
+ # birds near the sun, with a fine brush
+ d.size(0.10)
+ pen = d.Pen()
+ for bx, by, s in ((1500, 760, 26), (1560, 720, 20), (1620, 790, 16)):
+ pen.stroke([(bx - s, by - s * 0.4, 0.4), (bx - s * 0.5, by - s * 0.6, 0.5), (bx, by, 0.6),
+ (bx + s * 0.5, by - s * 0.6, 0.5), (bx + s, by - s * 0.4, 0.4)], step_ms=25)
+ pen.play("birds")
+
+
+def stars():
+ d.new_layer()
+ d.brush("Airbrushing", 1)
+ d.size(0.14)
+ d.colour("FFFFFF", wait=0.5)
+ pen = d.Pen()
+ for _ in range(46):
+ x = random.uniform(90, 2500)
+ y = random.uniform(120, 560) if random.random() < 0.8 else random.uniform(560, 760)
+ if (x - SUN[0]) ** 2 + (y - SUN[1]) ** 2 < 330 ** 2:
+ continue
+ p = random.uniform(0.35, 1.0)
+ pen.stroke([(x, y, p)] * 4, step_ms=8)
+ pen.play("stars")
+
+
+def bloom():
+ d.tap(*d.ADJUST, wait=0.8)
+ d.tap(300, 1122, wait=1.2)
+ d.swipe(700, 800, 1900, 800, 1600, wait=1.2)
+ d.tap(*d.ADJUST, wait=1.0)
+
+
+def tour():
+ d.tap(*d.LAYERS, wait=2.6)
+ d.tap(*d.LAYERS, wait=0.8)
+
+
+def gallery():
+ d.tap(136, 42, wait=2.5)
+
+
+STAGES = {"new": new_canvas, "sky": sky, "sun": sun, "hills": hills, "trees": trees, "stars": stars,
+ "bloom": bloom, "tour": tour, "gallery": gallery}
+
+if __name__ == "__main__":
+ for name in (sys.argv[1:] or list(STAGES)):
+ t = time.time()
+ STAGES[name]()
+ print(f"{name}: {time.time() - t:.1f} s", flush=True)
diff --git a/tools/showcase/penhid.py b/tools/showcase/penhid.py
new file mode 100644
index 0000000..abc6cb6
--- /dev/null
+++ b/tools/showcase/penhid.py
@@ -0,0 +1,92 @@
+"""Writes scripts for Android's `hid` shell command that register a virtual pen digitizer (tip,
+barrel, eraser, in range, X, Y and 12-bit pressure) and play pen strokes on it, with real timing.
+
+Coordinates are fractions of the digitizer's area (0..1 each way); pressure is 0..1.
+"""
+import json
+
+DESCRIPTOR = [
+ 0x05, 0x0D, # Usage Page (Digitizer)
+ 0x09, 0x02, # Usage (Pen)
+ 0xA1, 0x01, # Collection (Application)
+ 0x85, 0x01, # Report ID (1)
+ 0x09, 0x20, # Usage (Stylus)
+ 0xA1, 0x00, # Collection (Physical)
+ 0x09, 0x42, # Usage (Tip Switch)
+ 0x09, 0x44, # Usage (Barrel Switch)
+ 0x09, 0x45, # Usage (Eraser)
+ 0x09, 0x32, # Usage (In Range)
+ 0x15, 0x00, # Logical Minimum (0)
+ 0x25, 0x01, # Logical Maximum (1)
+ 0x75, 0x01, # Report Size (1)
+ 0x95, 0x04, # Report Count (4)
+ 0x81, 0x02, # Input (Data, Var, Abs)
+ 0x95, 0x04, # Report Count (4)
+ 0x81, 0x03, # Input (Const)
+ 0x05, 0x01, # Usage Page (Generic Desktop)
+ 0x09, 0x30, # Usage (X)
+ 0x15, 0x00, # Logical Minimum (0)
+ 0x26, 0xFF, 0x7F, # Logical Maximum (32767)
+ 0x35, 0x00, # Physical Minimum (0)
+ 0x46, 0x10, 0x27, # Physical Maximum (10000)
+ 0x55, 0x0E, # Unit Exponent (-2)
+ 0x65, 0x11, # Unit (cm)
+ 0x75, 0x10, # Report Size (16)
+ 0x95, 0x01, # Report Count (1)
+ 0x81, 0x02, # Input (Data, Var, Abs)
+ 0x09, 0x31, # Usage (Y)
+ 0x46, 0x10, 0x27, # Physical Maximum (10000)
+ 0x81, 0x02, # Input (Data, Var, Abs)
+ 0x05, 0x0D, # Usage Page (Digitizer)
+ 0x09, 0x30, # Usage (Tip Pressure)
+ 0x15, 0x00, # Logical Minimum (0)
+ 0x26, 0xFF, 0x0F, # Logical Maximum (4095)
+ 0x75, 0x10, # Report Size (16)
+ 0x95, 0x01, # Report Count (1)
+ 0x81, 0x02, # Input (Data, Var, Abs)
+ 0xC0, # End Collection
+ 0xC0, # End Collection
+]
+
+
+class Script:
+ def __init__(self, map_xy=lambda x, y: (x, y)):
+ self.events = [{"id": 1, "command": "register", "name": "Showcase Pen", "vid": 0x1209, "pid": 0x0D0D,
+ "bus": "usb", "descriptor": DESCRIPTOR}]
+ self.map_xy = map_xy
+ self.delay(800)
+
+ def delay(self, ms):
+ self.events.append({"id": 1, "command": "delay", "duration": int(ms)})
+
+ def report(self, tip, in_range, x, y, pressure):
+ x, y = self.map_xy(x, y)
+ xi = max(0, min(32767, int(round(x * 32767))))
+ yi = max(0, min(32767, int(round(y * 32767))))
+ pi = max(0, min(4095, int(round(pressure * 4095))))
+ buttons = (1 if tip else 0) | (8 if in_range else 0)
+ self.events.append({"id": 1, "command": "report",
+ "report": [1, buttons, xi & 255, xi >> 8, yi & 255, yi >> 8, pi & 255, pi >> 8]})
+
+ def stroke(self, points, step_ms=8):
+ """points: (x, y, pressure) in 0..1; the pen comes into range, touches, moves and lifts."""
+ x0, y0, _ = points[0]
+ self.report(False, True, x0, y0, 0)
+ self.delay(step_ms * 2)
+ for x, y, p in points:
+ self.report(True, True, x, y, max(p, 0.02))
+ self.delay(step_ms)
+ x1, y1, _ = points[-1]
+ self.report(False, True, x1, y1, 0)
+ self.delay(step_ms)
+ self.report(False, False, x1, y1, 0)
+ self.delay(step_ms * 3)
+
+ def tap(self, x, y, hold_ms=60):
+ self.stroke([(x, y, 0.6)] * max(1, hold_ms // 8))
+
+ def write(self, path):
+ self.delay(500)
+ with open(path, "w", encoding="utf-8", newline="\n") as f:
+ for e in self.events:
+ f.write(json.dumps(e, separators=(",", ":")) + "\n")
diff --git a/tools/showcase/to_srgb.py b/tools/showcase/to_srgb.py
new file mode 100644
index 0000000..5ada852
--- /dev/null
+++ b/tools/showcase/to_srgb.py
@@ -0,0 +1,34 @@
+"""The tablet's screen captures hold Display P3 values; this turns them into sRGB JPEGs for the web.
+
+usage: python -I to_srgb.py <out dir> <width> <png ...>
+"""
+import os
+import sys
+
+import numpy as np
+from PIL import Image
+
+P3_TO_SRGB = np.array([[1.2249, -0.2247, 0.0], [-0.0420, 1.0419, 0.0], [-0.0197, -0.0786, 1.0979]])
+
+
+def decode(v):
+ return np.where(v <= 0.04045, v / 12.92, ((v + 0.055) / 1.055) ** 2.4)
+
+
+def encode(v):
+ v = np.clip(v, 0, 1)
+ return np.where(v <= 0.0031308, v * 12.92, 1.055 * v ** (1 / 2.4) - 0.055)
+
+
+out_dir, width = sys.argv[1], int(sys.argv[2])
+os.makedirs(out_dir, exist_ok=True)
+for path in sys.argv[3:]:
+ im = Image.open(path).convert("RGB")
+ a = np.asarray(im, dtype=np.float64) / 255
+ lin = decode(a) @ P3_TO_SRGB.T
+ rgb = Image.fromarray((encode(lin) * 255 + 0.5).astype(np.uint8))
+ if rgb.width != width:
+ rgb = rgb.resize((width, round(rgb.height * width / rgb.width)), Image.LANCZOS)
+ name = os.path.splitext(os.path.basename(path))[0] + ".jpg"
+ rgb.save(os.path.join(out_dir, name), quality=88, optimize=True, progressive=True)
+ print(name, os.path.getsize(os.path.join(out_dir, name)) // 1024, "KB")