Skip to content

Commit

Permalink
Add some filters and render view
Browse files Browse the repository at this point in the history
  • Loading branch information
yangKJ committed Mar 29, 2024
1 parent 7b8da87 commit 57f39af
Show file tree
Hide file tree
Showing 25 changed files with 404 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Demo/Harbeth-SwiftUI-Demo/Views/CustomViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct CustomViews<F: C7FilterProtocol>: View {

var body: some View {
VStack {
FilterableView(image: inputImage, filters: [filtering(value)], content: { image in
HarbethView(image: inputImage, filters: [filtering(value)], content: { image in
image.resizable()
.aspectRatio(contentMode: .fit)
.frame(idealHeight: R.width-30 / 2 * 3)
Expand Down
30 changes: 16 additions & 14 deletions Demo/Harbeth-iOS-Demo/Modules/UnitTestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ class UnitTestViewController: UIViewController {

let originImage = R.image("AX")

lazy var ImageView: UIImageView = {
let imageView = UIImageView(image: originImage)
lazy var renderView: RenderView = {
let view = RenderView.init(image: originImage)
view.keepAroundForSynchronousRender = false
//imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.borderColor = UIColor.background2?.cgColor
imageView.layer.borderWidth = 0.5
return imageView
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.borderColor = UIColor.background2?.cgColor
view.layer.borderWidth = 0.5
return view
}()

lazy var leftBarButton: UIBarButtonItem = {
Expand Down Expand Up @@ -51,13 +52,13 @@ class UnitTestViewController: UIViewController {
title = "Unit"
navigationItem.rightBarButtonItem = leftBarButton
view.backgroundColor = UIColor.background
view.addSubview(ImageView)
view.addSubview(renderView)
NSLayoutConstraint.activate([
ImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -20),
ImageView.heightAnchor.constraint(equalTo: ImageView.widthAnchor, multiplier: 1.0),
//ImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100),
ImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15),
ImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -15),
renderView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -20),
renderView.heightAnchor.constraint(equalTo: renderView.widthAnchor, multiplier: 1.0),
//renderView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -100),
renderView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 15),
renderView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -15),
])
}

Expand All @@ -78,7 +79,8 @@ extension UnitTestViewController {
func unitTest() {
let filter = C7Storyboard(ranks: 2)

let dest = BoxxIO.init(element: originImage, filters: [filter])
ImageView.image = try? dest.output()
//let dest = BoxxIO.init(element: originImage, filters: [filter])
//renderView.image = try? dest.output()
renderView.filters = [filter]
}
}
4 changes: 2 additions & 2 deletions Demo/Harbeth-macOS-Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="22505" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22505"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down
2 changes: 1 addition & 1 deletion Harbeth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Harbeth'
s.version = '1.1.2'
s.version = '1.1.3'
s.summary = 'About image and video add filter for metal.'

# This description is used to generate tags and improve search results.
Expand Down
20 changes: 20 additions & 0 deletions Harbeth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
875EEBAE2994E0C300EC62B3 /* MetalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 875EEBAD2994E0C300EC62B3 /* MetalKit.framework */; };
875EEBB02994E0D800EC62B3 /* CoreImage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 875EEBAF2994E0D700EC62B3 /* CoreImage.framework */; };
875EEBB22994E0DF00EC62B3 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 875EEBB12994E0DF00EC62B3 /* ImageIO.framework */; };
87660ADA2BB25A500065717A /* Renderable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87660AD92BB25A500065717A /* Renderable.swift */; };
87671B7E2A8DAB7800C0A5F0 /* Files.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87671B7D2A8DAB7800C0A5F0 /* Files.swift */; };
87671B812A8E01B000C0A5F0 /* C7CircleBlur.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87671B802A8E01B000C0A5F0 /* C7CircleBlur.swift */; };
87671B832A8E01B800C0A5F0 /* C7CircleBlur.metal in Sources */ = {isa = PBXBuildFile; fileRef = 87671B822A8E01B800C0A5F0 /* C7CircleBlur.metal */; };
Expand All @@ -257,12 +258,16 @@
87901EF12A726588008FCB47 /* C7ColorGradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87901EF02A726588008FCB47 /* C7ColorGradient.swift */; };
87901EF32A7265A1008FCB47 /* C7ColorGradient.metal in Sources */ = {isa = PBXBuildFile; fileRef = 87901EF22A7265A1008FCB47 /* C7ColorGradient.metal */; };
8795CE1A2B96C4C50094BE4F /* CIColorMonochrome.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8795CE192B96C4C50094BE4F /* CIColorMonochrome.swift */; };
87A249E92BAA75B900AF7D1A /* HugeImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87A249E82BAA75B900AF7D1A /* HugeImage.swift */; };
87C72F8D29AF3F0900C4C2E6 /* README_CN.md in Resources */ = {isa = PBXBuildFile; fileRef = 87C72F8B29AF3F0900C4C2E6 /* README_CN.md */; };
87C72F8E29AF3F0900C4C2E6 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 87C72F8C29AF3F0900C4C2E6 /* README.md */; };
87C72F9029AF3F1800C4C2E6 /* Harbeth.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 87C72F8F29AF3F1800C4C2E6 /* Harbeth.podspec */; };
87C874A12A7BAA2A00EC8093 /* MPSHistogram.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87C874A02A7BAA2A00EC8093 /* MPSHistogram.swift */; };
87CFF2652A81CD1700330A0B /* C7CombinationBeautiful.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87CFF2642A81CD1700330A0B /* C7CombinationBeautiful.swift */; };
87CFF2682A81D2B100330A0B /* C7CombinationBeautiful.metal in Sources */ = {isa = PBXBuildFile; fileRef = 87CFF2672A81D2B100330A0B /* C7CombinationBeautiful.metal */; };
87DBC3102BB6980900B55909 /* CIPhotoEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DBC30F2BB6980900B55909 /* CIPhotoEffect.swift */; };
87DC84722BB10D2A0071D816 /* CINoiseReduction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DC84712BB10D2A0071D816 /* CINoiseReduction.swift */; };
87DC84742BB110E40071D816 /* RenderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DC84732BB110E40071D816 /* RenderView.swift */; };
87F038C92A8322E700F67638 /* MTLSize+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87F038C82A8322E700F67638 /* MTLSize+Ext.swift */; };
87F038CB2A833C8400F67638 /* C7CombinationBilateralBlur.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87F038CA2A833C8400F67638 /* C7CombinationBilateralBlur.swift */; };
87F038D02A835E7D00F67638 /* C7BilateralBlur.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87F038CE2A835E7D00F67638 /* C7BilateralBlur.swift */; };
Expand Down Expand Up @@ -524,6 +529,7 @@
875EEBAD2994E0C300EC62B3 /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/iOSSupport/System/Library/Frameworks/MetalKit.framework; sourceTree = DEVELOPER_DIR; };
875EEBAF2994E0D700EC62B3 /* CoreImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreImage.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/CoreImage.framework; sourceTree = DEVELOPER_DIR; };
875EEBB12994E0DF00EC62B3 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/ImageIO.framework; sourceTree = DEVELOPER_DIR; };
87660AD92BB25A500065717A /* Renderable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Renderable.swift; sourceTree = "<group>"; };
87671B7D2A8DAB7800C0A5F0 /* Files.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Files.swift; sourceTree = "<group>"; };
87671B802A8E01B000C0A5F0 /* C7CircleBlur.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = C7CircleBlur.swift; sourceTree = "<group>"; };
87671B822A8E01B800C0A5F0 /* C7CircleBlur.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = C7CircleBlur.metal; sourceTree = "<group>"; };
Expand All @@ -536,12 +542,16 @@
87901EF02A726588008FCB47 /* C7ColorGradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = C7ColorGradient.swift; sourceTree = "<group>"; };
87901EF22A7265A1008FCB47 /* C7ColorGradient.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = C7ColorGradient.metal; sourceTree = "<group>"; };
8795CE192B96C4C50094BE4F /* CIColorMonochrome.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CIColorMonochrome.swift; sourceTree = "<group>"; };
87A249E82BAA75B900AF7D1A /* HugeImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HugeImage.swift; sourceTree = "<group>"; };
87C72F8B29AF3F0900C4C2E6 /* README_CN.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README_CN.md; sourceTree = "<group>"; };
87C72F8C29AF3F0900C4C2E6 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
87C72F8F29AF3F1800C4C2E6 /* Harbeth.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Harbeth.podspec; sourceTree = "<group>"; };
87C874A02A7BAA2A00EC8093 /* MPSHistogram.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPSHistogram.swift; sourceTree = "<group>"; };
87CFF2642A81CD1700330A0B /* C7CombinationBeautiful.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = C7CombinationBeautiful.swift; sourceTree = "<group>"; };
87CFF2672A81D2B100330A0B /* C7CombinationBeautiful.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = C7CombinationBeautiful.metal; sourceTree = "<group>"; };
87DBC30F2BB6980900B55909 /* CIPhotoEffect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CIPhotoEffect.swift; sourceTree = "<group>"; };
87DC84712BB10D2A0071D816 /* CINoiseReduction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CINoiseReduction.swift; sourceTree = "<group>"; };
87DC84732BB110E40071D816 /* RenderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RenderView.swift; sourceTree = "<group>"; };
87F038C82A8322E700F67638 /* MTLSize+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MTLSize+Ext.swift"; sourceTree = "<group>"; };
87F038CA2A833C8400F67638 /* C7CombinationBilateralBlur.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = C7CombinationBilateralBlur.swift; sourceTree = "<group>"; };
87F038CE2A835E7D00F67638 /* C7BilateralBlur.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = C7BilateralBlur.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -689,6 +699,8 @@
F7621A922A7FA45400614542 /* CIGaussianBlur.swift */,
87265FBA2994D82100E612C7 /* CIHighlight.swift */,
F7621AAA2A7FE13800614542 /* CILookupTable.swift */,
87DC84712BB10D2A0071D816 /* CINoiseReduction.swift */,
87DBC30F2BB6980900B55909 /* CIPhotoEffect.swift */,
F7621A962A7FABAD00614542 /* CISaturation.swift */,
F7621A982A7FABF300614542 /* CIShadows.swift */,
F7621A9A2A7FAC7000614542 /* CISharpen.swift */,
Expand Down Expand Up @@ -784,8 +796,11 @@
children = (
87265FF82994D82100E612C7 /* BoxxIO.swift */,
87265FF52994D82100E612C7 /* Destype.swift */,
87A249E82BAA75B900AF7D1A /* HugeImage.swift */,
87265FC62994D82100E612C7 /* Operators.swift */,
87265FF62994D82100E612C7 /* Outputable.swift */,
87660AD92BB25A500065717A /* Renderable.swift */,
87DC84732BB110E40071D816 /* RenderView.swift */,
);
path = Outputs;
sourceTree = "<group>";
Expand Down Expand Up @@ -1237,6 +1252,8 @@
872661122994D82200E612C7 /* C7Sharpen.metal in Sources */,
872660C72994D82200E612C7 /* Device.swift in Sources */,
872661612994D82200E612C7 /* C7Posterize.swift in Sources */,
87DC84722BB10D2A0071D816 /* CINoiseReduction.swift in Sources */,
87DC84742BB110E40071D816 /* RenderView.swift in Sources */,
872660D72994D82200E612C7 /* Matrix4x5.swift in Sources */,
87671B812A8E01B000C0A5F0 /* C7CircleBlur.swift in Sources */,
8726615E2994D82200E612C7 /* C7Luminance.metal in Sources */,
Expand Down Expand Up @@ -1368,8 +1385,10 @@
872660C92994D82200E612C7 /* Matrix3x3.swift in Sources */,
872660D22994D82200E612C7 /* Size.swift in Sources */,
872661192994D82200E612C7 /* C7Sobel.swift in Sources */,
87DBC3102BB6980900B55909 /* CIPhotoEffect.swift in Sources */,
872660E02994D82200E612C7 /* Matrix4x4.swift in Sources */,
872661942994D82200E612C7 /* C7RedMonochromeBlur.metal in Sources */,
87660ADA2BB25A500065717A /* Renderable.swift in Sources */,
872661642994D82200E612C7 /* C7Opacity.metal in Sources */,
872661572994D82200E612C7 /* C7LuminanceThreshold.swift in Sources */,
872661832994D82200E612C7 /* C7BlendLinearBurn.metal in Sources */,
Expand All @@ -1385,6 +1404,7 @@
8726619B2994D82200E612C7 /* C7ShiftGlitch.metal in Sources */,
872660CC2994D82200E612C7 /* Error.swift in Sources */,
872661682994D82200E612C7 /* C7Haze.swift in Sources */,
87A249E92BAA75B900AF7D1A /* HugeImage.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ dest.transmitOutput(success: { [weak self] image in
})
```

### ImageView

- Just use the [RenderView](https://github.com/yangKJ/Harbeth/blob/master/Sources/Basic/Outputs/RenderView.swift) and set the filters.

```
// It's used the same way as NSImageView.
let renderView = RenderView.init(image: originImage)
renderView.filters = [C7Storyboard(ranks: 2)]
```

### Camera

- 📸 Camera capture generates pictures.
Expand Down
6 changes: 3 additions & 3 deletions Sources/Basic/Core/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class Device: Cacheable {
/// Transform using color space
lazy var colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
/// We are likely to encounter images with wider colour than sRGB
lazy var workingColorSpace: CGColorSpace? = CGColorSpace(name: CGColorSpace.extendedLinearSRGB)
lazy var workingColorSpace = CGColorSpace(name: CGColorSpace.extendedLinearSRGB)
/// CIContexts
lazy var contexts = [CGColorSpace: CIContext]()

Expand All @@ -50,7 +50,7 @@ public final class Device: Cacheable {
self.harbethLibrary = Device.makeFrameworkLibrary(device, for: "Harbeth")

if defaultLibrary == nil && harbethLibrary == nil {
C7FailedErrorInDebug("Could not load library")
HarbethError.failed("Could not load library")
}
}

Expand All @@ -61,7 +61,7 @@ public final class Device: Cacheable {

extension Device {

static func makeFrameworkLibrary(_ device: MTLDevice, for resource: String) -> MTLLibrary? {
public static func makeFrameworkLibrary(_ device: MTLDevice, for resource: String) -> MTLLibrary? {
#if SWIFT_PACKAGE
/// Fixed the Swift PM cannot read the `.metal` file.
/// https://stackoverflow.com/questions/63237395/generating-resource-bundle-accessor-type-bundle-has-no-member-module
Expand Down
6 changes: 6 additions & 0 deletions Sources/Basic/Core/Filtering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public protocol CoreImageProtocol: C7FilterProtocol {
func coreImageApply(filter: CIFilter, input ciImage: CIImage) throws -> CIImage
}

extension CoreImageProtocol {
public func coreImageApply(filter: CIFilter, input ciImage: CIImage) throws -> CIImage {
return ciImage
}
}

// MARK: - render filter protocol
public protocol RenderProtocol: C7FilterProtocol {
/// Setup the vertex shader parameters.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Basic/Core/Rendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal struct Rendering {
renderPass.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.65, 0.8, 1)

guard let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPass) else {
C7FailedErrorInDebug("Could not create render encoder")
HarbethError.failed("Could not create render encoder")
return
}
let device = Device.device()
Expand Down
5 changes: 2 additions & 3 deletions Sources/Basic/Extensions/C7Image+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,10 @@ extension HarbethWrapper where Base: C7Image {
#if os(macOS)
return base
#else
let imageOrientation = base.imageOrientation
guard imageOrientation != .up, let cgImage = base.cgImage else {
guard base.imageOrientation != .up, let cgImage = base.cgImage else {
return base
}
return cgImage.c7.fixOrientation(from: imageOrientation).c7.drawing(refImage: base)
return cgImage.c7.drawing(refImage: base)
#endif
}
}
Expand Down
16 changes: 15 additions & 1 deletion Sources/Basic/Extensions/C7Image+macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@ import AppKit
extension C7Image {

public convenience init(cgImage: CGImage) {
//self.init(cgImage: cgImage, scale: 1.0, orientation: .up)
self.init(cgImage: cgImage, size: .zero)
}

public convenience init(cgImage: CGImage, scale: CGFloat, orientation: C7ImageOrientation) {
let cgImage: CGImage = {
orientation != .up ? cgImage.c7.fixOrientation(from: orientation) : cgImage
}()
let imageRep = NSBitmapImageRep(cgImage: cgImage)
let scale = max(1.0, scale)
let width = CGFloat(imageRep.pixelsWide) / scale
let height = CGFloat(imageRep.pixelsHigh) / scale
self.init(cgImage: cgImage, size: .init(width: width, height: height))
self.addRepresentation(imageRep)
}

public var cgImage: CGImage? {
return self.cgImage(forProposedRect: nil, context: nil, hints: nil)
var rect = NSRect(origin: .zero, size: self.size)
return self.cgImage(forProposedRect: &rect, context: nil, hints: nil)
}

public var scale: CGFloat {
Expand Down
Loading

0 comments on commit 57f39af

Please sign in to comment.