Code Monkey home page Code Monkey logo

harbeth's Introduction

Harbeth

x

Carthage compatible CocoaPods Compatible CocoaPods Compatible Platform

Harbeth is a tiny set of utils and extensions over Apple's Metal framework dedicated to make your Swift GPU code much cleaner and let you prototype your pipelines faster.

While this library doesn't introduce any new paradigms or concepts that significantly change the way you approach your Metal implementations, it has some optional-to-use things that you can incorporate in your apps if you find them as useful as library author did :)

Graphics processing And Filter production.๐Ÿ‘’๐Ÿ‘’๐Ÿ‘’


Features

๐ŸŸฃ At the moment, the most important features of Metal Moudle can be summarized as follows:

  • Blend: This module mainly contains image blend filters.
  • Blur: Blur effect
  • ColorProcess: basic pixel processing of images.
  • Effect: Effect processing.
  • Lookup: Lookup table filter.
  • Matrix: Matrix convolution filter.
  • Shape: Image shape size related.

A total of 90+ kinds of filters are currently available.

Overview

  • Core, basic core board

    • C7FilterProtocol: Filter designs must follow this protocol.
      • modifier: Encoder type and corresponding function name.
      • factors: Set modify parameter factor, you need to convert to Float.
      • otherInputTextures: Multiple input source extensions, An array containing the MTLTexture
      • outputSize: Change the size of the output image.
  • Outputs, output section

    • C7FilterSerializer: Output content protocol, all outputs must implement this protocol.
      • makeMTLTexture: Create a new texture based on the filter content, Please note that the order in which filters are added may affect the result of image generation.
      • makeImage: Generate data based on filter processing.
      • makeGroup: Multiple filter combinations, Please note that the order in which filters are added may affect the result of image generation.
    • C7FilterImage: Image input source based on C7FilterSerializer, The following modes support only the encoder based on parallel computing.

Usages

  • For example, how to design an soul filter.๐ŸŽท


  1. Accomplish C7FilterProtocal

    public struct C7SoulOut: C7FilterProtocol {
        public var soul: Float = 0.5
        public var maxScale: Float = 1.5
        public var maxAlpha: Float = 0.5
        
        public var modifier: Modifier {
            return .compute(kernel: "C7SoulOut")
        }
        
        public var factors: [Float] {
            return [soul, maxScale, maxAlpha]
        }
        
        public init() { }
    }
  2. Configure additional required textures.

  3. Configure the passed parameter factor, only supports Float type.

    • This filter requires three parameters:
      • soul: The adjusted soul, from 0.0 to 1.0, with a default of 0.5
      • maxScale: Maximum soul scale
      • maxAlpha: The transparency of the max soul
  4. Write a kernel function shader based on parallel computing.

    kernel void C7SoulOut(texture2d<half, access::write> outputTexture [[texture(0)]],
                          texture2d<half, access::sample> inputTexture [[texture(1)]],
                          constant float *soulPointer [[buffer(0)]],
                          constant float *maxScalePointer [[buffer(1)]],
                          constant float *maxAlphaPointer [[buffer(2)]],
                          uint2 grid [[thread_position_in_grid]]) {
        constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
        const half4 inColor = inputTexture.read(grid);
        const float x = float(grid.x) / outputTexture.get_width();
        const float y = float(grid.y) / outputTexture.get_height();
        
        const half soul = half(*soulPointer);
        const half maxScale = half(*maxScalePointer);
        const half maxAlpha = half(*maxAlphaPointer);
        
        const half alpha = maxAlpha * (1.0h - soul);
        const half scale = 1.0h + (maxScale - 1.0h) * soul;
        
        const half soulX = 0.5h + (x - 0.5h) / scale;
        const half soulY = 0.5h + (y - 0.5h) / scale;
        
        const half4 soulMask = inputTexture.sample(quadSampler, float2(soulX, soulY));
        const half4 outColor = inColor * (1.0h - alpha) + soulMask * alpha;
        
        outputTexture.write(outColor, grid);
    }
  5. Simple to use, since my design is based on a parallel computing pipeline, images can be generated directly.

    var filter = C7SoulOut()
    filter.soul = 0.5
    filter.maxScale = 2.0
    
    /// Display directly in ImageView
    ImageView.image = originImage.makeImage(filter: filter)
  6. As for the animation above, it is also very simple, add a timer, and then change the value of soul and you are done, simple.


CocoaPods

  • If you want to import Metal module, you need in your Podfile:
pod 'Harbeth'
  • If you want to import OpenCV image module, you need in your Podfile:
pod 'OpencvQueen'

Remarks

The general process is almost like this, the Demo is also written in great detail, you can check it out for yourself.๐ŸŽท

HarbethDemo

Tip: If you find it helpful, please help me with a star. If you have any questions or needs, you can also issue.

Thanks.๐ŸŽ‡

About the author


License

Harbeth is available under the MIT license. See the LICENSE file for more info.


harbeth's People

Contributors

safina008 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.