Skip to content

Instantly share code, notes, and snippets.

View kunofellasleep's full-sized avatar

Kuno Fell Asleep kunofellasleep

View GitHub Profile
func image(from mtlTexture: MTLTexture) -> UIImage {
//画像サイズ
let w = mtlTexture.width
let h = mtlTexture.height
let bytesPerPixel: Int = 4
let imageByteCount = w * h * bytesPerPixel
let bytesPerRow = w * bytesPerPixel
var src = [UInt8](repeating: 0, count: Int(imageByteCount))
//CGImageに変換
let region = MTLRegionMake2D(0, 0, w, h)
import Metal
import MetalKit
class ImageProcessor: NSObject {
//ハードウェアとしてのGPUを抽象化したプロトコル
lazy var device: MTLDevice! = MTLCreateSystemDefaultDevice()
//コマンドバッファの実行順を管理するキュー
var commandQueue: MTLCommandQueue!
kernel void sharpen(
texture2d<float, access::write> outTex [[texture(0)]],
texture2d<float, access::read> inTex [[texture(1)]],
uint2 gid [[thread_position_in_grid]]) {
float4 c = float4(0.0, 0.0, 0.0, 1.0);
//隣接ピクセルを使用して色を決定するためのパラメータ
float weight[3][3] = {
{0, -1, 0},
{-1, 5, -1},
{0, -1, 0}
kernel void chromaticAberration(
texture2d<float, access::write> outTex [[texture(0)]],
texture2d<float, access::read> inTex [[texture(1)]],
uint2 gid [[thread_position_in_grid]]) {
float filterLevel = 3.5;
float4 c = float4(0.0, 0.0, 0.0, 1.0);
//収差に使用するレイヤーの色味
float3 weight[4] = {
float3(0.0f, 0.0f, 0.5f),
float3(0.0f, 0.5f, 0.5f),
kernel void contrast(
texture2d<float, access::write> outTex [[texture(0)]],
texture2d<float, access::read> inTex [[texture(1)]],
uint2 gid [[thread_position_in_grid]]) {
//HIGH: More than 1, LOW: Less than 1
float filterLevel = 1.2;
float4 c = inTex.read(gid).rgba;
c = float4((c.rgb - float3(0.5)) * filterLevel + float3(0.5), 1.0);
outTex.write(c.rgba, gid);
}
kernel void saturation(
texture2d<float, access::write> outTex [[texture(0)]],
texture2d<float, access::read> inTex [[texture(1)]],
uint2 gid [[thread_position_in_grid]]) {
//HIGH: More than 1, LOW: Less than 1
float filterLevel = 1.1;
float4 c = inTex.read(gid).rgba;
float3 hsv = rgb2hsv(c.rgb);
hsv[1] = hsv[1] * filterLevel;
c.rgb = hsv2rgb(hsv);
kernel void blendAdd(
texture2d<float, access::write> outTex [[texture(0)]],
texture2d<float, access::read> inTex [[texture(1)]],
texture2d<float, access::read> inTex2 [[texture(2)]],
uint2 gid [[thread_position_in_grid]]) {
float4 c = inTex.read(gid).rgba;
float4 add = inTex2.read(gid).rgba;
c.rgb += add.rgb;
outTex.write(c.rgba, gid);
}
@kunofellasleep
kunofellasleep / InstaStories.swift
Created February 27, 2019 08:14
Share on Instagram stories
import UIKit
class InstaStories: NSObject {
private let urlScheme = URL(string: "instagram-stories://share")!
enum optionsKey: String {
case StickerImage = "com.instagram.sharedSticker.stickerImage"
case bgImage = "com.instagram.sharedSticker.backgroundImage"
case bgVideo = "com.instagram.sharedSticker.backgroundVideo"
@kunofellasleep
kunofellasleep / keystore gen
Created March 20, 2019 06:55
keystore gen
keytool -genkey -v -keyalg RSA -keystore /Users/kuno/path/is/here/[name].keystore -alias [appname] -validity 10000
@kunofellasleep
kunofellasleep / adb install overwrite
Created March 20, 2019 07:00
apk上書きインストール
adb install -r /Users/path/is/here/app.apk