Skip to content

Commit

Permalink
iOS impl
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Jan 27, 2024
1 parent 34463f9 commit 05678e8
Show file tree
Hide file tree
Showing 23 changed files with 827 additions and 396 deletions.
92 changes: 92 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
AccessModifierOffset: -1
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ForEachMacros: [ FOR_EACH_RANGE, FOR_EACH, ]
IncludeCategories:
- Regex: '^<.*\.h(pp)?>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IndentCaseLabels: true
IndentWidth: 2
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
---
Language: ObjC
ColumnLimit: 120
BreakBeforeBraces: WebKit
PointerAlignment: Right
...
4 changes: 3 additions & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,8 @@ PODS:
- glog
- React-debug
- react-native-blurhash (1.1.11):
- glog
- RCT-Folly (= 2022.05.16.00)
- React-Core
- React-nativeconfig (0.73.2)
- React-NativeModulesApple (0.73.2):
Expand Down Expand Up @@ -1349,7 +1351,7 @@ SPEC CHECKSUMS:
React-jsinspector: 03644c063fc3621c9a4e8bf263a8150909129618
React-logger: 66b168e2b2bee57bd8ce9e69f739d805732a5570
React-Mapbuffer: 9ee041e1d7be96da6d76a251f92e72b711c651d6
react-native-blurhash: a59e6bf8117a0304488ed576abd440f4b0777a8c
react-native-blurhash: bb99ded7a87d193bfe05faa28ccbc62b582d8c3d
React-nativeconfig: d753fbbc8cecc8ae413d615599ac378bbf6999bb
React-NativeModulesApple: 964f4eeab1b4325e8b6a799cf4444c3fd4eb0a9c
React-perflogger: 29efe63b7ef5fbaaa50ef6eaa92482f98a24b97e
Expand Down
12 changes: 10 additions & 2 deletions example/ios/example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,17 @@
);
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "$(inherited)";
OTHER_CFLAGS = (
"$(inherited)",
" ",
);
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-DFOLLY_NO_CONFIG",
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
"-DFOLLY_CFG_NO_COROUTINES=1",
" ",
);
OTHER_LDFLAGS = (
"$(inherited)",
Expand Down Expand Up @@ -644,13 +648,17 @@
"\"$(inherited)\"",
);
MTL_ENABLE_DEBUG_INFO = NO;
OTHER_CFLAGS = "$(inherited)";
OTHER_CFLAGS = (
"$(inherited)",
" ",
);
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-DFOLLY_NO_CONFIG",
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
"-DFOLLY_CFG_NO_COROUTINES=1",
" ",
);
OTHER_LDFLAGS = (
"$(inherited)",
Expand Down
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function App() {
const [blurhash, setBlurhash] = useState('LGFFaXYk^6#M@-5c,1J5@[or[Q6.');
const [decodeAsync, setDecodeAsync] = useState(true);
const [encodingImageUri, setEncodingImageUri] = useState(
'https://blurha.sh/assets/images/img4.jpg',
'https://github.com/woltapp/blurhash/blob/master/Website/assets/images/img1.jpg?raw=true',
);
const [isEncoding, setIsEncoding] = useState(false);
//#endregion
Expand Down
18 changes: 9 additions & 9 deletions ios/BinaryInteger+Encode83.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
import Foundation

extension BinaryInteger {
func encode83(length: Int) -> String {
var result = ""
for i in 1 ... length {
let digit = (Int(self) / pow(83, length - i)) % 83
result += encodeCharacters[Int(digit)]
}
return result
}
func encode83(length: Int) -> String {
var result = ""
for i in 1 ... length {
let digit = (Int(self) / pow(83, length - i)) % 83
result += encodeCharacters[Int(digit)]
}
return result
}
}

private func pow(_ base: Int, _ exponent: Int) -> Int {
return (0 ..< exponent).reduce(1) { value, _ in value * base }
return (0 ..< exponent).reduce(1) { value, _ in value * base }
}
33 changes: 13 additions & 20 deletions ios/BlurhashCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,19 @@
import Foundation

final class BlurhashCache {
var blurhash: String?
var decodeWidth: Int
var decodeHeight: Int
var decodePunch: Float
var blurhash: String?
var decodeWidth: Int
var decodeHeight: Int
var decodePunch: Float

init(blurhash: String?, decodeWidth: Int, decodeHeight: Int, decodePunch: Float) {
self.blurhash = blurhash
self.decodeWidth = decodeWidth
self.decodeHeight = decodeHeight
self.decodePunch = decodePunch
}
init(blurhash: String?, decodeWidth: Int, decodeHeight: Int, decodePunch: Float) {
self.blurhash = blurhash
self.decodeWidth = decodeWidth
self.decodeHeight = decodeHeight
self.decodePunch = decodePunch
}

init(blurhash: NSString?, decodeWidth: NSNumber, decodeHeight: NSNumber, decodePunch: NSNumber) {
self.blurhash = blurhash as String?
self.decodeWidth = decodeWidth.intValue
self.decodeHeight = decodeHeight.intValue
self.decodePunch = decodePunch.floatValue
}

final func isDifferent(blurhash: NSString, decodeWidth: NSNumber, decodeHeight: NSNumber, decodePunch: NSNumber) -> Bool {
return self.blurhash != blurhash as String || self.decodeWidth != decodeWidth.intValue || self.decodeHeight != decodeHeight.intValue || self.decodePunch != decodePunch.floatValue
}
final func isDifferent(blurhash: String, decodeWidth: Int, decodeHeight: Int, decodePunch: Float) -> Bool {
return self.blurhash != blurhash || self.decodeWidth != decodeWidth || self.decodeHeight != decodeHeight || self.decodePunch != decodePunch
}
}
10 changes: 5 additions & 5 deletions ios/BlurhashError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import Foundation

enum BlurhashError: Error {
case invalidBlurhashString(isNil: Bool)
case invalidBlurhashDecodeWidth(actualValue: Int)
case invalidBlurhashDecodeHeight(actualValue: Int)
case invalidBlurhashDecodePunch(actualValue: Float)
case decodeError(message: String?)
case invalidBlurhashString(isNil: Bool)
case invalidBlurhashDecodeWidth(actualValue: Int)
case invalidBlurhashDecodeHeight(actualValue: Int)
case invalidBlurhashDecodePunch(actualValue: Float)
case decodeError(message: String?)
}
70 changes: 34 additions & 36 deletions ios/BlurhashHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,67 @@ import Foundation
// MARK: Helper funcs

func signPow(_ value: Float, _ exp: Float) -> Float {
return copysign(pow(abs(value), exp), value)
return copysign(pow(abs(value), exp), value)
}

func linearTosRGB(_ value: Float) -> Int {
let v = max(0, min(1, value))
if v <= 0.0031308 { return Int(v * 12.92 * 255 + 0.5) }
else { return Int((1.055 * pow(v, 1 / 2.4) - 0.055) * 255 + 0.5) }
let v = max(0, min(1, value))
if v <= 0.0031308 { return Int(v * 12.92 * 255 + 0.5) }
else { return Int((1.055 * pow(v, 1 / 2.4) - 0.055) * 255 + 0.5) }
}

func sRGBToLinear<Type: BinaryInteger>(_ value: Type) -> Float {
let v = Float(Int64(value)) / 255
if v <= 0.04045 { return v / 12.92 }
else { return pow((v + 0.055) / 1.055, 2.4) }
let v = Float(Int64(value)) / 255
if v <= 0.04045 { return v / 12.92 }
else { return pow((v + 0.055) / 1.055, 2.4) }
}

// MARK: Encoder

let encodeCharacters: [String] = {
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~".map { String($0) }
}()
let encodeCharacters: [String] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~".map { String($0) }

func encodeDC(_ value: (Float, Float, Float)) -> Int {
let roundedR = linearTosRGB(value.0)
let roundedG = linearTosRGB(value.1)
let roundedB = linearTosRGB(value.2)
return (roundedR << 16) + (roundedG << 8) + roundedB
let roundedR = linearTosRGB(value.0)
let roundedG = linearTosRGB(value.1)
let roundedB = linearTosRGB(value.2)
return (roundedR << 16) + (roundedG << 8) + roundedB
}

func encodeAC(_ value: (Float, Float, Float), maximumValue: Float) -> Int {
let quantR = Int(max(0, min(18, floor(signPow(value.0 / maximumValue, 0.5) * 9 + 9.5))))
let quantG = Int(max(0, min(18, floor(signPow(value.1 / maximumValue, 0.5) * 9 + 9.5))))
let quantB = Int(max(0, min(18, floor(signPow(value.2 / maximumValue, 0.5) * 9 + 9.5))))
let quantR = Int(max(0, min(18, floor(signPow(value.0 / maximumValue, 0.5) * 9 + 9.5))))
let quantG = Int(max(0, min(18, floor(signPow(value.1 / maximumValue, 0.5) * 9 + 9.5))))
let quantB = Int(max(0, min(18, floor(signPow(value.2 / maximumValue, 0.5) * 9 + 9.5))))

return quantR * 19 * 19 + quantG * 19 + quantB
return quantR * 19 * 19 + quantG * 19 + quantB
}

// MARK: Decoder

let decodeCharacters: [String: Int] = {
var dict: [String: Int] = [:]
for (index, character) in encodeCharacters.enumerated() {
dict[character] = index
}
return dict
var dict: [String: Int] = [:]
for (index, character) in encodeCharacters.enumerated() {
dict[character] = index
}
return dict
}()

func decodeDC(_ value: Int) -> (Float, Float, Float) {
let intR = value >> 16
let intG = (value >> 8) & 255
let intB = value & 255
return (sRGBToLinear(intR), sRGBToLinear(intG), sRGBToLinear(intB))
let intR = value >> 16
let intG = (value >> 8) & 255
let intB = value & 255
return (sRGBToLinear(intR), sRGBToLinear(intG), sRGBToLinear(intB))
}

func decodeAC(_ value: Int, maximumValue: Float) -> (Float, Float, Float) {
let quantR = value / (19 * 19)
let quantG = (value / 19) % 19
let quantB = value % 19
let quantR = value / (19 * 19)
let quantG = (value / 19) % 19
let quantB = value % 19

let rgb = (
signPow((Float(quantR) - 9) / 9, 2) * maximumValue,
signPow((Float(quantG) - 9) / 9, 2) * maximumValue,
signPow((Float(quantB) - 9) / 9, 2) * maximumValue
)
let rgb = (
signPow((Float(quantR) - 9) / 9, 2) * maximumValue,
signPow((Float(quantG) - 9) / 9, 2) * maximumValue,
signPow((Float(quantB) - 9) / 9, 2) * maximumValue
)

return rgb
return rgb
}
9 changes: 9 additions & 0 deletions ios/BlurhashModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <React/RCTBridgeModule.h>

NS_ASSUME_NONNULL_BEGIN

@interface BlurhashModule : NSObject <RCTBridgeModule>

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 05678e8

Please sign in to comment.