@native-sdk/cli 0.0.0 → 0.4.0
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.
- package/LICENSE +176 -0
- package/README.md +50 -1
- package/app.zon +30 -0
- package/bin/native.js +156 -0
- package/build/app.zig +716 -0
- package/build.zig +2593 -0
- package/build.zig.zon +24 -0
- package/native-sdk.d.ts +615 -0
- package/package.json +44 -8
- package/skill-data/automation/SKILL.md +248 -0
- package/skill-data/core/SKILL.md +249 -0
- package/skill-data/core/references/app-model-runtime.md +141 -0
- package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
- package/skill-data/core/references/frontend-assets.md +124 -0
- package/skill-data/core/references/project-anatomy.md +108 -0
- package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
- package/skill-data/native-ui/SKILL.md +1080 -0
- package/skills/native-sdk/SKILL.md +33 -0
- package/src/app_runner/root.zig +740 -0
- package/src/assets/root.zig +20 -0
- package/src/automation/protocol.zig +285 -0
- package/src/automation/root.zig +13 -0
- package/src/automation/server.zig +506 -0
- package/src/automation/snapshot.zig +1202 -0
- package/src/automation/watcher.zig +165 -0
- package/src/bridge/root.zig +507 -0
- package/src/debug/root.zig +246 -0
- package/src/embed/app_exports.zig +11 -0
- package/src/embed/c_api.zig +793 -0
- package/src/embed/c_exports.zig +10 -0
- package/src/embed/chrome.zig +168 -0
- package/src/embed/conversions.zig +359 -0
- package/src/embed/host.zig +960 -0
- package/src/embed/root.zig +81 -0
- package/src/embed/tests.zig +2552 -0
- package/src/embed/types.zig +382 -0
- package/src/embed/ui_host.zig +289 -0
- package/src/extensions/root.zig +197 -0
- package/src/frontend/root.zig +48 -0
- package/src/js/root.zig +65 -0
- package/src/platform/android/NativeSdkActivity.java +1370 -0
- package/src/platform/android/android_host.c +696 -0
- package/src/platform/android/files.zig +9 -0
- package/src/platform/android/native_sdk_app.h +237 -0
- package/src/platform/ios/files.zig +8 -0
- package/src/platform/ios/native_sdk_app.h +289 -0
- package/src/platform/ios/uikit_host.m +2739 -0
- package/src/platform/linux/cef_host.cpp +599 -0
- package/src/platform/linux/gtk_host.c +5362 -0
- package/src/platform/linux/gtk_host.h +265 -0
- package/src/platform/linux/root.zig +1519 -0
- package/src/platform/macos/appkit_host.h +604 -0
- package/src/platform/macos/appkit_host.m +10392 -0
- package/src/platform/macos/automation_host.h +14 -0
- package/src/platform/macos/automation_host.m +12 -0
- package/src/platform/macos/cef_host.mm +3028 -0
- package/src/platform/macos/root.zig +2260 -0
- package/src/platform/null_platform.zig +2232 -0
- package/src/platform/null_platform_tests.zig +670 -0
- package/src/platform/policy_values.zig +14 -0
- package/src/platform/root.zig +181 -0
- package/src/platform/types.zig +2681 -0
- package/src/platform/windows/cef_host.cpp +3 -0
- package/src/platform/windows/root.zig +1521 -0
- package/src/platform/windows/webview2_host.cpp +6230 -0
- package/src/primitives/app_dirs/root.zig +440 -0
- package/src/primitives/app_manifest/root.zig +109 -0
- package/src/primitives/app_manifest/tests.zig +758 -0
- package/src/primitives/app_manifest/types.zig +587 -0
- package/src/primitives/app_manifest/validation.zig +828 -0
- package/src/primitives/assets/root.zig +520 -0
- package/src/primitives/canvas/a11y_audit.zig +390 -0
- package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
- package/src/primitives/canvas/app_icon.zig +1590 -0
- package/src/primitives/canvas/chart.zig +392 -0
- package/src/primitives/canvas/chart_tests.zig +751 -0
- package/src/primitives/canvas/commands.zig +446 -0
- package/src/primitives/canvas/drawing.zig +309 -0
- package/src/primitives/canvas/equality.zig +316 -0
- package/src/primitives/canvas/events.zig +696 -0
- package/src/primitives/canvas/font_coverage.zig +118 -0
- package/src/primitives/canvas/font_ttf.zig +510 -0
- package/src/primitives/canvas/font_ttf_tests.zig +280 -0
- package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
- package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
- package/src/primitives/canvas/frame.zig +679 -0
- package/src/primitives/canvas/frame_metrics.zig +462 -0
- package/src/primitives/canvas/gpu.zig +639 -0
- package/src/primitives/canvas/hash.zig +112 -0
- package/src/primitives/canvas/icons/alert.svg +1 -0
- package/src/primitives/canvas/icons/archive.svg +1 -0
- package/src/primitives/canvas/icons/arrow-down.svg +1 -0
- package/src/primitives/canvas/icons/arrow-right.svg +1 -0
- package/src/primitives/canvas/icons/arrow-up.svg +1 -0
- package/src/primitives/canvas/icons/check-circle.svg +1 -0
- package/src/primitives/canvas/icons/check.svg +1 -0
- package/src/primitives/canvas/icons/chevron-down.svg +1 -0
- package/src/primitives/canvas/icons/chevron-left.svg +1 -0
- package/src/primitives/canvas/icons/chevron-right.svg +1 -0
- package/src/primitives/canvas/icons/chevron-up.svg +1 -0
- package/src/primitives/canvas/icons/circle-dot.svg +1 -0
- package/src/primitives/canvas/icons/clock.svg +1 -0
- package/src/primitives/canvas/icons/copy.svg +1 -0
- package/src/primitives/canvas/icons/download.svg +1 -0
- package/src/primitives/canvas/icons/edit.svg +1 -0
- package/src/primitives/canvas/icons/ellipsis.svg +1 -0
- package/src/primitives/canvas/icons/external-link.svg +1 -0
- package/src/primitives/canvas/icons/eye.svg +1 -0
- package/src/primitives/canvas/icons/file-text.svg +1 -0
- package/src/primitives/canvas/icons/folder-open.svg +1 -0
- package/src/primitives/canvas/icons/folder.svg +1 -0
- package/src/primitives/canvas/icons/git-branch.svg +1 -0
- package/src/primitives/canvas/icons/git-merge.svg +1 -0
- package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
- package/src/primitives/canvas/icons/info.svg +1 -0
- package/src/primitives/canvas/icons/menu.svg +1 -0
- package/src/primitives/canvas/icons/missing.svg +1 -0
- package/src/primitives/canvas/icons/moon.svg +1 -0
- package/src/primitives/canvas/icons/music.svg +1 -0
- package/src/primitives/canvas/icons/panel-left.svg +1 -0
- package/src/primitives/canvas/icons/panel-right.svg +1 -0
- package/src/primitives/canvas/icons/pause.svg +1 -0
- package/src/primitives/canvas/icons/play.svg +1 -0
- package/src/primitives/canvas/icons/plus.svg +1 -0
- package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
- package/src/primitives/canvas/icons/repeat.svg +1 -0
- package/src/primitives/canvas/icons/save.svg +1 -0
- package/src/primitives/canvas/icons/search.svg +1 -0
- package/src/primitives/canvas/icons/send.svg +1 -0
- package/src/primitives/canvas/icons/settings.svg +1 -0
- package/src/primitives/canvas/icons/shuffle.svg +1 -0
- package/src/primitives/canvas/icons/skip-back.svg +1 -0
- package/src/primitives/canvas/icons/skip-forward.svg +1 -0
- package/src/primitives/canvas/icons/sun.svg +1 -0
- package/src/primitives/canvas/icons/terminal.svg +1 -0
- package/src/primitives/canvas/icons/trash.svg +1 -0
- package/src/primitives/canvas/icons/volume.svg +1 -0
- package/src/primitives/canvas/icons/wrench.svg +1 -0
- package/src/primitives/canvas/icons/x-circle.svg +1 -0
- package/src/primitives/canvas/icons/x.svg +1 -0
- package/src/primitives/canvas/icons.zig +249 -0
- package/src/primitives/canvas/layout_audit.zig +744 -0
- package/src/primitives/canvas/layout_audit_tests.zig +426 -0
- package/src/primitives/canvas/markdown.zig +1031 -0
- package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
- package/src/primitives/canvas/markdown_tests.zig +538 -0
- package/src/primitives/canvas/plan_key_index.zig +120 -0
- package/src/primitives/canvas/png.zig +355 -0
- package/src/primitives/canvas/reference.zig +1457 -0
- package/src/primitives/canvas/reference_blur.zig +104 -0
- package/src/primitives/canvas/reference_memo.zig +322 -0
- package/src/primitives/canvas/reference_paths.zig +184 -0
- package/src/primitives/canvas/reference_tests.zig +1650 -0
- package/src/primitives/canvas/render.zig +747 -0
- package/src/primitives/canvas/render_effects.zig +255 -0
- package/src/primitives/canvas/render_fingerprints.zig +256 -0
- package/src/primitives/canvas/render_generic_resources.zig +376 -0
- package/src/primitives/canvas/render_images.zig +247 -0
- package/src/primitives/canvas/render_layers.zig +260 -0
- package/src/primitives/canvas/render_paths.zig +347 -0
- package/src/primitives/canvas/render_resources.zig +51 -0
- package/src/primitives/canvas/render_tests.zig +3186 -0
- package/src/primitives/canvas/root.zig +744 -0
- package/src/primitives/canvas/serialization.zig +1528 -0
- package/src/primitives/canvas/svg_icon.zig +636 -0
- package/src/primitives/canvas/svg_icon_tests.zig +312 -0
- package/src/primitives/canvas/test_support.zig +422 -0
- package/src/primitives/canvas/testdata/markdown_document.md +83 -0
- package/src/primitives/canvas/tests.zig +30 -0
- package/src/primitives/canvas/text.zig +82 -0
- package/src/primitives/canvas/text_atlas.zig +408 -0
- package/src/primitives/canvas/text_batch_tests.zig +431 -0
- package/src/primitives/canvas/text_interaction.zig +615 -0
- package/src/primitives/canvas/text_layout.zig +1069 -0
- package/src/primitives/canvas/text_layout_cache.zig +281 -0
- package/src/primitives/canvas/text_layout_hash.zig +113 -0
- package/src/primitives/canvas/text_layout_types.zig +128 -0
- package/src/primitives/canvas/text_measure_cache.zig +279 -0
- package/src/primitives/canvas/text_metrics.zig +309 -0
- package/src/primitives/canvas/text_metrics_tests.zig +262 -0
- package/src/primitives/canvas/text_span_tests.zig +608 -0
- package/src/primitives/canvas/text_spans.zig +1010 -0
- package/src/primitives/canvas/text_tests.zig +1949 -0
- package/src/primitives/canvas/themes/geist.zig +470 -0
- package/src/primitives/canvas/tokens.zig +1522 -0
- package/src/primitives/canvas/ui.zig +2686 -0
- package/src/primitives/canvas/ui_markup.zig +3791 -0
- package/src/primitives/canvas/ui_markup_binary.zig +522 -0
- package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
- package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
- package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
- package/src/primitives/canvas/ui_markup_edit.zig +416 -0
- package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
- package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
- package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
- package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
- package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
- package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
- package/src/primitives/canvas/ui_markup_view.zig +2352 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
- package/src/primitives/canvas/ui_provenance.zig +96 -0
- package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
- package/src/primitives/canvas/ui_schema.zig +825 -0
- package/src/primitives/canvas/ui_schema_tests.zig +128 -0
- package/src/primitives/canvas/ui_tests.zig +1432 -0
- package/src/primitives/canvas/vector.zig +919 -0
- package/src/primitives/canvas/vector_tests.zig +547 -0
- package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
- package/src/primitives/canvas/virtual_extents.zig +540 -0
- package/src/primitives/canvas/widget_access.zig +208 -0
- package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
- package/src/primitives/canvas/widget_invalidation.zig +748 -0
- package/src/primitives/canvas/widget_layout.zig +2055 -0
- package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
- package/src/primitives/canvas/widget_metrics.zig +228 -0
- package/src/primitives/canvas/widget_render.zig +2534 -0
- package/src/primitives/canvas/widget_render_controls.zig +1563 -0
- package/src/primitives/canvas/widget_render_scroll.zig +136 -0
- package/src/primitives/canvas/widget_render_style.zig +626 -0
- package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
- package/src/primitives/canvas/widget_routing.zig +475 -0
- package/src/primitives/canvas/widget_runtime.zig +307 -0
- package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
- package/src/primitives/canvas/widget_semantics.zig +480 -0
- package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
- package/src/primitives/canvas/widget_tests.zig +8 -0
- package/src/primitives/canvas/widget_text_input.zig +351 -0
- package/src/primitives/canvas/widget_text_select.zig +147 -0
- package/src/primitives/canvas/widget_tree.zig +232 -0
- package/src/primitives/canvas/widgets.zig +1319 -0
- package/src/primitives/diagnostics/root.zig +445 -0
- package/src/primitives/geometry/root.zig +889 -0
- package/src/primitives/json/root.zig +272 -0
- package/src/primitives/platform_info/root.zig +450 -0
- package/src/primitives/trace/root.zig +569 -0
- package/src/root.zig +165 -0
- package/src/runtime/api.zig +431 -0
- package/src/runtime/async_bridge.zig +49 -0
- package/src/runtime/automation_commands.zig +686 -0
- package/src/runtime/automation_liveness_tests.zig +140 -0
- package/src/runtime/automation_snapshot.zig +246 -0
- package/src/runtime/automation_widget_dispatch.zig +617 -0
- package/src/runtime/bridge_dispatch_tests.zig +202 -0
- package/src/runtime/bridge_payload.zig +194 -0
- package/src/runtime/bridge_permission_tests.zig +551 -0
- package/src/runtime/bridge_responses.zig +414 -0
- package/src/runtime/builtin_bridge.zig +554 -0
- package/src/runtime/canvas_budget_edge_tests.zig +415 -0
- package/src/runtime/canvas_font_tests.zig +410 -0
- package/src/runtime/canvas_fonts.zig +229 -0
- package/src/runtime/canvas_frame.zig +1734 -0
- package/src/runtime/canvas_frame_animation_tests.zig +609 -0
- package/src/runtime/canvas_frame_cache_tests.zig +333 -0
- package/src/runtime/canvas_frame_helpers.zig +414 -0
- package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
- package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
- package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
- package/src/runtime/canvas_frame_retained_tests.zig +753 -0
- package/src/runtime/canvas_frame_tests.zig +9 -0
- package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
- package/src/runtime/canvas_image_tests.zig +362 -0
- package/src/runtime/canvas_images.zig +224 -0
- package/src/runtime/canvas_limits.zig +317 -0
- package/src/runtime/canvas_screenshot_tests.zig +289 -0
- package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
- package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
- package/src/runtime/canvas_widget_chart_tests.zig +249 -0
- package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
- package/src/runtime/canvas_widget_context_menu.zig +318 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
- package/src/runtime/canvas_widget_control_tests.zig +2164 -0
- package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
- package/src/runtime/canvas_widget_display.zig +677 -0
- package/src/runtime/canvas_widget_event_tests.zig +1779 -0
- package/src/runtime/canvas_widget_events.zig +1098 -0
- package/src/runtime/canvas_widget_floating_tests.zig +695 -0
- package/src/runtime/canvas_widget_layout_tests.zig +26 -0
- package/src/runtime/canvas_widget_runtime.zig +1335 -0
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
- package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
- package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
- package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
- package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
- package/src/runtime/canvas_widget_state.zig +798 -0
- package/src/runtime/canvas_widget_text_tests.zig +2104 -0
- package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
- package/src/runtime/clock.zig +231 -0
- package/src/runtime/core.zig +927 -0
- package/src/runtime/effects.zig +4197 -0
- package/src/runtime/effects_audio_tests.zig +690 -0
- package/src/runtime/effects_clipboard_tests.zig +317 -0
- package/src/runtime/effects_fetch_tests.zig +1031 -0
- package/src/runtime/effects_file_tests.zig +407 -0
- package/src/runtime/effects_tests.zig +1251 -0
- package/src/runtime/flow.zig +1175 -0
- package/src/runtime/frame_profile.zig +221 -0
- package/src/runtime/gpu_surface_events.zig +639 -0
- package/src/runtime/launch_timing.zig +59 -0
- package/src/runtime/markdown_app_tests.zig +224 -0
- package/src/runtime/native_command_event_tests.zig +431 -0
- package/src/runtime/platform_bridge_tests.zig +8 -0
- package/src/runtime/platform_config_tests.zig +392 -0
- package/src/runtime/root.zig +150 -0
- package/src/runtime/runtime_event_tests.zig +1079 -0
- package/src/runtime/session_journal.zig +1522 -0
- package/src/runtime/session_record.zig +332 -0
- package/src/runtime/session_replay.zig +274 -0
- package/src/runtime/session_state.zig +30 -0
- package/src/runtime/session_tests.zig +399 -0
- package/src/runtime/shell_layout.zig +472 -0
- package/src/runtime/shell_layout_tests.zig +1246 -0
- package/src/runtime/shell_view_tests.zig +604 -0
- package/src/runtime/state.zig +110 -0
- package/src/runtime/system_services.zig +392 -0
- package/src/runtime/test_support.zig +79 -0
- package/src/runtime/tests.zig +30 -0
- package/src/runtime/ui_app.zig +3290 -0
- package/src/runtime/ui_app_huge_model_tests.zig +351 -0
- package/src/runtime/ui_app_provenance.zig +370 -0
- package/src/runtime/ui_app_tests.zig +3254 -0
- package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
- package/src/runtime/ui_app_window_tests.zig +442 -0
- package/src/runtime/validation.zig +206 -0
- package/src/runtime/view.zig +908 -0
- package/src/runtime/view_canvas.zig +850 -0
- package/src/runtime/view_widget_control.zig +433 -0
- package/src/runtime/view_widget_scroll.zig +360 -0
- package/src/runtime/view_widget_text.zig +275 -0
- package/src/runtime/view_widget_tree.zig +843 -0
- package/src/runtime/webview_view_bridge_tests.zig +664 -0
- package/src/runtime/widget_bridge.zig +231 -0
- package/src/runtime/window_command_bridge_tests.zig +403 -0
- package/src/runtime/window_storage.zig +255 -0
- package/src/runtime/window_view_runtime.zig +727 -0
- package/src/runtime/window_views.zig +334 -0
- package/src/security/root.zig +118 -0
- package/src/tooling/android.zig +1172 -0
- package/src/tooling/assets.zig +128 -0
- package/src/tooling/buildgraph.zig +379 -0
- package/src/tooling/cef.zig +671 -0
- package/src/tooling/codesign.zig +172 -0
- package/src/tooling/components/identity_tests.zig +185 -0
- package/src/tooling/components/stepper.zig +113 -0
- package/src/tooling/components/timeline.native +27 -0
- package/src/tooling/components/timeline_item.zig +131 -0
- package/src/tooling/default_icon.icns +0 -0
- package/src/tooling/default_icon.png +0 -0
- package/src/tooling/dev.zig +172 -0
- package/src/tooling/doctor.zig +349 -0
- package/src/tooling/eject_components.zig +170 -0
- package/src/tooling/embedlib.zig +290 -0
- package/src/tooling/ios.zig +528 -0
- package/src/tooling/manifest.zig +1939 -0
- package/src/tooling/package.zig +2388 -0
- package/src/tooling/process_tree.zig +114 -0
- package/src/tooling/raw_manifest.zig +193 -0
- package/src/tooling/root.zig +22 -0
- package/src/tooling/templates.zig +3191 -0
- package/src/tooling/toolchain.zig +371 -0
- package/src/tooling/verbs.zig +204 -0
- package/src/tooling/web_engine.zig +135 -0
- package/src/tooling/xcodeproj.zig +462 -0
- package/src/window_state/root.zig +507 -0
- package/index.js +0 -2
|
@@ -0,0 +1,1590 @@
|
|
|
1
|
+
//! One-image app-icon pipeline: a single square source image — a PNG
|
|
2
|
+
//! (1:1, ideally 1024x1024) or an SVG in the supported stroke-icon
|
|
3
|
+
//! dialect — becomes every artifact the packaging targets need, with no
|
|
4
|
+
//! external tools anywhere:
|
|
5
|
+
//!
|
|
6
|
+
//! - macOS: a complete `.icns` (16/32/128/256/512 at 1x and 2x) with the
|
|
7
|
+
//! platform icon convention applied automatically — full-bleed artwork
|
|
8
|
+
//! is inset on the canvas and masked by the standard rounded rectangle
|
|
9
|
+
//! of the macOS icon grid, so a plain square image looks native in the
|
|
10
|
+
//! Dock. Artwork whose corners are already transparent (the author
|
|
11
|
+
//! shaped it themselves) ships as-is, never double-masked.
|
|
12
|
+
//! - Windows: a multi-size `.ico` of PNG-compressed entries.
|
|
13
|
+
//! - Linux and mobile targets: square PNGs at their standard sizes.
|
|
14
|
+
//!
|
|
15
|
+
//! Quality rules: every raster resize is an exact area average (box
|
|
16
|
+
//! filter) over premultiplied LINEAR-light values — a 1024px source
|
|
17
|
+
//! stays crisp at 16px and alpha edges blend correctly — and SVG sources
|
|
18
|
+
//! are rasterized per output size through the vector core instead of
|
|
19
|
+
//! being scaled from a master raster. PNG output uses real deflate
|
|
20
|
+
//! compression (still fully deterministic: identical input bytes produce
|
|
21
|
+
//! identical artifacts).
|
|
22
|
+
|
|
23
|
+
const std = @import("std");
|
|
24
|
+
const geometry = @import("geometry");
|
|
25
|
+
const drawing_model = @import("drawing.zig");
|
|
26
|
+
const vector = @import("vector.zig");
|
|
27
|
+
const svg_icon = @import("svg_icon.zig");
|
|
28
|
+
const png_model = @import("png.zig");
|
|
29
|
+
|
|
30
|
+
const PointF = geometry.PointF;
|
|
31
|
+
const Affine = drawing_model.Affine;
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// The macOS icon grid
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Modern macOS app icons are drawn on a 1024x1024 canvas whose artwork
|
|
37
|
+
// square is 824x824, centered (a 100px margin on every side), clipped by
|
|
38
|
+
// a rounded rectangle with a 185.4px corner radius. Expressed as ratios
|
|
39
|
+
// of the canvas edge so the same grid renders exactly at every size:
|
|
40
|
+
|
|
41
|
+
/// Artwork square edge / canvas edge (824/1024).
|
|
42
|
+
pub const macos_artwork_ratio: f64 = 824.0 / 1024.0;
|
|
43
|
+
/// Margin on each side / canvas edge (100/1024).
|
|
44
|
+
pub const macos_margin_ratio: f64 = 100.0 / 1024.0;
|
|
45
|
+
/// Rounded-rect corner radius / canvas edge (185.4/1024, which is 22.5%
|
|
46
|
+
/// of the artwork square). The corners here are circular arcs; the
|
|
47
|
+
/// platform's own icons use a continuous-curvature corner, but at every
|
|
48
|
+
/// shipped size the difference is below one pixel of coverage.
|
|
49
|
+
pub const macos_corner_radius_ratio: f64 = 185.4 / 1024.0;
|
|
50
|
+
|
|
51
|
+
/// Pixel sizes rendered for the `.icns` family (1x and 2x slots below
|
|
52
|
+
/// share renders: a 32px image serves both 32@1x and 16@2x).
|
|
53
|
+
pub const macos_render_sizes = [_]usize{ 16, 32, 64, 128, 256, 512, 1024 };
|
|
54
|
+
|
|
55
|
+
/// The `.icns` member types shipped, matching what the platform's own
|
|
56
|
+
/// icon tooling emits for the standard iconset family. The 16 and 32
|
|
57
|
+
/// pixel 1x slots (`ic04`/`ic05`) carry the ARGB form — an "ARGB" magic
|
|
58
|
+
/// followed by PackBits-style run-length-encoded A,R,G,B planes —
|
|
59
|
+
/// because system consumers read those two slots as RLE data, not PNG;
|
|
60
|
+
/// every other slot carries a PNG payload.
|
|
61
|
+
pub const IcnsPayload = enum { png, argb };
|
|
62
|
+
pub const IcnsSlot = struct { kind: [4]u8, size: usize, payload: IcnsPayload = .png };
|
|
63
|
+
pub const icns_slots = [_]IcnsSlot{
|
|
64
|
+
.{ .kind = "ic04".*, .size = 16, .payload = .argb }, // 16x16 @1x
|
|
65
|
+
.{ .kind = "ic11".*, .size = 32 }, // 16x16 @2x
|
|
66
|
+
.{ .kind = "ic05".*, .size = 32, .payload = .argb }, // 32x32 @1x
|
|
67
|
+
.{ .kind = "ic12".*, .size = 64 }, // 32x32 @2x
|
|
68
|
+
.{ .kind = "ic07".*, .size = 128 }, // 128x128 @1x
|
|
69
|
+
.{ .kind = "ic13".*, .size = 256 }, // 128x128 @2x
|
|
70
|
+
.{ .kind = "ic08".*, .size = 256 }, // 256x256 @1x
|
|
71
|
+
.{ .kind = "ic14".*, .size = 512 }, // 256x256 @2x
|
|
72
|
+
.{ .kind = "ic09".*, .size = 512 }, // 512x512 @1x
|
|
73
|
+
.{ .kind = "ic10".*, .size = 1024 }, // 512x512 @2x
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/// Windows `.ico` directory sizes (square, unmasked — Windows renders
|
|
77
|
+
/// icons full-bleed).
|
|
78
|
+
pub const ico_sizes = [_]usize{ 16, 24, 32, 48, 64, 128, 256 };
|
|
79
|
+
|
|
80
|
+
/// Linux hicolor theme sizes (share/icons/hicolor/<N>x<N>/apps).
|
|
81
|
+
pub const linux_sizes = [_]usize{ 16, 24, 32, 48, 64, 128, 256, 512 };
|
|
82
|
+
|
|
83
|
+
/// Android launcher mipmap densities: mdpi through xxxhdpi at the
|
|
84
|
+
/// standard 48dp launcher size.
|
|
85
|
+
pub const AndroidDensity = struct { name: []const u8, size: usize };
|
|
86
|
+
pub const android_densities = [_]AndroidDensity{
|
|
87
|
+
.{ .name = "mdpi", .size = 48 },
|
|
88
|
+
.{ .name = "hdpi", .size = 72 },
|
|
89
|
+
.{ .name = "xhdpi", .size = 96 },
|
|
90
|
+
.{ .name = "xxhdpi", .size = 144 },
|
|
91
|
+
.{ .name = "xxxhdpi", .size = 192 },
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/// iOS asset catalogs take one 1024px universal image.
|
|
95
|
+
pub const ios_icon_size: usize = 1024;
|
|
96
|
+
|
|
97
|
+
/// The macOS canvas the pipeline composes on (and the ideal source edge).
|
|
98
|
+
pub const master_size: usize = 1024;
|
|
99
|
+
|
|
100
|
+
/// Sources below this edge upscale into the large slots and look soft;
|
|
101
|
+
/// the tooling warns (never errors) under it.
|
|
102
|
+
pub const min_recommended_source_size: usize = 512;
|
|
103
|
+
|
|
104
|
+
/// Dimension cap for decoded sources: far past any sensible icon while
|
|
105
|
+
/// keeping a hostile PNG header from driving a giant allocation.
|
|
106
|
+
pub const max_source_dimension: usize = 16384;
|
|
107
|
+
|
|
108
|
+
pub const Error = error{
|
|
109
|
+
/// The source bytes are not a decodable PNG/SVG in the supported
|
|
110
|
+
/// forms (see `formatUnsupportedMessage`).
|
|
111
|
+
UnsupportedImage,
|
|
112
|
+
OutOfMemory,
|
|
113
|
+
} || vector.Error;
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// Teaching messages: the one authority packaging AND `native validate`/
|
|
117
|
+
// `native check` print, so every surface says the same thing.
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
pub fn formatNotSquareMessage(buffer: []u8, path: []const u8, width: usize, height: usize) []const u8 {
|
|
121
|
+
return std.fmt.bufPrint(buffer, "app icon source {s} is {d}x{d} - the source must be square (1:1); crop or pad the artwork so width equals height", .{ path, width, height }) catch "app icon source is not square (1:1) - crop or pad the artwork so width equals height";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
pub fn formatUnsupportedMessage(buffer: []u8, path: []const u8) []const u8 {
|
|
125
|
+
return std.fmt.bufPrint(buffer, "app icon source {s} could not be read - supply one square image as a .png (1:1, ideally 1024x1024) or an .svg in the supported icon dialect", .{path}) catch "app icon source could not be read - supply one square .png (ideally 1024x1024) or .svg";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
pub fn formatSmallSourceMessage(buffer: []u8, path: []const u8, width: usize, height: usize) []const u8 {
|
|
129
|
+
return std.fmt.bufPrint(buffer, "warning: app icon source {s} is {d}x{d} - upscaling never looks good; supply a 1024x1024 image (or an .svg) for crisp large sizes", .{ path, width, height }) catch "warning: app icon source is smaller than 512x512 - upscaling never looks good; supply 1024x1024 (or an .svg)";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
pub fn formatBadExtensionMessage(buffer: []u8, path: []const u8) []const u8 {
|
|
133
|
+
return std.fmt.bufPrint(buffer, "app.zon icons entry {s} is not a supported form - use one square .png or .svg source (every platform's icons generate from it), or a prebuilt .icns (macOS) / .ico (Windows) that ships untouched", .{path}) catch "app.zon icons entries must be a .png or .svg source, or a prebuilt .icns/.ico";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
// Source classification and loading
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
pub const SourceKind = enum { png, svg };
|
|
141
|
+
|
|
142
|
+
/// Kind of a generatable source path, by extension (ASCII
|
|
143
|
+
/// case-insensitive). Prebuilt containers (.icns/.ico) and anything else
|
|
144
|
+
/// return null: they are never generation inputs.
|
|
145
|
+
pub fn sourceKindForPath(path: []const u8) ?SourceKind {
|
|
146
|
+
if (pathHasExtension(path, ".png")) return .png;
|
|
147
|
+
if (pathHasExtension(path, ".svg")) return .svg;
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
pub fn pathHasExtension(path: []const u8, extension: []const u8) bool {
|
|
152
|
+
if (path.len < extension.len) return false;
|
|
153
|
+
return std.ascii.eqlIgnoreCase(path[path.len - extension.len ..], extension);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Straight-alpha 8-bit RGBA pixels.
|
|
157
|
+
pub const Rgba8Image = struct {
|
|
158
|
+
width: usize,
|
|
159
|
+
height: usize,
|
|
160
|
+
pixels: []u8,
|
|
161
|
+
|
|
162
|
+
pub fn deinit(self: Rgba8Image, gpa: std.mem.Allocator) void {
|
|
163
|
+
gpa.free(self.pixels);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/// A loaded, validated icon source.
|
|
168
|
+
pub const Source = struct {
|
|
169
|
+
kind: SourceKind,
|
|
170
|
+
/// Pixel dimensions for PNG sources; the (square) viewBox extent
|
|
171
|
+
/// rounded for SVG sources (SVGs never trigger the size warning).
|
|
172
|
+
width: usize,
|
|
173
|
+
height: usize,
|
|
174
|
+
image: ?Rgba8Image = null,
|
|
175
|
+
svg_buffer: ?*svg_icon.IconBuffer = null,
|
|
176
|
+
svg: svg_icon.Icon = undefined,
|
|
177
|
+
|
|
178
|
+
pub fn deinit(self: *Source, gpa: std.mem.Allocator) void {
|
|
179
|
+
if (self.image) |image| image.deinit(gpa);
|
|
180
|
+
if (self.svg_buffer) |buffer| gpa.destroy(buffer);
|
|
181
|
+
self.* = undefined;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
pub const LoadIssue = union(enum) {
|
|
186
|
+
/// Width and height that were actually found.
|
|
187
|
+
not_square: struct { width: usize, height: usize },
|
|
188
|
+
/// Undecodable or outside the supported forms.
|
|
189
|
+
unsupported,
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
pub const LoadResult = union(enum) {
|
|
193
|
+
ok: Source,
|
|
194
|
+
issue: LoadIssue,
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/// Decode and validate source bytes. Shape problems come back as typed
|
|
198
|
+
/// issues (so callers can print the teaching message with the found
|
|
199
|
+
/// dimensions); only allocation failure is an error.
|
|
200
|
+
pub fn loadSource(gpa: std.mem.Allocator, bytes: []const u8, kind: SourceKind) error{OutOfMemory}!LoadResult {
|
|
201
|
+
switch (kind) {
|
|
202
|
+
.png => {
|
|
203
|
+
const image = decodePng(gpa, bytes) catch |err| switch (err) {
|
|
204
|
+
error.OutOfMemory => return error.OutOfMemory,
|
|
205
|
+
error.UnsupportedImage => return .{ .issue = .unsupported },
|
|
206
|
+
};
|
|
207
|
+
if (image.width != image.height) {
|
|
208
|
+
const width = image.width;
|
|
209
|
+
const height = image.height;
|
|
210
|
+
image.deinit(gpa);
|
|
211
|
+
return .{ .issue = .{ .not_square = .{ .width = width, .height = height } } };
|
|
212
|
+
}
|
|
213
|
+
return .{ .ok = .{ .kind = .png, .width = image.width, .height = image.height, .image = image } };
|
|
214
|
+
},
|
|
215
|
+
.svg => {
|
|
216
|
+
const buffer = try gpa.create(svg_icon.IconBuffer);
|
|
217
|
+
errdefer gpa.destroy(buffer);
|
|
218
|
+
buffer.* = .{};
|
|
219
|
+
const icon = svg_icon.parse(bytes, buffer) catch {
|
|
220
|
+
gpa.destroy(buffer);
|
|
221
|
+
return .{ .issue = .unsupported };
|
|
222
|
+
};
|
|
223
|
+
const extent: usize = @intFromFloat(@max(1, @round(@max(icon.view_box.width, icon.view_box.height))));
|
|
224
|
+
return .{ .ok = .{ .kind = .svg, .width = extent, .height = extent, .svg_buffer = buffer, .svg = icon } };
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ---------------------------------------------------------------------------
|
|
230
|
+
// Linear-light working surface
|
|
231
|
+
// ---------------------------------------------------------------------------
|
|
232
|
+
// All compositing and resampling happens on premultiplied linear-light
|
|
233
|
+
// f32 RGBA: averaging premultiplied values is the alpha-correct blend,
|
|
234
|
+
// and averaging linear (not gamma-encoded) values is the
|
|
235
|
+
// brightness-correct one — a black/white checkerboard downscales to the
|
|
236
|
+
// gray a viewer perceives, not a too-dark mush.
|
|
237
|
+
|
|
238
|
+
const LinearImage = struct {
|
|
239
|
+
width: usize,
|
|
240
|
+
height: usize,
|
|
241
|
+
/// Premultiplied linear RGBA, four f32 per pixel.
|
|
242
|
+
px: []f32,
|
|
243
|
+
|
|
244
|
+
fn initZero(gpa: std.mem.Allocator, width: usize, height: usize) error{OutOfMemory}!LinearImage {
|
|
245
|
+
const px = try gpa.alloc(f32, width * height * 4);
|
|
246
|
+
@memset(px, 0);
|
|
247
|
+
return .{ .width = width, .height = height, .px = px };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
fn deinit(self: LinearImage, gpa: std.mem.Allocator) void {
|
|
251
|
+
gpa.free(self.px);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/// sRGB byte -> linear, precomputed once.
|
|
256
|
+
const srgb_to_linear_table = blk: {
|
|
257
|
+
@setEvalBranchQuota(4_000_000);
|
|
258
|
+
var table: [256]f32 = undefined;
|
|
259
|
+
for (&table, 0..) |*entry, index| {
|
|
260
|
+
const value = @as(f64, @floatFromInt(index)) / 255.0;
|
|
261
|
+
entry.* = @floatCast(srgbToLinear(value));
|
|
262
|
+
}
|
|
263
|
+
break :blk table;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
fn srgbToLinear(value: f64) f64 {
|
|
267
|
+
if (value <= 0.04045) return value / 12.92;
|
|
268
|
+
return std.math.pow(f64, (value + 0.055) / 1.055, 2.4);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
fn linearToSrgb(value: f64) f64 {
|
|
272
|
+
if (value <= 0.0031308) return value * 12.92;
|
|
273
|
+
return 1.055 * std.math.pow(f64, value, 1.0 / 2.4) - 0.055;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
fn linearFromRgba8(gpa: std.mem.Allocator, image: Rgba8Image) error{OutOfMemory}!LinearImage {
|
|
277
|
+
const px = try gpa.alloc(f32, image.width * image.height * 4);
|
|
278
|
+
var index: usize = 0;
|
|
279
|
+
while (index < image.width * image.height) : (index += 1) {
|
|
280
|
+
const base = index * 4;
|
|
281
|
+
const alpha = @as(f32, @floatFromInt(image.pixels[base + 3])) / 255.0;
|
|
282
|
+
px[base + 0] = srgb_to_linear_table[image.pixels[base + 0]] * alpha;
|
|
283
|
+
px[base + 1] = srgb_to_linear_table[image.pixels[base + 1]] * alpha;
|
|
284
|
+
px[base + 2] = srgb_to_linear_table[image.pixels[base + 2]] * alpha;
|
|
285
|
+
px[base + 3] = alpha;
|
|
286
|
+
}
|
|
287
|
+
return .{ .width = image.width, .height = image.height, .px = px };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
fn rgba8FromLinear(gpa: std.mem.Allocator, image: LinearImage) error{OutOfMemory}![]u8 {
|
|
291
|
+
const out = try gpa.alloc(u8, image.width * image.height * 4);
|
|
292
|
+
var index: usize = 0;
|
|
293
|
+
while (index < image.width * image.height) : (index += 1) {
|
|
294
|
+
const base = index * 4;
|
|
295
|
+
const alpha = std.math.clamp(image.px[base + 3], 0, 1);
|
|
296
|
+
if (alpha > 0.0001) {
|
|
297
|
+
out[base + 0] = quantizeSrgb(image.px[base + 0] / alpha);
|
|
298
|
+
out[base + 1] = quantizeSrgb(image.px[base + 1] / alpha);
|
|
299
|
+
out[base + 2] = quantizeSrgb(image.px[base + 2] / alpha);
|
|
300
|
+
} else {
|
|
301
|
+
out[base + 0] = 0;
|
|
302
|
+
out[base + 1] = 0;
|
|
303
|
+
out[base + 2] = 0;
|
|
304
|
+
}
|
|
305
|
+
out[base + 3] = @intFromFloat(@round(alpha * 255.0));
|
|
306
|
+
}
|
|
307
|
+
return out;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
fn quantizeSrgb(linear: f32) u8 {
|
|
311
|
+
const encoded = linearToSrgb(std.math.clamp(linear, 0, 1));
|
|
312
|
+
return @intFromFloat(std.math.clamp(encoded * 255.0 + 0.5, 0, 255));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/// Exact area-average (box) resample: every destination pixel is the
|
|
316
|
+
/// mean of the source area it covers, with fractional edge coverage —
|
|
317
|
+
/// no source pixel is skipped, so 1024 -> 16 keeps every detail's
|
|
318
|
+
/// energy instead of point-sampling a moire pattern out of it.
|
|
319
|
+
fn boxResample(gpa: std.mem.Allocator, source: LinearImage, width: usize, height: usize) error{OutOfMemory}!LinearImage {
|
|
320
|
+
const out = try gpa.alloc(f32, width * height * 4);
|
|
321
|
+
const ratio_x = @as(f64, @floatFromInt(source.width)) / @as(f64, @floatFromInt(width));
|
|
322
|
+
const ratio_y = @as(f64, @floatFromInt(source.height)) / @as(f64, @floatFromInt(height));
|
|
323
|
+
var oy: usize = 0;
|
|
324
|
+
while (oy < height) : (oy += 1) {
|
|
325
|
+
const sy0 = @as(f64, @floatFromInt(oy)) * ratio_y;
|
|
326
|
+
const sy1 = @as(f64, @floatFromInt(oy + 1)) * ratio_y;
|
|
327
|
+
var ox: usize = 0;
|
|
328
|
+
while (ox < width) : (ox += 1) {
|
|
329
|
+
const sx0 = @as(f64, @floatFromInt(ox)) * ratio_x;
|
|
330
|
+
const sx1 = @as(f64, @floatFromInt(ox + 1)) * ratio_x;
|
|
331
|
+
var acc = [4]f64{ 0, 0, 0, 0 };
|
|
332
|
+
var area: f64 = 0;
|
|
333
|
+
var sy: usize = @intFromFloat(@floor(sy0));
|
|
334
|
+
while (sy < source.height and @as(f64, @floatFromInt(sy)) < sy1) : (sy += 1) {
|
|
335
|
+
const cover_y = @min(sy1, @as(f64, @floatFromInt(sy + 1))) - @max(sy0, @as(f64, @floatFromInt(sy)));
|
|
336
|
+
if (cover_y <= 0) continue;
|
|
337
|
+
var sx: usize = @intFromFloat(@floor(sx0));
|
|
338
|
+
while (sx < source.width and @as(f64, @floatFromInt(sx)) < sx1) : (sx += 1) {
|
|
339
|
+
const cover_x = @min(sx1, @as(f64, @floatFromInt(sx + 1))) - @max(sx0, @as(f64, @floatFromInt(sx)));
|
|
340
|
+
if (cover_x <= 0) continue;
|
|
341
|
+
const weight = cover_x * cover_y;
|
|
342
|
+
const base = (sy * source.width + sx) * 4;
|
|
343
|
+
acc[0] += source.px[base + 0] * weight;
|
|
344
|
+
acc[1] += source.px[base + 1] * weight;
|
|
345
|
+
acc[2] += source.px[base + 2] * weight;
|
|
346
|
+
acc[3] += source.px[base + 3] * weight;
|
|
347
|
+
area += weight;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const base = (oy * width + ox) * 4;
|
|
351
|
+
if (area > 0) {
|
|
352
|
+
out[base + 0] = @floatCast(acc[0] / area);
|
|
353
|
+
out[base + 1] = @floatCast(acc[1] / area);
|
|
354
|
+
out[base + 2] = @floatCast(acc[2] / area);
|
|
355
|
+
out[base + 3] = @floatCast(acc[3] / area);
|
|
356
|
+
} else {
|
|
357
|
+
out[base + 0] = 0;
|
|
358
|
+
out[base + 1] = 0;
|
|
359
|
+
out[base + 2] = 0;
|
|
360
|
+
out[base + 3] = 0;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return .{ .width = width, .height = height, .px = out };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// ---------------------------------------------------------------------------
|
|
368
|
+
// SVG rasterization (per output size, through the vector core)
|
|
369
|
+
// ---------------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
const CoverageCompositeSink = struct {
|
|
372
|
+
image: *LinearImage,
|
|
373
|
+
/// Premultiplied linear source color.
|
|
374
|
+
r: f32,
|
|
375
|
+
g: f32,
|
|
376
|
+
b: f32,
|
|
377
|
+
a: f32,
|
|
378
|
+
|
|
379
|
+
pub fn pixel(self: *CoverageCompositeSink, x: i32, y: i32, coverage: f32) void {
|
|
380
|
+
if (x < 0 or y < 0) return;
|
|
381
|
+
const xu: usize = @intCast(x);
|
|
382
|
+
const yu: usize = @intCast(y);
|
|
383
|
+
if (xu >= self.image.width or yu >= self.image.height) return;
|
|
384
|
+
const clamped = std.math.clamp(coverage, 0, 1);
|
|
385
|
+
if (clamped <= 0) return;
|
|
386
|
+
const base = (yu * self.image.width + xu) * 4;
|
|
387
|
+
const inverse = 1 - self.a * clamped;
|
|
388
|
+
self.image.px[base + 0] = self.r * clamped + self.image.px[base + 0] * inverse;
|
|
389
|
+
self.image.px[base + 1] = self.g * clamped + self.image.px[base + 1] * inverse;
|
|
390
|
+
self.image.px[base + 2] = self.b * clamped + self.image.px[base + 2] * inverse;
|
|
391
|
+
self.image.px[base + 3] = self.a * clamped + self.image.px[base + 3] * inverse;
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
fn linearPaintSink(image: *LinearImage, paint: svg_icon.Paint) ?CoverageCompositeSink {
|
|
396
|
+
const color: drawing_model.Color = switch (paint) {
|
|
397
|
+
.none => return null,
|
|
398
|
+
// A standalone icon has no widget foreground to inherit;
|
|
399
|
+
// `currentColor` resolves to black, the SVG initial color.
|
|
400
|
+
.current_color => drawing_model.Color.rgb8(0, 0, 0),
|
|
401
|
+
.color => |value| value,
|
|
402
|
+
};
|
|
403
|
+
const linear_r: f32 = @floatCast(srgbToLinear(std.math.clamp(color.r, 0, 1)));
|
|
404
|
+
const linear_g: f32 = @floatCast(srgbToLinear(std.math.clamp(color.g, 0, 1)));
|
|
405
|
+
const linear_b: f32 = @floatCast(srgbToLinear(std.math.clamp(color.b, 0, 1)));
|
|
406
|
+
const alpha = std.math.clamp(color.a, 0, 1);
|
|
407
|
+
return .{ .image = image, .r = linear_r * alpha, .g = linear_g * alpha, .b = linear_b * alpha, .a = alpha };
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/// Rasterize a parsed SVG icon into `image`, its viewBox mapped to the
|
|
411
|
+
/// square (`origin`, `origin + extent`).
|
|
412
|
+
fn rasterizeSvg(image: *LinearImage, icon: svg_icon.Icon, origin: f32, extent: f32) vector.Error!void {
|
|
413
|
+
const box_extent = @max(icon.view_box.width, icon.view_box.height);
|
|
414
|
+
if (!(box_extent > 0) or !(extent > 0)) return;
|
|
415
|
+
const scale = extent / box_extent;
|
|
416
|
+
// Center a non-square viewBox inside the square target.
|
|
417
|
+
const offset_x = origin + (extent - icon.view_box.width * scale) * 0.5 - icon.view_box.x * scale;
|
|
418
|
+
const offset_y = origin + (extent - icon.view_box.height * scale) * 0.5 - icon.view_box.y * scale;
|
|
419
|
+
const transform = Affine.translate(offset_x, offset_y).multiply(Affine.scale(scale, scale));
|
|
420
|
+
const clip = vector.ClipRect{ .x0 = 0, .y0 = 0, .x1 = @intCast(image.width), .y1 = @intCast(image.height) };
|
|
421
|
+
|
|
422
|
+
for (icon.shapes) |shape| {
|
|
423
|
+
const elements = icon.elements[shape.start .. shape.start + shape.len];
|
|
424
|
+
if (linearPaintSink(image, shape.style.fill)) |sink| {
|
|
425
|
+
var fill_sink = sink;
|
|
426
|
+
try vector.fillPath(elements, transform, .nonzero, vector.default_tolerance, clip, &fill_sink);
|
|
427
|
+
}
|
|
428
|
+
if (linearPaintSink(image, shape.style.stroke)) |sink| {
|
|
429
|
+
var stroke_sink = sink;
|
|
430
|
+
try vector.strokePath(elements, transform, .{
|
|
431
|
+
.width = shape.style.stroke_width * scale,
|
|
432
|
+
.cap = shape.style.linecap,
|
|
433
|
+
.join = shape.style.linejoin,
|
|
434
|
+
}, vector.default_tolerance, clip, &stroke_sink);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// ---------------------------------------------------------------------------
|
|
440
|
+
// The macOS rounded-rect mask
|
|
441
|
+
// ---------------------------------------------------------------------------
|
|
442
|
+
|
|
443
|
+
const MaskSink = struct {
|
|
444
|
+
mask: []f32,
|
|
445
|
+
width: usize,
|
|
446
|
+
|
|
447
|
+
pub fn pixel(self: *MaskSink, x: i32, y: i32, coverage: f32) void {
|
|
448
|
+
if (x < 0 or y < 0) return;
|
|
449
|
+
const xu: usize = @intCast(x);
|
|
450
|
+
const yu: usize = @intCast(y);
|
|
451
|
+
if (xu >= self.width) return;
|
|
452
|
+
const index = yu * self.width + xu;
|
|
453
|
+
if (index >= self.mask.len) return;
|
|
454
|
+
self.mask[index] = std.math.clamp(self.mask[index] + coverage, 0, 1);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
/// Anti-aliased coverage of the icon-grid rounded rectangle on a
|
|
459
|
+
/// `size` x `size` canvas: one f32 per pixel in [0, 1].
|
|
460
|
+
pub fn macosMaskCoverage(gpa: std.mem.Allocator, size: usize) Error![]f32 {
|
|
461
|
+
const mask = try gpa.alloc(f32, size * size);
|
|
462
|
+
errdefer gpa.free(mask);
|
|
463
|
+
@memset(mask, 0);
|
|
464
|
+
|
|
465
|
+
const edge: f32 = @floatFromInt(size);
|
|
466
|
+
const x0: f32 = @floatCast(macos_margin_ratio * edge);
|
|
467
|
+
const extent: f32 = @floatCast(macos_artwork_ratio * edge);
|
|
468
|
+
const radius: f32 = @floatCast(macos_corner_radius_ratio * edge);
|
|
469
|
+
const x1 = x0 + extent;
|
|
470
|
+
const y0 = x0;
|
|
471
|
+
const y1 = x1;
|
|
472
|
+
|
|
473
|
+
var builder = vector.PathBuilder(64){};
|
|
474
|
+
try builder.moveTo(PointF.init(x0 + radius, y0));
|
|
475
|
+
try builder.lineTo(PointF.init(x1 - radius, y0));
|
|
476
|
+
try builder.arcTo(radius, radius, 0, false, true, PointF.init(x1, y0 + radius));
|
|
477
|
+
try builder.lineTo(PointF.init(x1, y1 - radius));
|
|
478
|
+
try builder.arcTo(radius, radius, 0, false, true, PointF.init(x1 - radius, y1));
|
|
479
|
+
try builder.lineTo(PointF.init(x0 + radius, y1));
|
|
480
|
+
try builder.arcTo(radius, radius, 0, false, true, PointF.init(x0, y1 - radius));
|
|
481
|
+
try builder.lineTo(PointF.init(x0, y0 + radius));
|
|
482
|
+
try builder.arcTo(radius, radius, 0, false, true, PointF.init(x0 + radius, y0));
|
|
483
|
+
try builder.close();
|
|
484
|
+
|
|
485
|
+
var sink = MaskSink{ .mask = mask, .width = size };
|
|
486
|
+
try vector.fillPath(
|
|
487
|
+
builder.slice(),
|
|
488
|
+
Affine.identity(),
|
|
489
|
+
.nonzero,
|
|
490
|
+
vector.default_tolerance,
|
|
491
|
+
.{ .x0 = 0, .y0 = 0, .x1 = @intCast(size), .y1 = @intCast(size) },
|
|
492
|
+
&sink,
|
|
493
|
+
);
|
|
494
|
+
return mask;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
fn applyMask(image: *LinearImage, mask: []const f32) void {
|
|
498
|
+
for (mask, 0..) |coverage, index| {
|
|
499
|
+
const base = index * 4;
|
|
500
|
+
image.px[base + 0] *= coverage;
|
|
501
|
+
image.px[base + 1] *= coverage;
|
|
502
|
+
image.px[base + 2] *= coverage;
|
|
503
|
+
image.px[base + 3] *= coverage;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/// A source whose four corners are already fully transparent shaped its
|
|
508
|
+
/// own silhouette (its author baked a mask and margins); the macOS path
|
|
509
|
+
/// ships it untouched instead of shrinking and re-masking it. Full-bleed
|
|
510
|
+
/// art — any opaque corner — gets the standard grid treatment.
|
|
511
|
+
pub fn imageIsPreShaped(image: Rgba8Image) bool {
|
|
512
|
+
if (image.width == 0 or image.height == 0) return false;
|
|
513
|
+
const corners = [_]usize{
|
|
514
|
+
0,
|
|
515
|
+
image.width - 1,
|
|
516
|
+
(image.height - 1) * image.width,
|
|
517
|
+
image.height * image.width - 1,
|
|
518
|
+
};
|
|
519
|
+
for (corners) |pixel_index| {
|
|
520
|
+
if (image.pixels[pixel_index * 4 + 3] != 0) return false;
|
|
521
|
+
}
|
|
522
|
+
return true;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
fn sourceIsPreShaped(gpa: std.mem.Allocator, source: *const Source) error{OutOfMemory}!bool {
|
|
526
|
+
switch (source.kind) {
|
|
527
|
+
.png => return imageIsPreShaped(source.image.?),
|
|
528
|
+
.svg => {
|
|
529
|
+
// Rasterize a small full-canvas probe and inspect its corners.
|
|
530
|
+
const probe_size: usize = 64;
|
|
531
|
+
var probe = try LinearImage.initZero(gpa, probe_size, probe_size);
|
|
532
|
+
defer probe.deinit(gpa);
|
|
533
|
+
rasterizeSvg(&probe, source.svg, 0, @floatFromInt(probe_size)) catch return false;
|
|
534
|
+
const corners = [_]usize{ 0, probe_size - 1, (probe_size - 1) * probe_size, probe_size * probe_size - 1 };
|
|
535
|
+
for (corners) |pixel_index| {
|
|
536
|
+
if (probe.px[pixel_index * 4 + 3] > 0.004) return false;
|
|
537
|
+
}
|
|
538
|
+
return true;
|
|
539
|
+
},
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// ---------------------------------------------------------------------------
|
|
544
|
+
// Renders
|
|
545
|
+
// ---------------------------------------------------------------------------
|
|
546
|
+
|
|
547
|
+
/// Square, unmasked render of the source at `size` (Windows/Linux/mobile
|
|
548
|
+
/// artifacts and the pre-shaped macOS path). Straight-alpha RGBA8.
|
|
549
|
+
pub fn renderSquare(gpa: std.mem.Allocator, source: *const Source, size: usize) Error![]u8 {
|
|
550
|
+
switch (source.kind) {
|
|
551
|
+
.png => {
|
|
552
|
+
var linear = try linearFromRgba8(gpa, source.image.?);
|
|
553
|
+
defer linear.deinit(gpa);
|
|
554
|
+
if (linear.width == size and linear.height == size) return rgba8FromLinear(gpa, linear);
|
|
555
|
+
var resampled = try boxResample(gpa, linear, size, size);
|
|
556
|
+
defer resampled.deinit(gpa);
|
|
557
|
+
return rgba8FromLinear(gpa, resampled);
|
|
558
|
+
},
|
|
559
|
+
.svg => {
|
|
560
|
+
var canvas = try LinearImage.initZero(gpa, size, size);
|
|
561
|
+
defer canvas.deinit(gpa);
|
|
562
|
+
try rasterizeSvg(&canvas, source.svg, 0, @floatFromInt(size));
|
|
563
|
+
return rgba8FromLinear(gpa, canvas);
|
|
564
|
+
},
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/// macOS canvas render at `size`: full-bleed sources are inset to the
|
|
569
|
+
/// icon-grid artwork square and masked; pre-shaped sources render
|
|
570
|
+
/// full-canvas untouched. Straight-alpha RGBA8.
|
|
571
|
+
pub fn renderMacosCanvas(gpa: std.mem.Allocator, source: *const Source, size: usize) Error![]u8 {
|
|
572
|
+
const renders = try renderMacosFamily(gpa, source, &.{size});
|
|
573
|
+
defer gpa.free(renders);
|
|
574
|
+
return renders[0];
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/// Render the macOS canvas at every requested size. PNG sources compose
|
|
578
|
+
/// a single 1024 master (inset + mask once, anti-aliased by the box
|
|
579
|
+
/// resample at each size); SVG sources rasterize and mask per size
|
|
580
|
+
/// directly. Caller owns each returned RGBA8 buffer and the slice.
|
|
581
|
+
fn renderMacosFamily(gpa: std.mem.Allocator, source: *const Source, sizes: []const usize) Error![][]u8 {
|
|
582
|
+
const out = try gpa.alloc([]u8, sizes.len);
|
|
583
|
+
var completed: usize = 0;
|
|
584
|
+
// errdefers run last-declared-first: free the renders before the
|
|
585
|
+
// slice that holds them.
|
|
586
|
+
errdefer gpa.free(out);
|
|
587
|
+
errdefer for (out[0..completed]) |render| gpa.free(render);
|
|
588
|
+
|
|
589
|
+
const pre_shaped = try sourceIsPreShaped(gpa, source);
|
|
590
|
+
switch (source.kind) {
|
|
591
|
+
.png => {
|
|
592
|
+
var master = try macosPngMaster(gpa, source.image.?, pre_shaped);
|
|
593
|
+
defer master.deinit(gpa);
|
|
594
|
+
for (sizes, 0..) |size, index| {
|
|
595
|
+
if (size == master.width) {
|
|
596
|
+
out[index] = try rgba8FromLinear(gpa, master);
|
|
597
|
+
} else {
|
|
598
|
+
var resampled = try boxResample(gpa, master, size, size);
|
|
599
|
+
defer resampled.deinit(gpa);
|
|
600
|
+
out[index] = try rgba8FromLinear(gpa, resampled);
|
|
601
|
+
}
|
|
602
|
+
completed += 1;
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
.svg => {
|
|
606
|
+
for (sizes, 0..) |size, index| {
|
|
607
|
+
var canvas = try LinearImage.initZero(gpa, size, size);
|
|
608
|
+
defer canvas.deinit(gpa);
|
|
609
|
+
if (pre_shaped) {
|
|
610
|
+
try rasterizeSvg(&canvas, source.svg, 0, @floatFromInt(size));
|
|
611
|
+
} else {
|
|
612
|
+
const edge: f32 = @floatFromInt(size);
|
|
613
|
+
const extent: f32 = @floatCast(macos_artwork_ratio * @as(f64, edge));
|
|
614
|
+
const origin: f32 = @floatCast(macos_margin_ratio * @as(f64, edge));
|
|
615
|
+
try rasterizeSvg(&canvas, source.svg, origin, extent);
|
|
616
|
+
const mask = try macosMaskCoverage(gpa, size);
|
|
617
|
+
defer gpa.free(mask);
|
|
618
|
+
applyMask(&canvas, mask);
|
|
619
|
+
}
|
|
620
|
+
out[index] = try rgba8FromLinear(gpa, canvas);
|
|
621
|
+
completed += 1;
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
}
|
|
625
|
+
return out;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/// The composed 1024 linear master for a PNG source.
|
|
629
|
+
fn macosPngMaster(gpa: std.mem.Allocator, image: Rgba8Image, pre_shaped: bool) Error!LinearImage {
|
|
630
|
+
var linear = try linearFromRgba8(gpa, image);
|
|
631
|
+
if (pre_shaped) {
|
|
632
|
+
if (linear.width == master_size) return linear;
|
|
633
|
+
defer linear.deinit(gpa);
|
|
634
|
+
return boxResample(gpa, linear, master_size, master_size);
|
|
635
|
+
}
|
|
636
|
+
defer linear.deinit(gpa);
|
|
637
|
+
|
|
638
|
+
const artwork_extent: usize = @intFromFloat(@round(macos_artwork_ratio * @as(f64, @floatFromInt(master_size))));
|
|
639
|
+
const margin: usize = (master_size - artwork_extent) / 2;
|
|
640
|
+
var artwork = try boxResample(gpa, linear, artwork_extent, artwork_extent);
|
|
641
|
+
defer artwork.deinit(gpa);
|
|
642
|
+
|
|
643
|
+
var master = try LinearImage.initZero(gpa, master_size, master_size);
|
|
644
|
+
errdefer master.deinit(gpa);
|
|
645
|
+
var y: usize = 0;
|
|
646
|
+
while (y < artwork_extent) : (y += 1) {
|
|
647
|
+
const source_row = artwork.px[y * artwork_extent * 4 .. (y + 1) * artwork_extent * 4];
|
|
648
|
+
const dest_start = ((y + margin) * master_size + margin) * 4;
|
|
649
|
+
@memcpy(master.px[dest_start .. dest_start + source_row.len], source_row);
|
|
650
|
+
}
|
|
651
|
+
const mask = try macosMaskCoverage(gpa, master_size);
|
|
652
|
+
defer gpa.free(mask);
|
|
653
|
+
applyMask(&master, mask);
|
|
654
|
+
return master;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// ---------------------------------------------------------------------------
|
|
658
|
+
// PNG encode (compressed, deterministic)
|
|
659
|
+
// ---------------------------------------------------------------------------
|
|
660
|
+
|
|
661
|
+
/// Encode straight-alpha RGBA8 as a real deflate-compressed PNG (Up row
|
|
662
|
+
/// filter). The canvas PNG writer (`png.zig`) emits stored blocks for
|
|
663
|
+
/// screenshot determinism; icon artifacts want small files, and flate
|
|
664
|
+
/// with fixed parameters is just as deterministic.
|
|
665
|
+
pub fn encodePng(gpa: std.mem.Allocator, rgba: []const u8, width: usize, height: usize) Error![]u8 {
|
|
666
|
+
if (width == 0 or height == 0 or rgba.len < width * height * 4) return error.UnsupportedImage;
|
|
667
|
+
const flate = std.compress.flate;
|
|
668
|
+
const row_len = 1 + width * 4;
|
|
669
|
+
const raw = try gpa.alloc(u8, row_len * height);
|
|
670
|
+
defer gpa.free(raw);
|
|
671
|
+
var y: usize = 0;
|
|
672
|
+
while (y < height) : (y += 1) {
|
|
673
|
+
const row = raw[y * row_len ..][0..row_len];
|
|
674
|
+
row[0] = 2; // Up filter: predict each byte from the row above.
|
|
675
|
+
const current = rgba[y * width * 4 ..][0 .. width * 4];
|
|
676
|
+
if (y == 0) {
|
|
677
|
+
@memcpy(row[1..], current);
|
|
678
|
+
} else {
|
|
679
|
+
const above = rgba[(y - 1) * width * 4 ..][0 .. width * 4];
|
|
680
|
+
for (current, above, row[1..]) |value, prediction, *out| out.* = value -% prediction;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const zlib_capacity = raw.len + raw.len / 8 + 1024;
|
|
685
|
+
const zlib_buffer = try gpa.alloc(u8, zlib_capacity);
|
|
686
|
+
defer gpa.free(zlib_buffer);
|
|
687
|
+
var zlib_writer = std.Io.Writer.fixed(zlib_buffer);
|
|
688
|
+
const window = try gpa.alloc(u8, flate.max_window_len * 2);
|
|
689
|
+
defer gpa.free(window);
|
|
690
|
+
var compress = flate.Compress.init(&zlib_writer, window, .zlib, .default) catch return error.UnsupportedImage;
|
|
691
|
+
compress.writer.writeAll(raw) catch return error.UnsupportedImage;
|
|
692
|
+
compress.finish() catch return error.UnsupportedImage;
|
|
693
|
+
const idat = zlib_writer.buffered();
|
|
694
|
+
|
|
695
|
+
var out: std.ArrayList(u8) = .empty;
|
|
696
|
+
errdefer out.deinit(gpa);
|
|
697
|
+
try out.appendSlice(gpa, &png_model.signature);
|
|
698
|
+
var ihdr: [13]u8 = undefined;
|
|
699
|
+
std.mem.writeInt(u32, ihdr[0..4], @intCast(width), .big);
|
|
700
|
+
std.mem.writeInt(u32, ihdr[4..8], @intCast(height), .big);
|
|
701
|
+
ihdr[8] = 8; // bit depth
|
|
702
|
+
ihdr[9] = 6; // truecolor with alpha
|
|
703
|
+
ihdr[10] = 0; // compression
|
|
704
|
+
ihdr[11] = 0; // filter method
|
|
705
|
+
ihdr[12] = 0; // interlace
|
|
706
|
+
try appendPngChunk(&out, gpa, "IHDR", &ihdr);
|
|
707
|
+
try appendPngChunk(&out, gpa, "IDAT", idat);
|
|
708
|
+
try appendPngChunk(&out, gpa, "IEND", &.{});
|
|
709
|
+
return out.toOwnedSlice(gpa);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
fn appendPngChunk(list: *std.ArrayList(u8), gpa: std.mem.Allocator, kind: *const [4]u8, data: []const u8) error{OutOfMemory}!void {
|
|
713
|
+
var length_bytes: [4]u8 = undefined;
|
|
714
|
+
std.mem.writeInt(u32, &length_bytes, @intCast(data.len), .big);
|
|
715
|
+
try list.appendSlice(gpa, &length_bytes);
|
|
716
|
+
try list.appendSlice(gpa, kind);
|
|
717
|
+
try list.appendSlice(gpa, data);
|
|
718
|
+
var crc = std.hash.Crc32.init();
|
|
719
|
+
crc.update(kind);
|
|
720
|
+
crc.update(data);
|
|
721
|
+
var crc_bytes: [4]u8 = undefined;
|
|
722
|
+
std.mem.writeInt(u32, &crc_bytes, crc.final(), .big);
|
|
723
|
+
try list.appendSlice(gpa, &crc_bytes);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// ---------------------------------------------------------------------------
|
|
727
|
+
// PNG decode (general-purpose reader for user-supplied sources)
|
|
728
|
+
// ---------------------------------------------------------------------------
|
|
729
|
+
|
|
730
|
+
pub const PngHeader = struct { width: usize, height: usize };
|
|
731
|
+
|
|
732
|
+
/// Cheap dimension peek (signature + IHDR only) for validation paths
|
|
733
|
+
/// that should not pay for a full decode.
|
|
734
|
+
pub fn pngHeader(bytes: []const u8) ?PngHeader {
|
|
735
|
+
if (bytes.len < png_model.signature.len + 25) return null;
|
|
736
|
+
if (!std.mem.eql(u8, bytes[0..png_model.signature.len], &png_model.signature)) return null;
|
|
737
|
+
const chunk = bytes[png_model.signature.len..];
|
|
738
|
+
if (!std.mem.eql(u8, chunk[4..8], "IHDR")) return null;
|
|
739
|
+
const width = std.mem.readInt(u32, chunk[8..12], .big);
|
|
740
|
+
const height = std.mem.readInt(u32, chunk[12..16], .big);
|
|
741
|
+
if (width == 0 or height == 0) return null;
|
|
742
|
+
return .{ .width = width, .height = height };
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/// Decode a PNG in the forms real icon sources take: 8- or 16-bit
|
|
746
|
+
/// samples; grayscale, truecolor, or palette color, each with or without
|
|
747
|
+
/// alpha (palette transparency via tRNS); all five row filters; single
|
|
748
|
+
/// or split IDAT chunks. Interlaced files and sub-byte depths are
|
|
749
|
+
/// rejected as unsupported. CRCs are not verified — a corrupt stream
|
|
750
|
+
/// fails at the zlib layer instead.
|
|
751
|
+
pub fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) error{ UnsupportedImage, OutOfMemory }!Rgba8Image {
|
|
752
|
+
if (bytes.len < png_model.signature.len) return error.UnsupportedImage;
|
|
753
|
+
if (!std.mem.eql(u8, bytes[0..png_model.signature.len], &png_model.signature)) return error.UnsupportedImage;
|
|
754
|
+
|
|
755
|
+
var width: usize = 0;
|
|
756
|
+
var height: usize = 0;
|
|
757
|
+
var bit_depth: u8 = 0;
|
|
758
|
+
var color_type: u8 = 0;
|
|
759
|
+
var palette: []const u8 = &.{};
|
|
760
|
+
var transparency: []const u8 = &.{};
|
|
761
|
+
var idat: std.ArrayList(u8) = .empty;
|
|
762
|
+
defer idat.deinit(gpa);
|
|
763
|
+
var saw_header = false;
|
|
764
|
+
var saw_end = false;
|
|
765
|
+
|
|
766
|
+
var offset: usize = png_model.signature.len;
|
|
767
|
+
while (offset + 12 <= bytes.len) {
|
|
768
|
+
const data_len = std.mem.readInt(u32, bytes[offset..][0..4], .big);
|
|
769
|
+
const chunk_type = bytes[offset + 4 .. offset + 8];
|
|
770
|
+
if (bytes.len - offset < 12 or bytes.len - offset - 12 < data_len) return error.UnsupportedImage;
|
|
771
|
+
const data = bytes[offset + 8 .. offset + 8 + data_len];
|
|
772
|
+
offset += 12 + data_len;
|
|
773
|
+
|
|
774
|
+
if (std.mem.eql(u8, chunk_type, "IHDR")) {
|
|
775
|
+
if (saw_header or data.len != 13) return error.UnsupportedImage;
|
|
776
|
+
saw_header = true;
|
|
777
|
+
width = std.mem.readInt(u32, data[0..4], .big);
|
|
778
|
+
height = std.mem.readInt(u32, data[4..8], .big);
|
|
779
|
+
if (width == 0 or height == 0) return error.UnsupportedImage;
|
|
780
|
+
if (width > max_source_dimension or height > max_source_dimension) return error.UnsupportedImage;
|
|
781
|
+
bit_depth = data[8];
|
|
782
|
+
color_type = data[9];
|
|
783
|
+
if (bit_depth != 8 and bit_depth != 16) return error.UnsupportedImage;
|
|
784
|
+
if (color_type == 3 and bit_depth != 8) return error.UnsupportedImage;
|
|
785
|
+
switch (color_type) {
|
|
786
|
+
0, 2, 3, 4, 6 => {},
|
|
787
|
+
else => return error.UnsupportedImage,
|
|
788
|
+
}
|
|
789
|
+
if (data[10] != 0 or data[11] != 0) return error.UnsupportedImage;
|
|
790
|
+
if (data[12] != 0) return error.UnsupportedImage; // no interlace
|
|
791
|
+
} else if (std.mem.eql(u8, chunk_type, "PLTE")) {
|
|
792
|
+
if (data.len == 0 or data.len % 3 != 0 or data.len > 256 * 3) return error.UnsupportedImage;
|
|
793
|
+
palette = data;
|
|
794
|
+
} else if (std.mem.eql(u8, chunk_type, "tRNS")) {
|
|
795
|
+
transparency = data;
|
|
796
|
+
} else if (std.mem.eql(u8, chunk_type, "IDAT")) {
|
|
797
|
+
try idat.appendSlice(gpa, data);
|
|
798
|
+
} else if (std.mem.eql(u8, chunk_type, "IEND")) {
|
|
799
|
+
saw_end = true;
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
// Ancillary chunks (gAMA, iCCP, tEXt, ...) are skipped.
|
|
803
|
+
}
|
|
804
|
+
if (!saw_header or !saw_end or idat.items.len == 0) return error.UnsupportedImage;
|
|
805
|
+
if (color_type == 3 and palette.len == 0) return error.UnsupportedImage;
|
|
806
|
+
|
|
807
|
+
const channels: usize = switch (color_type) {
|
|
808
|
+
0, 3 => 1,
|
|
809
|
+
2 => 3,
|
|
810
|
+
4 => 2,
|
|
811
|
+
6 => 4,
|
|
812
|
+
else => unreachable,
|
|
813
|
+
};
|
|
814
|
+
const sample_bytes: usize = bit_depth / 8;
|
|
815
|
+
const pixel_bytes = channels * sample_bytes;
|
|
816
|
+
const row_bytes = width * pixel_bytes;
|
|
817
|
+
const raw_len = height * (1 + row_bytes);
|
|
818
|
+
|
|
819
|
+
const raw = try gpa.alloc(u8, raw_len);
|
|
820
|
+
defer gpa.free(raw);
|
|
821
|
+
{
|
|
822
|
+
const flate = std.compress.flate;
|
|
823
|
+
var input: std.Io.Reader = .fixed(idat.items);
|
|
824
|
+
const window = try gpa.alloc(u8, flate.max_window_len);
|
|
825
|
+
defer gpa.free(window);
|
|
826
|
+
var decompress = flate.Decompress.init(&input, .zlib, window);
|
|
827
|
+
decompress.reader.readSliceAll(raw) catch return error.UnsupportedImage;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// Undo the per-row prediction filters in place.
|
|
831
|
+
var y: usize = 0;
|
|
832
|
+
while (y < height) : (y += 1) {
|
|
833
|
+
const row_start = y * (1 + row_bytes);
|
|
834
|
+
const filter = raw[row_start];
|
|
835
|
+
const row = raw[row_start + 1 .. row_start + 1 + row_bytes];
|
|
836
|
+
const above: ?[]u8 = if (y > 0) raw[(y - 1) * (1 + row_bytes) + 1 ..][0..row_bytes] else null;
|
|
837
|
+
switch (filter) {
|
|
838
|
+
0 => {},
|
|
839
|
+
1 => { // Sub
|
|
840
|
+
var index: usize = pixel_bytes;
|
|
841
|
+
while (index < row_bytes) : (index += 1) row[index] +%= row[index - pixel_bytes];
|
|
842
|
+
},
|
|
843
|
+
2 => { // Up
|
|
844
|
+
if (above) |previous| {
|
|
845
|
+
for (row, previous) |*value, up| value.* +%= up;
|
|
846
|
+
}
|
|
847
|
+
},
|
|
848
|
+
3 => { // Average
|
|
849
|
+
var index: usize = 0;
|
|
850
|
+
while (index < row_bytes) : (index += 1) {
|
|
851
|
+
const left: u16 = if (index >= pixel_bytes) row[index - pixel_bytes] else 0;
|
|
852
|
+
const up: u16 = if (above) |previous| previous[index] else 0;
|
|
853
|
+
row[index] +%= @truncate((left + up) / 2);
|
|
854
|
+
}
|
|
855
|
+
},
|
|
856
|
+
4 => { // Paeth
|
|
857
|
+
var index: usize = 0;
|
|
858
|
+
while (index < row_bytes) : (index += 1) {
|
|
859
|
+
const left: i32 = if (index >= pixel_bytes) row[index - pixel_bytes] else 0;
|
|
860
|
+
const up: i32 = if (above) |previous| previous[index] else 0;
|
|
861
|
+
const up_left: i32 = if (above != null and index >= pixel_bytes) above.?[index - pixel_bytes] else 0;
|
|
862
|
+
const estimate = left + up - up_left;
|
|
863
|
+
const delta_left = @abs(estimate - left);
|
|
864
|
+
const delta_up = @abs(estimate - up);
|
|
865
|
+
const delta_up_left = @abs(estimate - up_left);
|
|
866
|
+
const prediction: i32 = if (delta_left <= delta_up and delta_left <= delta_up_left) left else if (delta_up <= delta_up_left) up else up_left;
|
|
867
|
+
row[index] +%= @intCast(prediction);
|
|
868
|
+
}
|
|
869
|
+
},
|
|
870
|
+
else => return error.UnsupportedImage,
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// Expand to straight-alpha RGBA8 (16-bit samples keep the high byte).
|
|
875
|
+
const pixels = try gpa.alloc(u8, width * height * 4);
|
|
876
|
+
errdefer gpa.free(pixels);
|
|
877
|
+
y = 0;
|
|
878
|
+
while (y < height) : (y += 1) {
|
|
879
|
+
const row = raw[y * (1 + row_bytes) + 1 ..][0..row_bytes];
|
|
880
|
+
var x: usize = 0;
|
|
881
|
+
while (x < width) : (x += 1) {
|
|
882
|
+
const source = row[x * pixel_bytes ..][0..pixel_bytes];
|
|
883
|
+
const dest = pixels[(y * width + x) * 4 ..][0..4];
|
|
884
|
+
switch (color_type) {
|
|
885
|
+
0 => {
|
|
886
|
+
const value = source[0];
|
|
887
|
+
dest.* = .{ value, value, value, 255 };
|
|
888
|
+
},
|
|
889
|
+
2 => {
|
|
890
|
+
dest.* = .{ source[0], source[sample_bytes], source[sample_bytes * 2], 255 };
|
|
891
|
+
},
|
|
892
|
+
3 => {
|
|
893
|
+
const index: usize = source[0];
|
|
894
|
+
if (index * 3 + 2 >= palette.len) return error.UnsupportedImage;
|
|
895
|
+
const alpha: u8 = if (index < transparency.len) transparency[index] else 255;
|
|
896
|
+
dest.* = .{ palette[index * 3], palette[index * 3 + 1], palette[index * 3 + 2], alpha };
|
|
897
|
+
},
|
|
898
|
+
4 => {
|
|
899
|
+
const value = source[0];
|
|
900
|
+
dest.* = .{ value, value, value, source[sample_bytes] };
|
|
901
|
+
},
|
|
902
|
+
6 => {
|
|
903
|
+
dest.* = .{ source[0], source[sample_bytes], source[sample_bytes * 2], source[sample_bytes * 3] };
|
|
904
|
+
},
|
|
905
|
+
else => unreachable,
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
return .{ .width = width, .height = height, .pixels = pixels };
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
// ---------------------------------------------------------------------------
|
|
913
|
+
// ARGB payloads (the 16/32 px 1x members)
|
|
914
|
+
// ---------------------------------------------------------------------------
|
|
915
|
+
|
|
916
|
+
/// Encode straight-alpha RGBA8 as an icns ARGB payload: the "ARGB"
|
|
917
|
+
/// magic, then the A, R, G, B planes each run-length encoded with the
|
|
918
|
+
/// PackBits-style scheme icns uses (control < 0x80: that many + 1
|
|
919
|
+
/// literal bytes follow; control >= 0x80: repeat the next byte
|
|
920
|
+
/// control - 0x80 + 3 times).
|
|
921
|
+
pub fn encodeArgb(gpa: std.mem.Allocator, rgba: []const u8, width: usize, height: usize) Error![]u8 {
|
|
922
|
+
if (width == 0 or height == 0 or rgba.len < width * height * 4) return error.UnsupportedImage;
|
|
923
|
+
const pixel_count = width * height;
|
|
924
|
+
var out: std.ArrayList(u8) = .empty;
|
|
925
|
+
errdefer out.deinit(gpa);
|
|
926
|
+
try out.appendSlice(gpa, "ARGB");
|
|
927
|
+
const plane = try gpa.alloc(u8, pixel_count);
|
|
928
|
+
defer gpa.free(plane);
|
|
929
|
+
const channel_order = [4]usize{ 3, 0, 1, 2 }; // A, R, G, B
|
|
930
|
+
for (channel_order) |channel| {
|
|
931
|
+
var index: usize = 0;
|
|
932
|
+
while (index < pixel_count) : (index += 1) plane[index] = rgba[index * 4 + channel];
|
|
933
|
+
try appendPackBits(&out, gpa, plane);
|
|
934
|
+
}
|
|
935
|
+
return out.toOwnedSlice(gpa);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
fn appendPackBits(list: *std.ArrayList(u8), gpa: std.mem.Allocator, bytes: []const u8) error{OutOfMemory}!void {
|
|
939
|
+
var index: usize = 0;
|
|
940
|
+
while (index < bytes.len) {
|
|
941
|
+
var run: usize = 1;
|
|
942
|
+
while (index + run < bytes.len and bytes[index + run] == bytes[index] and run < 130) run += 1;
|
|
943
|
+
if (run >= 3) {
|
|
944
|
+
try list.append(gpa, @intCast(0x80 + run - 3));
|
|
945
|
+
try list.append(gpa, bytes[index]);
|
|
946
|
+
index += run;
|
|
947
|
+
continue;
|
|
948
|
+
}
|
|
949
|
+
// Literal segment: up to 128 bytes, stopping before the next
|
|
950
|
+
// three-byte repeat run.
|
|
951
|
+
var end = index + 1;
|
|
952
|
+
while (end < bytes.len and end - index < 128) {
|
|
953
|
+
if (end + 2 < bytes.len and bytes[end] == bytes[end + 1] and bytes[end] == bytes[end + 2]) break;
|
|
954
|
+
end += 1;
|
|
955
|
+
}
|
|
956
|
+
try list.append(gpa, @intCast(end - index - 1));
|
|
957
|
+
try list.appendSlice(gpa, bytes[index..end]);
|
|
958
|
+
index = end;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/// Decode an ARGB payload back to straight-alpha RGBA8 (tests and
|
|
963
|
+
/// round-trip validation of our own output).
|
|
964
|
+
pub fn decodeArgb(gpa: std.mem.Allocator, payload: []const u8, width: usize, height: usize) Error![]u8 {
|
|
965
|
+
if (payload.len < 4 or !std.mem.eql(u8, payload[0..4], "ARGB")) return error.UnsupportedImage;
|
|
966
|
+
const pixel_count = width * height;
|
|
967
|
+
const planes = try gpa.alloc(u8, pixel_count * 4);
|
|
968
|
+
defer gpa.free(planes);
|
|
969
|
+
var produced: usize = 0;
|
|
970
|
+
var offset: usize = 4;
|
|
971
|
+
while (produced < pixel_count * 4) {
|
|
972
|
+
if (offset >= payload.len) return error.UnsupportedImage;
|
|
973
|
+
const control = payload[offset];
|
|
974
|
+
offset += 1;
|
|
975
|
+
if (control >= 0x80) {
|
|
976
|
+
const run = @as(usize, control) - 0x80 + 3;
|
|
977
|
+
if (offset >= payload.len or produced + run > planes.len) return error.UnsupportedImage;
|
|
978
|
+
@memset(planes[produced .. produced + run], payload[offset]);
|
|
979
|
+
offset += 1;
|
|
980
|
+
produced += run;
|
|
981
|
+
} else {
|
|
982
|
+
const run = @as(usize, control) + 1;
|
|
983
|
+
if (offset + run > payload.len or produced + run > planes.len) return error.UnsupportedImage;
|
|
984
|
+
@memcpy(planes[produced .. produced + run], payload[offset .. offset + run]);
|
|
985
|
+
offset += run;
|
|
986
|
+
produced += run;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
const rgba = try gpa.alloc(u8, pixel_count * 4);
|
|
990
|
+
errdefer gpa.free(rgba);
|
|
991
|
+
var index: usize = 0;
|
|
992
|
+
while (index < pixel_count) : (index += 1) {
|
|
993
|
+
rgba[index * 4 + 3] = planes[index]; // A
|
|
994
|
+
rgba[index * 4 + 0] = planes[pixel_count + index]; // R
|
|
995
|
+
rgba[index * 4 + 1] = planes[pixel_count * 2 + index]; // G
|
|
996
|
+
rgba[index * 4 + 2] = planes[pixel_count * 3 + index]; // B
|
|
997
|
+
}
|
|
998
|
+
return rgba;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/// Encode one render as the payload its slot expects.
|
|
1002
|
+
pub fn encodeIcnsPayload(gpa: std.mem.Allocator, slot: IcnsSlot, rgba: []const u8) Error![]u8 {
|
|
1003
|
+
return switch (slot.payload) {
|
|
1004
|
+
.png => encodePng(gpa, rgba, slot.size, slot.size),
|
|
1005
|
+
.argb => encodeArgb(gpa, rgba, slot.size, slot.size),
|
|
1006
|
+
};
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
// ---------------------------------------------------------------------------
|
|
1010
|
+
// .icns container (a sequence of typed members)
|
|
1011
|
+
// ---------------------------------------------------------------------------
|
|
1012
|
+
|
|
1013
|
+
pub const IcnsMember = struct {
|
|
1014
|
+
kind: [4]u8,
|
|
1015
|
+
data: []const u8,
|
|
1016
|
+
};
|
|
1017
|
+
|
|
1018
|
+
/// Assemble an `.icns` file: the 8-byte header, a TOC member, then each
|
|
1019
|
+
/// member as type + big-endian total length + payload.
|
|
1020
|
+
pub fn writeIcns(gpa: std.mem.Allocator, members: []const IcnsMember) error{OutOfMemory}![]u8 {
|
|
1021
|
+
var total: usize = 8; // file header
|
|
1022
|
+
total += 8 + members.len * 8; // TOC
|
|
1023
|
+
for (members) |member| total += 8 + member.data.len;
|
|
1024
|
+
|
|
1025
|
+
var out = try gpa.alloc(u8, total);
|
|
1026
|
+
writeIcnsHeader(out[0..8], "icns".*, total);
|
|
1027
|
+
var offset: usize = 8;
|
|
1028
|
+
writeIcnsHeader(out[offset..][0..8], "TOC ".*, 8 + members.len * 8);
|
|
1029
|
+
offset += 8;
|
|
1030
|
+
for (members) |member| {
|
|
1031
|
+
writeIcnsHeader(out[offset..][0..8], member.kind, 8 + member.data.len);
|
|
1032
|
+
offset += 8;
|
|
1033
|
+
}
|
|
1034
|
+
for (members) |member| {
|
|
1035
|
+
writeIcnsHeader(out[offset..][0..8], member.kind, 8 + member.data.len);
|
|
1036
|
+
offset += 8;
|
|
1037
|
+
@memcpy(out[offset..][0..member.data.len], member.data);
|
|
1038
|
+
offset += member.data.len;
|
|
1039
|
+
}
|
|
1040
|
+
return out;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
fn writeIcnsHeader(dest: *[8]u8, kind: [4]u8, length: usize) void {
|
|
1044
|
+
dest[0..4].* = kind;
|
|
1045
|
+
std.mem.writeInt(u32, dest[4..8], @intCast(length), .big);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/// Iterator over the members of an `.icns` file (TOC members skipped),
|
|
1049
|
+
/// for tests and round-trip validation of our own output.
|
|
1050
|
+
pub const IcnsIterator = struct {
|
|
1051
|
+
bytes: []const u8,
|
|
1052
|
+
offset: usize,
|
|
1053
|
+
|
|
1054
|
+
pub fn init(bytes: []const u8) ?IcnsIterator {
|
|
1055
|
+
if (bytes.len < 8) return null;
|
|
1056
|
+
if (!std.mem.eql(u8, bytes[0..4], "icns")) return null;
|
|
1057
|
+
const total = std.mem.readInt(u32, bytes[4..8], .big);
|
|
1058
|
+
if (total != bytes.len) return null;
|
|
1059
|
+
return .{ .bytes = bytes, .offset = 8 };
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
pub fn next(self: *IcnsIterator) ?IcnsMember {
|
|
1063
|
+
while (self.offset + 8 <= self.bytes.len) {
|
|
1064
|
+
const kind: [4]u8 = self.bytes[self.offset..][0..4].*;
|
|
1065
|
+
const length = std.mem.readInt(u32, self.bytes[self.offset + 4 ..][0..4], .big);
|
|
1066
|
+
if (length < 8 or self.offset + length > self.bytes.len) return null;
|
|
1067
|
+
const data = self.bytes[self.offset + 8 .. self.offset + length];
|
|
1068
|
+
self.offset += length;
|
|
1069
|
+
if (std.mem.eql(u8, &kind, "TOC ")) continue;
|
|
1070
|
+
return .{ .kind = kind, .data = data };
|
|
1071
|
+
}
|
|
1072
|
+
return null;
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1075
|
+
|
|
1076
|
+
// ---------------------------------------------------------------------------
|
|
1077
|
+
// .ico container (a directory of PNG-compressed entries)
|
|
1078
|
+
// ---------------------------------------------------------------------------
|
|
1079
|
+
|
|
1080
|
+
pub const IcoEntry = struct {
|
|
1081
|
+
size: usize,
|
|
1082
|
+
data: []const u8,
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1085
|
+
/// Assemble an `.ico` file with PNG-compressed entries.
|
|
1086
|
+
pub fn writeIco(gpa: std.mem.Allocator, entries: []const IcoEntry) error{OutOfMemory}![]u8 {
|
|
1087
|
+
var total: usize = 6 + entries.len * 16;
|
|
1088
|
+
for (entries) |entry| total += entry.data.len;
|
|
1089
|
+
var out = try gpa.alloc(u8, total);
|
|
1090
|
+
|
|
1091
|
+
std.mem.writeInt(u16, out[0..2], 0, .little); // reserved
|
|
1092
|
+
std.mem.writeInt(u16, out[2..4], 1, .little); // type: icon
|
|
1093
|
+
std.mem.writeInt(u16, out[4..6], @intCast(entries.len), .little);
|
|
1094
|
+
var directory: usize = 6;
|
|
1095
|
+
var payload: usize = 6 + entries.len * 16;
|
|
1096
|
+
for (entries) |entry| {
|
|
1097
|
+
// A dimension byte of 0 means 256 in the .ico directory.
|
|
1098
|
+
const dimension: u8 = if (entry.size >= 256) 0 else @intCast(entry.size);
|
|
1099
|
+
out[directory + 0] = dimension; // width
|
|
1100
|
+
out[directory + 1] = dimension; // height
|
|
1101
|
+
out[directory + 2] = 0; // palette size
|
|
1102
|
+
out[directory + 3] = 0; // reserved
|
|
1103
|
+
std.mem.writeInt(u16, out[directory + 4 ..][0..2], 1, .little); // planes
|
|
1104
|
+
std.mem.writeInt(u16, out[directory + 6 ..][0..2], 32, .little); // bits per pixel
|
|
1105
|
+
std.mem.writeInt(u32, out[directory + 8 ..][0..4], @intCast(entry.data.len), .little);
|
|
1106
|
+
std.mem.writeInt(u32, out[directory + 12 ..][0..4], @intCast(payload), .little);
|
|
1107
|
+
directory += 16;
|
|
1108
|
+
@memcpy(out[payload..][0..entry.data.len], entry.data);
|
|
1109
|
+
payload += entry.data.len;
|
|
1110
|
+
}
|
|
1111
|
+
return out;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
pub const IcoDirectoryEntry = struct {
|
|
1115
|
+
/// Stored dimension (0 in the file means 256; reported as 256 here).
|
|
1116
|
+
size: usize,
|
|
1117
|
+
data: []const u8,
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
/// Iterator over `.ico` directory entries for tests and validation.
|
|
1121
|
+
pub const IcoIterator = struct {
|
|
1122
|
+
bytes: []const u8,
|
|
1123
|
+
count: usize,
|
|
1124
|
+
index: usize,
|
|
1125
|
+
|
|
1126
|
+
pub fn init(bytes: []const u8) ?IcoIterator {
|
|
1127
|
+
if (bytes.len < 6) return null;
|
|
1128
|
+
if (std.mem.readInt(u16, bytes[0..2], .little) != 0) return null;
|
|
1129
|
+
if (std.mem.readInt(u16, bytes[2..4], .little) != 1) return null;
|
|
1130
|
+
const count = std.mem.readInt(u16, bytes[4..6], .little);
|
|
1131
|
+
if (bytes.len < 6 + @as(usize, count) * 16) return null;
|
|
1132
|
+
return .{ .bytes = bytes, .count = count, .index = 0 };
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
pub fn next(self: *IcoIterator) ?IcoDirectoryEntry {
|
|
1136
|
+
if (self.index >= self.count) return null;
|
|
1137
|
+
const entry = self.bytes[6 + self.index * 16 ..][0..16];
|
|
1138
|
+
self.index += 1;
|
|
1139
|
+
const dimension: usize = if (entry[0] == 0) 256 else entry[0];
|
|
1140
|
+
const length = std.mem.readInt(u32, entry[8..12], .little);
|
|
1141
|
+
const offset = std.mem.readInt(u32, entry[12..16], .little);
|
|
1142
|
+
if (@as(usize, offset) + length > self.bytes.len) return null;
|
|
1143
|
+
return .{ .size = dimension, .data = self.bytes[offset..][0..length] };
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
|
|
1147
|
+
// ---------------------------------------------------------------------------
|
|
1148
|
+
// Top-level artifact builders
|
|
1149
|
+
// ---------------------------------------------------------------------------
|
|
1150
|
+
|
|
1151
|
+
/// Build the complete macOS `.icns` from a loaded source.
|
|
1152
|
+
pub fn buildIcns(gpa: std.mem.Allocator, source: *const Source) Error![]u8 {
|
|
1153
|
+
const renders = try renderMacosFamily(gpa, source, &macos_render_sizes);
|
|
1154
|
+
defer {
|
|
1155
|
+
for (renders) |render| gpa.free(render);
|
|
1156
|
+
gpa.free(renders);
|
|
1157
|
+
}
|
|
1158
|
+
var payloads: [icns_slots.len][]u8 = undefined;
|
|
1159
|
+
var encoded: usize = 0;
|
|
1160
|
+
defer for (payloads[0..encoded]) |bytes| gpa.free(bytes);
|
|
1161
|
+
var members: [icns_slots.len]IcnsMember = undefined;
|
|
1162
|
+
for (icns_slots, 0..) |slot, index| {
|
|
1163
|
+
payloads[index] = try encodeIcnsPayload(gpa, slot, renders[renderSizeIndex(slot.size)]);
|
|
1164
|
+
encoded += 1;
|
|
1165
|
+
members[index] = .{ .kind = slot.kind, .data = payloads[index] };
|
|
1166
|
+
}
|
|
1167
|
+
return writeIcns(gpa, &members);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
fn renderSizeIndex(size: usize) usize {
|
|
1171
|
+
for (macos_render_sizes, 0..) |candidate, index| {
|
|
1172
|
+
if (candidate == size) return index;
|
|
1173
|
+
}
|
|
1174
|
+
unreachable;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
/// Build the multi-size Windows `.ico` from a loaded source (square,
|
|
1178
|
+
/// unmasked).
|
|
1179
|
+
pub fn buildIco(gpa: std.mem.Allocator, source: *const Source) Error![]u8 {
|
|
1180
|
+
var pngs: [ico_sizes.len][]u8 = undefined;
|
|
1181
|
+
var encoded: usize = 0;
|
|
1182
|
+
defer for (pngs[0..encoded]) |bytes| gpa.free(bytes);
|
|
1183
|
+
var entries: [ico_sizes.len]IcoEntry = undefined;
|
|
1184
|
+
for (ico_sizes, 0..) |size, index| {
|
|
1185
|
+
const rgba = try renderSquare(gpa, source, size);
|
|
1186
|
+
defer gpa.free(rgba);
|
|
1187
|
+
pngs[index] = try encodePng(gpa, rgba, size, size);
|
|
1188
|
+
encoded += 1;
|
|
1189
|
+
entries[index] = .{ .size = size, .data = pngs[index] };
|
|
1190
|
+
}
|
|
1191
|
+
return writeIco(gpa, &entries);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/// Encode one square PNG at `size` from a loaded source (Linux hicolor
|
|
1195
|
+
/// sizes, mobile launcher densities, the iOS catalog image).
|
|
1196
|
+
pub fn buildSquarePng(gpa: std.mem.Allocator, source: *const Source, size: usize) Error![]u8 {
|
|
1197
|
+
const rgba = try renderSquare(gpa, source, size);
|
|
1198
|
+
defer gpa.free(rgba);
|
|
1199
|
+
return encodePng(gpa, rgba, size, size);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
// ---------------------------------------------------------------------------
|
|
1203
|
+
// Tests
|
|
1204
|
+
// ---------------------------------------------------------------------------
|
|
1205
|
+
|
|
1206
|
+
const testing = std.testing;
|
|
1207
|
+
|
|
1208
|
+
fn testGradientSource(gpa: std.mem.Allocator, extent: usize) !Source {
|
|
1209
|
+
const pixels = try gpa.alloc(u8, extent * extent * 4);
|
|
1210
|
+
var y: usize = 0;
|
|
1211
|
+
while (y < extent) : (y += 1) {
|
|
1212
|
+
var x: usize = 0;
|
|
1213
|
+
while (x < extent) : (x += 1) {
|
|
1214
|
+
const base = (y * extent + x) * 4;
|
|
1215
|
+
pixels[base + 0] = @truncate((x * 255) / extent);
|
|
1216
|
+
pixels[base + 1] = @truncate((y * 255) / extent);
|
|
1217
|
+
pixels[base + 2] = 96;
|
|
1218
|
+
pixels[base + 3] = 255;
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
return .{ .kind = .png, .width = extent, .height = extent, .image = .{ .width = extent, .height = extent, .pixels = pixels } };
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
test "png encode/decode round-trips rgba pixels" {
|
|
1225
|
+
const gpa = testing.allocator;
|
|
1226
|
+
const extent: usize = 21;
|
|
1227
|
+
var source = try testGradientSource(gpa, extent);
|
|
1228
|
+
defer source.deinit(gpa);
|
|
1229
|
+
const encoded = try encodePng(gpa, source.image.?.pixels, extent, extent);
|
|
1230
|
+
defer gpa.free(encoded);
|
|
1231
|
+
const decoded = try decodePng(gpa, encoded);
|
|
1232
|
+
defer decoded.deinit(gpa);
|
|
1233
|
+
try testing.expectEqual(extent, decoded.width);
|
|
1234
|
+
try testing.expectEqual(extent, decoded.height);
|
|
1235
|
+
try testing.expectEqualSlices(u8, source.image.?.pixels, decoded.pixels);
|
|
1236
|
+
|
|
1237
|
+
const header = pngHeader(encoded) orelse return error.TestUnexpectedResult;
|
|
1238
|
+
try testing.expectEqual(extent, header.width);
|
|
1239
|
+
try testing.expectEqual(extent, header.height);
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
test "png decoder reads the strict writer's stored-block output" {
|
|
1243
|
+
const gpa = testing.allocator;
|
|
1244
|
+
const width: usize = 5;
|
|
1245
|
+
const height: usize = 4;
|
|
1246
|
+
var pixels: [width * height * 4]u8 = undefined;
|
|
1247
|
+
var seed: u8 = 11;
|
|
1248
|
+
for (&pixels) |*byte| {
|
|
1249
|
+
byte.* = seed;
|
|
1250
|
+
seed = seed *% 31 +% 7;
|
|
1251
|
+
}
|
|
1252
|
+
var buffer: [1024]u8 = undefined;
|
|
1253
|
+
var writer = std.Io.Writer.fixed(&buffer);
|
|
1254
|
+
try png_model.writeRgba8(&writer, width, height, &pixels);
|
|
1255
|
+
const decoded = try decodePng(gpa, writer.buffered());
|
|
1256
|
+
defer decoded.deinit(gpa);
|
|
1257
|
+
try testing.expectEqualSlices(u8, &pixels, decoded.pixels);
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
/// Assemble a minimal PNG by hand with an arbitrary IHDR + filtered raw
|
|
1261
|
+
/// stream (stored deflate blocks), to exercise decode paths the two
|
|
1262
|
+
/// encoders never emit.
|
|
1263
|
+
fn testBuildPng(gpa: std.mem.Allocator, width: usize, height: usize, bit_depth: u8, color_type: u8, palette: []const u8, transparency: []const u8, raw: []const u8) ![]u8 {
|
|
1264
|
+
var out: std.ArrayList(u8) = .empty;
|
|
1265
|
+
errdefer out.deinit(gpa);
|
|
1266
|
+
try out.appendSlice(gpa, &png_model.signature);
|
|
1267
|
+
var ihdr: [13]u8 = undefined;
|
|
1268
|
+
std.mem.writeInt(u32, ihdr[0..4], @intCast(width), .big);
|
|
1269
|
+
std.mem.writeInt(u32, ihdr[4..8], @intCast(height), .big);
|
|
1270
|
+
ihdr[8] = bit_depth;
|
|
1271
|
+
ihdr[9] = color_type;
|
|
1272
|
+
ihdr[10] = 0;
|
|
1273
|
+
ihdr[11] = 0;
|
|
1274
|
+
ihdr[12] = 0;
|
|
1275
|
+
try appendPngChunk(&out, gpa, "IHDR", &ihdr);
|
|
1276
|
+
if (palette.len > 0) try appendPngChunk(&out, gpa, "PLTE", palette);
|
|
1277
|
+
if (transparency.len > 0) try appendPngChunk(&out, gpa, "tRNS", transparency);
|
|
1278
|
+
|
|
1279
|
+
// zlib stream of one stored deflate block.
|
|
1280
|
+
var idat: std.ArrayList(u8) = .empty;
|
|
1281
|
+
defer idat.deinit(gpa);
|
|
1282
|
+
try idat.appendSlice(gpa, &.{ 0x78, 0x01 });
|
|
1283
|
+
try idat.append(gpa, 1); // BFINAL, stored
|
|
1284
|
+
var length_bytes: [4]u8 = undefined;
|
|
1285
|
+
std.mem.writeInt(u16, length_bytes[0..2], @intCast(raw.len), .little);
|
|
1286
|
+
std.mem.writeInt(u16, length_bytes[2..4], @intCast(raw.len ^ 0xFFFF), .little);
|
|
1287
|
+
try idat.appendSlice(gpa, length_bytes[0..4]);
|
|
1288
|
+
try idat.appendSlice(gpa, raw);
|
|
1289
|
+
var adler_bytes: [4]u8 = undefined;
|
|
1290
|
+
std.mem.writeInt(u32, &adler_bytes, std.hash.Adler32.hash(raw), .big);
|
|
1291
|
+
try idat.appendSlice(gpa, &adler_bytes);
|
|
1292
|
+
try appendPngChunk(&out, gpa, "IDAT", idat.items);
|
|
1293
|
+
try appendPngChunk(&out, gpa, "IEND", &.{});
|
|
1294
|
+
return out.toOwnedSlice(gpa);
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
test "png decoder undoes sub, average, and paeth filters" {
|
|
1298
|
+
const gpa = testing.allocator;
|
|
1299
|
+
// 2x2 RGB, rows filtered with Sub then Paeth.
|
|
1300
|
+
// Row 0 pixels: (10,20,30) (14,25,37) -> Sub deltas (10,20,30) (4,5,7).
|
|
1301
|
+
// Row 1 pixels: (12,22,32) (16,27,39); Paeth predictor for the first
|
|
1302
|
+
// pixel is up (10,20,30) -> deltas (2,2,2); for the second, left/up/
|
|
1303
|
+
// up-left = (12,22,32)/(14,25,37)/(10,20,30) -> estimates pick up.
|
|
1304
|
+
const raw = [_]u8{
|
|
1305
|
+
1, 10, 20, 30, 4, 5, 7,
|
|
1306
|
+
4, 2, 2, 2, 2, 2, 2,
|
|
1307
|
+
};
|
|
1308
|
+
const encoded = try testBuildPng(gpa, 2, 2, 8, 2, &.{}, &.{}, &raw);
|
|
1309
|
+
defer gpa.free(encoded);
|
|
1310
|
+
const decoded = try decodePng(gpa, encoded);
|
|
1311
|
+
defer decoded.deinit(gpa);
|
|
1312
|
+
try testing.expectEqualSlices(u8, &.{ 10, 20, 30, 255, 14, 25, 37, 255 }, decoded.pixels[0..8]);
|
|
1313
|
+
try testing.expectEqual(@as(u8, 12), decoded.pixels[8]);
|
|
1314
|
+
try testing.expectEqual(@as(u8, 22), decoded.pixels[9]);
|
|
1315
|
+
try testing.expectEqual(@as(u8, 32), decoded.pixels[10]);
|
|
1316
|
+
try testing.expectEqual(@as(u8, 16), decoded.pixels[12]);
|
|
1317
|
+
|
|
1318
|
+
// Average filter, 1x2 grayscale: second row averages left=0, up=64.
|
|
1319
|
+
const gray_raw = [_]u8{ 0, 64, 3, 32 };
|
|
1320
|
+
const gray = try testBuildPng(gpa, 1, 2, 8, 0, &.{}, &.{}, &gray_raw);
|
|
1321
|
+
defer gpa.free(gray);
|
|
1322
|
+
const gray_decoded = try decodePng(gpa, gray);
|
|
1323
|
+
defer gray_decoded.deinit(gpa);
|
|
1324
|
+
try testing.expectEqual(@as(u8, 64), gray_decoded.pixels[0]);
|
|
1325
|
+
try testing.expectEqual(@as(u8, 64), gray_decoded.pixels[4]); // 32 + (0+64)/2
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
test "png decoder expands palette entries with transparency" {
|
|
1329
|
+
const gpa = testing.allocator;
|
|
1330
|
+
const palette = [_]u8{ 255, 0, 0, 0, 255, 0 };
|
|
1331
|
+
const transparency = [_]u8{ 128 }; // entry 0 half-transparent, entry 1 opaque
|
|
1332
|
+
const raw = [_]u8{ 0, 0, 1 }; // one row: filter 0, indexes 0 and 1
|
|
1333
|
+
const encoded = try testBuildPng(gpa, 2, 1, 8, 3, &palette, &transparency, &raw);
|
|
1334
|
+
defer gpa.free(encoded);
|
|
1335
|
+
const decoded = try decodePng(gpa, encoded);
|
|
1336
|
+
defer decoded.deinit(gpa);
|
|
1337
|
+
try testing.expectEqualSlices(u8, &.{ 255, 0, 0, 128, 0, 255, 0, 255 }, decoded.pixels);
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
test "png decoder rejects unsupported forms" {
|
|
1341
|
+
const gpa = testing.allocator;
|
|
1342
|
+
try testing.expectError(error.UnsupportedImage, decodePng(gpa, "not a png"));
|
|
1343
|
+
// Interlaced flag set.
|
|
1344
|
+
var interlaced = std.ArrayList(u8).empty;
|
|
1345
|
+
defer interlaced.deinit(gpa);
|
|
1346
|
+
try interlaced.appendSlice(gpa, &png_model.signature);
|
|
1347
|
+
var ihdr: [13]u8 = .{ 0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 1 };
|
|
1348
|
+
try appendPngChunk(&interlaced, gpa, "IHDR", &ihdr);
|
|
1349
|
+
try testing.expectError(error.UnsupportedImage, decodePng(gpa, interlaced.items));
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
test "load source reports non-square dimensions" {
|
|
1353
|
+
const gpa = testing.allocator;
|
|
1354
|
+
const pixels = try gpa.alloc(u8, 4 * 2 * 4);
|
|
1355
|
+
defer gpa.free(pixels);
|
|
1356
|
+
@memset(pixels, 200);
|
|
1357
|
+
const encoded = try encodePng(gpa, pixels, 4, 2);
|
|
1358
|
+
defer gpa.free(encoded);
|
|
1359
|
+
const result = try loadSource(gpa, encoded, .png);
|
|
1360
|
+
switch (result) {
|
|
1361
|
+
.ok => return error.TestUnexpectedResult,
|
|
1362
|
+
.issue => |issue| {
|
|
1363
|
+
try testing.expectEqual(@as(usize, 4), issue.not_square.width);
|
|
1364
|
+
try testing.expectEqual(@as(usize, 2), issue.not_square.height);
|
|
1365
|
+
},
|
|
1366
|
+
}
|
|
1367
|
+
var message_buffer: [512]u8 = undefined;
|
|
1368
|
+
const message = formatNotSquareMessage(&message_buffer, "assets/icon.png", 4, 2);
|
|
1369
|
+
try testing.expect(std.mem.indexOf(u8, message, "4x2") != null);
|
|
1370
|
+
try testing.expect(std.mem.indexOf(u8, message, "square") != null);
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
test "load source rejects undecodable bytes as unsupported" {
|
|
1374
|
+
const gpa = testing.allocator;
|
|
1375
|
+
const png_result = try loadSource(gpa, "definitely not an image", .png);
|
|
1376
|
+
try testing.expect(png_result == .issue and png_result.issue == .unsupported);
|
|
1377
|
+
const svg_result = try loadSource(gpa, "<div>not svg</div>", .svg);
|
|
1378
|
+
try testing.expect(svg_result == .issue and svg_result.issue == .unsupported);
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
test "checkerboard downscales to uniform gray, not moire" {
|
|
1382
|
+
const gpa = testing.allocator;
|
|
1383
|
+
const extent: usize = 256;
|
|
1384
|
+
const pixels = try gpa.alloc(u8, extent * extent * 4);
|
|
1385
|
+
defer gpa.free(pixels);
|
|
1386
|
+
var y: usize = 0;
|
|
1387
|
+
while (y < extent) : (y += 1) {
|
|
1388
|
+
var x: usize = 0;
|
|
1389
|
+
while (x < extent) : (x += 1) {
|
|
1390
|
+
const base = (y * extent + x) * 4;
|
|
1391
|
+
const value: u8 = if ((x / 2 + y / 2) % 2 == 0) 0 else 255;
|
|
1392
|
+
pixels[base + 0] = value;
|
|
1393
|
+
pixels[base + 1] = value;
|
|
1394
|
+
pixels[base + 2] = value;
|
|
1395
|
+
pixels[base + 3] = 255;
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
var source = Source{ .kind = .png, .width = extent, .height = extent, .image = .{ .width = extent, .height = extent, .pixels = pixels } };
|
|
1399
|
+
const small = try renderSquare(gpa, &source, 16);
|
|
1400
|
+
defer gpa.free(small);
|
|
1401
|
+
// Every pixel identical (16 divides the 2px cells evenly), gray, and
|
|
1402
|
+
// near the sRGB encoding of linear 0.5 (~188) — a naive gamma-space
|
|
1403
|
+
// average would land at 127 instead.
|
|
1404
|
+
const first = small[0];
|
|
1405
|
+
try testing.expect(first >= 180 and first <= 195);
|
|
1406
|
+
var index: usize = 0;
|
|
1407
|
+
while (index < 16 * 16) : (index += 1) {
|
|
1408
|
+
try testing.expectEqual(first, small[index * 4 + 0]);
|
|
1409
|
+
try testing.expectEqual(first, small[index * 4 + 1]);
|
|
1410
|
+
try testing.expectEqual(first, small[index * 4 + 2]);
|
|
1411
|
+
try testing.expectEqual(@as(u8, 255), small[index * 4 + 3]);
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
test "macos mask has transparent corners, opaque center, smooth edges" {
|
|
1416
|
+
const gpa = testing.allocator;
|
|
1417
|
+
const size: usize = 128;
|
|
1418
|
+
const mask = try macosMaskCoverage(gpa, size);
|
|
1419
|
+
defer gpa.free(mask);
|
|
1420
|
+
// Canvas corners sit in the margin: zero coverage.
|
|
1421
|
+
try testing.expectEqual(@as(f32, 0), mask[0]);
|
|
1422
|
+
try testing.expectEqual(@as(f32, 0), mask[size - 1]);
|
|
1423
|
+
try testing.expectEqual(@as(f32, 0), mask[(size - 1) * size]);
|
|
1424
|
+
try testing.expectEqual(@as(f32, 0), mask[size * size - 1]);
|
|
1425
|
+
// Center fully covered.
|
|
1426
|
+
try testing.expectEqual(@as(f32, 1), mask[(size / 2) * size + size / 2]);
|
|
1427
|
+
// The artwork corner arc produces fractional coverage somewhere: scan
|
|
1428
|
+
// the top-left corner region for a strictly partial pixel.
|
|
1429
|
+
var found_partial = false;
|
|
1430
|
+
var y: usize = 0;
|
|
1431
|
+
while (y < size / 2) : (y += 1) {
|
|
1432
|
+
var x: usize = 0;
|
|
1433
|
+
while (x < size / 2) : (x += 1) {
|
|
1434
|
+
const coverage = mask[y * size + x];
|
|
1435
|
+
if (coverage > 0.05 and coverage < 0.95) found_partial = true;
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
try testing.expect(found_partial);
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
test "full-bleed source is masked and inset on the macos canvas" {
|
|
1442
|
+
const gpa = testing.allocator;
|
|
1443
|
+
var source = try testGradientSource(gpa, 256);
|
|
1444
|
+
defer source.deinit(gpa);
|
|
1445
|
+
try testing.expect(!imageIsPreShaped(source.image.?));
|
|
1446
|
+
const canvas = try renderMacosCanvas(gpa, &source, 64);
|
|
1447
|
+
defer gpa.free(canvas);
|
|
1448
|
+
// Corners transparent (margin + mask), center opaque.
|
|
1449
|
+
try testing.expectEqual(@as(u8, 0), canvas[3]);
|
|
1450
|
+
try testing.expectEqual(@as(u8, 0), canvas[(64 * 64 - 1) * 4 + 3]);
|
|
1451
|
+
try testing.expectEqual(@as(u8, 255), canvas[(32 * 64 + 32) * 4 + 3]);
|
|
1452
|
+
// The margin band itself is empty: a pixel just inside the canvas
|
|
1453
|
+
// edge midline is outside the artwork square.
|
|
1454
|
+
try testing.expectEqual(@as(u8, 0), canvas[(32 * 64 + 0) * 4 + 3]);
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
test "pre-shaped source ships untouched on the macos canvas" {
|
|
1458
|
+
const gpa = testing.allocator;
|
|
1459
|
+
const extent: usize = 128;
|
|
1460
|
+
const pixels = try gpa.alloc(u8, extent * extent * 4);
|
|
1461
|
+
var y: usize = 0;
|
|
1462
|
+
while (y < extent) : (y += 1) {
|
|
1463
|
+
var x: usize = 0;
|
|
1464
|
+
while (x < extent) : (x += 1) {
|
|
1465
|
+
const base = (y * extent + x) * 4;
|
|
1466
|
+
// A centered opaque disc: corners transparent -> pre-shaped.
|
|
1467
|
+
const dx = @as(f32, @floatFromInt(x)) - 63.5;
|
|
1468
|
+
const dy = @as(f32, @floatFromInt(y)) - 63.5;
|
|
1469
|
+
const inside = dx * dx + dy * dy <= 60.0 * 60.0;
|
|
1470
|
+
pixels[base + 0] = 255;
|
|
1471
|
+
pixels[base + 1] = 0;
|
|
1472
|
+
pixels[base + 2] = 0;
|
|
1473
|
+
pixels[base + 3] = if (inside) 255 else 0;
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
var source = Source{ .kind = .png, .width = extent, .height = extent, .image = .{ .width = extent, .height = extent, .pixels = pixels } };
|
|
1477
|
+
defer source.deinit(gpa);
|
|
1478
|
+
try testing.expect(imageIsPreShaped(source.image.?));
|
|
1479
|
+
const canvas = try renderMacosCanvas(gpa, &source, 128);
|
|
1480
|
+
defer gpa.free(canvas);
|
|
1481
|
+
// The disc edge reaches x=4 at the midline — inside the 12.5% margin
|
|
1482
|
+
// a full-bleed source would have been inset behind.
|
|
1483
|
+
try testing.expect(canvas[(64 * 128 + 5) * 4 + 3] > 200);
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
test "icns round-trips its own members" {
|
|
1487
|
+
const gpa = testing.allocator;
|
|
1488
|
+
var source = try testGradientSource(gpa, 64);
|
|
1489
|
+
defer source.deinit(gpa);
|
|
1490
|
+
const icns = try buildIcns(gpa, &source);
|
|
1491
|
+
defer gpa.free(icns);
|
|
1492
|
+
|
|
1493
|
+
var iterator = IcnsIterator.init(icns) orelse return error.TestUnexpectedResult;
|
|
1494
|
+
var seen: usize = 0;
|
|
1495
|
+
while (iterator.next()) |member| {
|
|
1496
|
+
const slot = icns_slots[seen];
|
|
1497
|
+
try testing.expectEqualSlices(u8, &slot.kind, &member.kind);
|
|
1498
|
+
switch (slot.payload) {
|
|
1499
|
+
.png => {
|
|
1500
|
+
const header = pngHeader(member.data) orelse return error.TestUnexpectedResult;
|
|
1501
|
+
try testing.expectEqual(slot.size, header.width);
|
|
1502
|
+
try testing.expectEqual(slot.size, header.height);
|
|
1503
|
+
},
|
|
1504
|
+
.argb => {
|
|
1505
|
+
const rgba = try decodeArgb(gpa, member.data, slot.size, slot.size);
|
|
1506
|
+
defer gpa.free(rgba);
|
|
1507
|
+
try testing.expectEqual(slot.size * slot.size * 4, rgba.len);
|
|
1508
|
+
},
|
|
1509
|
+
}
|
|
1510
|
+
seen += 1;
|
|
1511
|
+
}
|
|
1512
|
+
try testing.expectEqual(icns_slots.len, seen);
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
test "argb payload round-trips rgba pixels" {
|
|
1516
|
+
const gpa = testing.allocator;
|
|
1517
|
+
const extent: usize = 16;
|
|
1518
|
+
var source = try testGradientSource(gpa, extent);
|
|
1519
|
+
defer source.deinit(gpa);
|
|
1520
|
+
const payload = try encodeArgb(gpa, source.image.?.pixels, extent, extent);
|
|
1521
|
+
defer gpa.free(payload);
|
|
1522
|
+
try testing.expectEqualSlices(u8, "ARGB", payload[0..4]);
|
|
1523
|
+
const decoded = try decodeArgb(gpa, payload, extent, extent);
|
|
1524
|
+
defer gpa.free(decoded);
|
|
1525
|
+
try testing.expectEqualSlices(u8, source.image.?.pixels, decoded);
|
|
1526
|
+
|
|
1527
|
+
// Long uniform runs compress: a solid square's payload is far
|
|
1528
|
+
// smaller than its raw planes.
|
|
1529
|
+
const solid = try gpa.alloc(u8, 32 * 32 * 4);
|
|
1530
|
+
defer gpa.free(solid);
|
|
1531
|
+
@memset(solid, 77);
|
|
1532
|
+
const solid_payload = try encodeArgb(gpa, solid, 32, 32);
|
|
1533
|
+
defer gpa.free(solid_payload);
|
|
1534
|
+
try testing.expect(solid_payload.len < 32 * 32);
|
|
1535
|
+
const solid_decoded = try decodeArgb(gpa, solid_payload, 32, 32);
|
|
1536
|
+
defer gpa.free(solid_decoded);
|
|
1537
|
+
try testing.expectEqualSlices(u8, solid, solid_decoded);
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
test "ico round-trips its own directory" {
|
|
1541
|
+
const gpa = testing.allocator;
|
|
1542
|
+
var source = try testGradientSource(gpa, 64);
|
|
1543
|
+
defer source.deinit(gpa);
|
|
1544
|
+
const ico = try buildIco(gpa, &source);
|
|
1545
|
+
defer gpa.free(ico);
|
|
1546
|
+
|
|
1547
|
+
var iterator = IcoIterator.init(ico) orelse return error.TestUnexpectedResult;
|
|
1548
|
+
var seen: usize = 0;
|
|
1549
|
+
while (iterator.next()) |entry| {
|
|
1550
|
+
try testing.expectEqual(ico_sizes[seen], entry.size);
|
|
1551
|
+
const header = pngHeader(entry.data) orelse return error.TestUnexpectedResult;
|
|
1552
|
+
try testing.expectEqual(ico_sizes[seen], header.width);
|
|
1553
|
+
seen += 1;
|
|
1554
|
+
}
|
|
1555
|
+
try testing.expectEqual(ico_sizes.len, seen);
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
test "svg source rasterizes per size" {
|
|
1559
|
+
const gpa = testing.allocator;
|
|
1560
|
+
const svg = "<svg viewBox=\"0 0 24 24\"><rect x=\"0\" y=\"0\" width=\"24\" height=\"24\" fill=\"#ff0000\"/></svg>";
|
|
1561
|
+
const result = try loadSource(gpa, svg, .svg);
|
|
1562
|
+
var source = switch (result) {
|
|
1563
|
+
.ok => |value| value,
|
|
1564
|
+
.issue => return error.TestUnexpectedResult,
|
|
1565
|
+
};
|
|
1566
|
+
defer source.deinit(gpa);
|
|
1567
|
+
|
|
1568
|
+
const square = try renderSquare(gpa, &source, 32);
|
|
1569
|
+
defer gpa.free(square);
|
|
1570
|
+
// Full-bleed red: center and corners opaque red.
|
|
1571
|
+
try testing.expectEqual(@as(u8, 255), square[(16 * 32 + 16) * 4 + 0]);
|
|
1572
|
+
try testing.expectEqual(@as(u8, 255), square[(16 * 32 + 16) * 4 + 3]);
|
|
1573
|
+
try testing.expectEqual(@as(u8, 255), square[3]);
|
|
1574
|
+
|
|
1575
|
+
// The macOS canvas masks the full-bleed rect: corners become
|
|
1576
|
+
// transparent, center stays opaque.
|
|
1577
|
+
const canvas = try renderMacosCanvas(gpa, &source, 32);
|
|
1578
|
+
defer gpa.free(canvas);
|
|
1579
|
+
try testing.expectEqual(@as(u8, 0), canvas[3]);
|
|
1580
|
+
try testing.expectEqual(@as(u8, 255), canvas[(16 * 32 + 16) * 4 + 3]);
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
test "source kind classification by extension" {
|
|
1584
|
+
try testing.expectEqual(SourceKind.png, sourceKindForPath("assets/icon.png").?);
|
|
1585
|
+
try testing.expectEqual(SourceKind.png, sourceKindForPath("assets/ICON.PNG").?);
|
|
1586
|
+
try testing.expectEqual(SourceKind.svg, sourceKindForPath("assets/icon.svg").?);
|
|
1587
|
+
try testing.expectEqual(@as(?SourceKind, null), sourceKindForPath("assets/icon.icns"));
|
|
1588
|
+
try testing.expectEqual(@as(?SourceKind, null), sourceKindForPath("assets/icon.ico"));
|
|
1589
|
+
try testing.expectEqual(@as(?SourceKind, null), sourceKindForPath("assets/icon.jpg"));
|
|
1590
|
+
}
|