Harbeth Documentation

repository·master·Indexed 20 days ago

https://github.com/yangkj/harbeth

A high-performance Swift library for GPU-accelerated image and video processing on Apple platforms (iOS, macOS, tvOS, and watchOS). Built on Metal, CoreImage, and Metal Performance Shaders (MPS), it provides over 200 built-in filters, custom Metal shader support, and LUT-based filtering. It features HarbethIO for batch processing, HarbethView for SwiftUI integration, and optimized memory access patterns for Apple Silicon and Intel architectures.

Tokens
11.8K
Snippets
17
Records
41
Agent score
22%

What's inside Harbeth

  1. Overview of Harbeth

    master

    Harbeth is a high-performance Swift library designed for GPU-accelerated real-time image processing, camera capture, and video processing on Apple platforms (iOS, macOS, tvOS, and watchOS). It serves as a GPU image and frame processing core, built on Metal technology and integrating with CoreImage and Metal Performance Shaders (MPS).

    Core Capabilities:

    • Input Sources: Processes MTLTexture, UIImage, NSImage, CIImage, CGImage, CMSampleBuffer, and CVPixelBuffer.
    • Filter Ecosystem: Includes over 200+ built-in filters (color adjustment, blur, stylization, etc.).
    • Customization: Supports custom Metal shaders and LUT-based filters (1D, 2D, 3D Cube, and Multi Zone Tables).
    • Real-time Performance: Optimized for 60 FPS processing using Metal-powered rendering and automatic texture pooling.
    • Integration: Designed to be embedded into existing pipelines like photo editors, camera previews, or video players. Note that Harbeth provides the processing core, while the host app manages camera sessions, recording, and export logic.
  2. Explore Harbeth Filter Categories

    master

    Harbeth provides a wide array of image and video processing filters categorized by their visual effect. Developers can choose from the following functional groups:

    • Edge & Detail: High-accuracy edge detection (e.g., C7Canny, C7Sobel) and sharpening/clarity enhancements (e.g., C7Clarity, C7SharpenDetail).
    • Distortion & Warp: Geometric and optical distortions (e.g., C7Bulge, C7Swirl, C7WaterRipple, C7Pixellated).
    • Stylization: Artistic effects like oil painting (C7OilPainting), glitch art (C7Glitch, C7ShiftGlitch), and cartooning (C7Toon).
    • Artistic, Sci-fi, Natural, Retro, Camera, and Scene Styles: Pre-set aesthetic filters ranging from C7PastelDream and C7Hologram to film simulations like C7FujiX100V and C7Polaroid.
    • Matrix Processing: Low-level mathematical transformations using color matrices (C7ColorMatrix4x4) or convolution matrices (C7ConvolutionMatrix3x3).
    • Utility: Tonal and color controls (e.g., C7ChromaKey, C7HighlightShadow, C7Levels) and skin smoothing (C7HighPassSkinSmoothing).
    • Geometric Transform: Basic image manipulations like C7Crop, C7Flip, C7Resize, and C7Rotate.
    • Generators & LUTs: Tools for creating new content (C7ColorGradient) or applying professional color grading via Look-Up Tables (C7ColorCube, C7LookupTable).
  3. Core Features of Harbeth

    master

    Harbeth is a high-performance image and video processing framework with the following key capabilities:

    • Cross-Platform Support: Runs on iOS, macOS, tvOS, and watchOS. Supports UIKit, AppKit, and SwiftUI.
    • Multi-Source Data Support: Processes MTLTexture, UIImage, NSImage, CIImage, CGImage, CMSampleBuffer, and CVPixelBuffer.
    • High Performance: Uses Metal-driven rendering and Metal Performance Shaders (MPS) for real-time performance. Includes automatic texture pooling and memory management.
    • Extensive Filter Ecosystem: Over 200+ built-in filters categorized by effect (Color, Blur, Edge, Distortion, Stylization, etc.).
    • Customization: Supports custom filters via LUT (Lookup Tables), .cube files, or custom Metal shaders. Advanced multi-step effects can be created by inheriting from C7CombinationBase.
    • Real-time & Video Processing: Supports real-time camera capture with filters and seamless video processing (local or network) using the integrated Kakapos library.
    • Developer Experience: Provides a Swift-friendly API with linkable filter operations and operator overloading.
  4. Understand the Integration Boundaries of Harbeth

    master

    To use Harbeth effectively, understand the division of responsibilities:

    What Harbeth Provides

    • GPU-accelerated filters and chain processing.
    • Support for texture, image, pixelBuffer, and sampleBuffer processing.
    • LUT pipelines and integration with CoreImage, MPS, and Metal.
    • SwiftUI previews.
    • Demo implementations for camera and video workflows.

    What Your Application Must Provide

    • Complete camera session strategies.
    • Recording user experience and UI.
    • Video timeline editing and playback logic.
    • Export orchestration and media library management.
    • Product-specific business logic.

    Customization & Commercial Support

    If you require private LUTs, branded filter packs, real-time camera/video tuning, or custom Metal kernels, you can use the open-source examples as a technical baseline and discuss specific integration scopes with the maintainer.

  5. Understand Harbeth memory access patterns and optimizations

    master

    Harbeth optimizes GPU performance by adjusting threadgroup sizes based on the specific memory access pattern required by a filter. This ensures better utilization of Apple Silicon and Intel architectures through dynamic threadgroup sizing and memory coalescing.

    Memory Access Patterns

    PatternDescriptionTypical Use CasesApple Silicon SizeIntel Size
    PointAccesses only the current pixelBrightness, Contrast, Saturation, Hue, HSL, Color Balance, Geometric Transforms32x3216x16
    NeighborhoodAccesses surrounding pixelsGaussian Blur, Edge Detection, Morphology, Clarity8x88x8
    Dual TextureAccesses two texturesBlend Modes, Lookup Tables24x2416x16
    Multi TextureAccesses multiple texturesCombination Filters, Blend with Mask16x1616x16

    Optimization Features

    • Dynamic Threadgroup Sizing: Automatically adjusts based on GPU architecture and memory access pattern.
    • Aspect Ratio Optimization: Adapts threadgroup shape to match texture dimensions for better memory coalescing.
    • Architecture-Aware: Uses larger threadgroups on Apple Silicon for better utilization.
    • Memory Coalescing: Optimizes memory access patterns to reduce GPU memory latency.
  6. Install Harbeth via CocoaPods or Carthage

    master

    Harbeth can be integrated into your project using standard dependency managers:

    • CocoaPods: Compatible with Harbeth and Kakapos (for video processing).
    • Carthage: Fully compatible.

    Refer to your preferred dependency manager's documentation to add the Harbeth repository to your project.

  7. Use HarbethView for SwiftUI integration

    master

    HarbethView is a native SwiftUI component that allows you to display filtered images within a SwiftUI view hierarchy. It supports state-driven updates, meaning if you pass a filter parameter tied to a @State variable, the view will automatically re-render with the new effect.

    Best Practices

    • Use @State for filter parameters to enable real-time adjustments.
    • Wrap HarbethView in VStack or ZStack for complex layouts.
    • Apply standard SwiftUI modifiers (like .cornerRadius() or .shadow()) directly to the output of the HarbethView closure.
    import SwiftUI
    import Harbeth
    
    struct FilteredImageView: View {
        @State private var inputImage: UIImage = UIImage(named: "sample")!
        @State private var intensity: Float = 0.5
    
        var body: some View {
            VStack {
                HarbethView(image: inputImage, filters: [
                    CIHighlight(highlight: intensity),
                    C7WaterRipple(ripple: intensity),
                ]) { image in
                    image
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .cornerRadius(12)
                        .shadow(radius: 5)
                }
    
                Slider(value: $intensity, in: 0...1)
                    .padding()
            }
            .padding()
        }
    }
  8. Implement Harbeth on macOS (AppKit/Cocoa)

    master

    Harbeth supports macOS with native AppKit integration. You can use HarbethIO to process NSImage objects within an NSViewController.

    Implementation Pattern

    1. Create a filter chain using types conforming to C7FilterProtocol.
    2. Initialize HarbethIO with the NSImage and the filter array.
    3. Use transmitOutput with a completion handler to update the NSImageView on the main thread.
    import Cocoa
    import Harbeth
    
    class ImageProcessingViewController: NSViewController {
        @IBOutlet weak var imageView: NSImageView!
    
        func applyFilter(to image: NSImage) {
            let filters: [C7FilterProtocol] = [
                C7Brightness(brightness: 0.1),
                C7Contrast(contrast: 1.2),
                C7Saturation(saturation: 1.1)
            ]
    
            let dest = HarbethIO(element: image, filters: filters)
    
            dest.transmitOutput { [weak self] result in
                switch result {
                case .success(let output):
                    DispatchQueue.main.async {
                        self?.imageView.image = output
                    }
                case .failure(let error):
                    print("Filter application failed: \(error)")
                }
            }
        }
    }
  9. Optimize Harbeth performance

    master

    Real-time Processing (e.g., Camera Capture)

    • Set transmitOutputRealTimeCommit = true.
    • Enable enableDoubleBuffer = true to manage memory effectively.

    Memory-Constrained Devices

    • Use Device.setMemoryLimitMB(value) to set a lower memory limit.
    • Ensure enableDoubleBuffer = true is set.
    • Use asynchronous processing (transmitOutput) to avoid memory spikes.

    High-Quality Output

    • Set createDestTexture = true to prevent texture overwriting.
    • Select an appropriate bufferPixelFormat based on requirements.

    Performance Monitoring

    • Enable monitoring with Device.setEnablePerformanceMonitor(true).
    • Retrieve statistics using PerformanceMonitor.shared.getStatistics().
  10. Monitor performance and optimize memory on macOS

    master

    Harbeth is optimized for macOS with AppKit integration and GPU utilization. For high-performance applications, follow these best practices:

    Performance Monitoring

    Enable the monitor on your HarbethIO instance to track statistics.

    let io = HarbethIO(element: image, filters: filters)
    io.enablePerformanceMonitor = true
    
    let result = try? io.output()
    print(PerformanceMonitor.shared.getStatistics())

    Best Practices

    • Large Images: Always use asynchronous processing (transmitOutput) to avoid blocking the main thread.
    • Real-time Preview: Enable transmitOutputRealTimeCommit for smoother interactions.
    • Memory Management: Use Device.setMemoryLimitMB() to set a hard limit on memory usage.
    • Batch Processing: Leverage the internal texture pool in HarbethIO for efficient multiple-image handling.
  11. Install Harbeth via CocoaPods or Swift Package Manager

    master

    You can integrate Harbeth into your project using either CocoaPods or Swift Package Manager (SPM).

    CocoaPods Add the following to your Podfile:

    pod 'Harbeth'

    Swift Package Manager Add the dependency to your Package.swift:

    dependencies: [
        .package(url: "https://github.com/yangKJ/Harbeth.git", branch: "master"),
    ]

    Alternatively, use the Xcode interface: File > Swift Packages > Add Package Dependency.

    pod 'Harbeth'